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
15
16
17
18
19
20
21 public class EJB3ScriptHelper
22 {
23
24
25
26
27
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
42
43
44
45
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
61
62
63
64
65
66
67
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
94
95
96
97
98
99
100
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
109 if (pObj instanceof EJB3EntityAttributeFacade)
110 {
111 EJB3EntityAttributeFacade attr = (EJB3EntityAttributeFacade)pObj;
112
113
114
115
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
123 return false;
124 }
125 }
126 });
127 }
128
129
130
131
132
133
134
135
136
137
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
148 EJB3EntityAttributeFacade attr = (EJB3EntityAttributeFacade)pObj;
149 return !attr.isVersion() && !attr.isColumnNullable() &&
150 (!attr.isIdentifier() || (!isCompositePKPresent &&
151 attr.isGeneratorTypeNone())
152
153
154
155 );
156 }
157 else
158 {
159 System.out.println("NOT EJB3EntityAttributeFacade: " + pObj);
160 return false;
161 }
162 }
163 });
164 }
165
166
167
168
169
170
171
172
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
185
186
187
188
189 }
190 else
191 {
192 System.out.println("NOT EJB3AssociationEndFacade: " + pObj);
193 return false;
194 }
195 }
196 });
197 }
198
199
200
201
202
203
204
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
215
216
217
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
230
231
232
233
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
257
258
259
260
261 public static String reversePackage(final String packageName)
262 {
263 return StringUtils.reverseDelimited(packageName, EJB3Globals.NAMESPACE_DELIMITER);
264 }
265 }