1 package org.andromda.cartridges.ejb3;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.StringTokenizer;
6 import org.andromda.cartridges.ejb3.metafacades.EJB3AssociationEndFacade;
7 import org.andromda.cartridges.ejb3.metafacades.EJB3EntityAttributeFacade;
8 import org.andromda.metafacades.uml.ModelElementFacade;
9 import org.apache.commons.collections.CollectionUtils;
10 import org.apache.commons.collections.Predicate;
11 import org.apache.commons.lang.StringUtils;
12
13 /**
14 * Transform class for the EJB3 cartridge.
15 *
16 * @author Richard Kunze
17 * @author Chad Brandon
18 * @author Vance Karimi
19 * @author Michail Plushnikov
20 */
21 public class EJB3ScriptHelper
22 {
23 /**
24 * Create a collection of String objects representing the argument names.
25 *
26 * @param args A comma separated list of arguments
27 * @return Collection A collection of of Strings representing the arguments
28 */
29 public Collection<String> getArgumentsAsList(final String args)
30 {
31 StringTokenizer st = new StringTokenizer(args, ",");
32 Collection<String> retval = new ArrayList<String>(st.countTokens());
33 while (st.hasMoreTokens())
34 {
35 retval.add(st.nextToken().trim());
36 }
37 return retval;
38 }
39
40 /**
41 * Filter a list of model elements by visibility.
42 *
43 * @param list the original list
44 * @param visibility the visibility - "public" "protected", "private" or the empty string (for package visibility)
45 * @return a list with all elements from the original list that have a matching visibility.
46 */
47 public Collection<ModelElementFacade> filterByVisibility(final Collection<ModelElementFacade> list, final String visibility)
48 {
49 return CollectionUtils.select(list, new Predicate()
50 {
51 public boolean evaluate(final Object pObj)
52 {
53 ModelElementFacade elem = (ModelElementFacade )pObj;
54 return visibility.equals(elem.getVisibility());
55 }
56 });
57 }
58
59 /**
60 * Filter a list of EntityAttributes by removing all non-updatable attributes.
61 * This filter currently removes all attributes that are of stereotype Version
62 * It also removes identifier attributes for an entity with a composite primary key class
63 * or if the identifier attribute has a specified generator.
64 *
65 * @param list The original list
66 * @param isCompositePKPresent True if entity has a composite primary key
67 * @return Collection A list of EntityAttributes from the original list that are updatable
68 */
69 public Collection<EJB3EntityAttributeFacade> filterUpdatableAttributes(final Collection<EJB3EntityAttributeFacade> list, final boolean isCompositePKPresent)
70 {
71 return CollectionUtils.select(list, new Predicate()
72 {
73 public boolean evaluate(final Object pObj)
74 {
75 if (pObj instanceof EJB3EntityAttributeFacade)
76 {
77 EJB3EntityAttributeFacade attr = (EJB3EntityAttributeFacade)pObj;
78 return !attr.isVersion() &&
79 ((isCompositePKPresent && !attr.isIdentifier()) ||
80 (!isCompositePKPresent && (attr.isIdentifier() && attr.isGeneratorTypeNone()) ||
81 !attr.isIdentifier()));
82 }
83 else
84 {
85 System.out.println("NOT EJB3EntityAttributeFacade: " + pObj);
86 return false;
87 }
88 }
89 });
90 }
91
92 /**
93 * Filter a list of EntityAttributes by removing all audit attributes.
94 * This filter currently removes all attributes that are of stereotype Version
95 * It also removes identifier attributes for an entity with a composite primary key class
96 * or if the identifier attribute have a specified generator.
97 *
98 * @param list The original list
99 * @param jpaFramework If using a JPA auditing framework. Filter none if not using it.
100 * @return Collection A list of EntityAttributes from the original list that are updatable
101 */
102 public Collection<EJB3EntityAttributeFacade> filterAuditAttributes(final Collection<EJB3EntityAttributeFacade> list, final boolean jpaFramework)
103 {
104 return CollectionUtils.select(list, new Predicate()
105 {
106 public boolean evaluate(final Object pObj)
107 {
108 // returns true if attribute should not be filtered out
109 if (pObj instanceof EJB3EntityAttributeFacade)
110 {
111 EJB3EntityAttributeFacade attr = (EJB3EntityAttributeFacade)pObj;
112 /*System.out.println(attr.getOwner().getName() + " jpa=" + jpaFramework + " attr=" + attr.getName()
113 + " return=" + (!jpaFramework || !(attr.getName().equals("createDate")
114 || attr.getName().equals("createdBy") || attr.getName().equals("updateDate")
115 || attr.getName().equals("updatedBy"))));*/
116 return (!jpaFramework || !(attr.getName().equals("createDate")
117 || attr.getName().equals("createdBy") || attr.getName().equals("updateDate")
118 || attr.getName().equals("updatedBy")));
119 }
120 else
121 {
122 //System.out.println("NOT EJB3EntityAttributeFacade: " + pObj);
123 return false;
124 }
125 }
126 });
127 }
128
129 /**
130 * Filter a list of EntityAttributes by removing all non-required attributes.
131 * This filter currently removes all attributes that are of stereotype Version
132 * It also removes identifier attributes for an entity with a composite primary key class
133 * or if the identifier attribute have a specified generator.
134 *
135 * @param list The original list
136 * @param isCompositePKPresent True if entity has a composite primary key
137 * @return Collection A list of EntityAttributes from the original list that are updatable
138 */
139 public Collection<EJB3EntityAttributeFacade> filterRequiredAttributes(final Collection<EJB3EntityAttributeFacade> list, final boolean isCompositePKPresent)
140 {
141 return CollectionUtils.select(list, new Predicate()
142 {
143 public boolean evaluate(final Object pObj)
144 {
145 if (pObj instanceof EJB3EntityAttributeFacade)
146 {
147 // Wrapped primitive may be set to column nullable = false to override nullable datatype
148 EJB3EntityAttributeFacade attr = (EJB3EntityAttributeFacade)pObj;
149 return !attr.isVersion() && !attr.isColumnNullable() &&
150 (!attr.isIdentifier() || (!isCompositePKPresent &&
151 attr.isGeneratorTypeNone())
152 /*((isCompositePKPresent && !attr.isIdentifier()) ||
153 (!isCompositePKPresent && (!attr.isIdentifier() ||
154 (attr.isGeneratorTypeNone()))*/
155 );
156 }
157 else
158 {
159 System.out.println("NOT EJB3EntityAttributeFacade: " + pObj);
160 return false;
161 }
162 }
163 });
164 }
165
166 /**
167 * Filter a list of EntityAssociationEnds by removing all non-required AssociationEnds.
168 * It also removes identifier AssociationEnds for an entity with a composite primary key class
169 *
170 * @param list The original list
171 * @param isCompositePKPresent True if entity has a composite primary key
172 * @return Collection A list of EntityAssociationEnds from the original list that are updatable
173 */
174 public Collection<EJB3AssociationEndFacade> filterRequiredAssociations(final Collection<EJB3AssociationEndFacade> list, final boolean isCompositePKPresent)
175 {
176 return CollectionUtils.select(list, new Predicate()
177 {
178 public boolean evaluate(final Object pObj)
179 {
180 if (pObj instanceof EJB3AssociationEndFacade)
181 {
182 EJB3AssociationEndFacade assoc = (EJB3AssociationEndFacade)pObj;
183 return assoc.getOtherEnd().isRequired() || assoc.isIdentifier();
184 /*return attr.isRequired() &&
185 ((isCompositePKPresent && !attr.isIdentifier()) ||
186 (!isCompositePKPresent && (!attr.isIdentifier() ||
187 (attr.isIdentifier()))
188 ));*/
189 }
190 else
191 {
192 System.out.println("NOT EJB3AssociationEndFacade: " + pObj);
193 return false;
194 }
195 }
196 });
197 }
198
199 /**
200 * Replaces all instances of the dot (.) in the name argument with an underscore (_)
201 * and returns the string response.
202 *
203 * @param name The name, typically a fully qualified name with dot notation
204 * @return The string with all dots replaced with underscore.
205 */
206 public String toUnderscoreName(final String name)
207 {
208 return StringUtils.replaceChars(name, '.', '_');
209 }
210
211 private static final String BACKSLASH = "\"";
212 private static final String QUOTE = "'";
213 /**
214 * Removes instances of the quotation marks (" and ') at the beginning and end of the value argument
215 *
216 * @param pValue The value, which can contains leading and trailing quotation marks
217 * @return The string without quotation marks
218 */
219 public String removeQuotationmarks(final String pValue)
220 {
221 String result = StringUtils.removeStart(pValue, BACKSLASH);
222 result = StringUtils.removeEnd(result, BACKSLASH);
223 result = StringUtils.removeStart(result, QUOTE);
224 return StringUtils.removeEnd(result, QUOTE);
225 }
226
227 private static final String COMMA = ", ";
228 /**
229 * Returns the comma separated list of interceptor classes.
230 *
231 * @param interceptors The collection ModelElementFacade elements representing the interceptors
232 * @param prepend Prefix any interceptors to the comma separated list
233 * @return String containing the comma separated fully qualified class names
234 */
235 public String getInterceptorsAsList(final Collection<ModelElementFacade> interceptors, final String prepend)
236 {
237 StringBuilder sb = new StringBuilder();
238 String separator = "";
239
240 if (StringUtils.isNotBlank(prepend))
241 {
242 sb.append(prepend);
243 separator = COMMA;
244 }
245
246 for (ModelElementFacade interceptor : interceptors)
247 {
248 sb.append(separator);
249 separator = COMMA;
250 sb.append(interceptor.getFullyQualifiedName()).append(".class");
251 }
252 return sb.toString();
253 }
254
255 /**
256 * Reverses the <code>packageName</code>.
257 *
258 * @param packageName the package name to reverse.
259 * @return the reversed package name.
260 */
261 public static String reversePackage(final String packageName)
262 {
263 return StringUtils.reverseDelimited(packageName, EJB3Globals.NAMESPACE_DELIMITER);
264 }
265 }