r2546 - in trunk: topia-persistence/src/main/java/org/nuiton/topia/generator topia-persistence-tck/src/main/xmi
Author: tchemit Date: 2012-05-28 11:24:04 +0200 (Mon, 28 May 2012) New Revision: 2546 Url: http://nuiton.org/repositories/revision/topia/2546 Log: improve natural id generation (using getter) + push tag values in model Modified: trunk/topia-persistence-tck/src/main/xmi/topia-tck-mapping.properties trunk/topia-persistence-tck/src/main/xmi/topia-tck-mapping.zargo trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityJPAMappingGenerator.java trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityJPAMappingGenerator.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityJPAMappingGenerator.java 2012-05-28 09:22:35 UTC (rev 2545) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityJPAMappingGenerator.java 2012-05-28 09:24:04 UTC (rev 2546) @@ -393,7 +393,9 @@ private void generateEmbeddedAttribute(Writer output, ObjectModelClass input) throws IOException { Map<String, String> xmlAttributes = Maps.newTreeMap(); - addAttribute(xmlAttributes, ATTRIBUTE_NAME, EntityTransformer.NATURAL_ID_FIELD_NAME); + String name = Introspector.decapitalize( + TopiaGeneratorUtil.GET_NATURAL_ID_CLASS_SIMPLE_NAME.apply(input)); + addAttribute(xmlAttributes, ATTRIBUTE_NAME, name); addAttribute(xmlAttributes, ATTRIBUTE_ACCESS, AccessType.FIELD); /*{<%=flushTag(TAG_EMBEDDED, true, xmlAttributes)%> }*/ 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 2012-05-28 09:22:35 UTC (rev 2545) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2012-05-28 09:24:04 UTC (rev 2546) @@ -46,6 +46,8 @@ import org.nuiton.topia.persistence.TopiaEntityAbstract; import org.nuiton.topia.persistence.TopiaEntityContextable; +import java.beans.Introspector; +import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList; @@ -85,9 +87,6 @@ /** Logger */ private static final Log log = LogFactory.getLog(EntityTransformer.class); - public static final String NATURAL_ID_FIELD_NAME = "naturalId"; - - protected ObjectModelInterface outputInterface; protected ObjectModelClass outputAbstract; @@ -108,16 +107,29 @@ protected boolean generateBooleanGetMethods; + protected String naturalIdClassName; + + protected String naturalIdFieldName; + + protected String naturalIdGetterName; + // la liste des attributs faisant parti de la clef metier - protected List<ObjectModelAttribute> naturalAttributes = new ArrayList<ObjectModelAttribute>(); + protected final List<ObjectModelAttribute> naturalAttributes = + new ArrayList<ObjectModelAttribute>(); // la liste des autres attributs - protected List<ObjectModelAttribute> noneNaturalAttributes = new ArrayList<ObjectModelAttribute>(); + protected final List<ObjectModelAttribute> noneNaturalAttributes = + new ArrayList<ObjectModelAttribute>(); + protected void clean() { outputInterface = null; outputAbstract = null; outputImpl = null; outputNaturalIdEmbedded = null; + naturalAttributes.clear(); + noneNaturalAttributes.clear(); + generateInterface = generateAbstract = + generateImpl = generateBooleanGetMethods = associationClass = false; } @Override @@ -134,10 +146,6 @@ log.debug("Will use classLoader " + getClassLoader()); } - naturalAttributes = new ArrayList<ObjectModelAttribute>(); - - noneNaturalAttributes = new ArrayList<ObjectModelAttribute>(); - // fix once for all the constant prefix to use String prefix = getConstantPrefix(input, ""); if (StringUtils.isEmpty(prefix)) { @@ -163,10 +171,13 @@ } } + naturalIdClassName = + TopiaGeneratorUtil.GET_NATURAL_ID_CLASS_SIMPLE_NAME.apply(input); + generateInterface = isGenerateInterface(input); generateAbstract = isGenerateAbstract(input); generateImpl = isGenerateImpl(input); - generateNaturalIdEmbedded= isGenerateNaturalIdEmbedded(input); + generateNaturalIdEmbedded = isGenerateNaturalIdEmbedded(input); String noGenerateBooleanGetMethods = TopiaGeneratorUtil.getDoNotGenerateBooleanGetMethods(model, input); @@ -188,6 +199,11 @@ if (generateNaturalIdEmbedded) { + naturalIdFieldName = Introspector.decapitalize(naturalIdClassName); + + naturalIdGetterName = getJavaBeanMethodName("get", + naturalIdFieldName); + // Create Entity Natural Id class and its header createEntityNaturalIdEmbeddedClass(input); } @@ -245,29 +261,15 @@ generateSerialVersionUID(input, outputImpl); } + if (generateNaturalIdEmbedded) { + generateSerialVersionUID(outputNaturalIdEmbedded, + outputNaturalIdEmbedded); + } - // Clean data output after transformation clean(); } - protected void generateSerialVersionUID(ObjectModelClass input, - ObjectModelClass ouput) { - - // serialVersionUID - String svUID = TopiaGeneratorUtil.findTagValue(TopiaGeneratorUtil.SERIAL_VERSION_UID, - input, - model - ); - if (svUID == null) { - - // use a default one - svUID = TopiaGeneratorUtil.generateSerialVersionUID(ouput) + "L"; - } - addConstant(ouput, TopiaGeneratorUtil.SERIAL_VERSION_UID, long.class, svUID, - ObjectModelModifier.PRIVATE); - } - protected void createEntityInterface(ObjectModelClass input) { outputInterface = createInterface(input.getName(), @@ -352,46 +354,14 @@ addContextableMethods(input, outputAbstract); } - protected void createNaturalIdField(ObjectModelClass input) { + protected void createEntityNaturalIdEmbeddedClass(ObjectModelClass input) { - String fieldName = NATURAL_ID_FIELD_NAME; + outputNaturalIdEmbedded = createClass( + TopiaGeneratorUtil.GET_NATURAL_ID_CLASS_SIMPLE_NAME.apply(input), + input.getPackageName()); - // add a naturalId field - String naturalIdClassName = getNaturalIdClassName(input); + addInterface(outputNaturalIdEmbedded, Serializable.class); - addAttribute(outputAbstract, - fieldName, naturalIdClassName, "new " + naturalIdClassName + "()", - //ObjectModelModifier.toValue(attrVisibility), - ObjectModelModifier.PROTECTED - ); - - // add a getter - - // Interface operation - ObjectModelOperation interfaceOperation = - addOperation(outputInterface, getJavaBeanMethodName("get", fieldName), - naturalIdClassName, ObjectModelModifier.PUBLIC); - - // Implementation - ObjectModelOperation implOperation = - createImplOperation(interfaceOperation); - - String attrType = - TopiaGeneratorUtil.getSimpleName(naturalIdClassName); - - setOperationBody(implOperation, "" -/*{ - <%=attrType%> result = this.<%=fieldName%>; - return result; - }*/ - ); - } - - protected void createEntityNaturalIdEmbeddedClass(ObjectModelClass input) { - - outputNaturalIdEmbedded = createClass(getNaturalIdClassName(input), - input.getPackageName()); - // Documentation StringBuilder doc = new StringBuilder(); doc.append("Classe représentant la clef naturel de l'entité {@link "); @@ -401,44 +371,21 @@ setDocumentation(outputNaturalIdEmbedded, doc.toString()); } - protected String getNaturalIdClassName(ObjectModelClass input) { - return TopiaGeneratorUtil.GET_NATURAL_ID_CLASS_SIMPLE_NAME.apply(input); - } + protected void generateSerialVersionUID(ObjectModelClass input, + ObjectModelClass ouput) { - /** - * Ajout les methodes necessaire à l'interface {@link TopiaEntityContextable} - * si le tagValue {@link TopiaTagValues#TAG_CONTEXTABLE} est renseigné. - * @param input - * @param outputAbstract - */ - protected void addContextableMethods(ObjectModelClass input, - ObjectModelClass outputAbstract) { + // serialVersionUID + String svUID = TopiaGeneratorUtil.findTagValue(TopiaGeneratorUtil.SERIAL_VERSION_UID, + input, + model + ); + if (svUID == null) { - if (TopiaGeneratorUtil.isContextable(input)) { - - addImport(outputAbstract, TopiaContextImplementor.class); - - ObjectModelOperation op = addOperation(outputAbstract, "update", "void", - ObjectModelModifier.PUBLIC); - addException(op, TopiaException.class); -// setDocumentation(op,"@since 2.5.3"); - addAnnotation(outputAbstract, op, Override.class.getSimpleName()); - setOperationBody(op, "" -/*{ - ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=input.getName()%>.class).update(this); -}*/ - ); - - op = addOperation(outputAbstract, "delete", "void", ObjectModelModifier.PUBLIC); - addException(op, TopiaException.class); -// setDocumentation(op,"@since 2.5.3"); - addAnnotation(outputAbstract, op, Override.class.getSimpleName()); - setOperationBody(op, "" -/*{ - ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=input.getName()%>.class).delete(this); -}*/ - ); + // use a default one + svUID = TopiaGeneratorUtil.generateSerialVersionUID(ouput) + "L"; } + addConstant(ouput, TopiaGeneratorUtil.SERIAL_VERSION_UID, long.class, svUID, + ObjectModelModifier.PRIVATE); } protected boolean isGenerateInterface(ObjectModelClass input) { @@ -456,8 +403,7 @@ protected boolean isGenerateNaturalIdEmbedded(ObjectModelClass input) { - String fqn = getNaturalIdClassName(input); - boolean alreadyInClassPath = isInClassPath(fqn); + boolean alreadyInClassPath = isInClassPath(naturalIdClassName); if (alreadyInClassPath) { return false; @@ -497,6 +443,72 @@ return true; } + protected void createNaturalIdField(ObjectModelClass input) { + + // add a naturalId field + + ObjectModelAttribute attribute = addAttribute( + outputAbstract, + naturalIdFieldName, naturalIdClassName, "new " + naturalIdClassName + "()", + ObjectModelModifier.PROTECTED,ObjectModelModifier.FINAL + ); + setDocumentation(attribute, "Clef métier de l'entité."); + + // add a getter + + // Interface operation + ObjectModelOperation interfaceOperation = + addOperation(outputInterface, naturalIdGetterName, + naturalIdClassName, ObjectModelModifier.PUBLIC); + + // Implementation + ObjectModelOperation implOperation = + createImplOperation(interfaceOperation); + + setOperationBody(implOperation, "" +/*{ + <%=naturalIdClassName%> result = this.<%=naturalIdFieldName%>; + return result; + }*/ + ); + } + + /** + * Ajout les methodes necessaire à l'interface {@link TopiaEntityContextable} + * si le tagValue {@link TopiaTagValues#TAG_CONTEXTABLE} est renseigné. + * @param input + * @param outputAbstract + */ + protected void addContextableMethods(ObjectModelClass input, + ObjectModelClass outputAbstract) { + + if (TopiaGeneratorUtil.isContextable(input)) { + + addImport(outputAbstract, TopiaContextImplementor.class); + + ObjectModelOperation op = addOperation(outputAbstract, "update", "void", + ObjectModelModifier.PUBLIC); + addException(op, TopiaException.class); +// setDocumentation(op,"@since 2.5.3"); + addAnnotation(outputAbstract, op, Override.class.getSimpleName()); + setOperationBody(op, "" +/*{ + ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=input.getName()%>.class).update(this); +}*/ + ); + + op = addOperation(outputAbstract, "delete", "void", ObjectModelModifier.PUBLIC); + addException(op, TopiaException.class); +// setDocumentation(op,"@since 2.5.3"); + addAnnotation(outputAbstract, op, Override.class.getSimpleName()); + setOperationBody(op, "" +/*{ + ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=input.getName()%>.class).delete(this); +}*/ + ); + } + } + protected void generateImpl(ObjectModelClass input) { String implName = input.getName() + "Impl"; @@ -639,7 +651,6 @@ ObjectModelAttribute property = addAttribute(isNaturalId ? outputNaturalIdEmbedded : outputAbstract, attrName, attrType, null, - //ObjectModelModifier.toValue(attrVisibility), ObjectModelModifier.PROTECTED ); @@ -799,6 +810,20 @@ fireOnPostWrite(<%=constantName%>, oldValue, <%=attrName%>); }*/ ); + + if (isNaturalId(attribute)) { + + // add also a setter in the natural id class + implOperation = + cloneOperationSignature(interfaceOperation, outputNaturalIdEmbedded, false, + ObjectModelModifier.PUBLIC ); + + setOperationBody(implOperation, "" +/*{ + this.<%=attrName%> = <%=attrName%>; + }*/ + ); + } } /** @@ -1012,7 +1037,7 @@ protected String getReceiverName(ObjectModelAttribute attribute, String attrName) { String receiverName; if (isNaturalId(attribute)) { - receiverName = NATURAL_ID_FIELD_NAME + "." + attrName; + receiverName = naturalIdGetterName + "()." + attrName; } else { receiverName = "this." + attrName; } Modified: trunk/topia-persistence-tck/src/main/xmi/topia-tck-mapping.properties =================================================================== --- trunk/topia-persistence-tck/src/main/xmi/topia-tck-mapping.properties 2012-05-28 09:22:35 UTC (rev 2545) +++ trunk/topia-persistence-tck/src/main/xmi/topia-tck-mapping.properties 2012-05-28 09:24:04 UTC (rev 2546) @@ -24,7 +24,3 @@ ### model.tagvalue.generateOperatorForDAOHelper=true model.tagvalue.constantPrefix=PROPERTY_ - -org.nuiton.topia.tck.mapping.test12.A12A.attribute.integerField.tagValue.notNull=true -org.nuiton.topia.tck.mapping.test12.A12A.attribute.dateField.tagValue.notNull=true -org.nuiton.topia.tck.mapping.test12.A12A.attribute.stringField.tagValue.notNull=true Modified: trunk/topia-persistence-tck/src/main/xmi/topia-tck-mapping.zargo =================================================================== (Binary files differ)
participants (1)
-
tchemit@users.nuiton.org