r1980 - in trunk/topia-persistence: . src/main/java/org/nuiton/topia/generator src/test/xmi
Author: fdesbois Date: 2010-05-27 18:44:07 +0200 (Thu, 27 May 2010) New Revision: 1980 Url: http://nuiton.org/repositories/revision/topia/1980 Log: - Add EntityTransformer for tests (exclude old transformers) - Evo #643 : manage getter in EntityTransformer (with 'is' prefix for boolean attributes) Modified: trunk/topia-persistence/pom.xml trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java trunk/topia-persistence/src/test/xmi/topiatest.zargo Modified: trunk/topia-persistence/pom.xml =================================================================== --- trunk/topia-persistence/pom.xml 2010-05-26 14:17:56 UTC (rev 1979) +++ trunk/topia-persistence/pom.xml 2010-05-27 16:44:07 UTC (rev 1980) @@ -187,11 +187,18 @@ <phase>generate-test-sources</phase> <configuration> <testPhase>true</testPhase> - <templates>org.nuiton.topia.generator.TopiaMetaTransformer, + <templates> + org.nuiton.topia.generator.TopiaMetaTransformer, + org.nuiton.topia.generator.EntityTransformer, org.nuiton.topia.generator.InterfaceTransformer, org.nuiton.topia.generator.BeanTransformer, org.nuiton.topia.generator.EntityDTOTransformer </templates> + <excludeTemplates> + <excludeTemplate>org.nuiton.topia.generator.EntityAbstractTransformer</excludeTemplate> + <excludeTemplate>org.nuiton.topia.generator.EntityImplTransformer</excludeTemplate> + <excludeTemplate>org.nuiton.topia.generator.EntityInterfaceTransformer</excludeTemplate> + </excludeTemplates> <fullPackagePath>org.nuiton.topia</fullPackagePath> <defaultPackage>org.nuiton.topia</defaultPackage> </configuration> Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2010-05-26 14:17:56 UTC (rev 1979) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2010-05-27 16:44:07 UTC (rev 1980) @@ -81,16 +81,28 @@ * <li>There is no abstract method</li> * <li>There is no already defined such class in class-path</li> * </ul> - * + * * @author tchemit <chemit@codelutin.com> * @since 2.4 * @plexus.component role="org.nuiton.eugene.Template" role-hint="org.nuiton.topia.generator.EntityTransformer" */ public class EntityTransformer extends ObjectModelTransformerToJava { - + /** Logger */ private static final Log log = LogFactory.getLog(EntityTransformer.class); + protected ObjectModelInterface outputInterface; + + protected ObjectModelClass outputAbstract; + + protected ObjectModelClass outputImpl; + + protected void clean() { + outputInterface = null; + outputAbstract = null; + outputImpl = null; + } + @Override public void transformFromClass(ObjectModelClass input) { @@ -99,12 +111,12 @@ // not an entity, skip class. return; } - + if (log.isDebugEnabled()) { log.debug("for entity : "+input.getQualifiedName()); log.info("Will use classLoader "+ getClassLoader()); } - + // fix once for all the constant prefix to use String prefix = getConstantPrefix(input, ""); @@ -126,23 +138,30 @@ Collection<ObjectModelOperation> operations = input.getOperations(); - ObjectModelClassifier output; + // Create classifiers : Interface, Abstract + outputInterface = createInterface(clazzName, packageName); + outputAbstract = createAbstractClass(clazzName + "Abstract", packageName); - // generate interface + generateInterface(input, outputInterface, attributes, operations); + generateAbstract(input, outputAbstract, attributes, operations); - { - output = createInterface(clazzName, packageName); - generateInterface(input, (ObjectModelInterface) output, attributes, operations); - } - - // generate abstract +// ObjectModelClassifier output; +// +// // generate interface +// +// { +// outputInterface = createInterface(clazzName, packageName); +// generateInterface(input, outputInterface, attributes, operations); +// } +// +// // generate abstract +// +// { +// outputAbstract = createAbstractClass( clazzName + "Abstract", packageName); +// generateAbstract(input, outputAbstract, attributes, operations); +// +// } - { - output = createAbstractClass( clazzName + "Abstract", packageName); - generateAbstract(input, (ObjectModelClass) output, attributes, operations); - - } - boolean generateImpl = isGenerateImpl(input, operations); if (generateImpl) { @@ -153,14 +172,17 @@ } if (isAbstract(input)) { - output = createAbstractClass(implName, packageName); + outputImpl = createAbstractClass(implName, packageName); } else { - output = createClass(implName, packageName); + outputImpl = createClass(implName, packageName); } // generate impl - generateImpl(input, (ObjectModelClass) output); - } + generateImpl(input, outputImpl); + } + + // Clean data output after transformation + clean(); } protected boolean isGenerateImpl(ObjectModelClass input, @@ -178,7 +200,7 @@ } return false; } - + // On ne génère pas le impl si l'entité a des opérations qui ne sont // pas seulement pour le DAO if (!operations.isEmpty()) { @@ -215,7 +237,7 @@ if (log.isDebugEnabled()) { log.debug("Add constants from dependency : " + constants); } - + if (TopiaGeneratorUtil.hasDocumentation(input)) { setDocumentation(output,input.getDocumentation()); } @@ -262,7 +284,7 @@ } //Méthodes d'accès aux attributs d'une classe d'associations - + if (input instanceof ObjectModelAssociationClass) { ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) input; @@ -593,13 +615,11 @@ // getXXX - op = addOperation(output, - "get" + StringUtils.capitalize(attrName), - attrType, - ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - setDocumentation(op, attr.getDocumentation()); - } + // Add getter for simple property. + // Only one call for this method, no need for abstract generation + // TODO-fdesbois-2010-05-27 : manage all attribute cases in this method + addSimpleGetterOperation(attr, null); + } else { String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); @@ -738,7 +758,7 @@ attr2 = addParameter(op, assocClassFQN, GeneratorUtil.toLowerCaseFirstLetter(assocClassName)); setDocumentation(attr2, "La valeur de l'attribut " + assocClassName + " à positionner"); - // getXXX + // getXXX : getter interface for association attribute with unique multiplicity addOperation(output, "get" + StringUtils.capitalize(assocAttrName), @@ -868,7 +888,7 @@ addException(op2, exception); } } - + private void generateInterfaceAssociationAccessors(ObjectModelInterface output, String attrName, String attrType) { @@ -931,7 +951,7 @@ }*/ ); - // getXXX + // getXXX : getter for association attribute with unique multiplicity op = addOperation(result, "get" + StringUtils.capitalize(assocAttrName), @@ -960,20 +980,11 @@ ); - // getXXX + // getXXX : getter for simple attribute (as bean convention) - op = addOperation(result, - "get" + StringUtils.capitalize(attrName), - attrType, - ObjectModelModifier.PUBLIC); - setOperationBody(op, "" -/*{ - fireOnPreRead(<%=getConstantName(attrName)%>, <%=attrName%>); - <%=attrType%> result = this.<%=attrName%>; - fireOnPostRead(<%=getConstantName(attrName)%>, <%=attrName%>); - return result; -}*/ - ); + // Getter is already set in abstract when added in interface + // see {@link #addSimpleGetterOperation(ObjectModelAttribute, String)} + } } else { //NMultiplicity String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); @@ -1340,7 +1351,7 @@ if (!attr.hasAssociationClass()) { - // getXXX + // getXXX : getter for collection entity attribute (N multiplicity) op = addOperation(result, "get" + StringUtils.capitalize(attrName), @@ -1385,7 +1396,8 @@ String assocClassFQN = attr.getAssociationClass().getQualifiedName(); // String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); - // getXXX + // getXXX : getter for association attribute with no parameter + // (return a collection) op = addOperation(result, "get" + StringUtils.capitalize(assocAttrName), @@ -1397,7 +1409,8 @@ }*/ ); - // getXXX + // getXXX : getter for association attribute with one parameter + // (return a single element) op = addOperation(result, "get" + StringUtils.capitalize(assocAttrName), @@ -1451,6 +1464,63 @@ } } + /** + * Add getter for simple property (neither association nor multiple). + * Will add two different operations for boolean case ('is' method and + * 'get' method). This method add the operation in both {@code + * outputAbstract} and {@code outputInterface}. + * + * @param attribute ObjectModelAttribute for getter operation + * @param operationPrefix Operation prefix : 'get' by default, if prefix + * is null + */ + protected void addSimpleGetterOperation(ObjectModelAttribute attribute, + String operationPrefix) { + + String attrName = attribute.getName(); + String attrType = attribute.getType(); + + if (operationPrefix == null) { + operationPrefix = TopiaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX; + } + + if (log.isDebugEnabled()) { + log.debug("Add getter operation for " + attrName + + " prefix = " + operationPrefix); + } + + // CONTRACT in Interface + ObjectModelOperation contract = addOperation(outputInterface, + operationPrefix + StringUtils.capitalize(attrName), + attrType, + ObjectModelModifier.PACKAGE); + + if (TopiaGeneratorUtil.hasDocumentation(attribute)) { + setDocumentation(contract, attribute.getDocumentation()); + } + + // IMPLEMENTATION in Abstract + ObjectModelOperation impl = addOperation(outputAbstract, + contract.getName(), + contract.getReturnType(), + ObjectModelModifier.PUBLIC); + setOperationBody(impl, "" +/*{ + fireOnPreRead(<%=getConstantName(attrName)%>, <%=attrName%>); + <%=attrType%> result = this.<%=attrName%>; + fireOnPostRead(<%=getConstantName(attrName)%>, <%=attrName%>); + return result; +}*/ + ); + + // Generate 'is' getter for boolean attributes + if (attrType.toLowerCase().contains("boolean") && + !operationPrefix.equals(TopiaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX)) { + addSimpleGetterOperation(attribute, + TopiaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX); + } + } + protected void generateToStringMethod(ObjectModelClass output, ObjectModelClass clazz) { if (log.isDebugEnabled()) { Modified: trunk/topia-persistence/src/test/xmi/topiatest.zargo =================================================================== (Binary files differ)
participants (1)
-
fdesbois@users.nuiton.org