1 package org.andromda.core.metafacade;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.Iterator;
7 import java.util.LinkedHashMap;
8 import java.util.LinkedHashSet;
9 import java.util.List;
10 import java.util.ListIterator;
11 import java.util.Map;
12 import java.util.Set;
13 import org.andromda.core.common.ClassUtils;
14 import org.andromda.core.profile.Profile;
15 import org.apache.commons.lang.StringUtils;
16
17
18
19
20
21
22
23
24 public class MetafacadeMapping
25 {
26
27
28
29 private Class metafacadeClass = null;
30
31
32
33
34
35
36 public Class getMetafacadeClass()
37 {
38 return metafacadeClass;
39 }
40
41
42
43
44
45
46 public void setMetafacadeClassName(final String metafacadeClassName)
47 {
48 try
49 {
50 this.metafacadeClass = ClassUtils.loadClass(StringUtils.trimToEmpty(metafacadeClassName));
51 }
52 catch (final Throwable throwable)
53 {
54 throw new MetafacadeMappingsException(throwable);
55 }
56 }
57
58
59
60
61
62 private Set<String> mappingClassNames = new HashSet<String>();
63
64
65
66
67
68
69 protected Set<String> getMappingClassNames()
70 {
71
72 if (this.mappingClassNames.isEmpty())
73 {
74
75 this.mappingClassNames = MetafacadeUtils.getInheritedMappingClassNames(this);
76 }
77 return this.mappingClassNames;
78 }
79
80
81
82
83
84
85 final boolean isMappingClassNamePresent()
86 {
87 return !this.mappingClassNames.isEmpty();
88 }
89
90
91
92
93
94
95 public void setMappingClassName(final String mappingClassName)
96 {
97 if(!StringUtils.isBlank(mappingClassName))
98 {
99 this.mappingClassNames.clear();
100 this.mappingClassNames.add(StringUtils.trimToEmpty(mappingClassName));
101 }
102 }
103
104
105
106
107 private boolean contextRoot = false;
108
109
110
111
112
113
114
115
116
117 public boolean isContextRoot()
118 {
119 return this.contextRoot;
120 }
121
122
123
124
125
126
127
128 public void setContextRoot(final boolean contextRoot)
129 {
130 this.contextRoot = contextRoot;
131 }
132
133
134
135
136 private final List<String> stereotypes = new ArrayList<String>();
137
138
139
140
141
142
143 public void addStereotype(final String stereotype)
144 {
145 this.stereotypes.add(stereotype);
146 }
147
148
149
150
151
152
153 final List<String> getStereotypes()
154 {
155 for (final ListIterator<String> iterator = this.stereotypes.listIterator(); iterator.hasNext();)
156 {
157 iterator.set(Profile.instance().get(iterator.next()));
158 }
159 return this.stereotypes;
160 }
161
162
163
164
165
166
167 final boolean hasStereotypes()
168 {
169 return !this.stereotypes.isEmpty();
170 }
171
172
173
174
175 private final Collection<String> propertyReferences = new LinkedHashSet<String>();
176
177
178
179
180
181
182
183
184 public void addPropertyReference(final String reference)
185 {
186 this.propertyReferences.add(reference);
187 }
188
189
190
191
192
193 public Collection<String> getPropertyReferences()
194 {
195 return this.propertyReferences;
196 }
197
198
199
200
201 private PropertyGroup mappingProperties = null;
202
203
204
205
206
207
208
209
210 public void addMappingProperty(
211 final String name,
212 final String value)
213 {
214 if (value != null)
215 {
216 if (this.mappingProperties == null)
217 {
218 this.mappingProperties = new PropertyGroup();
219
220
221
222 this.mappingPropertyGroups.add(this.mappingProperties);
223 }
224 this.mappingProperties.addProperty(new Property(
225 name,
226 value));
227 }
228 }
229
230
231
232
233
234 private final Collection<PropertyGroup> mappingPropertyGroups = new ArrayList<PropertyGroup>();
235
236
237
238
239
240
241 final void addMappingPropertyGroup(final PropertyGroup propertyGroup)
242 {
243 this.mappingPropertyGroups.add(propertyGroup);
244 }
245
246
247
248
249
250 final Collection<PropertyGroup> getMappingPropertyGroups()
251 {
252 return this.mappingPropertyGroups;
253 }
254
255
256
257
258
259
260
261 final PropertyGroup getMappingProperties()
262 {
263 return this.mappingProperties;
264 }
265
266
267
268
269
270
271 final boolean hasMappingProperties()
272 {
273 return this.mappingProperties != null && !this.mappingProperties.getProperties().isEmpty();
274 }
275
276
277
278
279
280
281
282 public void addPropertyReferences(final Collection<String> propertyReferences)
283 {
284 if (propertyReferences != null)
285 {
286 this.propertyReferences.addAll(propertyReferences);
287 }
288 }
289
290
291
292
293 private String context = "";
294
295
296
297
298
299
300 public void setContext(final String context)
301 {
302 this.context = StringUtils.trimToEmpty(context);
303 }
304
305
306
307
308
309
310 final String getContext()
311 {
312 return this.context;
313 }
314
315
316
317
318
319
320 final boolean hasContext()
321 {
322 return StringUtils.isNotBlank(this.context);
323 }
324
325
326
327
328 private MetafacadeMappings mappings;
329
330
331
332
333
334
335
336 final void setMetafacadeMappings(final MetafacadeMappings mappings)
337 {
338 this.mappings = mappings;
339 }
340
341
342
343
344
345
346 final MetafacadeMappings getMetafacadeMappings()
347 {
348 return this.mappings;
349 }
350
351
352
353
354
355
356
357 final boolean match(final MetafacadeMapping mapping)
358 {
359 boolean match =
360 mapping != null && this.getMetafacadeClass().equals(mapping.getMetafacadeClass()) &&
361 this.getStereotypes().equals(mapping.getStereotypes()) && this.getContext().equals(mapping.getContext());
362
363
364 if (match && !this.mappingClassNames.isEmpty() && mapping != null && !mapping.mappingClassNames.isEmpty())
365 {
366 match = this.getMappingClassNames().equals(mapping.getMappingClassNames());
367 }
368 return match;
369 }
370
371
372
373
374 public String toString()
375 {
376 return super.toString() + '[' + this.getMetafacadeClass() + "], mappingClassName[" + this.mappingClassNames +
377 "], properties[" + this.getMappingProperties() + "], stereotypes" + this.stereotypes + ", context[" +
378 this.context + "], propertiesReferences" + this.getPropertyReferences();
379 }
380
381
382
383
384
385
386
387
388 static final class PropertyGroup
389 {
390 private final Map<String, Property> properties = new LinkedHashMap<String, Property>();
391
392
393
394
395
396
397 final void addProperty(final Property property)
398 {
399 final String name = property.getName();
400 if (!this.properties.containsKey(name))
401 {
402 this.properties.put(
403 name,
404 property);
405 }
406 }
407
408
409
410
411
412
413 final Collection<Property> getProperties()
414 {
415 return this.properties.values();
416 }
417
418
419
420
421 public String toString()
422 {
423 final StringBuilder toString = new StringBuilder();
424 char separator = ':';
425 for (final Iterator<Property> iterator = this.getProperties().iterator(); iterator.hasNext();)
426 {
427 final Property property = iterator.next();
428 toString.append(property.getName());
429 if (StringUtils.isNotBlank(property.getValue()))
430 {
431 toString.append(separator).append(property.getValue());
432 }
433 if (iterator.hasNext())
434 {
435 toString.append(separator);
436 }
437 }
438 return toString.toString();
439 }
440 }
441
442
443
444
445 static final class Property
446 {
447 private String name;
448 private String value;
449
450
451
452
453
454 Property(
455 final String name,
456 final String value)
457 {
458 this.name = StringUtils.trimToEmpty(name);
459 this.value = value;
460 }
461
462
463
464
465
466
467 final String getName()
468 {
469 return StringUtils.trimToEmpty(this.name);
470 }
471
472
473
474
475
476
477 final String getValue()
478 {
479 return StringUtils.trimToEmpty(this.value);
480 }
481 }
482 }