001: package org.andromda.cartridges.ejb.metafacades;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Collections;
006: import java.util.Iterator;
007: import java.util.List;
008:
009: import org.andromda.cartridges.ejb.EJBGlobals;
010: import org.andromda.cartridges.ejb.EJBProfile;
011: import org.andromda.core.common.ExceptionRecorder;
012: import org.andromda.metafacades.uml.AttributeFacade;
013: import org.andromda.metafacades.uml.ClassifierFacade;
014: import org.andromda.metafacades.uml.DependencyFacade;
015: import org.andromda.metafacades.uml.MetafacadeUtils;
016: import org.andromda.metafacades.uml.OperationFacade;
017: import org.andromda.metafacades.uml.TypeMappings;
018: import org.andromda.metafacades.uml.UMLMetafacadeProperties;
019: import org.apache.commons.collections.CollectionUtils;
020: import org.apache.commons.collections.Predicate;
021: import org.apache.commons.lang.StringUtils;
022:
023: /**
024: * <p/>
025: * Represents an entity EJB. </p> Metaclass facade implementation.
026: */
027: public class EJBEntityFacadeLogicImpl extends EJBEntityFacadeLogic {
028: // ---------------- constructor -------------------------------
029: public EJBEntityFacadeLogicImpl(java.lang.Object metaObject,
030: String context) {
031: super (metaObject, context);
032: }
033:
034: public Collection handleGetIdentifiers() {
035: Collection identifiers = new ArrayList();
036: Iterator iter = this .getSourceDependencies().iterator();
037: while (iter.hasNext()) {
038: DependencyFacade dep = (DependencyFacade) iter.next();
039: if (dep.hasStereotype(EJBProfile.STEREOTYPE_IDENTIFIER)) {
040: identifiers = ((ClassifierFacade) dep
041: .getTargetElement()).getInstanceAttributes();
042: MetafacadeUtils.filterByStereotype(identifiers,
043: EJBProfile.STEREOTYPE_IDENTIFIER);
044: return identifiers;
045: }
046: }
047:
048: // No PK dependency found - try a PK attribute
049: if (super .getIdentifiers() != null
050: && !super .getIdentifiers().isEmpty()) {
051: AttributeFacade attr = (AttributeFacade) super
052: .getIdentifiers().iterator().next();
053: identifiers.add(attr);
054: return identifiers;
055: }
056:
057: // Still nothing found - recurse up the inheritance tree
058: EJBEntityFacade decorator = (EJBEntityFacade) this
059: .getGeneralization();
060: return decorator.getIdentifiers();
061: }
062:
063: /**
064: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getAllEntityRelations()
065: */
066: protected java.util.Collection handleGetAllEntityRelations() {
067: // Only concrete entities may have EJB relations. Return
068: // an empty collection for everything else
069: if (this .isAbstract()) {
070: return Collections.EMPTY_LIST;
071: }
072: Collection result = new ArrayList();
073: result.addAll(getEntityRelations());
074: ClassifierFacade classifier = (ClassifierFacade) this
075: .getGeneralization();
076: while (classifier != null
077: && classifier instanceof EJBEntityFacade
078: && classifier.isAbstract()) {
079: EJBEntityFacade entity = (EJBEntityFacade) classifier;
080: result.addAll(entity.getEntityRelations());
081: classifier = (ClassifierFacade) classifier
082: .getGeneralization();
083: }
084: return result;
085: }
086:
087: /**
088: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getViewType()
089: */
090: protected String handleGetViewType() {
091: return EJBMetafacadeUtils.getViewType(this );
092: }
093:
094: /**
095: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getEntityRelations()
096: */
097: protected java.util.Collection handleGetEntityRelations() {
098: Collection result = new ArrayList();
099: for (final Iterator iterator = this .getAssociationEnds()
100: .iterator(); iterator.hasNext();) {
101: EJBAssociationEndFacade associationEnd = (EJBAssociationEndFacade) iterator
102: .next();
103: ClassifierFacade target = associationEnd.getOtherEnd()
104: .getType();
105: if (target instanceof EJBEntityFacade
106: && associationEnd.getOtherEnd().isNavigable()) {
107: // Check the integrity constraint
108: Object value = associationEnd.getOtherEnd()
109: .getAssociation().findTaggedValue(
110: EJBProfile.TAGGEDVALUE_GENERATE_CMR);
111: String generateCmr = value == null ? null : value
112: .toString();
113: if (target.isAbstract()
114: && !"false".equalsIgnoreCase(generateCmr)) {
115: throw new IllegalStateException(
116: "Relation '"
117: + associationEnd.getAssociation()
118: .getName()
119: + "' has the abstract target '"
120: + target.getName()
121: + "'. Abstract targets are not allowed in EJB.");
122: }
123: result.add(associationEnd);
124: }
125: }
126:
127: return result;
128: }
129:
130: /**
131: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getAllInstanceAttributes()
132: */
133: protected List handleGetAllInstanceAttributes() {
134: return EJBMetafacadeUtils.getAllInstanceAttributes(this );
135: }
136:
137: /**
138: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getInheritedInstanceAttributes()
139: */
140: protected List handleGetInheritedInstanceAttributes() {
141: return EJBMetafacadeUtils.getInheritedInstanceAttributes(this );
142: }
143:
144: /**
145: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getCreateMethods(boolean)
146: */
147: protected Collection handleGetCreateMethods(boolean follow) {
148: return EJBMetafacadeUtils.getCreateMethods(this , follow);
149: }
150:
151: /**
152: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getSelectMethods(boolean)
153: */
154: protected Collection handleGetSelectMethods(boolean follow) {
155: Collection retval = new ArrayList();
156: EJBEntityFacade entity = null;
157: do {
158: Collection ops = this .getOperations();
159: for (final Iterator i = ops.iterator(); i.hasNext();) {
160: OperationFacade op = (OperationFacade) i.next();
161: if (op
162: .hasStereotype(EJBProfile.STEREOTYPE_SELECT_METHOD)) {
163: retval.add(op);
164: }
165: }
166: if (follow) {
167: entity = (EJBEntityFacade) this .getGeneralization();
168: } else {
169: break;
170: }
171: } while (entity != null);
172: return retval;
173: }
174:
175: /**
176: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getHomeInterfaceName()
177: */
178: protected String handleGetHomeInterfaceName() {
179: return EJBMetafacadeUtils.getHomeInterfaceName(this );
180: }
181:
182: /**
183: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getEnvironmentEntries(boolean)
184: */
185: protected Collection handleGetEnvironmentEntries(boolean follow) {
186: return EJBMetafacadeUtils.getEnvironmentEntries(this , follow);
187: }
188:
189: /**
190: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getConstants(boolean)
191: */
192: protected Collection handleGetConstants(boolean follow) {
193: return EJBMetafacadeUtils.getConstants(this , follow);
194: }
195:
196: /**
197: * @see org.andromda.cartridges.ejb.metafacades.EJBEntity#getJndiName()
198: */
199: protected java.lang.String handleGetJndiName() {
200: StringBuffer jndiName = new StringBuffer();
201: String jndiNamePrefix = StringUtils.trimToEmpty(this
202: .getJndiNamePrefix());
203: if (StringUtils.isNotEmpty(jndiNamePrefix)) {
204: jndiName.append(jndiNamePrefix);
205: jndiName.append("/");
206: }
207: jndiName.append("ejb/");
208: jndiName.append(this .getFullyQualifiedName());
209: return jndiName.toString();
210: }
211:
212: /**
213: * Gets the <code>jndiNamePrefix</code> for this EJB.
214: *
215: * @return the EJB Jndi name prefix.
216: */
217: protected String getJndiNamePrefix() {
218: String prefix = null;
219: if (this .isConfiguredProperty(EJBGlobals.JNDI_NAME_PREFIX)) {
220: prefix = (String) this
221: .getConfiguredProperty(EJBGlobals.JNDI_NAME_PREFIX);
222: }
223: return prefix;
224: }
225:
226: /**
227: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#allowSyntheticCreateMethod()
228: */
229: protected boolean handleIsSyntheticCreateMethodAllowed() {
230: return EJBMetafacadeUtils.allowSyntheticCreateMethod(this );
231: }
232:
233: /**
234: * @see org.andromda.metafacades.uml.EntityFacade#getBusinessOperations()
235: */
236: public Collection getBusinessOperations() {
237: Collection operations = super .getBusinessOperations();
238: CollectionUtils.filter(operations, new Predicate() {
239: public boolean evaluate(Object object) {
240: boolean businessOperation = false;
241: if (EJBOperationFacade.class.isAssignableFrom(object
242: .getClass())) {
243: businessOperation = ((EJBOperationFacade) object)
244: .isBusinessOperation();
245: }
246: return businessOperation;
247: }
248: });
249: return operations;
250: }
251:
252: /**
253: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getValueDependencies()
254: */
255: protected Collection handleGetValueDependencies() {
256: Collection dependencies = super .getSourceDependencies();
257: CollectionUtils.filter(dependencies, new Predicate() {
258: public boolean evaluate(Object object) {
259: boolean isValueRef = false;
260: if (object instanceof DependencyFacade) {
261: DependencyFacade dep = (DependencyFacade) object;
262: isValueRef = dep.getStereotypeNames().contains(
263: EJBProfile.STEREOTYPE_VALUE_REF)
264: && dep
265: .getTargetElement()
266: .hasExactStereotype(
267: EJBProfile.STEREOTYPE_VALUE_OBJECT);
268: }
269: return isValueRef;
270: }
271: });
272: return dependencies;
273: }
274:
275: /**
276: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#containsIdentifier(java.lang.String)
277: */
278: protected boolean handleIsIdentifierPresent(String identifier) {
279: Collection collIdentifier = this .getIdentifiers(true);
280: Iterator it = collIdentifier.iterator();
281: while (it.hasNext()) {
282: AttributeFacade attr = (AttributeFacade) it.next();
283: if (attr.getName().equalsIgnoreCase(identifier)) {
284: return true;
285: }
286: }
287: return false;
288: }
289:
290: /**
291: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#containsAttribute(java.lang.String)
292: */
293: protected boolean handleIsAttributePresent(String strAttr) {
294: Collection collAttrib = this .getAttributes(true);
295: Iterator it = collAttrib.iterator();
296: while (it.hasNext()) {
297: AttributeFacade attr = (AttributeFacade) it.next();
298: if (attr.getName().equalsIgnoreCase(strAttr)) {
299: return true;
300: }
301: }
302: return false;
303: }
304:
305: /**
306: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#containsOperation(java.lang.String)
307: */
308: protected boolean handleIsOperationPresent(String op) {
309: Collection collOps = this .getOperations();
310: Iterator it = collOps.iterator();
311: while (it.hasNext()) {
312: OperationFacade operation = (OperationFacade) it.next();
313: if (operation.getName().equalsIgnoreCase(op)) {
314: return true;
315: }
316: }
317: return false;
318: }
319:
320: /**
321: * Gets a Mappings instance from a property registered under the given <code>propertyName</code>.
322: *
323: * @param propertyName the property name to register under.
324: * @return the Mappings instance.
325: */
326: private TypeMappings getMappingsProperty(final String propertyName) {
327: Object property = this .getConfiguredProperty(propertyName);
328: TypeMappings mappings = null;
329: String uri = null;
330: if (property instanceof String) {
331: uri = (String) property;
332: try {
333: mappings = TypeMappings.getInstance(uri);
334: this .setProperty(propertyName, mappings);
335: } catch (Throwable th) {
336: String errMsg = "Error getting '" + propertyName
337: + "' --> '" + uri + "'";
338: logger.error(errMsg);
339: // don't throw the exception
340: ExceptionRecorder.instance().record(errMsg, th);
341: }
342: } else {
343: mappings = (TypeMappings) property;
344: }
345: return mappings;
346: }
347:
348: /**
349: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getSqlType()
350: */
351: protected String handleGetSqlType() {
352: String mpSql = this .getMappingsProperty(
353: UMLMetafacadeProperties.SQL_MAPPINGS_URI).getMappings()
354: .getName();
355: if (mpSql.startsWith("Oracle")) {
356: mpSql = "ORACLE";
357: }
358: return mpSql;
359: }
360:
361: /**
362: * @see org.andromda.cartridges.ejb.metafacades.EJBEntityFacade#getTransactionType()
363: */
364: protected java.lang.String handleGetTransactionType() {
365: String transactionType = (String) this
366: .findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_TRANSACTION_TYPE);
367: if (StringUtils.isBlank(transactionType)) {
368: transactionType = transactionType = String
369: .valueOf(this
370: .getConfiguredProperty(EJBGlobals.TRANSACTION_TYPE));
371: }
372: return transactionType;
373: }
374: }
|