Wiki source code of sPearCat-Ready Superclass Template
Last modified by David Avendasora on 2008/01/28 14:52
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
3.1 | 1 | {{code}} |
2 | |||
![]() |
1.1 | 3 | #if ($entity.packageName) |
4 | package $entity.superclassPackageName; | ||
5 | #end | ||
6 | |||
![]() |
3.1 | 7 | import com.webobjects.foundation.*; |
![]() |
1.1 | 8 | #set ($useControl = "false") |
9 | #if (!$entity.sortedClassToManyRelationships.empty) | ||
10 | #set ($useControl = "true") | ||
11 | #elseif (!$entity.sortedFetchSpecs.empty) | ||
12 | #set ($useControl = "true") | ||
13 | #elseif (!$entity.isAbstract) | ||
14 | #set ($useControl = "true") | ||
15 | #elseif (!$entity.parentSet) | ||
16 | #set ($useControl = "true") | ||
17 | #end | ||
18 | #if ($useControl == "true") | ||
![]() |
3.1 | 19 | import com.webobjects.eocontrol.*; |
![]() |
1.1 | 20 | #end |
21 | #set ($useBigDecimal = "false") | ||
22 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
23 | #if (!$attribute.inherited) | ||
24 | #if ($attribute.javaClassName == "BigDecimal") #set ($useBigDecimal = "true")#end | ||
25 | #end | ||
26 | #end | ||
27 | #if ($useBigDecimal == "true") | ||
28 | import java.math.BigDecimal; | ||
29 | #end | ||
30 | #if (!$entity.sortedClassToManyRelationships.empty) | ||
![]() |
3.1 | 31 | import java.util.*; |
32 | #end | ||
![]() |
1.1 | 33 | |
![]() |
3.1 | 34 | /** |
35 | * Java Class ${entity.prefixClassNameWithOptionalPackage}.java generated for entity ${entity.name} | ||
![]() |
1.1 | 36 | #if ($entity.parentSet) |
![]() |
3.1 | 37 | * <p> |
38 | * The entity parent is ${entity.parent.classNameWithDefault} | ||
39 | * </p> | ||
![]() |
1.1 | 40 | #end |
![]() |
3.1 | 41 | * <p><b> |
42 | * DO NOT EDIT. Make changes to ${entity.classNameWithOptionalPackage}.java instead. | ||
43 | * </b></p> | ||
44 | * <p> | ||
45 | * copyright ${copyrightYear} ${copyrightBy} | ||
![]() |
1.1 | 46 | * </p> |
47 | */ | ||
48 | public abstract class ${entity.prefixClassNameWithoutPackage} extends#if ($entity.parentSet) ${entity.parent.classNameWithDefault}#else EOGenericRecord#end { | ||
49 | private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger( ${entity.prefixClassNameWithoutPackage}.class); | ||
50 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
51 | #if (!$attribute.inherited) | ||
52 | |||
53 | /** | ||
54 | * Key value for attribute $attribute.name | ||
55 | */ | ||
56 | public static final String ${attribute.capitalizedName}Key = "$attribute.name"; | ||
57 | #else | ||
58 | |||
59 | // Inherited attribute $attribute.name | ||
60 | #end | ||
61 | #end | ||
62 | #foreach ($relationship in $entity.sortedClassToOneRelationships) | ||
63 | #if (!$relationship.inherited) | ||
64 | |||
65 | /** | ||
66 | * Key value for to one relationship $relationship.name | ||
67 | */ | ||
68 | public static final String ${relationship.capitalizedName}Key = "$relationship.name"; | ||
69 | #else | ||
70 | |||
71 | // Inherited relationship $relationship.name | ||
72 | #end | ||
73 | #end | ||
74 | #foreach ($relationship in $entity.sortedClassToManyRelationships) | ||
75 | #if (!$relationship.inherited) | ||
76 | |||
77 | /** | ||
78 | * Key value for to many relationship $relationship.name | ||
79 | */ | ||
80 | public static final String ${relationship.capitalizedName}Key = "$relationship.name"; | ||
81 | #else | ||
82 | |||
83 | // Inherited relationship $relationship.name | ||
84 | #end | ||
85 | #end | ||
86 | #if (!$entity.isAbstract) | ||
87 | |||
88 | /** | ||
89 | * Returns a newly initialized instance of ${entity.name}. The new object is inserted in the editing context. | ||
90 | * | ||
91 | * @param context | ||
92 | * editing context to insert in. | ||
93 | * @return newly initialized instance | ||
94 | */ | ||
95 | public static ${entity.prefixClassNameWithoutPackage} new${entity.classNameWithoutPackage}Instance(EOEditingContext context) { | ||
96 | EOClassDescription description = EOClassDescription.classDescriptionForEntityName("${entity.name}"); | ||
97 | EOEnterpriseObject object = description.createInstanceWithEditingContext(context, null); | ||
98 | context.insertObject(object); | ||
99 | return (${entity.prefixClassNameWithoutPackage})object; | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * Returns a list of all the object in the shared editing context. | ||
104 | * | ||
105 | * @return list of ${entity.name} | ||
106 | */ | ||
107 | protected static NSArray<? extends $entity.prefixClassNameWithoutPackage> _all${entity.classNameWithoutPackage}Objects() { | ||
108 | NSMutableArray<$entity.prefixClassNameWithoutPackage> result = null; | ||
109 | NSArray<EOEnterpriseObject> aList = EOSharedEditingContext.defaultSharedEditingContext().objectsByEntityName().objectForKey("${entity.name}"); | ||
110 | if (aList != null) { | ||
111 | result = new NSMutableArray<$entity.prefixClassNameWithoutPackage>(aList.count()); | ||
112 | for (EOEnterpriseObject anObject : aList) { | ||
113 | result.addObject(anObject); | ||
114 | } | ||
115 | } | ||
116 | return (result != null ? result : NSArray.<$entity.prefixClassNameWithoutPackage> emptyArray()); | ||
117 | } | ||
118 | #end | ||
119 | |||
120 | /** | ||
121 | * Returns the name of the entity attached to this class. | ||
122 | * | ||
123 | * @return name of the entity | ||
124 | */ | ||
125 | public static String entity() { | ||
126 | return "${entity.name}"; | ||
127 | } | ||
128 | |||
129 | /** | ||
130 | * Sole Constructor | ||
131 | */ | ||
132 | public ${entity.prefixClassNameWithoutPackage}() { | ||
133 | super(); | ||
134 | } | ||
135 | #foreach ($fetchSpecification in $entity.sortedFetchSpecs) | ||
136 | |||
137 | /** | ||
138 | * Returns the object matching the fetch specification "$fetchSpecification.name". | ||
139 | * | ||
140 | * @param context | ||
141 | * editing context to fetch in. | ||
142 | #foreach ($binding in $fetchSpecification.distinctBindings) | ||
143 | * @param ${binding.name}Binding | ||
144 | * fetch specification binding. | ||
145 | #end | ||
146 | * @return list of ${entity.name} | ||
147 | */ | ||
148 | public static NSArray<?> objectsFor${fetchSpecification.capitalizedName}(EOEditingContext context#foreach ($binding in $fetchSpecification.distinctBindings), ${binding.javaClassName} ${binding.name}Binding#end) { | ||
149 | EOFetchSpecification spec = EOFetchSpecification.fetchSpecificationNamed("${fetchSpecification.name}", "${entity.name}"); | ||
150 | #if (!$fetchSpecification.distinctBindings.empty) | ||
151 | NSMutableDictionary<String, Object> bindings = new NSMutableDictionary<String, Object>(); | ||
152 | #foreach ($binding in $fetchSpecification.distinctBindings) | ||
153 | if (${binding.name}Binding != null) | ||
154 | bindings.setObjectForKey(${binding.name}Binding, "${binding.name}"); | ||
155 | #end | ||
156 | spec = spec.fetchSpecificationWithQualifierBindings(bindings); | ||
157 | #end | ||
158 | return context.objectsWithFetchSpecification(spec); | ||
159 | } | ||
160 | #end | ||
161 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
162 | #if (!$attribute.inherited) | ||
163 | |||
164 | /** | ||
165 | * Returns the attribute "$attribute.name". | ||
166 | * | ||
167 | * @return $attribute.name | ||
168 | */ | ||
169 | public $attribute.javaClassName ${attribute.name}() { | ||
170 | return ($attribute.javaClassName)this.storedValueForKey("${attribute.name}"); | ||
171 | } | ||
172 | |||
173 | /** | ||
174 | * Sets the attribute "$attribute.name". | ||
175 | * | ||
176 | * @param value | ||
177 | * $attribute.name. | ||
178 | */ | ||
179 | public void set${attribute.capitalizedName}($attribute.javaClassName value) { | ||
180 | if (logger.isDebugEnabled()) { | ||
181 | logger.debug( "value: " + value); | ||
182 | } | ||
183 | this.takeStoredValueForKey(value, "${attribute.name}"); | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * Initialize the attribute "$attribute.name". | ||
188 | * | ||
189 | * @param value | ||
190 | * $attribute.name. | ||
191 | */ | ||
192 | public void initialize${attribute.capitalizedName}($attribute.javaClassName value) { | ||
193 | this.set${attribute.capitalizedName}(value); | ||
194 | } | ||
195 | #end | ||
196 | #end | ||
197 | #foreach ($relationship in $entity.sortedClassToOneRelationships) | ||
198 | #if (!$relationship.inherited) | ||
199 | |||
200 | /** | ||
201 | * Returns the relationship "$relationship.name". | ||
202 | * | ||
203 | * @return $relationship.name | ||
204 | */ | ||
205 | public ${relationship.actualDestination.classNameWithDefault} ${relationship.name}() { | ||
206 | return (${relationship.actualDestination.classNameWithDefault})this.storedValueForKey("${relationship.name}"); | ||
207 | } | ||
208 | |||
209 | /** | ||
210 | * Sets the relationship "$relationship.name". | ||
211 | * | ||
212 | * @param value | ||
213 | * $relationship.name. | ||
214 | */ | ||
215 | public void set${relationship.capitalizedName}(${relationship.actualDestination.classNameWithDefault} value) { | ||
216 | if (logger.isDebugEnabled()) { | ||
217 | logger.debug( "value: " + value); | ||
218 | } | ||
219 | this.takeStoredValueForKey(value, "${relationship.name}"); | ||
220 | } | ||
221 | |||
222 | /** | ||
223 | * Returns the relationship "$relationship.name". | ||
224 | * | ||
225 | * @return $relationship.name | ||
226 | */ | ||
227 | public ${relationship.actualDestination.classNameWithDefault} ${relationship.name}Relationship() { | ||
228 | return (${relationship.actualDestination.classNameWithDefault})this.storedValueForKey("${relationship.name}"); | ||
229 | } | ||
230 | |||
231 | /** | ||
232 | * Sets the relationship "$relationship.name". This method takes care of setting the reverse relationship if it exists. | ||
233 | * | ||
234 | * @param value | ||
235 | * $relationship.name. | ||
236 | */ | ||
237 | public void set${relationship.capitalizedName}Relationship(${relationship.actualDestination.classNameWithDefault} value) { | ||
238 | if (value == null) { | ||
239 | ${relationship.actualDestination.classNameWithDefault} object = this.${relationship.name}(); | ||
240 | if (object != null) | ||
241 | this.removeObjectFromBothSidesOfRelationshipWithKey(object, "${relationship.name}"); | ||
242 | } else { | ||
243 | this.addObjectToBothSidesOfRelationshipWithKey(value, "${relationship.name}"); | ||
244 | } | ||
245 | } | ||
246 | |||
247 | /** | ||
248 | * Initialize the relationship "$relationship.name". | ||
249 | * | ||
250 | * @param value | ||
251 | * $relationship.name. | ||
252 | */ | ||
253 | public void initialize${relationship.capitalizedName}(${relationship.actualDestination.classNameWithDefault} value) { | ||
254 | this.set${relationship.capitalizedName}Relationship(value); | ||
255 | } | ||
256 | #end | ||
257 | #end | ||
258 | #foreach ($relationship in $entity.sortedClassToManyRelationships) | ||
259 | #if (!$relationship.inherited) | ||
260 | |||
261 | /** | ||
262 | * Returns the relationship "$relationship.name". | ||
263 | * | ||
264 | * @return list of ${relationship.actualDestination.classNameWithDefault} | ||
265 | */ | ||
266 | @SuppressWarnings("unchecked") | ||
267 | public NSArray<${relationship.actualDestination.classNameWithDefault}> ${relationship.name}() { | ||
268 | NSArray aList = (NSArray)this.storedValueForKey("${relationship.name}"); | ||
269 | if (aList != null ) { | ||
270 | return aList; | ||
271 | } else { | ||
272 | return NSArray.emptyArray(); | ||
273 | } | ||
274 | } | ||
275 | |||
276 | /** | ||
277 | * Sets the relationship "$relationship.name". | ||
278 | * | ||
279 | * @param value | ||
280 | * list of ${relationship.actualDestination.classNameWithDefault}. | ||
281 | */ | ||
282 | public void set${relationship.capitalizedName}(NSArray<${relationship.actualDestination.classNameWithDefault}> value) { | ||
283 | this.takeStoredValueForKey(value, "${relationship.name}"); | ||
284 | } | ||
285 | |||
286 | /** | ||
287 | * Adds an object to the relationship "$relationship.name". This method log a warning if the object is not in the same editing context than the receiver. | ||
288 | * | ||
289 | * @param object | ||
290 | * ${relationship.actualDestination.classNameWithDefault}. | ||
291 | */ | ||
292 | @SuppressWarnings("cast") | ||
293 | public void addTo${relationship.capitalizedName}(${relationship.actualDestination.classNameWithDefault} object) { | ||
294 | if (logger.isDebugEnabled()) { | ||
295 | logger.debug( "object: " + object); | ||
296 | if ( (this.editingContext() != null) && (object != null) && ((! (this instanceof com.sPearWay.access.NamedObjectInterface)) || (! ((com.sPearWay.access.NamedObjectInterface)this).isReadOnly())) && ((! (object instanceof com.sPearWay.access.NamedObjectInterface)) || (! ((com.sPearWay.access.NamedObjectInterface)object).isReadOnly())) && ( ! this.editingContext().equals(object.editingContext()) ) ) { | ||
297 | try { | ||
298 | throw new Exception("WrongEditingContext"); | ||
299 | } catch(Exception exception) { | ||
![]() |
3.1 | 300 | logger.warn("Relationship \"$relationship.name\" Exception " , exception); |
![]() |
1.1 | 301 | } |
302 | } | ||
303 | } | ||
304 | this.includeObjectIntoPropertyWithKey(object, "${relationship.name}"); | ||
305 | } | ||
306 | |||
307 | /** | ||
308 | * Removes an object from the relationship "$relationship.name". | ||
309 | * | ||
310 | * @param object | ||
311 | * ${relationship.actualDestination.classNameWithDefault}. | ||
312 | */ | ||
313 | public void removeFrom${relationship.capitalizedName}(${relationship.actualDestination.classNameWithDefault} object) { | ||
314 | if (logger.isDebugEnabled()) { | ||
315 | logger.debug( "object: " + object); | ||
316 | } | ||
317 | this.excludeObjectFromPropertyWithKey(object, "${relationship.name}"); | ||
318 | } | ||
319 | |||
320 | /** | ||
321 | * Adds an object to the relationship "$relationship.name". This method takes care of setting the reverse relationship if it exists. | ||
322 | * | ||
323 | * @param object | ||
324 | * ${relationship.actualDestination.classNameWithDefault}. | ||
325 | */ | ||
326 | public void addTo${relationship.capitalizedName}Relationship(${relationship.actualDestination.classNameWithDefault} object) { | ||
327 | this.addObjectToBothSidesOfRelationshipWithKey(object, "${relationship.name}"); | ||
328 | } | ||
329 | |||
330 | /** | ||
331 | * Removes an object from the relationship "$relationship.name". This method takes care of setting the reverse relationship if it exists. | ||
332 | * | ||
333 | * @param object | ||
334 | * ${relationship.actualDestination.classNameWithDefault}. | ||
335 | */ | ||
336 | public void removeFrom${relationship.capitalizedName}Relationship(${relationship.actualDestination.classNameWithDefault} object) { | ||
337 | this.removeObjectFromBothSidesOfRelationshipWithKey(object, "${relationship.name}"); | ||
338 | } | ||
339 | |||
340 | /** | ||
341 | * Creates and returns a new object for the relationship "$relationship.name". | ||
342 | * | ||
343 | * @return ${relationship.actualDestination.classNameWithDefault}. | ||
344 | */ | ||
345 | public ${relationship.actualDestination.classNameWithDefault} create${relationship.capitalizedName}Relationship() { | ||
346 | EOClassDescription classDescription = EOClassDescription.classDescriptionForEntityName("${relationship.actualDestination.name}"); | ||
347 | EOEnterpriseObject object = classDescription.createInstanceWithEditingContext(this.editingContext(), null); | ||
348 | this.editingContext().insertObject(object); | ||
349 | this.addObjectToBothSidesOfRelationshipWithKey(object, "${relationship.name}"); | ||
350 | return (${relationship.actualDestination.classNameWithDefault})object; | ||
351 | } | ||
352 | |||
353 | /** | ||
354 | * Removes and deletes if appropriate an object from the relationship "$relationship.name". | ||
355 | * <p> | ||
356 | * The delete rule for "$relationship.name" is: ${relationship.deleteRule.name}#if (${relationship.ownsDestination}), and own destination#end<br/> | ||
357 | * Possible choices are "Nullify", "Cascade", "Deny", "NoAction" | ||
358 | * </p> | ||
359 | * <p> | ||
360 | #if (${relationship.deleteRule.name} != "Deny") | ||
361 | * The object is removed from the relationship "$relationship.name" and its inverse relationship if it exists. | ||
362 | #if (${relationship.deleteRule.name} == "Cascade") | ||
363 | * The receiver cascade the delete rule, the object will be deleted from the editing context. | ||
364 | #elseif (${relationship.ownsDestination}) | ||
365 | * The receiver owns the object, the object will be deleted from the editing context. | ||
366 | #end | ||
367 | #else | ||
368 | * The remove will be denied. This method does nothing. | ||
369 | #end | ||
370 | * </p> | ||
371 | * | ||
372 | * @param object | ||
373 | * ${relationship.actualDestination.classNameWithDefault}. | ||
374 | */ | ||
375 | public void delete${relationship.capitalizedName}Relationship(${relationship.actualDestination.classNameWithDefault} object) { | ||
376 | // Delete rule for ${relationship.name} is: ${relationship.deleteRule.name} | ||
377 | // Possible choices are "Nullify", "Cascade", "Deny", "NoAction" | ||
378 | #if (${relationship.deleteRule.name} != "Deny") | ||
379 | this.removeObjectFromBothSidesOfRelationshipWithKey(object, "${relationship.name}"); | ||
380 | #if (${relationship.deleteRule.name} == "Cascade") | ||
381 | // Cascade delete rule | ||
382 | this.editingContext().deleteObject(object); | ||
383 | #elseif (${relationship.ownsDestination}) | ||
384 | // Own destination delete rule | ||
385 | this.editingContext().deleteObject(object); | ||
386 | #end | ||
387 | #else | ||
388 | // Deny delete rule | ||
389 | #end | ||
390 | } | ||
391 | |||
392 | /** | ||
393 | * Deletes all object from the relationship "$relationship.name". All delete rules are applied. | ||
394 | */ | ||
395 | public void deleteAll${relationship.capitalizedName}Relationships() { | ||
396 | for ( Iterator<${relationship.actualDestination.classNameWithDefault}> objects = this.${relationship.name}().vector().iterator(); objects.hasNext(); ) | ||
397 | this.delete${relationship.capitalizedName}Relationship(objects.next()); | ||
398 | } | ||
399 | |||
400 | /** | ||
401 | * Initialize the relationship "$relationship.name" with the list of values | ||
402 | * | ||
403 | * @param value | ||
404 | * list of ${relationship.actualDestination.classNameWithDefault}. | ||
405 | */ | ||
406 | public void initialize${relationship.capitalizedName}(NSArray<${relationship.actualDestination.classNameWithDefault}> value) { | ||
407 | this.set${relationship.capitalizedName}(value); | ||
408 | } | ||
409 | |||
410 | /** | ||
411 | * Initialize the relationship "$relationship.name" with one object | ||
412 | * | ||
413 | * @param value | ||
414 | * ${relationship.actualDestination.classNameWithDefault}. | ||
415 | */ | ||
416 | public void initialize${relationship.capitalizedName}(${relationship.actualDestination.classNameWithDefault} value) { | ||
417 | this.addTo${relationship.capitalizedName}Relationship(value); | ||
418 | } | ||
419 | #end | ||
420 | #end | ||
421 | } | ||
![]() |
3.1 | 422 | |
423 | {{/code}} |