1 package fr.ifremer.quadrige2.magicdraw.helper;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.List;
31 import java.util.Properties;
32
33 import javax.swing.text.html.parser.Entity;
34
35 import com.nomagic.uml2.ext.jmi.helpers.StereotypesHelper;
36 import com.nomagic.uml2.ext.magicdraw.auxiliaryconstructs.mdmodels.Model;
37 import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Class;
38 import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Element;
39 import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.EnumerationLiteral;
40 import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.NamedElement;
41 import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Property;
42 import com.nomagic.uml2.ext.magicdraw.mdprofiles.Stereotype;
43
44 public class TransformModelHelper {
45
46 public static boolean isIdentifier(Class entity, Property attribute, java.util.Properties properties) {
47 String propertyName = attribute.getName();
48 if (propertyName == null || "".equals(propertyName)) {
49 if (attribute.getType() != null) {
50 propertyName = attribute.getType().getName();
51 }
52 else {
53 return false;
54 }
55 }
56
57 return "true".equalsIgnoreCase(getDefaultValue(entity.getName(), propertyName, "identifier",properties));
58 }
59
60
61
62 public static String getClassName2TableName(String ClassName) {
63
64 int i = 0;
65 String result ="";
66 String table[] = ClassName.split("(?=\\p{Upper})");
67 int l = table.length;
68 if (l>1){
69 result = table[1].toUpperCase();
70 for (i=2; i<l; i++) {
71 table[i] = table[i].toUpperCase();
72 result = result + "_" + table[i];
73 }
74 }else{
75
76 result = table[0].toUpperCase();
77
78 }
79
80 return result;
81 }
82
83
84
85
86
87
88 public static String getColumnNameFromProperty(Property attribute) {
89 String propertyName = attribute.getName();
90 if (propertyName == null || "".equals(propertyName)) {
91 if (attribute.getType() != null) {
92 propertyName = attribute.getType().getName();
93 }
94 else {
95 return null;
96 }
97 }
98
99 return getColumnNameFromPropertyName(propertyName);
100 }
101
102
103
104
105
106
107 public static String getColumnNameFromPropertyName(String propertyName) {
108
109
110 int i = 0;
111 String result ="";
112 String table[] = propertyName.split("(?=\\p{Upper})");
113 int l = table.length;
114 result = table[0].toUpperCase();
115 for (i=1; i<l; i++) {
116 table[i] = table[i].toUpperCase();
117 result = result + "_" + table[i];
118 }
119
120 return result;
121 }
122
123 public static String getDefaultValue(Class entity, Property attribute, String features, java.util.Properties properties) {
124
125 String entityName = entity.getName();
126 if (entityName == null || entityName.trim().length() == 0) {
127 return null;
128 }
129
130 String propertyName = attribute.getName();
131 if (propertyName == null || propertyName.trim().length() == 0) {
132 if (attribute.getType() != null) {
133 propertyName = attribute.getType().getName();
134 }
135 else {
136 return null;
137 }
138 }
139
140 return getDefaultValue(entityName, propertyName, features, properties);
141 }
142
143 public static String getDefaultValue(com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Package aPackage, String features,java.util.Properties properties) {
144 String packageName = getPackageFullName(aPackage);
145 if (packageName == null || "".equals(packageName)) {
146 return null;
147 }
148
149 String defaultValue = null;
150 String suffix = "." + features;
151
152 do {
153 defaultValue = properties.getProperty(packageName + suffix);
154 if (defaultValue != null && defaultValue.length() > 0) {
155 return defaultValue;
156 }
157
158 if (packageName.contains(".")) {
159 packageName = packageName.substring(0, packageName.lastIndexOf('.'));
160 }
161 else {
162 packageName = null;
163 }
164 } while (packageName != null);
165
166 return null;
167 }
168
169 public static String getDefaultValue(com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Package aPackage, String entityName, String features, Properties properties) {
170 if (entityName == null || entityName.length() == 0) {
171 return null;
172 }
173
174 String defaultValue = null;
175 String suffix = "." + features;
176
177 defaultValue = properties.getProperty(entityName + suffix);
178 if (defaultValue != null && defaultValue.length() > 0) {
179 return defaultValue;
180 }
181
182 return getDefaultValue(aPackage, features, properties);
183 }
184
185
186
187 public static String getDefaultValue(String entityName, String propertyName, String features, Properties properties) {
188
189 String result = getDefaultValueFromProperties(entityName, propertyName, features, properties);
190
191
192 if (result == null) {
193 String tableName = TransformModelHelper.getTableNameFromEntityName(entityName);
194 String columnName = TransformModelHelper.getColumnNameFromPropertyName(propertyName);
195 if (tableName != null && columnName != null) {
196 return getDefaultValueFromProperties(tableName, columnName, features, properties);
197 }
198 }
199
200 return result;
201 }
202
203 protected static String getDefaultValueFromProperties(String entityName, String propertyName, String features, Properties properties) {
204
205 if (propertyName == null || propertyName.length() == 0) {
206 return null;
207 }
208
209 String defaultValue = null;
210 String suffix = "." + features;
211
212 defaultValue = properties.getProperty(entityName + "." + propertyName + suffix);
213 if (defaultValue != null && defaultValue.length() > 0) {
214 return defaultValue;
215 }
216
217 defaultValue = properties.getProperty("column." + propertyName + suffix);
218 if (defaultValue != null && defaultValue.length() > 0) {
219 return defaultValue;
220 }
221
222 for (Object keyObj : properties.keySet()) {
223 String key = (String) keyObj;
224 if (key.startsWith("column.regex.")) {
225 String columnRegex = key.substring("column.regex.".length());
226 if (key.endsWith(suffix)) {
227 columnRegex = columnRegex.substring(0, columnRegex.length() - suffix.length());
228 columnRegex = columnRegex.replaceAll("[%]", ".*");
229 if (propertyName.matches(columnRegex)) {
230 defaultValue = properties.getProperty(key);
231 if (defaultValue != null & defaultValue.length() > 0) {
232 return defaultValue;
233 }
234 }
235 }
236 }
237 }
238
239 return null;
240 }
241
242 public static String getUniqueConstraintsName(Class entity, Stereotype entityStereotype) {
243 return getEntityConstrainName(entity, "_NATIDC", entityStereotype);
244 }
245
246 public static String getUniqueIndexName(Class entity, Stereotype entityStereotype) {
247 return getEntityConstrainName(entity, "_NATIDX", entityStereotype);
248 }
249
250 public static String getEntityConstrainName(Class entity, String constraintSuffix, Stereotype entityStereotype) {
251 String tableName = getStereotypePropertyValue(entity, entityStereotype, "andromda_persistence_table");
252 if (tableName == null) {
253
254
255 tableName = getTableNameFromEntityName(entity.getName());
256
257
258 Collection<Class> classes = entity.getSuperClass();
259 if (classes != null && classes.size() > 0) {
260 for (Class superEntity : classes) {
261 String persistenceInheritance = getStereotypePropertyValue(superEntity, entityStereotype, "andromda_persistence_inheritance");
262
263 if ("SINGLE_TABLE".equals(persistenceInheritance)) {
264 String superTableName = getStereotypePropertyValue(superEntity, entityStereotype, "andromda_persistence_table");
265
266 if (superTableName == null) {
267 tableName = getTableNameFromEntityName(superEntity.getName());
268 }
269 break;
270 }
271 }
272 }
273 }
274 if (tableName.length() + constraintSuffix.length() > 30) {
275 tableName = tableName.substring(0, tableName.length() - constraintSuffix.length());
276 }
277 return tableName + constraintSuffix;
278 }
279
280
281
282
283
284
285
286
287 public static String getTableNameFromEntity(Class entity) {
288 return getTableNameFromEntityName(entity.getName());
289 }
290
291
292
293
294
295
296
297
298 public static String getTableNameFromEntityName(String entityName) {
299 StringBuilder sb = new StringBuilder();
300 for (int i = 0; i < entityName.length(); i++) {
301 String letter = entityName.substring(i, i + 1);
302
303 if (letter.toUpperCase().equals(letter)) {
304 if (sb.length() > 0)
305 sb.append('_');
306 }
307 sb.append(letter.toUpperCase());
308 }
309 return sb.toString();
310 }
311
312 public static String getStereotypePropertyValue(Element element, Stereotype stereotype, String tagValueName) {
313 List oldTagValues = StereotypesHelper.getStereotypePropertyValue(
314 element, stereotype, tagValueName);
315 String oldTagValue = "";
316 for (int k = 0; k < oldTagValues.size(); ++k)
317 {
318
319 Object previousTagValue = (Object) oldTagValues.get(k);
320 if (k > 0)
321 oldTagValue += ",";
322 if (previousTagValue instanceof EnumerationLiteral) {
323 EnumerationLiteral aEnum = (EnumerationLiteral) previousTagValue;
324 oldTagValue += aEnum.getName();
325 }
326 else {
327 oldTagValue += previousTagValue.toString();
328 }
329 }
330 return "".equals(oldTagValue) ? null : oldTagValue;
331 }
332
333 public static Property getRemoteId(Class entity) {
334 for (Property p : entity.getOwnedAttribute()) {
335 if ("remoteId".equals(p.getName())) {
336 return p;
337 }
338 }
339 return null;
340 }
341
342 public static String getPackageFullName(com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Package aPackage) {
343 return getElementFullName(aPackage);
344 }
345
346 public static String getElementFullName(Element element) {
347 String elementName = "";
348 if (element instanceof NamedElement) {
349 elementName = ((NamedElement) element).getName();
350 }
351
352 String parentName = "";
353 if (element.getOwner() != null) {
354 parentName = getElementFullName(element.getOwner());
355 }
356 else {
357 return "";
358 }
359 if (parentName != null && parentName.length() > 0) {
360 elementName = parentName + "." + elementName;
361 }
362 return elementName;
363 }
364
365 public static String getAttributeName2TableName(String ClassName) {
366
367 int i = 0;
368 String result = "";
369 String table[] = ClassName.split("(?=\\p{Upper})");
370 int l = table.length;
371 if (l > 1) {
372 result = table[0].toUpperCase();
373 for (i = 1; i < l; i++) {
374 table[i] = table[i].toUpperCase();
375 result = result + "_" + table[i];
376 }
377 } else {
378
379 result = table[0].toUpperCase();
380
381 }
382
383 return result;
384 }
385
386 public static boolean isQuadrigeIdentifier(Class entity, Property attribute) {
387 String propertyName = attribute.getName();
388 if (propertyName == null || "".equals(propertyName)) {
389 if (attribute.getType() != null) {
390 propertyName = attribute.getType().getName();
391 }
392 else {
393 return false;
394 }
395 }
396 String entityName = getClassName2TableName(entity.getName());
397 List<String> entityPartName = Arrays.asList(entityName.split("_"));
398 List<String> propertyPartName = Arrays.asList(getAttributeName2TableName(propertyName).split("_"));
399 if (entityPartName.size() > propertyPartName.size()) {
400 return false;
401 }
402 int i = 0;
403 while (i < entityPartName.size()) {
404 if (!(entityPartName.get(i).startsWith(propertyPartName.get(i)))) {
405 return false;
406 }
407 i++;
408 }
409 return propertyPartName.size() -1 == entityPartName.size();
410 }
411
412
413 public static Element getPackageFromFullQualifiedName(String fullName, Model model, Element e) {
414
415 String pkgName = "";
416 String remaining = "";
417 if (fullName == null) {
418 return e;
419 } else if (fullName.indexOf(".") == -1) {
420 pkgName = fullName;
421 remaining = null;
422 } else {
423 pkgName = fullName.substring(0,fullName.indexOf("."));
424 remaining = fullName.substring(fullName.indexOf(".")+1);
425 }
426 if (e == null) {
427 for (Element elem : model.getNestedPackage()) {
428 String name = ((com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Package)elem).getName();
429 if (pkgName.equals(name)) {
430 return getPackageFromFullQualifiedName(remaining,model,elem);
431 }
432 }
433 } else {
434 for (Element elem : e.getOwnedElement()) {
435
436 if ("Paquet".equals(elem.getHumanType())) {
437 String name = ((com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Package)elem).getName();
438 if (pkgName.equals(name)) {
439 return getPackageFromFullQualifiedName(remaining,model,elem);
440 }
441 }
442 }
443 }
444 return null;
445 }
446
447
448 public static void removeStereotype(Class c,Stereotype s) {
449 for (Property p : c.getAttribute()) {
450 if (StereotypesHelper.hasStereotype(p, s)) {
451 StereotypesHelper.removeStereotype(p,s);
452 }
453 }
454 }
455
456 public static String getClassNameForId(Class entity) {
457 if (entity == null || entity.getName().length() <2 )
458 return "";
459 return Character.toLowerCase(entity.getName().charAt(0)) + entity.getName().substring(1);
460 }
461
462
463 }