r509 - in trunk: . wikitty-api wikitty-dto wikitty-dto/src/main/java/org/nuiton/wikitty/generator wikitty-generators/src/main/java/org/nuiton/wikitty/generator
Author: tchemit Date: 2010-11-24 15:22:43 +0100 (Wed, 24 Nov 2010) New Revision: 509 Url: http://nuiton.org/repositories/revision/wikitty/509 Log: move DTO generator + fix poms Added: trunk/wikitty-generators/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java Removed: trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java Modified: trunk/pom.xml trunk/wikitty-api/pom.xml trunk/wikitty-dto/pom.xml Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2010-11-24 13:39:47 UTC (rev 508) +++ trunk/pom.xml 2010-11-24 14:22:43 UTC (rev 509) @@ -191,7 +191,7 @@ <dependency> <groupId>org.nuiton.i18n</groupId> <artifactId>nuiton-i18n</artifactId> - <version>1.2.2</version> + <version>${nuitonI18nVersion}</version> <scope>compile</scope> </dependency> @@ -522,6 +522,9 @@ <!-- common versions used in sub-poms --> <eugeneVersion>2.2.1-SNAPSHOT</eugeneVersion> <nuitonUtilsVersion>1.5.2-SNAPSHOT</nuitonUtilsVersion> + <nuitonI18nVersion>2.0</nuitonI18nVersion> + <processPluginVersion>1.0.3</processPluginVersion> + <springVersion>3.0.1.RELEASE</springVersion> <jettyMorbayVersion>6.1.22</jettyMorbayVersion> @@ -558,8 +561,15 @@ <plugin> <groupId>org.nuiton.processor</groupId> <artifactId>maven-processor-plugin</artifactId> - <version>1.0.3</version> + <version>${processPluginVersion}</version> </plugin> + + <!-- eugene plugin --> + <plugin> + <groupId>org.nuiton.eugene</groupId> + <artifactId>maven-eugene-plugin</artifactId> + <version>${eugeneVersion}</version> + </plugin> </plugins> </pluginManagement> </build> Modified: trunk/wikitty-api/pom.xml =================================================================== --- trunk/wikitty-api/pom.xml 2010-11-24 13:39:47 UTC (rev 508) +++ trunk/wikitty-api/pom.xml 2010-11-24 14:22:43 UTC (rev 509) @@ -155,7 +155,6 @@ <plugin> <groupId>org.nuiton.eugene</groupId> <artifactId>maven-eugene-plugin</artifactId> - <version>${eugeneVersion}</version> <dependencies> <dependency> <groupId>org.nuiton.wikitty</groupId> Modified: trunk/wikitty-dto/pom.xml =================================================================== --- trunk/wikitty-dto/pom.xml 2010-11-24 13:39:47 UTC (rev 508) +++ trunk/wikitty-dto/pom.xml 2010-11-24 14:22:43 UTC (rev 509) @@ -3,156 +3,165 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.nuiton</groupId> - <artifactId>wikitty</artifactId> - <version>3.0-SNAPSHOT</version> - </parent> + <parent> + <groupId>org.nuiton</groupId> + <artifactId>wikitty</artifactId> + <version>3.0-SNAPSHOT</version> + </parent> - <!-- ************************************************************* --> - <!-- *** POM Relationships *************************************** --> - <!-- ************************************************************* --> - <groupId>org.nuiton.wikitty</groupId> - <artifactId>wikitty-dto</artifactId> + <!-- ************************************************************* --> + <!-- *** POM Relationships *************************************** --> + <!-- ************************************************************* --> + <groupId>org.nuiton.wikitty</groupId> + <artifactId>wikitty-dto</artifactId> - <dependencies> + <dependencies> - <!-- sibling dependencies --> + <!-- sibling dependencies --> - <dependency> - <groupId>${project.groupId}</groupId> - <artifactId>wikitty-generators</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>${project.groupId}</groupId> - <artifactId>wikitty-api</artifactId> - <version>${project.version}</version> - </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>wikitty-generators</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>wikitty-api</artifactId> + <version>${project.version}</version> + </dependency> - <dependency> - <groupId>org.nuiton.eugene</groupId> - <artifactId>eugene</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>commons-logging</groupId> - <artifactId>commons-logging</artifactId> - </dependency> - <dependency> - <groupId>commons-lang</groupId> - <artifactId>commons-lang</artifactId> - </dependency> + <dependency> + <groupId>org.nuiton.eugene</groupId> + <artifactId>eugene</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + </dependency> - <dependency> - <groupId>org.nuiton</groupId> - <artifactId>nuiton-utils</artifactId> - </dependency> + <dependency> + <groupId>org.nuiton</groupId> + <artifactId>nuiton-utils</artifactId> + </dependency> - <dependency> - <groupId>commons-beanutils</groupId> - <artifactId>commons-beanutils</artifactId> - </dependency> + <dependency> + <groupId>commons-beanutils</groupId> + <artifactId>commons-beanutils</artifactId> + </dependency> - - <!-- TEST --> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - </dependency> - </dependencies> + <!-- TEST --> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> - <!-- ************************************************************* --> - <!-- *** Project Information ************************************* --> - <!-- ************************************************************* --> + </dependencies> - <name>Wikitty :: dto</name> + <!-- ************************************************************* --> + <!-- *** Project Information ************************************* --> + <!-- ************************************************************* --> - <description>Provide DTOs for Wikitty</description> - <inceptionYear>2010</inceptionYear> + <name>Wikitty :: dto</name> - <!-- ************************************************************* --> - <!-- *** Build Settings ****************************************** --> - <!-- ************************************************************* --> + <description>Provide DTOs for Wikitty</description> + <inceptionYear>2010</inceptionYear> - <packaging>jar</packaging> - <properties> - - <!-- extra files to include in release --> - <redmine.releaseFiles> - target/${project.build.finalName}.${project.packaging} - </redmine.releaseFiles> - </properties> + <!-- ************************************************************* --> + <!-- *** Build Settings ****************************************** --> + <!-- ************************************************************* --> - <build> + <properties> - <plugins> - <!-- processor plugin --> - <plugin> - <groupId>org.nuiton.processor</groupId> - <artifactId>maven-processor-plugin</artifactId> - <executions> - <execution> - <phase>generate-sources</phase> - <goals> - <goal>process</goal> - </goals> - </execution> - </executions> - <configuration> - <includes>**/*.java</includes> - <filters> - org.nuiton.processor.filters.GeneratorTemplatesFilter - </filters> - </configuration> - </plugin> + <!-- extra files to include in release --> + <redmine.releaseFiles> + target/${project.build.finalName}.${project.packaging} + </redmine.releaseFiles> + </properties> - <plugin> - <groupId>org.nuiton.eugene</groupId> - <artifactId>maven-eugene-plugin</artifactId> - <executions> - <execution> - <id>api-dto-generation</id> - <phase>generate-test-sources</phase> - <configuration> - <testPhase>true</testPhase> - <inputs> - <input>classpath:model:/:wikitty.objectmodel</input> - </inputs> - <fullPackagePath>org.nuiton.wikitty.entities - </fullPackagePath> - <defaultPackage>org.nuiton.wikitty.entities - </defaultPackage> - <extractedPackages>org.nuiton.wikitty.entities - </extractedPackages> - <templates> - org.nuiton.wikitty.generator.WikittyDTOGenerator - </templates> - </configuration> - <goals> - <goal>smart-generate</goal> - </goals> - </execution> - </executions> - </plugin> + <build> - <!-- expose new plexus components --> - <plugin> - <groupId>org.codehaus.plexus</groupId> - <artifactId>plexus-component-metadata</artifactId> - <executions> - <execution> - <goals> - <goal>generate-metadata</goal> - </goals> - </execution> - </executions> - </plugin> - </plugins> + <plugins> + <!-- processor plugin --> + <plugin> + <groupId>org.nuiton.processor</groupId> + <artifactId>maven-processor-plugin</artifactId> + <executions> + <execution> + <phase>generate-sources</phase> + <goals> + <goal>process</goal> + </goals> + </execution> + </executions> + <configuration> + <includes>**/*.java</includes> + <filters> + org.nuiton.processor.filters.GeneratorTemplatesFilter + </filters> + </configuration> + </plugin> - </build> + <plugin> + <groupId>org.nuiton.eugene</groupId> + <artifactId>maven-eugene-plugin</artifactId> + <executions> + <execution> + <id>api-dto-generation</id> + <phase>generate-test-sources</phase> + <configuration> + <testPhase>true</testPhase> + <inputs> + <input>classpath:model:/:wikitty.objectmodel</input> + </inputs> + <fullPackagePath> + org.nuiton.wikitty.entities + </fullPackagePath> + <defaultPackage> + org.nuiton.wikitty.entities + </defaultPackage> + <extractedPackages> + org.nuiton.wikitty.entities + </extractedPackages> + <templates> + org.nuiton.wikitty.generator.WikittyDTOGenerator + </templates> + </configuration> + <goals> + <goal>smart-generate</goal> + </goals> + </execution> + </executions> + <!--dependencies> + <dependency> + <groupId>org.nuiton.wikitty</groupId> + <artifactId>wikitty-dto</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies--> + </plugin> + <!-- expose new plexus components --> + <plugin> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-component-metadata</artifactId> + <executions> + <execution> + <goals> + <goal>generate-metadata</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + + </build> + </project> Deleted: trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java =================================================================== --- trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java 2010-11-24 13:39:47 UTC (rev 508) +++ trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java 2010-11-24 14:22:43 UTC (rev 509) @@ -1,562 +0,0 @@ -/* - * #%L - * Wikitty :: dto - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2010 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.wikitty.generator; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.nuiton.eugene.Transformer; -import org.nuiton.eugene.java.ObjectModelTransformerToJava; -import org.nuiton.eugene.models.object.ObjectModel; -import org.nuiton.eugene.models.object.ObjectModelAttribute; -import org.nuiton.eugene.models.object.ObjectModelClass; -import org.nuiton.eugene.models.object.ObjectModelModifier; -import org.nuiton.eugene.models.object.ObjectModelOperation; -import org.nuiton.eugene.models.object.ObjectModelParameter; - - -/*{generator option: writeString = }*/ -/*{generator option: parentheses = false}*/ -/*{generator option: writeString = +}*/ - -/** - * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.wikitty.generator.WikittyDTOGenerator" - */ -public class WikittyDTOGenerator extends ObjectModelTransformerToJava { - - private static final Log log = LogFactory.getLog(WikittyDTOGenerator.class); - - /** - * map busines entity from source model to generated abstract class - */ - protected Map<ObjectModelClass, ObjectModelClass> processedClasses = - new HashMap<ObjectModelClass, ObjectModelClass>(); - - /** - * pattern to parse toString tagValue - * for the string "abc{foo|bar}defrzeg{uvw|xyz}oeira" - * will match {foo|bar} and {uvw|xyz} with groups for foo, bar, uvw and xyz - */ - protected Pattern toStringTagValuePattern = Pattern.compile("\\{(([^|}])*)(?:\\|([^}]*))?\\}"); - - /** - * map "Client.name" to "getName()" or any getter to read this attribute - * those getter names are stored while generating in order to be found - * later when generating toString() - */ - protected Map<String, String> attributeToGetterName = new HashMap<String, String>(); - - @Override - protected Transformer<ObjectModel, ObjectModel> initPreviousTransformer() { - return new WikittyPurifierTransformer(); - } - - protected List<ObjectModelClass> entitiesWithInheritedOperations = - new ArrayList<ObjectModelClass>(); - - @Override - public void transformFromModel(ObjectModel model) { - - // contains boths businessEntities and metaExtensions - // elements may have one of the two sterotypes or both - // but elements with none of them aren't in this list - List<ObjectModelClass> modelBoth = new ArrayList<ObjectModelClass>(); - - // fill modelBusinessEntities with entities found in model - for (ObjectModelClass clazz : model.getClasses()) { - if (WikittyTransformerUtil.isBusinessEntity(clazz) || - WikittyTransformerUtil.isMetaExtension(clazz)) { - modelBoth.add(clazz); - } - } - - for (ObjectModelClass clazz : modelBoth) { - ObjectModelClass abstractClass = createClass( - businessEntityToDTOName(clazz), - clazz.getPackageName()); - processedClasses.put(clazz, abstractClass); - addInterface(abstractClass, clazz.getQualifiedName()); - } - - for (ObjectModelClass clazz : modelBoth) { - if (WikittyTransformerUtil.isMetaExtension(clazz)) { - addMetaExtensionOperations(clazz, processedClasses.get(clazz)); - } else if (WikittyTransformerUtil.isBusinessEntity(clazz)) { - addOperations(clazz, processedClasses.get(clazz)); - } - } - - // at this time, all operations in generated abstracts are just the operations - // like get/set etc. we will copy all operations of a given class to all children - // that's why constructors and others operations are not yet added - for (ObjectModelClass clazz : modelBoth) { - if (WikittyTransformerUtil.isBusinessEntity(clazz)) { - addInheritedOperations(clazz, processedClasses.get(clazz)); - } - } - - for (ObjectModelClass clazz : modelBoth) { - ObjectModelClass abstractClassForThisEntity = processedClasses.get(clazz); - addConstructors(abstractClassForThisEntity); - addToString(clazz, abstractClassForThisEntity); - addBusinessEntityMethods(abstractClassForThisEntity); - addModelOperations(clazz, abstractClassForThisEntity); - } - - processedClasses.clear(); - } - - protected void addSerialVersionUID(ObjectModelClass clazz) { - // adding a generated serialVersionUID - Random random = new Random(); - Long serialVersionUIDs = random.nextLong(); - addConstant(clazz, - "serialVersionUID", - "long", - serialVersionUIDs.toString() + "L", - ObjectModelModifier.PRIVATE); - } - - protected void addConstructors(ObjectModelClass clazz) { - - ObjectModelOperation constructor = addConstructor(clazz, ObjectModelModifier.PUBLIC); - setOperationBody(constructor, "" -/*{ -}*/); - - constructor = addConstructor(clazz, ObjectModelModifier.PUBLIC); - addParameter(constructor, WikittyTransformerUtil.WIKITTY_CLASS_FQN, "wikitty"); - setOperationBody(constructor, "" -/*{ - wikittyId=wikitty.getId(); -}*/); - - constructor = addConstructor(clazz, ObjectModelModifier.PUBLIC); - addParameter(constructor, "String", "wikittyId"); - setOperationBody(constructor, "" -/*{ - this.wikittyId=wikittyId; -}*/); - - } - - protected void addOperations(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { - // generating operations with bodies to realize contract - for (ObjectModelAttribute attribute : businessEntity.getAttributes()) { - if (attribute.isNavigable()) { - // needed below, in templates - String attributeType = WikittyTransformerUtil.generateResultType(attribute, false); - String attributeName = attribute.getName(); - - //add necessary import - addImport(abstractClass, attribute.getType()); - - //If alternative name, use it - if (attribute.hasTagValue(WikittyTransformerUtil.TAG_ALTERNATIVE_NAME)) { - attributeName = attribute.getTagValue(WikittyTransformerUtil.TAG_ALTERNATIVE_NAME); - } - - String getterName; - - if (WikittyTransformerUtil.isAttributeCollection(attribute)) { - // attribute is a collection, we will generate operations - // get, add, remove and clear - String collectionType = WikittyTransformerUtil.getCollectionTypeName(attribute); - if (collectionType.equals("Set")){ - addImport(abstractClass, java.util.Set.class); - } - if (collectionType.equals("List")){ - addImport(abstractClass, java.util.List.class); - - } - - String attributeTypeInSet = WikittyTransformerUtil.generateResultType(attribute, true); - - //adding the attribute - ObjectModelAttribute modelAttribute = addAttribute(abstractClass, attributeName, attributeTypeInSet); - String annotation = "WikittyField(fqn=\""+ businessEntity.getName() + "." + attributeName +"\")"; - addAnnotation(abstractClass, modelAttribute, annotation); - addImport(abstractClass, org.nuiton.wikitty.entities.WikittyField.class); - - - // adding the getter - getterName = "get" + StringUtils.capitalize(attributeName); - ObjectModelOperation getter = addOperation(abstractClass, getterName, attributeTypeInSet); - addAnnotation(abstractClass, getter, "Override"); - String getterBody = "" -/*{ - return <%=attributeName%>; -}*/; - setOperationBody(getter, getterBody); - - // adding the setter - getterName = "set" + StringUtils.capitalize(attributeName); - ObjectModelOperation setter = addOperation(abstractClass, getterName, "void"); - addAnnotation(abstractClass, setter, "Override"); - addParameter(setter, attributeTypeInSet, attributeName); - String setterBody = "" -/*{ - this.<%=attributeName%>=<%=attributeName%>; -}*/; - setOperationBody(setter, setterBody); - - //adding the add method - String addName = "add" + StringUtils.capitalize(attributeName); - ObjectModelOperation adder = addOperation(abstractClass, addName, "void"); - addAnnotation(abstractClass, adder, "Override"); - addParameter(adder, attributeType, "element"); - String adderBody = "" -/*{ - <%=attributeName%>.add(element); -}*/; - setOperationBody(adder, adderBody); - - //adding the remove method - String removeName = "remove" + StringUtils.capitalize(attributeName); - ObjectModelOperation remover = addOperation(abstractClass, removeName, "void"); - addAnnotation(abstractClass, remover, "Override"); - addParameter(remover, attributeType, "element"); - String removerBody = "" -/*{ - <%=attributeName%>.remove(element); -}*/; - setOperationBody(remover, removerBody); - - //adding the clear method - String clearName = "clear" + StringUtils.capitalize(attributeName); - ObjectModelOperation clear = addOperation(abstractClass, clearName, "void"); - addAnnotation(abstractClass, clear, "Override"); - String clearBody = "" -/*{ - <%=attributeName%>.clear(); -}*/; - setOperationBody(clear, clearBody); - - } else { - //No multiplicity - - //add necessary import - addImport(abstractClass, attribute.getType()); - - //adding the attribute - ObjectModelAttribute modelAttribute = addAttribute(abstractClass, attributeName, attributeType); - String annotation = "WikittyField(fqn=\"" + businessEntity.getName() + "."+attributeName + "\")"; - addAnnotation(abstractClass, modelAttribute, annotation); - addImport(abstractClass, org.nuiton.wikitty.entities.WikittyField.class); - - // adding getter - getterName = "get" + StringUtils.capitalize(attributeName); - ObjectModelOperation getter = addOperation(abstractClass, getterName, attributeType); - addAnnotation(abstractClass, getter, "Override"); - setOperationBody(getter, "" -/*{ - return <%=attributeName%>; -}*/); - - //adding setter - String setterName = "set" + StringUtils.capitalize(attributeName); - ObjectModelOperation setter = addOperation(abstractClass, setterName, "void"); - addAnnotation(abstractClass, setter, "Override"); - addParameter(setter, attributeType, attributeName); - setOperationBody(setter, "" -/*{ - this.<%=attributeName%>=<%=attributeName%>; -}*/); - } - - // save the getter name for this attribute - attributeToGetterName.put(businessEntity.getName() + "." + attributeName, getterName); - } - } - } - - /** - * Add inherited operations to the abstract generated from an entity. - * Method browse superClasses to copy methods and their bodies. The method - * deal with imports, cause bodies call the Helper of the classe where - * the attribute was declared. - * <p/> - * If A inherit from B and B inherit from and A, B, C not in the same package - * we B need to import CHelper and A need to import BHelper. But, since A - * has C attributes too, it needs CHelper too. So when we will process B, - * we will save needed imports, A will get them thus import CHelper. - */ - protected void addInheritedOperations(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { - - if (!entitiesWithInheritedOperations.contains(businessEntity)) { - - // add attributes and methods inherited from super classes - for (ObjectModelClass superClass : businessEntity.getSuperclasses()) { - - // process super classes first, so we will get inherited operations from superclass - addInheritedOperations(superClass, processedClasses.get(superClass)); - - if (WikittyTransformerUtil.isBusinessEntity(superClass)) { - - //Add operations for the super class - addOperations(superClass, abstractClass); - } - } - - entitiesWithInheritedOperations.add(businessEntity); - } - } - - /** - * add a toString method - * if a toString tagValue is attached to businessEntity, it will be used - * to generate a toString as this : - * <p/> - * given "hello {Person.name|unknow}" - * <p/> - * will try to replace first {...} by name field value for extension Person. - * if this information is not available, will do unknow. - */ - protected void addToString(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { - - String toStringOperationBody; - - if (businessEntity.hasTagValue(WikittyTransformerUtil.TAG_TO_STRING)) { - String toStringPattern = businessEntity.getTagValue(WikittyTransformerUtil.TAG_TO_STRING); - - // toStringPattern is something like - // "hello {Person.name|unknow} employe of {Company.name|unknow}" - // - - Matcher matcher = toStringTagValuePattern.matcher(toStringPattern); - - while (matcher.find()) { - String wholeMatch = matcher.group(0); // "{foo|bar}" - String variableName = matcher.group(1); // "foo" - String defaultValue = matcher.group(3); // "bar", may be null - - if (defaultValue == null) { - defaultValue = ""; - } - - if (attributeToGetterName.containsKey(variableName)) { - String getterName = attributeToGetterName.get(variableName); - toStringPattern = toStringPattern.replace(wholeMatch, "" -/*{" - + <%=getterName%>().toString() + - "}*/); - } else { - log.warn("no field " + variableName + " in " + businessEntity.getQualifiedName()); - toStringPattern = toStringPattern.replace(wholeMatch, defaultValue); - } - } - - toStringOperationBody = "" -/*{ - return "<%=toStringPattern%>"; -}*/; - } else { - // no toString tagValue provided, generating a default toString - toStringOperationBody = "" -/*{ - return "dto:"+getWikittyId()+":"+getWikittyVersion(); -}*/; - } - - ObjectModelOperation toString = addOperation(abstractClass, "toString", "String"); - addAnnotation(abstractClass, toString, "Override"); - setOperationBody(toString, toStringOperationBody); - } - - protected void addMetaExtensionOperations(ObjectModelClass metaExtension, - ObjectModelClass abstractClassForThisMetaExtension) { - - //add standard attribute operations for meta-extension - addOperations(metaExtension, abstractClassForThisMetaExtension); - - } - - /** - * Method to get the generated class name - */ - protected String businessEntityToDTOName(ObjectModelClass clazz) { - return clazz.getName() + "DTO"; - } - - protected void addBusinessEntityMethods(ObjectModelClass dtoClass) { - - //adding wikittyId attribute - addAttribute(dtoClass, "wikittyId", "String"); - - //adding getWikittyId method - ObjectModelOperation getter = addOperation(dtoClass, "getWikittyId", "String"); - addAnnotation(dtoClass, getter, "Override"); - setOperationBody(getter, "" -/*{ - return wikittyId; -}*/); - - //adding setWikittyId method - ObjectModelOperation setter = addOperation(dtoClass, "setWikittyId", "void"); - addParameter(setter, "String", "wikittyId"); - setOperationBody(setter, "" -/*{ - this.wikittyId=wikittyId; -}*/); - - //adding wikittyVersion attribute - addAttribute(dtoClass, "wikittyVersion", "String"); - - - - //adding getWikittyVersion method - ObjectModelOperation getWikittyVersion = addOperation(dtoClass, - "getWikittyVersion", "String"); - addAnnotation(dtoClass, getWikittyVersion, "Override"); - setOperationBody(getWikittyVersion, "" -/*{ - return wikittyVersion; -}*/); - - //adding setWikittyVersion method - ObjectModelOperation setWikittyVersion = addOperation(dtoClass, - "setWikittyVersion", "void"); - addParameter(setWikittyVersion, "String", "wikittyVersion"); - setOperationBody(setWikittyVersion, "" -/*{ - this.wikittyVersion=wikittyVersion; -}*/); - - //adding necessary imports - addImport(dtoClass, java.beans.PropertyChangeListener.class); - addImport(dtoClass, java.util.Collection.class); - - // adding addPropertyChangeListener - ObjectModelOperation addPropertyChangeListener = addOperation(dtoClass, - "addPropertyChangeListener", "void"); - addAnnotation(dtoClass, addPropertyChangeListener, "Override"); - addParameter(addPropertyChangeListener, "PropertyChangeListener", "listener"); - setOperationBody(addPropertyChangeListener, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - - // adding removePropertyChangeListener - ObjectModelOperation removePropertyChangeListener = addOperation(dtoClass, - "removePropertyChangeListener", "void"); - addAnnotation(dtoClass, removePropertyChangeListener, "Override"); - addParameter(removePropertyChangeListener, "PropertyChangeListener", "listener"); - setOperationBody(removePropertyChangeListener, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - - // adding addPropertyChangeListener 2 parameters - ObjectModelOperation addPropertyChangeListener2 = addOperation(dtoClass, - "addPropertyChangeListener", "void"); - addAnnotation(dtoClass, addPropertyChangeListener2, "Override"); - addParameter(addPropertyChangeListener2, "String", "property"); - addParameter(addPropertyChangeListener2, "PropertyChangeListener", "listener"); - setOperationBody(addPropertyChangeListener2, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - - // adding removePropertyChangeListener 2 parameters - ObjectModelOperation removePropertyChangeListener2 = addOperation(dtoClass, - "removePropertyChangeListener", "void"); - addAnnotation(dtoClass, removePropertyChangeListener2, "Override"); - addParameter(removePropertyChangeListener2, "String", "property"); - addParameter(removePropertyChangeListener2, "PropertyChangeListener", "listener"); - setOperationBody(removePropertyChangeListener2, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - - //adding getExtensionFields method - ObjectModelOperation getExtensionFields = addOperation(dtoClass, - "getExtensionFields", "Collection<String>"); - addAnnotation(dtoClass, getExtensionFields, "Override"); - addParameter(getExtensionFields, "String", "ext"); - setOperationBody(getExtensionFields, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - - //adding getExtensionNames method - ObjectModelOperation getExtensionNames = addOperation(dtoClass, - "getExtensionNames", "Collection<String>"); - addAnnotation(dtoClass, getExtensionNames, "Override"); - setOperationBody(getExtensionNames, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - - //adding getField method - ObjectModelOperation getField = addOperation(dtoClass, "getField", "Object"); - addAnnotation(dtoClass, getField, "Override"); - addParameter(getField, "String", "ext"); - addParameter(getField, "String", "fieldName"); - setOperationBody(getField, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - - //adding setField method - ObjectModelOperation setField = addOperation(dtoClass, "setField", "void"); - addAnnotation(dtoClass, setField, "Override"); - addParameter(setField, "String", "ext"); - addParameter(setField, "String", "fieldName"); - addParameter(setField, "Object", "value"); - setOperationBody(setField, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - - } - - protected void addModelOperations(ObjectModelClass businessEntity, ObjectModelClass dtoClass){ - Collection<ObjectModelOperation> operations = businessEntity.getOperations(); - - for (ObjectModelOperation operation:operations){ - ObjectModelOperation dtoOperation = addOperation(dtoClass, - operation.getName(), operation.getReturnType()); - - Collection<ObjectModelParameter> parameters = operation.getParameters(); - for (ObjectModelParameter parameter:parameters){ - addParameter(dtoOperation, parameter.getType(), parameter.getName()); - } - addAnnotation(dtoClass, dtoOperation, "Override"); - setOperationBody(dtoOperation, "" -/*{ - throw new UnsupportedOperationException("Not supported yet."); -}*/); - } - } - -} Copied: trunk/wikitty-generators/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java (from rev 506, trunk/wikitty-dto/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java) =================================================================== --- trunk/wikitty-generators/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java (rev 0) +++ trunk/wikitty-generators/src/main/java/org/nuiton/wikitty/generator/WikittyDTOGenerator.java 2010-11-24 14:22:43 UTC (rev 509) @@ -0,0 +1,564 @@ +/* + * #%L + * Wikitty :: dto + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2010 CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.wikitty.generator; + +import java.beans.PropertyChangeListener; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.Transformer; +import org.nuiton.eugene.java.ObjectModelTransformerToJava; +import org.nuiton.eugene.models.object.ObjectModel; +import org.nuiton.eugene.models.object.ObjectModelAttribute; +import org.nuiton.eugene.models.object.ObjectModelClass; +import org.nuiton.eugene.models.object.ObjectModelModifier; +import org.nuiton.eugene.models.object.ObjectModelOperation; +import org.nuiton.eugene.models.object.ObjectModelParameter; + + +/*{generator option: writeString = }*/ +/*{generator option: parentheses = false}*/ +/*{generator option: writeString = +}*/ + +/** + * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.wikitty.generator.WikittyDTOGenerator" + */ +public class WikittyDTOGenerator extends ObjectModelTransformerToJava { + + private static final Log log = LogFactory.getLog(WikittyDTOGenerator.class); + + /** + * map busines entity from source model to generated abstract class + */ + protected Map<ObjectModelClass, ObjectModelClass> processedClasses = + new HashMap<ObjectModelClass, ObjectModelClass>(); + + /** + * pattern to parse toString tagValue + * for the string "abc{foo|bar}defrzeg{uvw|xyz}oeira" + * will match {foo|bar} and {uvw|xyz} with groups for foo, bar, uvw and xyz + */ + protected Pattern toStringTagValuePattern = Pattern.compile("\\{(([^|}])*)(?:\\|([^}]*))?\\}"); + + /** + * map "Client.name" to "getName()" or any getter to read this attribute + * those getter names are stored while generating in order to be found + * later when generating toString() + */ + protected Map<String, String> attributeToGetterName = new HashMap<String, String>(); + + @Override + protected Transformer<ObjectModel, ObjectModel> initPreviousTransformer() { + return new WikittyPurifierTransformer(); + } + + protected List<ObjectModelClass> entitiesWithInheritedOperations = + new ArrayList<ObjectModelClass>(); + + @Override + public void transformFromModel(ObjectModel model) { + + // contains boths businessEntities and metaExtensions + // elements may have one of the two sterotypes or both + // but elements with none of them aren't in this list + List<ObjectModelClass> modelBoth = new ArrayList<ObjectModelClass>(); + + // fill modelBusinessEntities with entities found in model + for (ObjectModelClass clazz : model.getClasses()) { + if (WikittyTransformerUtil.isBusinessEntity(clazz) || + WikittyTransformerUtil.isMetaExtension(clazz)) { + modelBoth.add(clazz); + } + } + + for (ObjectModelClass clazz : modelBoth) { + ObjectModelClass abstractClass = createClass( + businessEntityToDTOName(clazz), + clazz.getPackageName()); + processedClasses.put(clazz, abstractClass); + addInterface(abstractClass, clazz.getQualifiedName()); + } + + for (ObjectModelClass clazz : modelBoth) { + if (WikittyTransformerUtil.isMetaExtension(clazz)) { + addMetaExtensionOperations(clazz, processedClasses.get(clazz)); + } else if (WikittyTransformerUtil.isBusinessEntity(clazz)) { + addOperations(clazz, processedClasses.get(clazz)); + } + } + + // at this time, all operations in generated abstracts are just the operations + // like get/set etc. we will copy all operations of a given class to all children + // that's why constructors and others operations are not yet added + for (ObjectModelClass clazz : modelBoth) { + if (WikittyTransformerUtil.isBusinessEntity(clazz)) { + addInheritedOperations(clazz, processedClasses.get(clazz)); + } + } + + for (ObjectModelClass clazz : modelBoth) { + ObjectModelClass abstractClassForThisEntity = processedClasses.get(clazz); + addConstructors(abstractClassForThisEntity); + addToString(clazz, abstractClassForThisEntity); + addBusinessEntityMethods(abstractClassForThisEntity); + addModelOperations(clazz, abstractClassForThisEntity); + } + + processedClasses.clear(); + } + + protected void addSerialVersionUID(ObjectModelClass clazz) { + // adding a generated serialVersionUID + Random random = new Random(); + Long serialVersionUIDs = random.nextLong(); + addConstant(clazz, + "serialVersionUID", + "long", + serialVersionUIDs.toString() + "L", + ObjectModelModifier.PRIVATE); + } + + protected void addConstructors(ObjectModelClass clazz) { + + ObjectModelOperation constructor = addConstructor(clazz, ObjectModelModifier.PUBLIC); + setOperationBody(constructor, "" +/*{ +}*/); + + constructor = addConstructor(clazz, ObjectModelModifier.PUBLIC); + addParameter(constructor, WikittyTransformerUtil.WIKITTY_CLASS_FQN, "wikitty"); + setOperationBody(constructor, "" +/*{ + wikittyId=wikitty.getId(); +}*/); + + constructor = addConstructor(clazz, ObjectModelModifier.PUBLIC); + addParameter(constructor, "String", "wikittyId"); + setOperationBody(constructor, "" +/*{ + this.wikittyId=wikittyId; +}*/); + + } + + protected void addOperations(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { + // generating operations with bodies to realize contract + for (ObjectModelAttribute attribute : businessEntity.getAttributes()) { + if (attribute.isNavigable()) { + // needed below, in templates + String attributeType = WikittyTransformerUtil.generateResultType(attribute, false); + String attributeName = attribute.getName(); + + //add necessary import + addImport(abstractClass, attribute.getType()); + + //If alternative name, use it + if (attribute.hasTagValue(WikittyTransformerUtil.TAG_ALTERNATIVE_NAME)) { + attributeName = attribute.getTagValue(WikittyTransformerUtil.TAG_ALTERNATIVE_NAME); + } + + String getterName; + + if (WikittyTransformerUtil.isAttributeCollection(attribute)) { + // attribute is a collection, we will generate operations + // get, add, remove and clear + String collectionType = WikittyTransformerUtil.getCollectionTypeName(attribute); + if (collectionType.equals("Set")){ + addImport(abstractClass, Set.class); + } + if (collectionType.equals("List")){ + addImport(abstractClass, List.class); + + } + + String attributeTypeInSet = WikittyTransformerUtil.generateResultType(attribute, true); + + //adding the attribute + ObjectModelAttribute modelAttribute = addAttribute(abstractClass, attributeName, attributeTypeInSet); + String annotation = "WikittyField(fqn=\""+ businessEntity.getName() + "." + attributeName +"\")"; + addAnnotation(abstractClass, modelAttribute, annotation); + addImport(abstractClass, "org.nuiton.wikitty.entities.WikittyField"); + + + // adding the getter + getterName = "get" + StringUtils.capitalize(attributeName); + ObjectModelOperation getter = addOperation(abstractClass, getterName, attributeTypeInSet); + addAnnotation(abstractClass, getter, "Override"); + String getterBody = "" +/*{ + return <%=attributeName%>; +}*/; + setOperationBody(getter, getterBody); + + // adding the setter + getterName = "set" + StringUtils.capitalize(attributeName); + ObjectModelOperation setter = addOperation(abstractClass, getterName, "void"); + addAnnotation(abstractClass, setter, "Override"); + addParameter(setter, attributeTypeInSet, attributeName); + String setterBody = "" +/*{ + this.<%=attributeName%>=<%=attributeName%>; +}*/; + setOperationBody(setter, setterBody); + + //adding the add method + String addName = "add" + StringUtils.capitalize(attributeName); + ObjectModelOperation adder = addOperation(abstractClass, addName, "void"); + addAnnotation(abstractClass, adder, "Override"); + addParameter(adder, attributeType, "element"); + String adderBody = "" +/*{ + <%=attributeName%>.add(element); +}*/; + setOperationBody(adder, adderBody); + + //adding the remove method + String removeName = "remove" + StringUtils.capitalize(attributeName); + ObjectModelOperation remover = addOperation(abstractClass, removeName, "void"); + addAnnotation(abstractClass, remover, "Override"); + addParameter(remover, attributeType, "element"); + String removerBody = "" +/*{ + <%=attributeName%>.remove(element); +}*/; + setOperationBody(remover, removerBody); + + //adding the clear method + String clearName = "clear" + StringUtils.capitalize(attributeName); + ObjectModelOperation clear = addOperation(abstractClass, clearName, "void"); + addAnnotation(abstractClass, clear, "Override"); + String clearBody = "" +/*{ + <%=attributeName%>.clear(); +}*/; + setOperationBody(clear, clearBody); + + } else { + //No multiplicity + + //add necessary import + addImport(abstractClass, attribute.getType()); + + //adding the attribute + ObjectModelAttribute modelAttribute = addAttribute(abstractClass, attributeName, attributeType); + String annotation = "WikittyField(fqn=\"" + businessEntity.getName() + "."+attributeName + "\")"; + addAnnotation(abstractClass, modelAttribute, annotation); + addImport(abstractClass, "org.nuiton.wikitty.entities.WikittyField"); + + // adding getter + getterName = "get" + StringUtils.capitalize(attributeName); + ObjectModelOperation getter = addOperation(abstractClass, getterName, attributeType); + addAnnotation(abstractClass, getter, "Override"); + setOperationBody(getter, "" +/*{ + return <%=attributeName%>; +}*/); + + //adding setter + String setterName = "set" + StringUtils.capitalize(attributeName); + ObjectModelOperation setter = addOperation(abstractClass, setterName, "void"); + addAnnotation(abstractClass, setter, "Override"); + addParameter(setter, attributeType, attributeName); + setOperationBody(setter, "" +/*{ + this.<%=attributeName%>=<%=attributeName%>; +}*/); + } + + // save the getter name for this attribute + attributeToGetterName.put(businessEntity.getName() + "." + attributeName, getterName); + } + } + } + + /** + * Add inherited operations to the abstract generated from an entity. + * Method browse superClasses to copy methods and their bodies. The method + * deal with imports, cause bodies call the Helper of the classe where + * the attribute was declared. + * <p/> + * If A inherit from B and B inherit from and A, B, C not in the same package + * we B need to import CHelper and A need to import BHelper. But, since A + * has C attributes too, it needs CHelper too. So when we will process B, + * we will save needed imports, A will get them thus import CHelper. + */ + protected void addInheritedOperations(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { + + if (!entitiesWithInheritedOperations.contains(businessEntity)) { + + // add attributes and methods inherited from super classes + for (ObjectModelClass superClass : businessEntity.getSuperclasses()) { + + // process super classes first, so we will get inherited operations from superclass + addInheritedOperations(superClass, processedClasses.get(superClass)); + + if (WikittyTransformerUtil.isBusinessEntity(superClass)) { + + //Add operations for the super class + addOperations(superClass, abstractClass); + } + } + + entitiesWithInheritedOperations.add(businessEntity); + } + } + + /** + * add a toString method + * if a toString tagValue is attached to businessEntity, it will be used + * to generate a toString as this : + * <p/> + * given "hello {Person.name|unknow}" + * <p/> + * will try to replace first {...} by name field value for extension Person. + * if this information is not available, will do unknow. + */ + protected void addToString(ObjectModelClass businessEntity, ObjectModelClass abstractClass) { + + String toStringOperationBody; + + if (businessEntity.hasTagValue(WikittyTransformerUtil.TAG_TO_STRING)) { + String toStringPattern = businessEntity.getTagValue(WikittyTransformerUtil.TAG_TO_STRING); + + // toStringPattern is something like + // "hello {Person.name|unknow} employe of {Company.name|unknow}" + // + + Matcher matcher = toStringTagValuePattern.matcher(toStringPattern); + + while (matcher.find()) { + String wholeMatch = matcher.group(0); // "{foo|bar}" + String variableName = matcher.group(1); // "foo" + String defaultValue = matcher.group(3); // "bar", may be null + + if (defaultValue == null) { + defaultValue = ""; + } + + if (attributeToGetterName.containsKey(variableName)) { + String getterName = attributeToGetterName.get(variableName); + toStringPattern = toStringPattern.replace(wholeMatch, "" +/*{" + + <%=getterName%>().toString() + + "}*/); + } else { + log.warn("no field " + variableName + " in " + businessEntity.getQualifiedName()); + toStringPattern = toStringPattern.replace(wholeMatch, defaultValue); + } + } + + toStringOperationBody = "" +/*{ + return "<%=toStringPattern%>"; +}*/; + } else { + // no toString tagValue provided, generating a default toString + toStringOperationBody = "" +/*{ + return "dto:"+getWikittyId()+":"+getWikittyVersion(); +}*/; + } + + ObjectModelOperation toString = addOperation(abstractClass, "toString", "String"); + addAnnotation(abstractClass, toString, "Override"); + setOperationBody(toString, toStringOperationBody); + } + + protected void addMetaExtensionOperations(ObjectModelClass metaExtension, + ObjectModelClass abstractClassForThisMetaExtension) { + + //add standard attribute operations for meta-extension + addOperations(metaExtension, abstractClassForThisMetaExtension); + + } + + /** + * Method to get the generated class name + */ + protected String businessEntityToDTOName(ObjectModelClass clazz) { + return clazz.getName() + "DTO"; + } + + protected void addBusinessEntityMethods(ObjectModelClass dtoClass) { + + //adding wikittyId attribute + addAttribute(dtoClass, "wikittyId", "String"); + + //adding getWikittyId method + ObjectModelOperation getter = addOperation(dtoClass, "getWikittyId", "String"); + addAnnotation(dtoClass, getter, "Override"); + setOperationBody(getter, "" +/*{ + return wikittyId; +}*/); + + //adding setWikittyId method + ObjectModelOperation setter = addOperation(dtoClass, "setWikittyId", "void"); + addParameter(setter, "String", "wikittyId"); + setOperationBody(setter, "" +/*{ + this.wikittyId=wikittyId; +}*/); + + //adding wikittyVersion attribute + addAttribute(dtoClass, "wikittyVersion", "String"); + + + + //adding getWikittyVersion method + ObjectModelOperation getWikittyVersion = addOperation(dtoClass, + "getWikittyVersion", "String"); + addAnnotation(dtoClass, getWikittyVersion, "Override"); + setOperationBody(getWikittyVersion, "" +/*{ + return wikittyVersion; +}*/); + + //adding setWikittyVersion method + ObjectModelOperation setWikittyVersion = addOperation(dtoClass, + "setWikittyVersion", "void"); + addParameter(setWikittyVersion, "String", "wikittyVersion"); + setOperationBody(setWikittyVersion, "" +/*{ + this.wikittyVersion=wikittyVersion; +}*/); + + //adding necessary imports + addImport(dtoClass, PropertyChangeListener.class); + addImport(dtoClass, Collection.class); + + // adding addPropertyChangeListener + ObjectModelOperation addPropertyChangeListener = addOperation(dtoClass, + "addPropertyChangeListener", "void"); + addAnnotation(dtoClass, addPropertyChangeListener, "Override"); + addParameter(addPropertyChangeListener, "PropertyChangeListener", "listener"); + setOperationBody(addPropertyChangeListener, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + + // adding removePropertyChangeListener + ObjectModelOperation removePropertyChangeListener = addOperation(dtoClass, + "removePropertyChangeListener", "void"); + addAnnotation(dtoClass, removePropertyChangeListener, "Override"); + addParameter(removePropertyChangeListener, "PropertyChangeListener", "listener"); + setOperationBody(removePropertyChangeListener, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + + // adding addPropertyChangeListener 2 parameters + ObjectModelOperation addPropertyChangeListener2 = addOperation(dtoClass, + "addPropertyChangeListener", "void"); + addAnnotation(dtoClass, addPropertyChangeListener2, "Override"); + addParameter(addPropertyChangeListener2, "String", "property"); + addParameter(addPropertyChangeListener2, "PropertyChangeListener", "listener"); + setOperationBody(addPropertyChangeListener2, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + + // adding removePropertyChangeListener 2 parameters + ObjectModelOperation removePropertyChangeListener2 = addOperation(dtoClass, + "removePropertyChangeListener", "void"); + addAnnotation(dtoClass, removePropertyChangeListener2, "Override"); + addParameter(removePropertyChangeListener2, "String", "property"); + addParameter(removePropertyChangeListener2, "PropertyChangeListener", "listener"); + setOperationBody(removePropertyChangeListener2, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + + //adding getExtensionFields method + ObjectModelOperation getExtensionFields = addOperation(dtoClass, + "getExtensionFields", "Collection<String>"); + addAnnotation(dtoClass, getExtensionFields, "Override"); + addParameter(getExtensionFields, "String", "ext"); + setOperationBody(getExtensionFields, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + + //adding getExtensionNames method + ObjectModelOperation getExtensionNames = addOperation(dtoClass, + "getExtensionNames", "Collection<String>"); + addAnnotation(dtoClass, getExtensionNames, "Override"); + setOperationBody(getExtensionNames, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + + //adding getField method + ObjectModelOperation getField = addOperation(dtoClass, "getField", "Object"); + addAnnotation(dtoClass, getField, "Override"); + addParameter(getField, "String", "ext"); + addParameter(getField, "String", "fieldName"); + setOperationBody(getField, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + + //adding setField method + ObjectModelOperation setField = addOperation(dtoClass, "setField", "void"); + addAnnotation(dtoClass, setField, "Override"); + addParameter(setField, "String", "ext"); + addParameter(setField, "String", "fieldName"); + addParameter(setField, "Object", "value"); + setOperationBody(setField, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + + } + + protected void addModelOperations(ObjectModelClass businessEntity, ObjectModelClass dtoClass){ + Collection<ObjectModelOperation> operations = businessEntity.getOperations(); + + for (ObjectModelOperation operation:operations){ + ObjectModelOperation dtoOperation = addOperation(dtoClass, + operation.getName(), operation.getReturnType()); + + Collection<ObjectModelParameter> parameters = operation.getParameters(); + for (ObjectModelParameter parameter:parameters){ + addParameter(dtoOperation, parameter.getType(), parameter.getName()); + } + addAnnotation(dtoClass, dtoOperation, "Override"); + setOperationBody(dtoOperation, "" +/*{ + throw new UnsupportedOperationException("Not supported yet."); +}*/); + } + } + +}
participants (1)
-
tchemit@users.nuiton.org