001: package org.andromda.metafacades.emf.uml2;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Comparator;
006: import java.util.Iterator;
007: import java.util.LinkedHashSet;
008: import java.util.List;
009: import java.util.Set;
010: import java.util.TreeSet;
011:
012: import org.andromda.metafacades.uml.ActorFacade;
013: import org.andromda.metafacades.uml.AssociationEndFacade;
014: import org.andromda.metafacades.uml.AttributeFacade;
015: import org.andromda.metafacades.uml.ClassifierFacade;
016: import org.andromda.metafacades.uml.DependencyFacade;
017: import org.andromda.metafacades.uml.Entity;
018: import org.andromda.metafacades.uml.EntityAttribute;
019: import org.andromda.metafacades.uml.ManageableEntity;
020: import org.andromda.metafacades.uml.ModelElementFacade;
021: import org.andromda.metafacades.uml.UMLMetafacadeProperties;
022: import org.andromda.metafacades.uml.UMLProfile;
023: import org.apache.commons.lang.ObjectUtils;
024: import org.apache.commons.lang.StringUtils;
025:
026: /**
027: * MetafacadeLogic implementation for
028: * org.andromda.metafacades.uml.ManageableEntity.
029: *
030: * @see org.andromda.metafacades.uml.ManageableEntity
031: */
032: public class ManageableEntityLogicImpl extends ManageableEntityLogic {
033: public ManageableEntityLogicImpl(final Object metaObject,
034: final String context) {
035: super (metaObject, context);
036: }
037:
038: /**
039: * @return the configured property denoting the character sequence to use
040: * for the separation of namespaces
041: */
042: private String getNamespaceSeparator() {
043: return (String) this
044: .getConfiguredProperty(UMLMetafacadeProperties.NAMESPACE_SEPARATOR);
045: }
046:
047: /**
048: * @see org.andromda.metafacades.uml.ManageableEntity#getManageablePackageName()
049: */
050: protected java.lang.String handleGetManageablePackageName() {
051: String manageablePackageName = "";
052:
053: final String parentPackage = super .getPackageName();
054: if (parentPackage != null && parentPackage.length() > 0) {
055: manageablePackageName = parentPackage;
056: }
057:
058: final Object suffix = this
059: .getConfiguredProperty(UMLMetafacadeProperties.MANAGEABLE_PACKAGE_NAME_SUFFIX);
060: if (suffix != null && StringUtils.isNotBlank(suffix.toString())) {
061: manageablePackageName += this .getNamespaceSeparator()
062: + suffix;
063: }
064:
065: return manageablePackageName;
066: }
067:
068: protected String handleGetManageablePackagePath() {
069: return StringUtils.replace(this .getManageablePackageName(),
070: this .getNamespaceSeparator(), "/");
071: }
072:
073: protected java.util.List handleGetManageableAssociationEnds() {
074: final Set manageableAssociationEnds = new LinkedHashSet(); // linked
075:
076: // hashset
077: // to
078: // guarantee
079: // ordering
080: // wo/
081: // duplicates
082: collectAssociationEnds(manageableAssociationEnds, this );
083:
084: return new ArrayList(manageableAssociationEnds);
085: }
086:
087: /**
088: * This method recursively collects all association ends to which a
089: * manageable entity would need to navigate
090: *
091: * @param manageableAssociationEnds
092: * the collection in which to collect the association ends
093: * @param entity
094: * the entity from which to recursively gather the association
095: * ends
096: */
097: private static void collectAssociationEnds(
098: final Collection manageableAssociationEnds,
099: final ManageableEntity entity) {
100: final Collection associationEnds = entity.getAssociationEnds();
101: for (final Iterator associationEndIterator = associationEnds
102: .iterator(); associationEndIterator.hasNext();) {
103: final AssociationEndFacade associationEnd = (AssociationEndFacade) associationEndIterator
104: .next();
105: final AssociationEndFacade otherEnd = associationEnd
106: .getOtherEnd();
107:
108: if (otherEnd.isNavigable()
109: && otherEnd.getType() instanceof Entity) {
110: manageableAssociationEnds.add(otherEnd);
111: }
112: }
113:
114: // retrieve all association ends for all parents (recursively)
115: final Collection parentEntities = entity
116: .getAllGeneralizations();
117: for (final Iterator parentEntityIterator = parentEntities
118: .iterator(); parentEntityIterator.hasNext();) {
119: final Object parentEntityObject = parentEntityIterator
120: .next();
121: if (parentEntityObject instanceof ManageableEntity) {
122: collectAssociationEnds(manageableAssociationEnds,
123: (ManageableEntity) parentEntityObject);
124: }
125: }
126: }
127:
128: /**
129: * @see org.andromda.metafacades.uml.ManageableEntity#isCreate()
130: */
131: protected boolean handleIsCreate() {
132: return !this .isAbstract();
133: }
134:
135: protected String handleGetManageableServiceName() {
136: return this .getName() + "ManageableService";
137: }
138:
139: protected String handleGetManageableServiceFullPath() {
140: return '/' + StringUtils.replace(this
141: .getFullyQualifiedManageableServiceName(), this
142: .getNamespaceSeparator(), "/");
143: }
144:
145: protected String handleGetFullyQualifiedManageableServiceName() {
146: return this .getManageablePackageName()
147: + this .getNamespaceSeparator()
148: + this .getManageableServiceName();
149: }
150:
151: protected String handleGetManageableServiceAccessorCall() {
152: final String property = UMLMetafacadeProperties.MANAGEABLE_SERVICE_ACCESSOR_PATTERN;
153: final String accessorImplementation = this
154: .isConfiguredProperty(property) ? ObjectUtils
155: .toString(this .getConfiguredProperty(property)) : "";
156: return accessorImplementation.replaceAll("\\{0\\}",
157: this .getManageablePackageName()).replaceAll("\\{1\\}",
158: this .getManageableServiceName());
159: }
160:
161: protected boolean handleIsRead() {
162: return true;
163: }
164:
165: protected boolean handleIsUpdate() {
166: return this .getManageableIdentifier() != null; // @todo
167: }
168:
169: protected boolean handleIsDelete() {
170: return this .getManageableIdentifier() != null; // @todo
171: }
172:
173: protected List handleGetManageableAttributes() {
174: ArrayList attList = new ArrayList(this .getAttributes(true));
175: return attList;
176: }
177:
178: protected Object handleGetManageableIdentifier() {
179: return this .getIdentifiers(true).iterator().next();
180: }
181:
182: protected List handleGetManageableMembers() {
183: final List criteria = new ArrayList();
184: criteria.addAll(this .getManageableAttributes());
185: criteria.addAll(this .getManageableAssociationEnds());
186: return criteria;
187: }
188:
189: protected String handleListManageableMembers(final boolean withTypes) {
190: final StringBuffer buffer = new StringBuffer();
191:
192: final List attributes = this .getManageableAttributes();
193: for (int i = 0; i < attributes.size(); i++) {
194: if (buffer.length() > 0) {
195: buffer.append(", ");
196: }
197:
198: final AttributeFacade attribute = (AttributeFacade) attributes
199: .get(i);
200: final ClassifierFacade type = attribute.getType();
201: if (type != null) {
202: if (withTypes) {
203: buffer.append(type.getFullyQualifiedName());
204: buffer.append(' ');
205: }
206: buffer.append(attribute.getName());
207: }
208: }
209:
210: final List associationEnds = this
211: .getManageableAssociationEnds();
212: for (int i = 0; i < associationEnds.size(); i++) {
213: final AssociationEndFacade associationEnd = (AssociationEndFacade) associationEnds
214: .get(i);
215: final Entity entity = (Entity) associationEnd.getType();
216:
217: final Iterator identifierIterator = entity.getIdentifiers()
218: .iterator();
219: if (identifierIterator.hasNext()) {
220: final AttributeFacade identifier = (AttributeFacade) identifierIterator
221: .next();
222: if (identifier != null) {
223: if (buffer.length() > 0) {
224: buffer.append(", ");
225: }
226:
227: final ClassifierFacade type = identifier.getType();
228: if (type != null) {
229: if (withTypes) {
230: buffer.append(type.getFullyQualifiedName());
231: if (associationEnd.isMany()) {
232: buffer.append("[]");
233: }
234: buffer.append(' ');
235: }
236: buffer.append(associationEnd.getName());
237: }
238: }
239: }
240: }
241:
242: return buffer.toString();
243: }
244:
245: protected boolean handleIsManageable() {
246: return Boolean
247: .valueOf(
248: (String) this
249: .getConfiguredProperty(UMLMetafacadeProperties.ENABLE_MANAGEABLE_ENTITIES))
250: .booleanValue();
251: }
252:
253: protected java.util.List handleGetReferencingManageables() {
254: final Set referencingManageables = new LinkedHashSet();
255: final Collection associationEnds = this .getAssociationEnds();
256: for (final Iterator associationEndIterator = associationEnds
257: .iterator(); associationEndIterator.hasNext();) {
258: final AssociationEndFacade associationEnd = (AssociationEndFacade) associationEndIterator
259: .next();
260:
261: if (associationEnd.isNavigable()) {
262: if (associationEnd.isMany()
263: || (associationEnd.isOne2One() && associationEnd
264: .isChild())) {
265: final Object otherEndType = associationEnd
266: .getOtherEnd().getType();
267: if (otherEndType instanceof Entity) {
268: referencingManageables.add(otherEndType);
269: }
270: }
271: }
272: }
273: return new ArrayList(referencingManageables);
274: }
275:
276: protected Object handleGetDisplayAttribute() {
277: AttributeFacade displayAttribute = null;
278:
279: final Object taggedValueObject = this
280: .findTaggedValue(UMLProfile.TAGGEDVALUE_MANAGEABLE_DISPLAY_NAME);
281: if (taggedValueObject != null) {
282: displayAttribute = this .findAttribute(StringUtils
283: .trimToEmpty(taggedValueObject.toString()));
284: }
285:
286: final Collection attributes = this .getAttributes(true);
287: for (final Iterator attributeIterator = attributes.iterator(); attributeIterator
288: .hasNext()
289: && displayAttribute == null;) {
290: final EntityAttribute attribute = (EntityAttribute) attributeIterator
291: .next();
292: if (attribute.isUnique()) {
293: displayAttribute = attribute;
294: }
295: }
296:
297: if (displayAttribute == null) {
298: if (!this .getIdentifiers().isEmpty()) {
299: displayAttribute = (EntityAttribute) this
300: .getIdentifiers().iterator().next();
301: } else if (!attributes.isEmpty()) {
302: displayAttribute = (EntityAttribute) attributes
303: .iterator().next();
304: }
305: }
306:
307: return displayAttribute;
308: }
309:
310: protected java.util.List handleGetUsers() {
311: final Set users = new LinkedHashSet();
312:
313: final Collection dependencies = this .getTargetDependencies();
314: for (final Iterator dependencyIterator = dependencies
315: .iterator(); dependencyIterator.hasNext();) {
316: final DependencyFacade dependency = (DependencyFacade) dependencyIterator
317: .next();
318: final Object dependencyObject = dependency
319: .getSourceElement();
320:
321: if (!users.contains(dependencyObject)
322: && dependencyObject instanceof ActorFacade) {
323: this .collectActors((ActorFacade) dependencyObject,
324: users);
325: }
326: }
327:
328: return new ArrayList(users);
329: }
330:
331: private void collectActors(final ActorFacade actor,
332: final Collection actors) {
333: if (!actors.contains(actor)) {
334: actors.add(actor);
335:
336: final Collection childActors = actor
337: .getGeneralizedByActors();
338: for (final Iterator iterator = childActors.iterator(); iterator
339: .hasNext();) {
340: final ActorFacade childActor = (ActorFacade) iterator
341: .next();
342: this .collectActors(childActor, actors);
343: }
344: }
345: }
346:
347: protected int handleGetMaximumListSize() {
348: int maximumListSize;
349:
350: final Object taggedValueObject = this
351: .findTaggedValue(UMLProfile.TAGGEDVALUE_MANAGEABLE_MAXIMUM_LIST_SIZE);
352: if (taggedValueObject != null) {
353: try {
354: maximumListSize = Integer.parseInt(taggedValueObject
355: .toString());
356: } catch (NumberFormatException e) {
357: maximumListSize = this .internalDefaultMaximumListSize();
358: }
359: } else {
360: maximumListSize = this .internalDefaultMaximumListSize();
361: }
362:
363: return maximumListSize;
364: }
365:
366: private int internalDefaultMaximumListSize() {
367: int maximumListSize;
368:
369: try {
370: maximumListSize = Integer
371: .parseInt((String) this
372: .getConfiguredProperty(UMLMetafacadeProperties.PROPERTY_DEFAULT_MAX_LIST_SIZE));
373: } catch (NumberFormatException e1) {
374: maximumListSize = -1;
375: }
376:
377: return maximumListSize;
378: }
379:
380: protected int handleGetPageSize() {
381: int pageSize;
382:
383: final Object taggedValueObject = this
384: .findTaggedValue(UMLProfile.TAGGEDVALUE_MANAGEABLE_PAGE_SIZE);
385: if (taggedValueObject != null) {
386: try {
387: pageSize = Integer.parseInt(taggedValueObject
388: .toString());
389: } catch (NumberFormatException e) {
390: pageSize = this .internalDefaultPageSize();
391: }
392: } else {
393: pageSize = this .internalDefaultPageSize();
394: }
395:
396: return pageSize;
397: }
398:
399: private int internalDefaultPageSize() {
400: int pageSize;
401:
402: try {
403: pageSize = Integer
404: .parseInt((String) this
405: .getConfiguredProperty(UMLMetafacadeProperties.PROPERTY_DEFAULT_PAGE_SIZE));
406: } catch (NumberFormatException e1) {
407: pageSize = 20;
408: }
409:
410: return pageSize;
411: }
412:
413: protected boolean handleIsResolveable() {
414: boolean resolveable;
415:
416: final Object taggedValueObject = this
417: .findTaggedValue(UMLProfile.TAGGEDVALUE_MANAGEABLE_RESOLVEABLE);
418: if (taggedValueObject != null) {
419: try {
420: resolveable = Boolean.valueOf(
421: taggedValueObject.toString()).booleanValue();
422: } catch (NumberFormatException e) {
423: resolveable = this .internalDefaultResolveable();
424: }
425: } else {
426: resolveable = this .internalDefaultResolveable();
427: }
428:
429: return resolveable;
430: }
431:
432: private boolean internalDefaultResolveable() {
433: boolean resolveable;
434:
435: try {
436: resolveable = Boolean
437: .valueOf(
438: (String) this
439: .getConfiguredProperty(UMLMetafacadeProperties.PROPERTY_DEFAULT_RESOLVEABLE))
440: .booleanValue();
441: } catch (NumberFormatException ex) {
442: resolveable = true;
443: }
444:
445: return resolveable;
446: }
447:
448: protected java.util.List handleGetAllManageables() {
449: final Set allManageableEntities = new TreeSet(
450: new ManageableComparator());
451:
452: final Collection allClasses = this .getModel().getAllClasses();
453: for (final Iterator classIterator = allClasses.iterator(); classIterator
454: .hasNext();) {
455: final Object classObject = classIterator.next();
456: if (classObject instanceof ManageableEntity) {
457: allManageableEntities.add(classObject);
458: }
459: }
460: return new ArrayList(allManageableEntities);
461: }
462:
463: final static class ManageableComparator implements Comparator {
464: public int compare(final Object left, final Object right) {
465: final ModelElementFacade leftEntity = (ModelElementFacade) left;
466: final ModelElementFacade rightEntity = (ModelElementFacade) right;
467:
468: return leftEntity.getName()
469: .compareTo(rightEntity.getName());
470: }
471: }
472: }
|