Customization of the generation processes issues
Scenario Tree describes how to drive automatic generation processes.
Indeed, a scenario transforms an input (XML files, Pojos) into a GENERATED XML file which describes the body of a part (Editor, Query, Detail UI…).
The default generation process which is driven by Scenario Tree provided by Scenario.xml file (see Scenario architecture), may contain defects.
For instance, in our case, we want to make 3 improvements:
- For Film entity, Hibernate generates a Pojo which maps the year property to a date. The property must be changed to an int which can have a value between 1900 and 2155.
- For Film entity, property specialFeatures is a string corresponding to a set of values. MultiChoice control to display value is more appropriate.
- All POJOs that contain a lastUpdate property must extend a ASuperLastUpdate class that initializes the property value to the current date when creating or modifying entity.
Making changes to Scenario Tree file means update the generation processes. The belowing example shows differents possiblities:
- New scenarios, generators, path elements, parameters can be specified or added to the Scenario Tree before launching or relaunching generation processes.
- Pojo rewriter for injecting changes in Pojos (e.g. org.adichatz.scenario.generation.SuperPojoRewriter is a java class for rewrite Pojo in order to add a super class).
- Specific features as new item value, validators or listeners can be added to the scenario tree.
Store scheduled changes inside an XML file
These changes can be manually made by the developper inside the Scenario.xml file.
A other way is to assemble changes inside a new Scenario tree stored in a specific xml file, inject these changes inside the current Scenario.xml file and launch needed generation processes.
In the example below, previously described needed change are incorparated.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <scenarioTree xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.adichatz.org/xsd/v0.9.1/generator/scenarioTree.xsd"> <pathElements> <pathElement type="PROJECT" location="myscenario"/> </pathElements> <params> <param id="devl.default.integer.pattern" value="#####"/> </params> <generators> <generator treeClassName="org.adichatz.generator.wrapper.TableWrapper" generatorClassName="org.mycompany.myscenario.generator.MyTableGenerator"/> <generator treeClassName="#CONTROLGENERATOR()" generatorClassName="org.mycompany.myscenario.generator.MyControlGenerator"/> </generators> <generationScenario scenarioClassName="org.adichatz.scenario.impl.PluginEntityScenario"> <modelPart> <pojoRewriters> <pojoRewriter rewriterClassName="org.adichatz.scenario.generation.SuperPojoRewriter" propertyRegex="lastUpdate"> <params> <param id="superClassURI" value="bundleclass://./#PACKAGENAME().model.ASuperLastUpdate"/> <param id="sourceURI" value="platform:/plugin/org.adichatz.template/template/src/ASuperLastUpdate.java"/> </params> </pojoRewriter> </pojoRewriters> </modelPart> <propertyField id="lastUpdate"> <controlField xsi:type="dateTextType" enabled="false" style="SWT.BORDER | SWT.TIME"/> <columnField xsi:type="tableColumnType" property="lastUpdate" sorted="true" style="SWT.CENTER"/> </propertyField> <pluginEntity entityURI="adi://././FilmMM"> <messages> <message key="invalid.year.value" value="Invalid year '{0}'! Value must be between 1900 and 2155"/> </messages> <propertyField id="releaseYear" pojoType="short"> <controlField xsi:type="formattedTextType" editPattern="####" format="Integer"> <validators> <validator key="invalidYear" errorMessage="#MSG(film, invalid.year.value, #FV())"> <errorWhen>return #FV() < 1900 || #FV() > 2155;</errorWhen> </validator> </validators> </controlField> </propertyField> <propertyField id="specialFeatures"> <controlField xsi:type="multiChoiceType" multiChoiceType="String" values="Trailers,Commentaries,Deleted Scenes,Behind the Scenes" popupToolbar="true"/> </propertyField> </pluginEntity> <generationUnit scenarioClassName="org.adichatz.scenario.impl.GroupListNavigatorScenario" adiResourceURI="adi://groupNavigator/GroupNavigatorContent" type="NAVIGATOR"/> </generationScenario> <customPostAction injectInsideScenarioFile="true" generatePojo="true" generateEJB="true" generatePersistenceXml="true" deployEJB="true"> <customGenerationUnit type="ENTITY" propertyRegex="lastUpdate"/> <customGenerationUnit type="DETAIL" propertyRegex="lastUpdate"/> <customGenerationUnit type="TABLE" entityRegex="Film"/> <customGenerationUnit type="MESSAGE_BUNDLE" entityRegex="Film"/> </customPostAction> </scenarioTree>
Remarks:
Line 4: | A new project containing resources (classes) is added to the path for generation processes. |
---|---|
Lines 10-11: | 2 new Generators are used instead of standard generators. |
Lines 16-21: | 2 new pojo rewriters are executed after Pojos are generated. |
Lines 25-26: | The controller for lastUpdate property will be a DateTextController for any entity of the project. |
Line 30: | An item is added to the message bundle file linked to the Film entity (filmGENERATED.properties file). |
Lines 33-39: | Assumes that the value of the releaseYear property must be between 1900 and 2155 (Client side). |
Line 42 : | The value of the specialFeatures property is an array of predetermined values and uses a MultiChoiceController controller. |
Line 45: | A new navigator will be generated which provide List/Detail query editor. |
Line 48: | Regenerate Entity MetaModel (e.g. ActorMM) for all entity having a lastUpdate property. |
Line 49: | Regenerate Detail Include (e.g. ActorDI) for all entity having a lastUpdate property. |
Line 50: | Regenerate Table Include (e.g. FilmTI) for Film entity. |
Line 51: | Regenerate Message Bundle (e.g. film.properties) for Film entity. |
Specify a customization file for a data source
When defining a datasource, a Customization file can be specified linked to this data source.
Each time this data source is chosen, the customization file is suggested to bring changes to the scenario tree before launching the generation process.
|
Inject scheduled changes inside Scenario.xml file
|
|
So, Developpers can build and share powerfull aspects to easily enrich the generation processes.
Initial and final Scenario.xml can be respectively seen here and here.: