001: package org.andromda.cartridges.spring;
002:
003: import java.util.Collection;
004: import java.util.LinkedHashSet;
005: import java.util.List;
006: import java.util.ArrayList;
007: import java.util.Iterator;
008: import java.util.LinkedHashMap;
009: import java.util.Map;
010:
011: import org.andromda.cartridges.spring.metafacades.SpringService;
012: import org.andromda.metafacades.uml.Service;
013: import org.andromda.metafacades.uml.ModelElementFacade;
014: import org.apache.commons.collections.Closure;
015: import org.apache.commons.collections.CollectionUtils;
016: import org.apache.commons.collections.Predicate;
017:
018: /**
019: * Contains utilities used within the Spring cartridge.
020: *
021: * @author Chad Brandon
022: * @author Joel Kozikowski
023: */
024: public class SpringUtils {
025: /**
026: * Retrieves all roles from the given <code>services</code> collection.
027: *
028: * @param services the collection services.
029: * @return all roles from the collection.
030: */
031: public Collection getAllRoles(Collection services) {
032: final Collection allRoles = new LinkedHashSet();
033: CollectionUtils.forAllDo(services, new Closure() {
034: public void execute(Object object) {
035: if (object != null
036: && Service.class.isAssignableFrom(object
037: .getClass())) {
038: allRoles.addAll(((Service) object).getAllRoles());
039: }
040: }
041: });
042: return allRoles;
043: }
044:
045: /**
046: * Indicates if any remote EJBs are present in the collection
047: * of services.
048: *
049: * @param services the collection of services to check.
050: * @return true/false.
051: */
052: public boolean remoteEjbsPresent(final Collection services) {
053: boolean present = services != null && !services.isEmpty();
054: if (present) {
055: present = CollectionUtils.find(services, new Predicate() {
056: public boolean evaluate(final Object object) {
057: boolean valid = false;
058: if (object instanceof SpringService) {
059: final SpringService service = (SpringService) object;
060: valid = service.isEjbRemoteView();
061: }
062: return valid;
063: }
064: }) != null;
065: }
066: return present;
067: }
068:
069: /**
070: * Indicates if any local EJBs are present in the collection
071: * of services.
072: *
073: * @param services the collection of services to check.
074: * @return true/false.
075: */
076: public boolean localEjbsPresent(final Collection services) {
077: boolean present = services != null && !services.isEmpty();
078: if (present) {
079: present = CollectionUtils.find(services, new Predicate() {
080: public boolean evaluate(final Object object) {
081: boolean valid = false;
082: if (object instanceof SpringService) {
083: final SpringService service = (SpringService) object;
084: valid = service.isEjbLocalView();
085: }
086: return valid;
087: }
088: }) != null;
089: }
090: return present;
091: }
092:
093: /**
094: * Indicates if any Spring remotable services are present.
095: *
096: * @param services the collection of services to check.
097: * @return true/false.
098: */
099: public boolean remotableServicesPresent(final Collection services) {
100: boolean present = services != null && !services.isEmpty();
101: if (present) {
102: present = CollectionUtils.find(services, new Predicate() {
103: public boolean evaluate(final Object object) {
104: boolean valid = false;
105: if (object instanceof SpringService) {
106: final SpringService service = (SpringService) object;
107: valid = service.isRemotable();
108: }
109: return valid;
110: }
111: }) != null;
112: }
113: return present;
114: }
115:
116: /**
117: * Based on the given <code>value</code>, this method will return
118: * a formatted Spring property (including the handling of 'null').
119: *
120: * @param value the value from which to create the spring value.
121: * @return the spring value.
122: */
123: public String getSpringPropertyValue(final String value) {
124: String propertyValue = "";
125: if (value != null) {
126: if ("null".equalsIgnoreCase(value)) {
127: propertyValue = "<null/>";
128: } else {
129: propertyValue = "<value>" + value + "</value>";
130: }
131: }
132: return propertyValue;
133: }
134:
135: /**
136: * Removes generics from string. Currently used to strip generics
137: * from ejb-jar.xml method parameters.
138: *
139: * @param String containing generics
140: * @return String with generics stripped
141: */
142: public String removeGenerics(final String parameter) {
143: int position = parameter.indexOf("<");
144: String result = parameter;
145: if (position != -1) {
146: result = result.substring(0, position);
147: }
148: return result;
149: }
150:
151: /**
152: * Are we generating code for a rich client?
153: */
154: private boolean richClient = false;
155:
156: /**
157: * Sets if code is being generated for a rich client.
158: */
159: public void setRichClient(final boolean richClientProperty) {
160: this .richClient = richClientProperty;
161: }
162:
163: /**
164: * Returns TRUE if code is being generated for a rich client environment
165: */
166: public boolean isRichClient() {
167: return this .richClient;
168: }
169:
170: /**
171: * Returns the class name part of a fully qualified name
172: * @param fullyQualifiedName
173: * @return just the "class name" part of the fully qualified name
174: */
175: public String getClassName(String fullyQualifiedName) {
176: String className;
177: if (fullyQualifiedName != null
178: && fullyQualifiedName.length() > 0) {
179: int lastDot = fullyQualifiedName.lastIndexOf('.');
180: if (lastDot >= 0)
181: className = fullyQualifiedName.substring(lastDot + 1);
182: else
183: className = fullyQualifiedName;
184: } else
185: className = "";
186:
187: return className;
188: }
189:
190: /**
191: * Returns the package name part of a fully qualified name
192: * @param fullyQualifiedName
193: * @return just the "package" part of the fully qualified name
194: */
195: public String getPackageName(String fullyQualifiedName) {
196: String packageName;
197: if (fullyQualifiedName != null
198: && fullyQualifiedName.length() > 0) {
199: int lastDot = fullyQualifiedName.lastIndexOf('.');
200: if (lastDot >= 0)
201: packageName = fullyQualifiedName.substring(0, lastDot);
202: else
203: packageName = "";
204: } else
205: packageName = "";
206:
207: return packageName;
208: }
209:
210: /**
211: * Returns an ordered set containing the argument model elements, model elements with a name that is already
212: * used by another model element in the argument collection will not be returned.
213: * The first operation with a name not already encountered will be returned, the order inferred by the
214: * argument's iterator will determine the order of the returned list.
215: *
216: * @param modelElements a collection of model elements, elements that are not model elements will be ignored
217: * @return the argument model elements without, elements with a duplicate name will only be recorded once
218: */
219: public List filterUniqueByName(Collection modelElements) {
220: final Map filteredElements = new LinkedHashMap();
221:
222: for (final Iterator elementIterator = modelElements.iterator(); elementIterator
223: .hasNext();) {
224: final Object object = elementIterator.next();
225: if (object instanceof ModelElementFacade) {
226: final ModelElementFacade modelElement = (ModelElementFacade) object;
227: if (!filteredElements.containsKey(modelElement
228: .getName())) {
229: filteredElements.put(modelElement.getName(),
230: modelElement);
231: }
232: }
233: }
234:
235: return new ArrayList(filteredElements.values());
236: }
237: }
|