Wiki source code of Scala _Entity.java Template
Last modified by Ravi Mendis on 2010/09/07 05:06
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | This is a basic proof-of-concept Scala template. | ||
2 | Scala is supposed to reduce boilerplate and so potentially allows for a better or improved solution. | ||
3 | |||
4 | {{code}} | ||
5 | |||
6 | // $LastChangedRevision$ DO NOT EDIT. Make changes to ${entity.classNameWithOptionalPackage}.java instead. | ||
7 | #if ($entity.superclassPackageName) | ||
8 | package $entity.superclassPackageName | ||
9 | |||
10 | #end | ||
11 | import com.webobjects.eocontrol.{EOGenericRecord, EORelationshipManipulation} | ||
12 | import com.webobjects.foundation.{NSArray, NSData} | ||
13 | import org.apache.log4j.Logger | ||
14 | |||
15 | import scala.collection.JavaConversions._ | ||
16 | #foreach ($relationship in $entity.sortedClassToOneRelationships) | ||
17 | #if ($relationship.actualDestination.classNameWithDefault != "EOGenericRecord" && $relationship.actualDestination.packageName != $entity.packageName) | ||
18 | import $relationship.actualDestination.classNameWithDefault | ||
19 | #end | ||
20 | #end | ||
21 | #foreach ($relationship in $entity.sortedClassToManyRelationships) | ||
22 | #if ($relationship.actualDestination.classNameWithDefault != "EOGenericRecord" && $relationship.actualDestination.packageName != $entity.packageName) | ||
23 | import $relationship.actualDestination.classNameWithDefault | ||
24 | #end | ||
25 | #end | ||
26 | |||
27 | object ${entity.classNameWithoutPackage} { | ||
28 | #if ($entity.partialEntitySet) | ||
29 | val ENTITY_NAME = "$entity.partialEntity.name" | ||
30 | #else | ||
31 | val ENTITY_NAME = "$entity.name" | ||
32 | #end | ||
33 | |||
34 | /* | ||
35 | * API | ||
36 | */ | ||
37 | object Keys { | ||
38 | // Attributes | ||
39 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
40 | val ${attribute.uppercaseUnderscoreName} = "$attribute.name" | ||
41 | #end | ||
42 | |||
43 | // Relationships | ||
44 | #foreach ($relationship in $entity.sortedClassRelationships) | ||
45 | val ${relationship.uppercaseUnderscoreName} = "$relationship.name" | ||
46 | #end | ||
47 | } | ||
48 | } | ||
49 | |||
50 | abstract class ${entity.classNameWithoutPackage} extends#if ($entity.parentClassNameSet)${entity.parentClassName}#elseif ($entity.partialEntitySet)er.extensions.partials.ERXPartial<${entity.partialEntity.className}>#elseif ($entity.parentSet)${entity.parent.classNameWithDefault}#elseif ($EOGenericRecord)${EOGenericRecord}#else EOGenericRecord#end { | ||
51 | private lazy val logger = Logger.getLogger(this.getClass.getName) | ||
52 | |||
53 | #foreach ($attribute in $entity.sortedClassAttributes) | ||
54 | #if (!$attribute.inherited) | ||
55 | #if ($attribute.userInfo.ERXConstantClassName) | ||
56 | def ${attribute.name}: $attribute.userInfo.ERXConstantClassName = { | ||
57 | storedValueForKey(${entity.classNameWithoutPackage}.Keys.${attribute.uppercaseUnderscoreName}) | ||
58 | } | ||
59 | |||
60 | def set${attribute.capitalizedName}(value: $attribute.userInfo.ERXConstantClassName) = { | ||
61 | takeStoredValueForKey(value, ${entity.classNameWithoutPackage}.Keys.${attribute.uppercaseUnderscoreName}) | ||
62 | } | ||
63 | #else | ||
64 | def ${attribute.name} = { | ||
65 | storedValueForKey(${entity.classNameWithoutPackage}.Keys.${attribute.uppercaseUnderscoreName}).asInstanceOf[$attribute.getJavaClassName(true)] | ||
66 | } | ||
67 | |||
68 | def set${attribute.capitalizedName}(value: $attribute.javaClassName) { | ||
69 | logger.debug( "updating $attribute.name from " + ${attribute.name} + " to " + value) | ||
70 | takeStoredValueForKey(value, ${entity.classNameWithoutPackage}.Keys.${attribute.uppercaseUnderscoreName}) | ||
71 | } | ||
72 | #end | ||
73 | |||
74 | #end | ||
75 | #end | ||
76 | #foreach ($relationship in $entity.sortedClassToOneRelationships) | ||
77 | #if (!$relationship.inherited) | ||
78 | def ${relationship.name} = { | ||
79 | storedValueForKey(${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}).asInstanceOf[$relationship.actualDestination.classNameWithoutPackage] | ||
80 | } | ||
81 | |||
82 | def set${relationship.capitalizedName}Relationship(value :$relationship.actualDestination.classNameWithoutPackage) = { | ||
83 | logger.debug("updating $relationship.name from " + ${relationship.name} + " to " + value) | ||
84 | if (value == null) { | ||
85 | val oldValue: EORelationshipManipulation = ${relationship.name}.asInstanceOf[EORelationshipManipulation] | ||
86 | if (oldValue != null) { | ||
87 | removeObjectFromBothSidesOfRelationshipWithKey(oldValue, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}) | ||
88 | } | ||
89 | } else { | ||
90 | addObjectToBothSidesOfRelationshipWithKey(value, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}) | ||
91 | } | ||
92 | } | ||
93 | |||
94 | #end | ||
95 | #end | ||
96 | #foreach ($relationship in $entity.sortedClassToManyRelationships) | ||
97 | #if (!$relationship.inherited) | ||
98 | def ${relationship.name} = { | ||
99 | storedValueForKey(${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}).asInstanceOf[NSArray[${relationship.actualDestination.classNameWithoutPackage}]] | ||
100 | } | ||
101 | |||
102 | def ${relationship.name}List = { | ||
103 | ${relationship.name}.asInstanceOf[java.lang.Iterable[${relationship.actualDestination.classNameWithoutPackage}]].toList | ||
104 | } | ||
105 | |||
106 | def addTo${relationship.capitalizedName}Relationship(eo: $relationship.actualDestination.classNameWithoutPackage) = { | ||
107 | logger.debug("adding " + eo + " to ${relationship.name} relationship") | ||
108 | addObjectToBothSidesOfRelationshipWithKey(eo, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}) | ||
109 | } | ||
110 | |||
111 | def removeFrom${relationship.capitalizedName}Relationship(eo: $relationship.actualDestination.classNameWithoutPackage) = { | ||
112 | logger.debug("removing " + eo + " from ${relationship.name} relationship") | ||
113 | removeObjectFromBothSidesOfRelationshipWithKey(eo, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}) | ||
114 | } | ||
115 | |||
116 | def delete${relationship.capitalizedName}Relationship(eo: $relationship.actualDestination.classNameWithoutPackage) = { | ||
117 | removeObjectFromBothSidesOfRelationshipWithKey(eo, ${entity.classNameWithoutPackage}.Keys.${relationship.uppercaseUnderscoreName}) | ||
118 | #if (!$relationship.ownsDestination) | ||
119 | editingContext.deleteObject(eo) | ||
120 | #end | ||
121 | } | ||
122 | #end | ||
123 | #end | ||
124 | |||
125 | } | ||
126 | |||
127 | |||
128 | {{/code}} |