Changes for page EOF-Using EOF-EOGenerator
Last modified by Pascal Robert on 2012/01/21 22:03
From version 13.1
edited by smmccraw
on 2007/07/08 09:44
on 2007/07/08 09:44
Change comment:
There is no comment for this version
To version 14.1
edited by Quinton Dolan
on 2007/07/17 02:07
on 2007/07/17 02:07
Change comment:
fixed formatting of mike's code (again) to better match original wiki. still looks aweful in safari.
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. smmccraw1 +XWiki.qdolan - Content
-
... ... @@ -1,3 +1,8 @@ 1 +|=Contents 2 +| 3 + 4 +{{toc style="disc" minLevel="2"}}{{/toc}} 5 + 1 1 == Overview == 2 2 3 3 If you've ever used EOModeler's Java source code generator, you know how much of a pain it can be when you make changes to your model objects and have to merge changes in later. One solution for this is to use [[EOGenerator>>http://www.rubicode.com/Software/EOGenerator/]], an application developed by Rubicode Software, which uses the Generation Gap pattern to create your Java files from your EOModels. EOGenerator produces TWO java files for each Entity rather than one. Take the example of a Person entity. The first java file is //Person.java, which contains all of the autogenerated methods. The second java file is Person.java, and Person extends //Person. The second file is where you place all of your customizations. Any time your model changes, only your //Xxx.java files are updated, and your customizations are left untouched. Additionally, EOGenerator allows for the creation of extensive custom templates for your files, which provides the ability to place convenience methods in your //Xxx.java files. ... ... @@ -21,7 +21,8 @@ 21 21 22 22 {{panel}} 23 23 24 - eogenerator -model /path/to/model/YourModel.eomodeld -destination /path/to/source/folder -subclassDestination /path/to/source/folder -templatedir /path/to/EOGenerator/templates -java -packagedirs 29 +eogenerator -model /path/to/model/YourModel.eomodeld -destination /path/to/source/folder 30 + -subclassDestination /path/to/source/folder -templatedir /path/to/EOGenerator/templates -java -packagedirs 25 25 26 26 {{/panel}} 27 27 ... ... @@ -45,7 +45,7 @@ 45 45 46 46 Allow setting nulls on a to-one relationship (and turn it into a remove). Note, this is also included in Jonathan Rentzsch's templates. 47 47 48 -{{ panel}}54 +{{code}} 49 49 50 50 public void save<$ToOneRelationship.name.initialCapitalString$>(<$ToOneRelationship.destinationEntity.referenceJavaClassName$> value) 51 51 { ... ... @@ -61,13 +61,13 @@ 61 61 } 62 62 } 63 63 64 -{{/ panel}}70 +{{/code}} 65 65 66 66 === Chuck Hill === 67 67 68 68 Return the list of changes between the current EO and the last committed version of the EO: 69 69 70 -{{ panel}}76 +{{code}} 71 71 72 72 public NSDictionary changedProperties() { 73 73 NSDictionary commitedValues = editingContext().committedSnapshotForObject(this); ... ... @@ -74,7 +74,7 @@ 74 74 return changesFromSnapshot(commitedValues); 75 75 } 76 76 77 -{{/ panel}}83 +{{/code}} 78 78 79 79 === Jonathan Rentzsch === 80 80 ... ... @@ -87,32 +87,22 @@ 87 87 Constants for all attributes and relationships. This allows compile time error checking in situations like 88 88 addObjecttoBothSidesOfRelationshipWithKey(myObject, Person.TO//MANY//Children) 89 89 90 -{{ panel}}96 +{{code}} 91 91 92 92 <$foreach attribute classAttributes.@reversedArray do$> 93 93 public static final String ATTRIBUTE_<$attribute.name$> = "<$attribute.name$>";<$endforeach do$> 94 94 95 -{{/panel}} 96 - 97 97 <$foreach ToOneRelationship classToOneRelationships.@reversedArray do$> 98 - 99 -{{panel}} 100 - 101 101 public static final String TO_ONE_<$ToOneRelationship.name$> = "<$ToOneRelationship.name$>";<$endforeach do$> 102 102 103 -{{/panel}} 104 - 105 105 <$foreach ToManyRelationship classToManyRelationships.@reversedArray do$> 106 - 107 -{{panel}} 108 - 109 109 public static final String TO_MANY_<$ToManyRelationship.name$> = "<$ToManyRelationship.name$>";<$endforeach do$> 110 110 111 -{{/ panel}}107 +{{/code}} 112 112 113 113 We also make heavy use of the user info dictionary on entity and attribute level. Allows to generate customized methods and what not. One example is booleans that are stored in the DB as strings with values "true" and "false". 114 114 115 -{{ panel}}111 +{{code}} 116 116 117 117 <$if attribute.userInfo.usage h1. booleanFlag $> // boolean accessors 118 118 public void <$attribute.userInfo.setterName$>(boolean newBoolean) { ... ... @@ -135,69 +135,67 @@ 135 135 } 136 136 <$endif$> 137 137 138 -{{/ panel}}134 +{{/code}} 139 139 140 140 === Mike Schrag === 141 141 142 142 Add a constant that represents the name of the entity so that you can refer to Person.ENTITY//NAME in fetches rather than the String (allows refactoring support in Eclipse)~:// 143 143 144 -{{ panel}}140 +{{code}} 145 145 146 146 public static final String ENTITY_NAME = "<$name$>"; 147 147 148 -{{/ panel}}144 +{{/code}} 149 149 150 150 Add a static factory method to your EO's ( Person createPerson(...) ) that shows you what required attributes and relationships are configured for you entity (attempts to provide a replacement "constructor" since EO constructors are empty): 151 151 152 -{{ panel}}148 +{{code}} 153 153 154 - 155 - 156 - 157 - 158 - 159 - 150 +public static <$classNameWithoutPackage$> create<$classNameWithoutPackage$>(EOEditingContext _editingContext<$foreach Attribute classAttributes.@sortedNameArray do$><$if !Attribute.allowsNull$>, <$Attribute.javaValueClassName$> _<$Attribute.name$><$endif$><$endforeach do$><$foreach ToOneRelationship classToOneRelationships.@sortedNameArray do$><$if ToOneRelationship.isMandatory$>, <$ToOneRelationship.destinationEntity.referenceJavaClassName$> _<$ToOneRelationship.name$><$endif$><$endforeach do$>) { 151 + <$classNameWithoutPackage$> eoObject = (<$classNameWithoutPackage$>)EOUtilities.createAndInsertInstance(_editingContext, <$GEN_PREFIX$><$classNameWithoutPackage$>.ENTITY_NAME);<$foreach Attribute classAttributes.@sortedNameArray do$><$if !Attribute.allowsNull$> 152 + eoObject.set<$Attribute.name.initialCapitalString$>(_<$Attribute.name$>);<$endif$><$endforeach do$><$foreach ToOneRelationship classToOneRelationships.@sortedNameArray do$><$if ToOneRelationship.isMandatory$> 153 + eoObject.set<$ToOneRelationship.name.initialCapitalString$>Relationship(_<$ToOneRelationship.name$>);<$endif$><$endforeach do$> 154 + return eoObject; 155 +} 160 160 161 -{{/ panel}}157 +{{/code}} 162 162 163 163 Here's a little bitty fancier (read: nastier) version that also handles superclass mandatory attributes and fields (one level). It skips any attribute that is referenced in the restricting qualifier of your subclass (since you are probably going to set that in your awakeFromInsertion): 164 164 165 -{{ panel}}161 +{{code}} 166 166 167 - 168 - 169 - 170 - 171 - 172 - 173 - 174 - 163 +public static <$classNameWithoutPackage$> create<$classNameWithoutPackage$>(EOEditingContext editingContext<$foreach Attribute classAttributes.@sortedNameArray do$><$if !Attribute.allowsNull$>, <$Attribute.javaValueClassName$> <$Attribute.name$><$endif$><$endforeach do$><$foreach Attribute parentEntity.classAttributes.@sortedNameArray do$><$if !Attribute.allowsNull$><$set RestrictingQualifierKey = false$><$foreach QualifierKey restrictingQualifier.allQualifierKeys do$><$if Attribute.name = QualifierKey$><$set RestrictingQualifierKey = true$><$endif$><$endforeach do$><$if RestrictingQualifierKey = false$>, <$Attribute.javaValueClassName$> <$Attribute.name$><$endif$><$endif$><$endforeach do$><$foreach ToOneRelationship classToOneRelationships.@sortedNameArray do$><$if ToOneRelationship.isMandatory$>, <$ToOneRelationship.destinationEntity.referenceJavaClassName$> <$ToOneRelationship.name$><$endif$><$endforeach do$><$foreach ToOneRelationship parentEntity.classToOneRelationships.@sortedNameArray do$><$if ToOneRelationship.isMandatory$>, <$ToOneRelationship.destinationEntity.referenceJavaClassName$> <$ToOneRelationship.name$><$endif$><$endforeach do$>) { 164 + <$classNameWithoutPackage$> eoObject = (<$classNameWithoutPackage$>)EOUtilities.createAndInsertInstance(editingContext, <$GEN_PREFIX$><$classNameWithoutPackage$>.ENTITY_NAME);<$foreach Attribute classAttributes.@sortedNameArray do$><$if !Attribute.allowsNull$> 165 + eoObject.set<$Attribute.name.initialCapitalString$>(<$Attribute.name$>);<$endif$><$endforeach do$><$foreach ToOneRelationship classToOneRelationships.@sortedNameArray do$><$if ToOneRelationship.isMandatory$> 166 + eoObject.set<$ToOneRelationship.name.initialCapitalString$>Relationship(<$ToOneRelationship.name$>);<$endif$><$endforeach do$><$foreach Attribute parentEntity.classAttributes.@sortedNameArray do$><$if !Attribute.allowsNull$><$set RestrictingQualifierKey = false$><$foreach QualifierKey restrictingQualifier.allQualifierKeys do$><$if Attribute.name = QualifierKey$><$set RestrictingQualifierKey = true$><$endif$><$endforeach do$><$if RestrictingQualifierKey = false$> 167 + eoObject.set<$Attribute.name.initialCapitalString$>(<$Attribute.name$>);<$endif$><$endif$><$endforeach do$><$foreach ToOneRelationship parentEntity.classToOneRelationships.@sortedNameArray do$><$if ToOneRelationship.isMandatory$> 168 + eoObject.set<$ToOneRelationship.name.initialCapitalString$>Relationship(<$ToOneRelationship.name$>);<$endif$><$endforeach do$> 169 + return eoObject; 170 +} 175 175 176 -{{/ panel}}172 +{{/code}} 177 177 178 178 Add a bunch of convience fetch methods (fetchAllPersons, fetchRequiredPerson, and other variants). It's not smart about pluralization, so it's just going to put an "s" on the end of the entity name: 179 179 180 -{{ panel}}176 +{{code}} 181 181 182 182 public static NSArray fetchAll<$classNameWithoutPackage$>s(EOEditingContext _editingContext) { 183 183 return <$GEN_PREFIX$><$classNameWithoutPackage$>.fetchAll<$classNameWithoutPackage$>s(_editingContext, null); 184 184 } 185 185 186 -{{/ panel}}182 +{{/code}} 187 187 188 - publicstatic NSArray fetchAll<$classNameWithoutPackage$>s(EOEditingContext //editingContext, NSArray //sortOrderings) {184 +{{code}} 189 189 190 -{{panel}} 191 - 186 + public static NSArray fetchAll<$classNameWithoutPackage$>s(EOEditingContext _editingContext, NSArray _sortOrderings) { 192 192 return <$GEN_PREFIX$><$classNameWithoutPackage$>.fetch<$classNameWithoutPackage$>s(_editingContext, null, _sortOrderings); 193 193 } 194 194 195 -{{/ panel}}190 +{{/code}} 196 196 197 - publicstatic NSArray fetch<$classNameWithoutPackage$>s(EOEditingContext //editingContext, EOQualifier //qualifier, NSArray //sortOrderings) {//192 +{{code}} 198 198 199 -{{panel}} 200 - 194 + public static NSArray fetch<$classNameWithoutPackage$>s(EOEditingContext _editingContext, EOQualifier _qualifier, NSArray _sortOrderings) { 201 201 EOFetchSpecification fetchSpec = new EOFetchSpecification(<$GEN_PREFIX$><$classNameWithoutPackage$>.ENTITY_NAME, _qualifier, _sortOrderings); 202 202 fetchSpec.setIsDeep(true); 203 203 NSArray eoObjects = _editingContext.objectsWithFetchSpecification(fetchSpec); ... ... @@ -204,21 +204,19 @@ 204 204 return eoObjects; 205 205 } 206 206 207 -{{/ panel}}201 +{{/code}} 208 208 209 - publicstatic <$classNameWithoutPackage$> fetch<$classNameWithoutPackage$>(EOEditingContext //editingContext, String //keyName, Object //value) {//203 +{{code}} 210 210 211 -{{panel}} 212 - 205 + public static <$classNameWithoutPackage$> fetch<$classNameWithoutPackage$>(EOEditingContext _editingContext, String _keyName, Object _value) { 213 213 return <$GEN_PREFIX$><$classNameWithoutPackage$>.fetch<$classNameWithoutPackage$>(_editingContext, new EOKeyValueQualifier(_keyName, EOQualifier.QualifierOperatorEqual, _value)); 214 214 } 215 215 216 -{{/ panel}}209 +{{/code}} 217 217 218 - publicstatic <$classNameWithoutPackage$> fetch<$classNameWithoutPackage$>(EOEditingContext //editingContext, EOQualifier //qualifier) {211 +{{code}} 219 219 220 -{{panel}} 221 - 213 + public static <$classNameWithoutPackage$> fetch<$classNameWithoutPackage$>(EOEditingContext _editingContext, EOQualifier _qualifier) { 222 222 NSArray eoObjects = <$GEN_PREFIX$><$classNameWithoutPackage$>.fetch<$classNameWithoutPackage$>s(_editingContext, _qualifier, null); 223 223 <$classNameWithoutPackage$> eoObject; 224 224 int count = eoObjects.count(); ... ... @@ -233,17 +233,20 @@ 233 233 } 234 234 return eoObject; 235 235 } 236 - 228 + 229 +{{/code}} 230 + 231 +{{code}} 232 + 237 237 public static <$classNameWithoutPackage$> fetchRequired<$classNameWithoutPackage$>(EOEditingContext _editingContext, String _keyName, Object _value) { 238 238 return <$GEN_PREFIX$><$classNameWithoutPackage$>.fetchRequired<$classNameWithoutPackage$>(_editingContext, new EOKeyValueQualifier(_keyName, EOQualifier.QualifierOperatorEqual, _value)); 239 239 } 240 240 241 -{{/ panel}}237 +{{/code}} 242 242 243 - publicstatic <$classNameWithoutPackage$> fetchRequired<$classNameWithoutPackage$>(EOEditingContext //editingContext, EOQualifier //qualifier) {239 +{{code}} 244 244 245 -{{panel}} 246 - 241 + public static <$classNameWithoutPackage$> fetchRequired<$classNameWithoutPackage$>(EOEditingContext _editingContext, EOQualifier _qualifier) { 247 247 <$classNameWithoutPackage$> eoObject = <$GEN_PREFIX$><$classNameWithoutPackage$>.fetch<$classNameWithoutPackage$>(_editingContext, _qualifier); 248 248 if (eoObject h1. null) { 249 249 throw new NoSuchElementException("There was no <$classNameWithoutPackage$> that matched the qualifier '" + _qualifier + "'."); ... ... @@ -251,30 +251,29 @@ 251 251 return eoObject; 252 252 } 253 253 254 -{{/ panel}}249 +{{/code}} 255 255 256 256 Add methods for getting local instances of EO's. The static one is handy if you have a reference to an EO that might be null (it does a null check first): 257 257 258 -{{ panel}}253 +{{code}} 259 259 260 260 public <$classNameWithoutPackage$> localInstanceOf<$classNameWithoutPackage$>(EOEditingContext _editingContext) { 261 261 return (<$classNameWithoutPackage$>)EOUtilities.localInstanceOfObject(_editingContext, this); 262 262 } 263 263 264 -{{/ panel}}259 +{{/code}} 265 265 266 - publicstatic <$classNameWithoutPackage$> localInstanceOf<$classNameWithoutPackage$>(EOEditingContext //editingContext, <$classNameWithoutPackage$> //eo) {261 +{{code}} 267 267 268 -{{panel}} 269 - 263 + public static <$classNameWithoutPackage$> localInstanceOf<$classNameWithoutPackage$>(EOEditingContext _editingContext, <$classNameWithoutPackage$> _eo) { 270 270 return (_eo null) ? null : (<$classNameWithoutPackage$>)EOUtilities.localInstanceOfObject(_editingContext, _eo); 271 271 } 272 272 273 -{{/ panel}}267 +{{/code}} 274 274 275 275 If you've ever wanted to be able to qualify a toMany relationship on your EO's, but sometimes you want to fetch them w/ a fetch spec, and sometimes you want to filter in-memory, you can use the following: 276 276 277 -{{ panel}}271 +{{code}} 278 278 279 279 <$if !ToManyRelationship.inverseRelationship$> 280 280 public NSArray <$ToManyRelationship.name$>(EOQualifier qualifier) { ... ... @@ -296,7 +296,8 @@ 296 296 <$if ToManyRelationship.inverseRelationship$> 297 297 if (fetch) { 298 298 EOQualifier fullQualifier; 299 - EOQualifier inverseQualifier = new EOKeyValueQualifier(<$ToManyRelationship.destination.className$>.<$ToManyRelationship.inverseRelationship.name.uppercaseUnderbarString$>_KEY, EOQualifier.QualifierOperatorEqual, this); 293 + EOQualifier inverseQualifier = new EOKeyValueQualifier(<$ToManyRelationship.destination.className$>. 294 + <$ToManyRelationship.inverseRelationship.name.uppercaseUnderbarString$>_KEY, EOQualifier.QualifierOperatorEqual, this); 300 300 if (qualifier == null) { 301 301 fullQualifier = inverseQualifier; 302 302 } ... ... @@ -323,35 +323,35 @@ 323 323 return results; 324 324 } 325 325 326 -{{/ panel}}321 +{{/code}} 327 327 328 328 === John Huss === 329 329 330 330 I wanted to share a wonderful bit of knowledge I learned today. If you're using Java 1.5 you can add @SuppressWarnings("all") to the template for your //EO base classes and eliminate annoying compiler messages (usually uneeded import statements).// 331 331 332 -{{ panel}}327 +{{code}} 333 333 334 334 @SuppressWarnings("all") 335 335 public class _Invoice extends ERXGenericRecord { 336 336 } 337 337 338 -{{/ panel}}333 +{{/code}} 339 339 340 340 === Guido Neitzer === 341 341 342 342 Create awakeFromInsertion() and awakeFromFetch() in your EOGenerator template as a method stub that only calls super() and has a comment for "initialize your object here ...". You only have to put in the code at that place and can't possibly forget to call super(). Here is an example: 343 343 344 -{{ panel}}339 +{{code}} 345 345 346 - 347 - 348 - 349 - 350 - 351 - 352 - 341 +/** 342 + * Initialization of the instance while inserting it into an editing context 343 + */ 344 +public void awakeFromInsertion (EOEditingContext editingContext) { 345 + super.awakeFromInsertion (editingContext); 346 + // initialize your object here 347 +} 353 353 354 -{{/ panel}}349 +{{/code}} 355 355 356 356 This is from my JavaSubclassSourceTemplate.eotemplate 357 357