Changes for page EOF-Using EOF-EOGenerator

Last modified by Pascal Robert on 2012/01/21 22:03

From version 17.1
edited by David Holt
on 2007/08/14 11:27
Change comment: Corrected some slight errors in the code that were created in the transfer from the older Wiki
To version 18.1
edited by David LeBer
on 2007/12/22 13:19
Change comment: There is no comment for this version

Summary

Details

Page properties
Title
... ... @@ -1,1 +1,1 @@
1 -Programming__WebObjects-EOF-Using EOF-EOGenerator
1 +EOF-Using EOF-EOGenerator
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.dholt
1 +XWiki.dleber
Content
... ... @@ -1,14 +1,18 @@
1 -|=Contents
2 -|
1 +|= Contents
2 +| {{toc style="disc" minLevel="2"}}{{/toc}}
3 3  
4 -{{toc style="disc" minLevel="2"}}{{/toc}}
4 +== Overview ==
5 5  
6 -== Overview ==
6 +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.
7 7  
8 -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.
8 +{{warning}}
9 9  
10 -== Advantages ==
10 +EOGenerator doesn't work on Mac OS X 10.5. You have to use [JavaEOGenerator|http://developer.apple.com/samplecode/JavaEOGenerator/] or [Velocity EOGenerator|WOL:Velocity EOGenerator (Veogen)].
11 11  
12 +{{/warning}}
13 +
14 +== Advantages ==
15 +
12 12  There are several advantages to using EOGenerator over EOModeler's default Java file generation and merging with FileMerge.
13 13  
14 14  * EOGenerator uses the Generation Gap pattern, which provides a much cleaner separation of autogenerated vs customized code with no need to deal with merging at all. There are border cases with FileMerge that can cause you to deal with annoying conflicts.
... ... @@ -15,7 +15,7 @@
15 15  * EOGenerator uses the MiscMerge language for its templates. This allows you to extend the core templates with extensive customizations (see the EOGenerator Mods section below), better supporting your own custom development process and workflow.
16 16  * As David LaBer put it, "all the cool kids use it - and we all know looking cool is the **most** important criteria".
17 17  
18 -== How To Use It ==
22 +== How To Use It ==
19 19  
20 20  Kieran Kelleher has writtten an [[Introduction to EOGenerator>>http://homepage.mac.com/kelleherk/iblog/C1837526061/E1908382110/index.html]] on his blog.
21 21  
... ... @@ -26,34 +26,34 @@
26 26  
27 27  {{panel}}
28 28  
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
33 +eogenerator \-model /path/to/model/YourModel.eomodeld \-destination /path/to/source/folder
34 +\-subclassDestination /path/to/source/folder \-templatedir /path/to/EOGenerator/templates \-java \-packagedirs
31 31  
32 32  {{/panel}}
33 33  
34 34  Voila. EOGenerator will spit out your Java files for you. Let's break down the commands you can pass in:
35 35  
36 -* --define--EOGenericRecord <class>, allows you to specify the //Person class's superclass. For instance, if you use Project Wonder, you would specify --define--EOGenericRecord er.extensions.ERXGenericRecord//
37 -* --destination <path>, the folder that //Person.java//--//style java files will be produced in (the non-editable files)//
38 -* --java, produce java files--
39 -* --javaTemplate <filename>, the name of the Java template to use inside of the template dir (//Person)//--
40 -* --model <path>, Passes in the path of a .eomodeld you would like to generate Java files for. You can actually include multiple --model commands on the commandline
41 -* --packagedirs, produce package directory for any package statements defined in your Java files (not necessary if you don't specify package names on your entities. By the way, you should specify packages on your entities :) )--
42 -* --refmodel <path>, Passes in the path of an .eomodeld that is required for generating Java files, but that won't actually have Java files generated for it. For instance, you should --refmodel any prototypes, or any models in other frameworks that you depend on
43 -* --subclassDestination <path>, the folder that Person.java--style java files will be produced in (the editable files)
44 -* --subclassJavaTemplate <filename>, the name of the Java subclass template to use inside of the template dir (Person)--
45 -* --templatedir <path>, the path to the folder that contains EOGenerator templates--
46 -* --verbose, turn on verbose output--
40 +* define-EOGenericRecord <class>, allows you to specify the Person class's superclass. For instance, if you use Project Wonder, you would specify define-EOGenericRecord er.extensions.ERXGenericRecord
41 +* destination <path>, the folder that Person.java-style java files will be produced in (the non-editable files)
42 +* java, produce java files
43 +* javaTemplate <filename>, the name of the Java template to use inside of the template dir (//Person)//
44 +* model <path>, Passes in the path of a .eomodeld you would like to generate Java files for. You can actually include multiple model commands on the commandline
45 +* packagedirs, produce package directory for any package statements defined in your Java files (not necessary if you don't specify package names on your entities. By the way, you should specify packages on your entities :-) )
46 +* refmodel <path>, Passes in the path of an .eomodeld that is required for generating Java files, but that won't actually have Java files generated for it. For instance, you should refmodel any prototypes, or any models in other frameworks that you depend on
47 +* subclassDestination <path>, the folder that Person.java-style java files will be produced in (the editable files)
48 +* subclassJavaTemplate <filename>, the name of the Java subclass template to use inside of the template dir (Person)
49 +* templatedir <path>, the path to the folder that contains EOGenerator templates
50 +* verbose, turn on verbose output
47 47  
48 -== Custom EOGenerator Mods ==
52 +== Custom EOGenerator Mods ==
49 49  
50 -=== Zak Burke ===
54 +=== Zak Burke ===
51 51  
52 52  Allow setting nulls on a to-one relationship (and turn it into a remove). Note, this is also included in Jonathan Rentzsch's templates.
53 53  
54 54  {{code}}
55 55  
56 - public void save<$ToOneRelationship.name.initialCapitalString$>(<$ToOneRelationship.destinationEntity.referenceJavaClassName$> value)
60 +public void save<$ToOneRelationship.name.initialCapitalString$>(<$ToOneRelationship.destinationEntity.referenceJavaClassName$> value)
57 57   {
58 58   if (value == null)
59 59   {
... ... @@ -69,13 +69,13 @@
69 69  
70 70  {{/code}}
71 71  
72 -=== Chuck Hill ===
76 +=== Chuck Hill ===
73 73  
74 74  Return the list of changes between the current EO and the last committed version of the EO:
75 75  
76 76  {{code}}
77 77  
78 - public NSDictionary changedProperties() {
82 +public NSDictionary changedProperties() {
79 79   NSDictionary commitedValues = editingContext().committedSnapshotForObject(this);
80 80   return changesFromSnapshot(commitedValues);
81 81   }
... ... @@ -82,13 +82,13 @@
82 82  
83 83  {{/code}}
84 84  
85 -=== Jonathan Rentzsch ===
89 +=== Jonathan Rentzsch ===
86 86  
87 87  Jonathan Rentzsch has provided his base EOGenerator templates, which are a must-have:
88 88  
89 -http:~/~/rentzsch.com/share/eogenerator52templates.zip
93 +[[http://rentzsch.com/share/eogenerator52templates.zip]]
90 90  
91 -=== Markus Ruggiero ===
95 +=== Markus Ruggiero ===
92 92  
93 93  Constants for all attributes and relationships. This allows compile time error checking in situations like
94 94  addObjecttoBothSidesOfRelationshipWithKey(myObject, Person.TO//MANY//Children)
... ... @@ -95,7 +95,7 @@
95 95  
96 96  {{code}}
97 97  
98 - <$foreach attribute classAttributes.@reversedArray do$>
102 +<$foreach attribute classAttributes.@reversedArray do$>
99 99   public static final String ATTRIBUTE_<$attribute.name$> = "<$attribute.name$>";<$endforeach do$>
100 100  
101 101   <$foreach ToOneRelationship classToOneRelationships.@reversedArray do$>
... ... @@ -110,15 +110,15 @@
110 110  
111 111  {{code}}
112 112  
113 - <$if attribute.userInfo.usage h1. booleanFlag $> // boolean accessors
117 +<$if attribute.userInfo.usage h1. booleanFlag $> // boolean accessors
114 114   public void <$attribute.userInfo.setterName$>(boolean newBoolean) {
115 115   set<$attribute.name.initialCapitalString$>(newBoolean ? "true" : "false");
116 116   }
117 -
121 +
118 118   public boolean <$attribute.userInfo.getterName$>() {
119 119   return "true".equals(<$attribute.name$>()) ? true : false;
120 120   }
121 -
125 +
122 122   // validation
123 123   public String validate<$attribute.name.initialCapitalString$>(String newValue) {
124 124   if ( newValue null ) {
... ... @@ -133,13 +133,13 @@
133 133  
134 134  {{/code}}
135 135  
136 -=== Mike Schrag ===
140 +=== Mike Schrag ===
137 137  
138 138  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)~://
139 139  
140 140  {{code}}
141 141  
142 - public static final String ENTITY_NAME = "<$name$>";
146 +public static final String ENTITY_NAME = "<$name$>";
143 143  
144 144  {{/code}}
145 145  
... ... @@ -175,7 +175,7 @@
175 175  
176 176  {{code}}
177 177  
178 - public static NSArray fetchAll<$classNameWithoutPackage$>s(EOEditingContext _editingContext) {
182 +public static NSArray fetchAll<$classNameWithoutPackage$>s(EOEditingContext _editingContext) {
179 179   return <$GEN_PREFIX$><$classNameWithoutPackage$>.fetchAll<$classNameWithoutPackage$>s(_editingContext, null);
180 180   }
181 181  
... ... @@ -183,7 +183,7 @@
183 183  
184 184  {{code}}
185 185  
186 - public static NSArray fetchAll<$classNameWithoutPackage$>s(EOEditingContext _editingContext, NSArray _sortOrderings) {
190 +public static NSArray fetchAll<$classNameWithoutPackage$>s(EOEditingContext _editingContext, NSArray _sortOrderings) {
187 187   return <$GEN_PREFIX$><$classNameWithoutPackage$>.fetch<$classNameWithoutPackage$>s(_editingContext, null, _sortOrderings);
188 188   }
189 189  
... ... @@ -191,7 +191,7 @@
191 191  
192 192  {{code}}
193 193  
194 - public static NSArray fetch<$classNameWithoutPackage$>s(EOEditingContext _editingContext, EOQualifier _qualifier, NSArray _sortOrderings) {
198 +public static NSArray fetch<$classNameWithoutPackage$>s(EOEditingContext _editingContext, EOQualifier _qualifier, NSArray _sortOrderings) {
195 195   EOFetchSpecification fetchSpec = new EOFetchSpecification(<$GEN_PREFIX$><$classNameWithoutPackage$>.ENTITY_NAME, _qualifier, _sortOrderings);
196 196   fetchSpec.setIsDeep(true);
197 197   NSArray eoObjects = _editingContext.objectsWithFetchSpecification(fetchSpec);
... ... @@ -202,7 +202,7 @@
202 202  
203 203  {{code}}
204 204  
205 - public static <$classNameWithoutPackage$> fetch<$classNameWithoutPackage$>(EOEditingContext _editingContext, String _keyName, Object _value) {
209 +public static <$classNameWithoutPackage$> fetch<$classNameWithoutPackage$>(EOEditingContext _editingContext, String _keyName, Object _value) {
206 206   return <$GEN_PREFIX$><$classNameWithoutPackage$>.fetch<$classNameWithoutPackage$>(_editingContext, new EOKeyValueQualifier(_keyName, EOQualifier.QualifierOperatorEqual, _value));
207 207   }
208 208  
... ... @@ -210,7 +210,7 @@
210 210  
211 211  {{code}}
212 212  
213 - public static <$classNameWithoutPackage$> fetch<$classNameWithoutPackage$>(EOEditingContext _editingContext, EOQualifier _qualifier) {
217 +public static <$classNameWithoutPackage$> fetch<$classNameWithoutPackage$>(EOEditingContext _editingContext, EOQualifier _qualifier) {
214 214   NSArray eoObjects = <$GEN_PREFIX$><$classNameWithoutPackage$>.fetch<$classNameWithoutPackage$>s(_editingContext, _qualifier, null);
215 215   <$classNameWithoutPackage$> eoObject;
216 216   int count = eoObjects.count();
... ... @@ -230,7 +230,7 @@
230 230  
231 231  {{code}}
232 232  
233 - public static <$classNameWithoutPackage$> fetchRequired<$classNameWithoutPackage$>(EOEditingContext _editingContext, String _keyName, Object _value) {
237 +public static <$classNameWithoutPackage$> fetchRequired<$classNameWithoutPackage$>(EOEditingContext _editingContext, String _keyName, Object _value) {
234 234   return <$GEN_PREFIX$><$classNameWithoutPackage$>.fetchRequired<$classNameWithoutPackage$>(_editingContext, new EOKeyValueQualifier(_keyName, EOQualifier.QualifierOperatorEqual, _value));
235 235   }
236 236  
... ... @@ -238,7 +238,7 @@
238 238  
239 239  {{code}}
240 240  
241 - public static <$classNameWithoutPackage$> fetchRequired<$classNameWithoutPackage$>(EOEditingContext _editingContext, EOQualifier _qualifier) {
245 +public static <$classNameWithoutPackage$> fetchRequired<$classNameWithoutPackage$>(EOEditingContext _editingContext, EOQualifier _qualifier) {
242 242   <$classNameWithoutPackage$> eoObject = <$GEN_PREFIX$><$classNameWithoutPackage$>.fetch<$classNameWithoutPackage$>(_editingContext, _qualifier);
243 243   if (eoObject == null) {
244 244   throw new NoSuchElementException("There was no <$classNameWithoutPackage$> that matched the qualifier '" + _qualifier + "'.");
... ... @@ -252,7 +252,7 @@
252 252  
253 253  {{code}}
254 254  
255 - public <$classNameWithoutPackage$> localInstanceOf<$classNameWithoutPackage$>(EOEditingContext _editingContext) {
259 +public <$classNameWithoutPackage$> localInstanceOf<$classNameWithoutPackage$>(EOEditingContext _editingContext) {
256 256   return (<$classNameWithoutPackage$>)EOUtilities.localInstanceOfObject(_editingContext, this);
257 257   }
258 258  
... ... @@ -260,7 +260,7 @@
260 260  
261 261  {{code}}
262 262  
263 - public static <$classNameWithoutPackage$> localInstanceOf<$classNameWithoutPackage$>(EOEditingContext _editingContext, <$classNameWithoutPackage$> _eo) {
267 +public static <$classNameWithoutPackage$> localInstanceOf<$classNameWithoutPackage$>(EOEditingContext _editingContext, <$classNameWithoutPackage$> _eo) {
264 264   return (_eo == null) ? null : (<$classNameWithoutPackage$>)EOUtilities.localInstanceOfObject(_editingContext, _eo);
265 265   }
266 266  
... ... @@ -270,7 +270,7 @@
270 270  
271 271  {{code}}
272 272  
273 - <$if !ToManyRelationship.inverseRelationship$>
277 +<$if !ToManyRelationship.inverseRelationship$>
274 274   public NSArray <$ToManyRelationship.name$>(EOQualifier qualifier) {
275 275   return <$ToManyRelationship.name$>(qualifier, null);
276 276   }
... ... @@ -279,12 +279,12 @@
279 279   public NSArray <$ToManyRelationship.name$>(EOQualifier qualifier) {
280 280   return <$ToManyRelationship.name$>(qualifier, null, false);
281 281   }
282 -
283 - public NSArray <$ToManyRelationship.name$>(EOQualifier qualifier, boolean fetch) {
286 +
287 + public NSArray <$ToManyRelationship.name$>(EOQualifier qualifier, boolean fetch) {
284 284   return <$ToManyRelationship.name$>(qualifier, null, fetch);
285 285   }
286 286   <$endif$>
287 -
291 +
288 288   public NSArray <$ToManyRelationship.name$>(EOQualifier qualifier, NSArray sortOrderings<$if ToManyRelationship.inverseRelationship$>, boolean fetch<$endif$>) {
289 289   NSArray results;
290 290   <$if ToManyRelationship.inverseRelationship$>
... ... @@ -320,25 +320,25 @@
320 320  
321 321  {{/code}}
322 322  
323 -=== John Huss ===
327 +=== John Huss ===
324 324  
325 -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).//
329 +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).
326 326  
327 327  {{code}}
328 328  
329 - @SuppressWarnings("all")
333 +@SuppressWarnings("all")
330 330   public class _Invoice extends ERXGenericRecord {
331 331   }
332 332  
333 333  {{/code}}
334 334  
335 -=== Guido Neitzer ===
339 +=== Guido Neitzer ===
336 336  
337 337  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:
338 338  
339 339  {{code}}
340 340  
341 -/**
345 +/**
342 342   * Initialization of the instance while inserting it into an editing context
343 343   */
344 344  public void awakeFromInsertion (EOEditingContext editingContext) {
... ... @@ -349,5 +349,3 @@
349 349  {{/code}}
350 350  
351 351  This is from my JavaSubclassSourceTemplate.eotemplate
352 -
353 -Category:WebObjects