001: package org.andromda.cartridges.spring.metafacades;
002:
003: import java.text.MessageFormat;
004: import java.util.ArrayList;
005: import java.util.Collection;
006: import java.util.Iterator;
007:
008: import org.andromda.cartridges.spring.SpringProfile;
009: import org.andromda.cartridges.spring.SpringHibernateUtils;
010: import org.andromda.metafacades.uml.AttributeFacade;
011: import org.andromda.metafacades.uml.ClassifierFacade;
012: import org.andromda.metafacades.uml.DependencyFacade;
013: import org.andromda.metafacades.uml.EnumerationFacade;
014: import org.andromda.metafacades.uml.FilteredCollection;
015: import org.andromda.metafacades.uml.GeneralizableElementFacade;
016: import org.andromda.metafacades.uml.OperationFacade;
017: import org.andromda.metafacades.uml.UMLProfile;
018: import org.andromda.metafacades.uml.ValueObject;
019: import org.apache.commons.collections.CollectionUtils;
020: import org.apache.commons.lang.StringUtils;
021:
022: /**
023: * MetafacadeLogic implementation for org.andromda.cartridges.spring.metafacades.SpringEntity.
024: *
025: * @see org.andromda.cartridges.spring.metafacades.SpringEntity
026: */
027: public class SpringEntityLogicImpl extends SpringEntityLogic {
028: public SpringEntityLogicImpl(Object metaObject, String context) {
029: super (metaObject, context);
030: }
031:
032: /**
033: * Value for one Table per root class
034: */
035: private static final String INHERITANCE_STRATEGY_CLASS = "class";
036:
037: /**
038: * Value for joined-subclass
039: */
040: private static final String INHERITANCE_STRATEGY_SUBCLASS = "subclass";
041:
042: /**
043: * Value for one Table per concrete class
044: */
045: private static final String INHERITANCE_STRATEGY_CONCRETE = "concrete";
046:
047: /**
048: * Value make Entity an interface, delegate attributes to subclasses.
049: */
050: private static final String INHERITANCE_STRATEGY_INTERFACE = "interface";
051:
052: /**
053: * Stores the valid inheritance strategies.
054: */
055: private static final Collection inheritanceStrategies = new ArrayList();
056:
057: static {
058: inheritanceStrategies.add(INHERITANCE_STRATEGY_CLASS);
059: inheritanceStrategies.add(INHERITANCE_STRATEGY_SUBCLASS);
060: inheritanceStrategies.add(INHERITANCE_STRATEGY_CONCRETE);
061: inheritanceStrategies.add(INHERITANCE_STRATEGY_INTERFACE);
062: }
063:
064: /**
065: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getDaoName()
066: */
067: protected java.lang.String handleGetDaoName() {
068: return this .getDaoNamePattern().replaceAll("\\{0\\}",
069: this .getName());
070: }
071:
072: /**
073: * Gets the value of the {@link SpringGlobals#DAO_PATTERN}
074: *
075: * @return the DAO name pattern.
076: */
077: private String getDaoNamePattern() {
078: return String.valueOf(this
079: .getConfiguredProperty(SpringGlobals.DAO_PATTERN));
080: }
081:
082: /**
083: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getFullyQualifiedDaoName()
084: */
085: protected java.lang.String handleGetFullyQualifiedDaoName() {
086: return SpringMetafacadeUtils.getFullyQualifiedName(this
087: .getPackageName(), this .getDaoName());
088: }
089:
090: /**
091: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getDaoImplementationName()
092: */
093: protected java.lang.String handleGetDaoImplementationName() {
094: return this .getDaoImplementationNamePattern().replaceAll(
095: "\\{0\\}", this .getName());
096: }
097:
098: /**
099: * Gets the value of the {@link SpringGlobals#DAO_IMPLEMENTATION_PATTERN}
100: *
101: * @return the DAO implementation name pattern.
102: */
103: private String getDaoImplementationNamePattern() {
104: return String
105: .valueOf(this
106: .getConfiguredProperty(SpringGlobals.DAO_IMPLEMENTATION_PATTERN));
107: }
108:
109: /**
110: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getFullyQualifiedDaoImplementationName()
111: */
112: protected java.lang.String handleGetFullyQualifiedDaoImplementationName() {
113: return SpringMetafacadeUtils.getFullyQualifiedName(this
114: .getPackageName(), this .getDaoImplementationName());
115: }
116:
117: /**
118: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getDaoBaseName()
119: */
120: protected java.lang.String handleGetDaoBaseName() {
121: return this .getDaoBaseNamePattern().replaceAll("\\{0\\}",
122: this .getName());
123: }
124:
125: /**
126: * Gets the value of the {@link SpringGlobals#DAO_BASE_PATTERN}
127: *
128: * @return the DAO base name pattern.
129: */
130: private String getDaoBaseNamePattern() {
131: return String.valueOf(this
132: .getConfiguredProperty(SpringGlobals.DAO_BASE_PATTERN));
133: }
134:
135: /**
136: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getFullyQualifiedDaoBaseName()
137: */
138: protected java.lang.String handleGetFullyQualifiedDaoBaseName() {
139: return SpringMetafacadeUtils.getFullyQualifiedName(this
140: .getPackageName(), this .getDaoBaseName());
141: }
142:
143: /**
144: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getImplementationName()
145: */
146: protected java.lang.String handleGetEntityImplementationName() {
147: return this .getEntityName()
148: + SpringGlobals.IMPLEMENTATION_SUFFIX;
149: }
150:
151: /**
152: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getFullyQualifiedEntityImplementationName()
153: */
154: protected java.lang.String handleGetFullyQualifiedEntityImplementationName() {
155: return SpringMetafacadeUtils.getFullyQualifiedName(this
156: .getPackageName(), this .getEntityName(),
157: SpringGlobals.IMPLEMENTATION_SUFFIX);
158: }
159:
160: /**
161: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getBeanName(boolean)
162: */
163: protected java.lang.String handleGetBeanName(boolean targetSuffix) {
164: final String beanName = StringUtils.uncapitalize(StringUtils
165: .trimToEmpty(this .getName()));
166: final StringBuffer beanNameBuffer = new StringBuffer(this
167: .getDaoNamePattern().replaceAll("\\{0\\}", beanName));
168: if (targetSuffix) {
169: beanNameBuffer
170: .append(SpringGlobals.BEAN_NAME_TARGET_SUFFIX);
171: }
172: return beanNameBuffer.toString();
173: }
174:
175: /**
176: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getEntityName()
177: */
178: protected String handleGetEntityName() {
179: final String entityNamePattern = (String) this
180: .getConfiguredProperty("entityNamePattern");
181: return MessageFormat
182: .format(entityNamePattern, new Object[] { StringUtils
183: .trimToEmpty(this .getName()) });
184: }
185:
186: /**
187: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getFullyQualifiedEntityName()
188: */
189: protected String handleGetFullyQualifiedEntityName() {
190: return SpringMetafacadeUtils.getFullyQualifiedName(this
191: .getPackageName(), this .getEntityName(), null);
192: }
193:
194: /**
195: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getRoot()
196: */
197: protected Object handleGetRoot() {
198: GeneralizableElementFacade generalization = this ;
199: for (; generalization.getGeneralization() != null
200: && generalization instanceof SpringEntity; generalization = generalization
201: .getGeneralization())
202: ;
203: return generalization;
204: }
205:
206: /**
207: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#isDaoBusinessOperationsPresent()
208: */
209: protected boolean handleIsDaoBusinessOperationsPresent() {
210: return this .getDaoBusinessOperations() != null
211: && !this .getDaoBusinessOperations().isEmpty();
212: }
213:
214: /**
215: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getDaoBusinessOperations()
216: */
217: protected Collection handleGetDaoBusinessOperations() {
218: // operations that are not finders and static
219: Collection finders = this .getQueryOperations();
220: Collection operations = this .getOperations();
221:
222: Collection nonFinders = CollectionUtils.subtract(operations,
223: finders);
224: return new FilteredCollection(nonFinders) {
225: public boolean evaluate(Object object) {
226: return ((OperationFacade) object).isStatic();
227: }
228: };
229: }
230:
231: /**
232: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getValueObjectReferences()
233: */
234: protected Collection handleGetValueObjectReferences() {
235: return this .getValueObjectReferences(false);
236: }
237:
238: /**
239: * Retrieves the values object references for this entity. If
240: * <code>follow</code> is true, then all value object references
241: * (including those that were inherited) will be retrieved.
242: */
243: protected Collection getValueObjectReferences(boolean follow) {
244: final Collection sourceDependencies = new ArrayList(this
245: .getSourceDependencies());
246: if (follow) {
247: for (GeneralizableElementFacade entity = this
248: .getGeneralization(); entity != null; entity = entity
249: .getGeneralization()) {
250: sourceDependencies.addAll(entity
251: .getSourceDependencies());
252: }
253: }
254: return new FilteredCollection(sourceDependencies) {
255: public boolean evaluate(Object object) {
256: boolean valid = false;
257: Object targetElement = ((DependencyFacade) object)
258: .getTargetElement();
259: if (targetElement instanceof ClassifierFacade) {
260: ClassifierFacade element = (ClassifierFacade) targetElement;
261: valid = element.isDataType()
262: || element instanceof ValueObject
263: || element instanceof EnumerationFacade;
264: }
265: return valid;
266: }
267: };
268: }
269:
270: /**
271: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getAllValueObjectReferences()
272: */
273: protected Collection handleGetAllValueObjectReferences() {
274: return this .getValueObjectReferences(true);
275: }
276:
277: /**
278: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#isDaoImplementationRequired()
279: */
280: protected boolean handleIsDaoImplementationRequired() {
281: return !this .getValueObjectReferences().isEmpty()
282: || !this .getDaoBusinessOperations().isEmpty()
283: || !this .getQueryOperations(true).isEmpty();
284: }
285:
286: /**
287: * The suffix given to the no transformation constant.
288: */
289: private static final String NO_TRANSFORMATION_CONSTANT_SUFFIX = "NONE";
290:
291: /**
292: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getDaoNoTransformationConstantName()
293: */
294: protected String handleGetDaoNoTransformationConstantName() {
295: return SpringGlobals.TRANSFORMATION_CONSTANT_PREFIX
296: + NO_TRANSFORMATION_CONSTANT_SUFFIX;
297: }
298:
299: /**
300: * Common routine to check inheritance.
301: */
302: protected boolean checkHibInheritance(String inheritance) {
303: return inheritance.equals(getHibernateInheritanceStrategy());
304: }
305:
306: /**
307: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#isHibernateInheritanceClass()
308: */
309: protected boolean handleIsHibernateInheritanceClass() {
310: return checkHibInheritance(INHERITANCE_STRATEGY_CLASS);
311: }
312:
313: /**
314: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#isHibernateInheritanceInterface()
315: */
316: protected boolean handleIsHibernateInheritanceInterface() {
317: return checkHibInheritance(INHERITANCE_STRATEGY_INTERFACE);
318: }
319:
320: /**
321: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#isHibernateInheritanceSubclass()
322: */
323: protected boolean handleIsHibernateInheritanceSubclass() {
324: return checkHibInheritance(INHERITANCE_STRATEGY_SUBCLASS);
325: }
326:
327: /**
328: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#isHibernateInheritanceConcrete()
329: */
330: protected boolean handleIsHibernateInheritanceConcrete() {
331: return checkHibInheritance(INHERITANCE_STRATEGY_CONCRETE);
332: }
333:
334: /**
335: * Stores the default hibernate inheritance strategy.
336: */
337: private static final String INHERITANCE_STRATEGY = "hibernateInheritanceStrategy";
338:
339: /**
340: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getHibernateInheritanceStrategy()
341: */
342: protected String handleGetHibernateInheritanceStrategy() {
343: String inheritance = this .getInheritance(this );
344: for (SpringEntity super Entity = this .getSuperEntity(); super Entity != null
345: && StringUtils.isBlank(inheritance);) {
346: inheritance = super Entity.getHibernateInheritanceStrategy();
347: }
348: inheritance = inheritance != null ? inheritance.toLowerCase()
349: : null;
350: if (StringUtils.isBlank(inheritance)
351: || !inheritanceStrategies.contains(inheritance)) {
352: inheritance = this .getDefaultInheritanceStrategy();
353: }
354: return inheritance;
355: }
356:
357: /**
358: * Gets the default hibernate inhertance strategy.
359: *
360: * @return the default hibernate inheritance strategy.
361: */
362: private String getDefaultInheritanceStrategy() {
363: return String.valueOf(this
364: .getConfiguredProperty(INHERITANCE_STRATEGY));
365: }
366:
367: /**
368: * Return the inheritance tagged value for for given <code>entity</code>.
369: *
370: * @param the SpringEntity from which to retrieve the inheritance tagged value.
371: * @return String inheritance tagged value.
372: */
373: private String getInheritance(SpringEntity entity) {
374: String inheritance = null;
375: if (entity != null) {
376: Object value = entity
377: .findTaggedValue(SpringProfile.TAGGEDVALUE_HIBERNATE_INHERITANCE);
378: if (value != null) {
379: inheritance = String.valueOf(value);
380: }
381: }
382: return inheritance;
383: }
384:
385: /**
386: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#isRequiresHibernateMapping()
387: */
388: protected boolean handleIsRequiresHibernateMapping() {
389: final SpringEntity super Entity = this .getSuperEntity();
390: return SpringHibernateUtils
391: .mapSubclassesInSeparateFile((String) this
392: .getConfiguredProperty(SpringGlobals.HIBERNATE_MAPPING_STRATEGY))
393: || this .isRoot()
394: && (!this .isHibernateInheritanceInterface()
395: || this .getSpecializations().isEmpty() || (super Entity != null && super Entity
396: .isHibernateInheritanceInterface()));
397: }
398:
399: /**
400: * Indicates if this entity as a <code>root</code> entity (meaning it doesn't specialize anything).
401: */
402: private boolean isRoot() {
403: final SpringEntity super Entity = this .getSuperEntity();
404: boolean abstractConcreteEntity = (this
405: .isHibernateInheritanceConcrete() || this
406: .isHibernateInheritanceInterface())
407: && this .isAbstract();
408: return (this .getSuperEntity() == null || (super Entity
409: .isHibernateInheritanceInterface() || super Entity
410: .isHibernateInheritanceConcrete()))
411: && !abstractConcreteEntity;
412: }
413:
414: /**
415: * Gets the super entity for this entity (if one exists). If a generalization does not exist OR if it's not an
416: * instance of SpringEntity then return null.
417: *
418: * @return the super entity or null if one doesn't exist.
419: */
420: private SpringEntity getSuperEntity() {
421: SpringEntity super Entity = null;
422: if (this .getGeneralization() != null
423: && this .getGeneralization() instanceof SpringEntity) {
424: super Entity = (SpringEntity) this .getGeneralization();
425: }
426: return super Entity;
427: }
428:
429: /**
430: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#getAttributeEmbeddedValueList()
431: */
432: protected String handleGetAttributeEmbeddedValueList() {
433: final Collection embeddedValues = new ArrayList();
434: for (final Iterator iterator = this .getAttributes(true)
435: .iterator(); iterator.hasNext();) {
436: final AttributeFacade attribute = (AttributeFacade) iterator
437: .next();
438: final ClassifierFacade type = attribute.getType();
439: if (type != null
440: && type
441: .hasStereotype(UMLProfile.STEREOTYPE_EMBEDDED_VALUE)) {
442: embeddedValues.add(attribute.getName());
443: }
444: }
445: final StringBuffer buffer = new StringBuffer();
446: for (final Iterator iterator = embeddedValues.iterator(); iterator
447: .hasNext();) {
448: final String name = (String) iterator.next();
449: if (StringUtils.isNotBlank(name)) {
450: buffer.append('\"' + name + '\"');
451: if (iterator.hasNext()) {
452: buffer.append(", ");
453: }
454: }
455: }
456: return buffer.toString();
457: }
458:
459: /**
460: * @see org.andromda.cartridges.spring.metafacades.SpringEntity#isRichClient()
461: */
462: protected boolean handleIsRichClient() {
463: String richClient = StringUtils.trimToEmpty(String.valueOf(this
464: .getConfiguredProperty("richClient")));
465:
466: return richClient.equalsIgnoreCase("true");
467: }
468:
469: }
|