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