001: package org.andromda.cartridges.ejb.metafacades;
002:
003: import org.andromda.cartridges.ejb.EJBProfile;
004: import org.andromda.core.common.ExceptionUtils;
005: import org.andromda.metafacades.uml.AttributeFacade;
006: import org.andromda.metafacades.uml.ClassifierFacade;
007: import org.andromda.metafacades.uml.ModelElementFacade;
008: import org.andromda.metafacades.uml.OperationFacade;
009: import org.andromda.metafacades.uml.UMLProfile;
010: import org.apache.commons.collections.CollectionUtils;
011: import org.apache.commons.collections.Predicate;
012: import org.apache.commons.lang.StringUtils;
013:
014: import java.util.ArrayList;
015: import java.util.Collection;
016: import java.util.Iterator;
017: import java.util.List;
018:
019: /**
020: * Contains utilities for use with EJB metafacades.
021: *
022: * @author Chad Brandon
023: */
024: class EJBMetafacadeUtils {
025:
026: /**
027: * Gets all create methods for the given <code>classifier</code>.
028: *
029: * @param follow if true, all super type create methods are also retrieved
030: * @return Collection of create methods found.
031: */
032: static Collection getCreateMethods(ClassifierFacade classifier,
033: boolean follow) {
034: ExceptionUtils.checkNull("classifer", classifier);
035: Collection retval = new ArrayList();
036: ClassifierFacade entity = classifier;
037: do {
038: Collection ops = entity.getOperations();
039: for (final Iterator i = ops.iterator(); i.hasNext();) {
040: OperationFacade op = (OperationFacade) i.next();
041: if (op
042: .hasStereotype(EJBProfile.STEREOTYPE_CREATE_METHOD)) {
043: retval.add(op);
044: }
045: }
046: if (follow) {
047: entity = (ClassifierFacade) entity.getGeneralization();
048: }
049: } while (follow && entity != null);
050: return retval;
051: }
052:
053: /**
054: * Gets the interface name for the passed in <code>classifier</code>. Returns 'LocalHome' if the mode element has
055: * the entity stereotype, returns 'Home' otherwise.
056: *
057: * @return the interface name.
058: */
059: static String getHomeInterfaceName(ClassifierFacade classifier) {
060: ExceptionUtils.checkNull("classifer", classifier);
061: String homeInterfaceName;
062: if (classifier.hasStereotype(UMLProfile.STEREOTYPE_ENTITY)) {
063: homeInterfaceName = classifier.getName() + "LocalHome";
064: } else {
065: homeInterfaceName = classifier.getName() + "Home";
066: }
067: return homeInterfaceName;
068: }
069:
070: /**
071: * Gets the view type for the passed in <code>classifier</code>. Returns 'local' if the model element has the entity
072: * stereotype, others checks there ejb tagged value and if there is no value defined, returns 'remote'.
073: *
074: * @return String the view type name.
075: */
076: static String getViewType(ClassifierFacade classifier) {
077: ExceptionUtils.checkNull("classifer", classifier);
078: String viewType = "local";
079: if (classifier.hasStereotype(EJBProfile.STEREOTYPE_SERVICE)) {
080: String viewTypeValue = (String) classifier
081: .findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_VIEWTYPE);
082: // if the view type wasn't found, search all super classes
083: if (StringUtils.isEmpty(viewTypeValue)) {
084: viewType = (String) CollectionUtils.find(classifier
085: .getAllGeneralizations(), new Predicate() {
086: public boolean evaluate(Object object) {
087: return ((ModelElementFacade) object)
088: .findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_VIEWTYPE) != null;
089: }
090: });
091: }
092: if (StringUtils.isNotEmpty(viewTypeValue)) {
093: viewType = viewTypeValue;
094: } else {
095: viewType = "remote";
096: }
097: }
098: return viewType.toLowerCase();
099: }
100:
101: /**
102: * Gets all the inherited instance attributes, excluding the instance attributes directory from this
103: * <code>classifier</code>.
104: *
105: * @param classifer the ClassifierFacade from which to retrieve the inherited attributes.
106: * @return a list of ordered attributes.
107: */
108: static List getInheritedInstanceAttributes(
109: ClassifierFacade classifier) {
110: ExceptionUtils.checkNull("classifer", classifier);
111: ClassifierFacade current = (ClassifierFacade) classifier
112: .getGeneralization();
113: if (current == null) {
114: return new ArrayList();
115: }
116: List retval = getInheritedInstanceAttributes(current);
117:
118: if (current.getInstanceAttributes() != null) {
119: retval.addAll(current.getInstanceAttributes());
120: }
121: return retval;
122: }
123:
124: /**
125: * Gets all instance attributes including those instance attributes belonging to the <code>classifier</code> and any
126: * inherited ones.
127: *
128: * @param classifier the ClassifierFacade from which to retrieve the instance attributes.
129: * @return the list of all instance attributes.
130: */
131: static List getAllInstanceAttributes(ClassifierFacade classifier) {
132: ExceptionUtils.checkNull("classifer", classifier);
133: List retval = getInheritedInstanceAttributes(classifier);
134: retval.addAll(classifier.getInstanceAttributes());
135: return retval;
136: }
137:
138: /**
139: * Gets all environment entries for the specified <code>classifier</code>. If <code>follow</code> is true, then a
140: * search up the inheritance hierachy will be performed and all super type environment entries will also be
141: * retrieved.
142: *
143: * @param classifier the classifier from which to retrieve the env-entries
144: * @param follow true/false on whether or not to 'follow' the inheritance hierarchy when retrieving the
145: * env-entries.
146: * @return the collection of enviroment entries
147: */
148: static Collection getEnvironmentEntries(
149: ClassifierFacade classifier, boolean follow) {
150: ExceptionUtils.checkNull("classifer", classifier);
151:
152: Collection attributes = classifier.getStaticAttributes();
153:
154: if (follow) {
155: for (classifier = (ClassifierFacade) classifier
156: .getGeneralization(); classifier != null; classifier = (ClassifierFacade) classifier
157: .getGeneralization()) {
158: attributes.addAll(classifier.getStaticAttributes());
159: }
160: }
161:
162: CollectionUtils.filter(attributes, new Predicate() {
163: public boolean evaluate(Object object) {
164: return ((AttributeFacade) object)
165: .hasStereotype(EJBProfile.STEREOTYPE_ENV_ENTRY);
166: }
167: });
168:
169: return attributes;
170: }
171:
172: /**
173: * Gets all constants for the specified <code>classifier</code>. If <code>follow</code> is true, then a search up
174: * the inheritance hierachy will be performed and all super type constants will also be retrieved.
175: *
176: * @param classifier the classifier from which to retrieve the constants
177: * @param follow true/false on whether or not to 'follow' the inheritance hierarchy when retrieving the
178: * constants.
179: * @return the collection of enviroment entries
180: */
181: static Collection getConstants(ClassifierFacade classifier,
182: boolean follow) {
183: ExceptionUtils.checkNull("classifer", classifier);
184:
185: Collection attributes = classifier.getStaticAttributes();
186:
187: if (follow) {
188: for (classifier = (ClassifierFacade) classifier
189: .getGeneralization(); classifier != null; classifier = (ClassifierFacade) classifier
190: .getGeneralization()) {
191: attributes.addAll(classifier.getStaticAttributes());
192: }
193: }
194:
195: CollectionUtils.filter(attributes, new Predicate() {
196: public boolean evaluate(Object object) {
197: return !((AttributeFacade) object)
198: .hasStereotype(EJBProfile.STEREOTYPE_ENV_ENTRY);
199: }
200: });
201:
202: return attributes;
203: }
204:
205: /**
206: * Returns true/false based on whether or not synthetic or auto generated create methods should be allowed.
207: *
208: * @param classifier the entity or session EJB.
209: * @return true/false
210: */
211: static boolean allowSyntheticCreateMethod(
212: ClassifierFacade classifier) {
213: ExceptionUtils.checkNull("classifer", classifier);
214: return !classifier.isAbstract()
215: && classifier
216: .findTaggedValue(EJBProfile.TAGGEDVALUE_EJB_NO_SYNTHETIC_CREATE_METHOD) == null;
217: }
218:
219: }
|