Home > Model Transformation > Model-to-Text (M2T) > OOMEGA Generator > Create Templates
Template creation is very easy: Simply create a new file with the suffix ".jgen" and place it in a directory structure as you would do with any other Java file.
Default behaviour supports the persistency domain
The template's filename controls the default behaviour of the code generator and determines the resulting filenames of generated code fragments. The filename is made up of three parts.
- The first part of the filename controls the behaviour of the code generator. The filename can start with "", "Class", "EntityClass" or "AttributeClass". If the first part is empty, the template will be executed once for every OOMEGA metamodel. Otherwise it will be executed for every class, entity class and attribute class respectively. In the latter case the prefix will be substituted with the actual class name of the computed metamodel class.
- The second part of the filename constitutes the filename's suffix. This text is adopted one-to-one and appended to the generated files' names. It might be composed of a name suffix and a file type separated by a dot.
- The third part of the filename is ".jgen" to indicate the nature of the file: it's a template. This file suffix is required. Of course it is not included in generated filenames.
Accordant to that logical separation the filename "EntityClassBean.java.jgen" would be separated as follows:
[EntityClass] [Bean.java] [.jgen]
Hence the template would be processed for every entity class. If you have for example two entity classes in your metamodel, namely "Person" and "Address", you would end up with these generated files: "PersonBean.java" and "AddressBean.java". As a matter of course, these files are placed in the correct directory reflecting your package information of your metamodel.
Customisation for arbitrary domains
The default behaviour as coded in org.oomega.core.Template can be customised, too. Every template is a subclass of org.oomega.core.Template, so you can easily overload the methods CId<EO> getExtentCId(), String getPath() and String getFileName() in your JSP template. For example the following statements ensure template execution for every package of your metamodel, the resulting files are named jSDL.java and stored in a directory structure which reflects the information of your package object.
<%! public CId getExtentCId() { return DCPackage.CID; } public String getPath() { return ((DCPackage)obj).getName().replace('.','/'); } public String getFileName() { return "jSDL.java"; } %>
Perhaps the following question comes to your mind: How can I restrict template execution to specific objects of a given class extent?
This can be done with the help of a database query: just overload the method Predicate predicate() in your JSP template. For example the following statement ensures template execution for every package containing the characters core in its name.
<%! public Predicate predicate() { return like(P(DCPackage.P.name), "*core*"); } %>
The examples shown here refer all to the meta-metamodel, i.e. the persistency domain. For sure you can use the customisation approach for custom metamodels, too!
<%! public CId getExtentCId() { return Person.CID; } public String getPath() { return "persons"; } public String getFileName() { return ((Person)obj).getName(); } %>
Next chapter: Code Templates