Author: fdesbois Date: 2010-06-25 14:41:09 +0200 (Fri, 25 Jun 2010) New Revision: 2032 Url: http://nuiton.org/repositories/revision/topia/2032 Log: Evo #609 : Refactor generation for properties operations for association class case. Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 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-06-25 12:40:15 UTC (rev 2031) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2010-06-25 12:41:09 UTC (rev 2032) @@ -140,7 +140,7 @@ outputInterface = createInterface(clazzName, packageName); outputAbstract = createAbstractClass(clazzName + "Abstract", packageName); - // generateOperationsFromAttributes(input.getAttributes()); is executed in generateInterface + //generateOperationsFromAttributes(input.getAttributes()); is executed in generateInterface generateInterface(input, outputInterface, attributes, operations); generateAbstract(input, outputAbstract, attributes, operations); @@ -218,18 +218,13 @@ continue; } - // Don't manage association class for instance - if (attribute.hasAssociationClass()) { - continue; - } - if (attribute.getMaxMultiplicity() == 1) { // setXXX - addSimpleSetOperation(attribute); + addSingleSetOperation(attribute); // getXXX - addSimpleGetOperation(attribute, null); + addSingleGetOperation(attribute, null); } else { @@ -255,7 +250,7 @@ addMultipleAddAllOperation(attribute, collectionInterface); // setXXX - addMultipleSetOperation(attribute, collectionInterface); + addMultipleSetOperation(attribute, collectionInterface, collectionImpl); // removeXXX addMultipleRemoveOperation(attribute); @@ -273,6 +268,11 @@ addMultipleGetTopiaIdOperation(attribute); } + if (attribute.hasAssociationClass()) { + // getXXX with entity parameter + addMultipleGetOperationFromEntity(attribute); + } + // sizeXXX addMultipleSizeOperation(attribute); @@ -283,6 +283,22 @@ } } + protected String getPropertyName(ObjectModelAttribute attribute) { + String propertyName = attribute.getName(); + if (attribute.hasAssociationClass()) { + propertyName = TopiaGeneratorUtil.getAssocAttrName(attribute); + } + return propertyName; + } + + protected String getPropertyType(ObjectModelAttribute attribute) { + String propertyType = attribute.getType(); + if (attribute.hasAssociationClass()) { + propertyType = attribute.getAssociationClass().getQualifiedName(); + } + return propertyType; + } + // TODO-fdesbois-2010-06-25 : This method can be put in JavaBuilder or ObjectModelTransformerToJava protected ObjectModelOperation createPropertySetterSignature(ObjectModelClassifier classifier, String propertyType, @@ -305,10 +321,10 @@ return operation; } - protected void addSimpleSetOperation(ObjectModelAttribute attribute) { + protected void addSingleSetOperation(ObjectModelAttribute attribute) { - String attrName = attribute.getName(); - String attrType = attribute.getType(); + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); // Interface operation ObjectModelOperation interfaceOperation = @@ -319,6 +335,8 @@ ObjectModelOperation implOperation = cloneOperationSignature(interfaceOperation, outputAbstract); + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + setOperationBody(implOperation, "" /*{ <%=attrType%> oldValue = this.<%=attrName%>; @@ -339,11 +357,11 @@ * @param operationPrefix Operation prefix : 'get' by default, if prefix * is null */ - protected void addSimpleGetOperation(ObjectModelAttribute attribute, + protected void addSingleGetOperation(ObjectModelAttribute attribute, String operationPrefix) { - String attrName = attribute.getName(); - String attrType = attribute.getType(); + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); if (operationPrefix == null) { operationPrefix = TopiaGeneratorUtil.OPERATION_GETTER_DEFAULT_PREFIX; @@ -368,6 +386,8 @@ ObjectModelOperation implOperation = cloneOperationSignature(interfaceOperation, outputAbstract); + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + setOperationBody(implOperation, "" /*{ fireOnPreRead(<%=getConstantName(attrName)%>, <%=attrName%>); @@ -380,15 +400,15 @@ // Generate 'is' getter for boolean attributes if (attrType.toLowerCase().contains("boolean") && !operationPrefix.equals(TopiaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX)) { - addSimpleGetOperation(attribute, + addSingleGetOperation(attribute, TopiaGeneratorUtil.OPERATION_GETTER_BOOLEAN_PREFIX); } } protected void addMultipleAddOperation(ObjectModelAttribute attribute, String collectionImpl) { - String attrName = attribute.getName(); - String attrType = attribute.getType(); + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); ObjectModelAttribute reverse = attribute.getReverseAttribute(); // Interface operation @@ -408,6 +428,8 @@ ObjectModelOperation implOperation = cloneOperationSignature(interfaceOperation, outputAbstract); + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + StringBuilder body = new StringBuilder(); body.append("" @@ -429,6 +451,7 @@ <%=attrName%>.set<%=reverseAttrName%>(this); }*/ ); + // WARN-fdesbois-2010-06-25 : in previous code no else for association class } else { body.append("" /*{ @@ -451,8 +474,8 @@ protected void addMultipleAddAllOperation(ObjectModelAttribute attribute, String collectionInterface) { - String attrName = attribute.getName(); - String attrType = attribute.getType(); + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); // Interface operation ObjectModelOperation interfaceOperation = @@ -471,6 +494,8 @@ ObjectModelOperation implOperation = cloneOperationSignature(interfaceOperation, outputAbstract); + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + setOperationBody(implOperation, "" /*{ if (<%=attrName%> == null) { @@ -483,10 +508,11 @@ ); } - protected void addMultipleSetOperation(ObjectModelAttribute attribute, String collectionInterface) { + protected void addMultipleSetOperation(ObjectModelAttribute attribute, String collectionInterface, String collectionImpl) { - String attrName = attribute.getName(); - String attrType = collectionInterface + "<" + attribute.getType() + ">"; + String attrName = getPropertyName(attribute); + String referenceType = getPropertyType(attribute); + String attrType = collectionInterface + "<" + referenceType + ">"; // Interface operation ObjectModelOperation interfaceOperation = @@ -496,20 +522,25 @@ ObjectModelOperation implOperation = cloneOperationSignature(interfaceOperation, outputAbstract); + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + referenceType = TopiaGeneratorUtil.getSimpleName(referenceType); + // Force fire for collection setOperationBody(implOperation, "" /*{ - fireOnPreWrite(<%=getConstantName(attrName)%>, null, <%=attrName%>); + // Copy elements to keep data for fire with new reference + <%=attrType%> oldValue = this.<%=attrName%> != null ? new <%=collectionImpl%><<%=referenceType%>>(this.<%=attrName%>) : null; + fireOnPreWrite(<%=getConstantName(attrName)%>, oldValue, <%=attrName%>); this.<%=attrName%> = <%=attrName%>; - fireOnPostWrite(<%=getConstantName(attrName)%>, null, <%=attrName%>); + fireOnPostWrite(<%=getConstantName(attrName)%>, oldValue, <%=attrName%>); }*/ ); } protected void addMultipleRemoveOperation(ObjectModelAttribute attribute) { - String attrName = attribute.getName(); - String attrType = attribute.getType(); + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); ObjectModelAttribute reverse = attribute.getReverseAttribute(); // Interface operation @@ -529,6 +560,8 @@ ObjectModelOperation implOperation = cloneOperationSignature(interfaceOperation, outputAbstract); + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + StringBuilder body = new StringBuilder(); body.append("" @@ -549,6 +582,7 @@ <%=attrName%>.set<%=reverseAttrName%>(null); }*/ ); + // WARN-fdesbois-2010-06-25 : in previous code no else for association class } else { body.append("" /*{ @@ -569,8 +603,8 @@ String collectionInterface, String collectionImpl) { - String attrName = attribute.getName(); - String attrType = attribute.getType(); + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); ObjectModelAttribute reverse = attribute.getReverseAttribute(); // Interface operation @@ -587,6 +621,8 @@ ObjectModelOperation implOperation = cloneOperationSignature(interfaceOperation, outputAbstract); + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + StringBuilder body = new StringBuilder("" /*{ if (this.<%=attrName%> == null) { @@ -607,6 +643,7 @@ /*{ item.set<%=reverseAttrName%>(null); }*/ ); + // WARN-fdesbois-2010-06-25 : in previous code no else for association class } else { body.append("" /*{ item.get<%=reverseAttrName%>().remove(this); @@ -618,12 +655,11 @@ }*/ ); } - // FIXME-fdesbois-2010-06-25 : Very strange behavior, why the oldValue is a new instance ? body.append("" -/*{ <%=collectionInterface%><<%=attrType%>> _oldValue = new <%=collectionImpl%><<%=attrType%>>(this.<%=attrName%>); - fireOnPreWrite(<%=getConstantName(attrName)%>, _oldValue, this.<%=attrName%>); +/*{ <%=collectionInterface%><<%=attrType%>> oldValue = new <%=collectionImpl%><<%=attrType%>>(this.<%=attrName%>); + fireOnPreWrite(<%=getConstantName(attrName)%>, oldValue, this.<%=attrName%>); this.<%=attrName%>.clear(); - fireOnPostWrite(<%=getConstantName(attrName)%>, _oldValue, this.<%=attrName%>); + fireOnPostWrite(<%=getConstantName(attrName)%>, oldValue, this.<%=attrName%>); }*/ ); setOperationBody(implOperation, body.toString()); @@ -632,8 +668,8 @@ protected void addMultipleGetOperation(ObjectModelAttribute attribute, String collectionInterface) { - String attrName = attribute.getName(); - String attrType = collectionInterface + "<" + attribute.getType() + ">"; + String attrName = getPropertyName(attribute); + String attrType = collectionInterface + "<" + getPropertyType(attribute) + ">"; // Interface operation ObjectModelOperation interfaceOperation = @@ -660,8 +696,8 @@ protected void addMultipleGetTopiaIdOperation(ObjectModelAttribute attribute) { - String attrName = attribute.getName(); - String attrType = attribute.getType(); + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); // Interface operation ObjectModelOperation interfaceOperation = @@ -688,9 +724,50 @@ ); } + protected void addMultipleGetOperationFromEntity(ObjectModelAttribute attribute) { + + // reference to the real attribute name + String referenceName = attribute.getName(); + String referenceType = attribute.getType(); + + // association attribute name + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); + + // Interface operation + ObjectModelOperation interfaceOperation = + addOperation(outputInterface, + "get" + StringUtils.capitalize(attrName), attrType); + + addParameter(interfaceOperation, referenceType, referenceName); + + // Documentation + // no doc from previous code + + // Implementation + ObjectModelOperation implOperation = + cloneOperationSignature(interfaceOperation, outputAbstract); + + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + + setOperationBody(implOperation, "" +/*{ + if (<%=referenceName%> == null || <%=attrName%> == null) { + return null; + } + for (<%=attrType%> item : <%=attrName%>) { + if (<%=referenceName%>.equals(item.get<%=StringUtils.capitalize(referenceName)%>())) { + return item; + } + } + return null; +}*/ + ); + } + protected void addMultipleSizeOperation(ObjectModelAttribute attribute) { - String attrName = attribute.getName(); + String attrName = getPropertyName(attribute); // Interface operation ObjectModelOperation interfaceOperation = @@ -716,7 +793,7 @@ protected void addMultipleIsEmptyOperation(ObjectModelAttribute attribute) { - String attrName = attribute.getName(); + String attrName = getPropertyName(attribute); // Interface operation ObjectModelOperation interfaceOperation = @@ -783,24 +860,24 @@ generateOperationsFromAttributes(attributes); - for (ObjectModelAttribute attr : attributes) { - ObjectModelAttribute reverse = attr.getReverseAttribute(); - if (!attr.isNavigable() && - !TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType( - reverse, model)) { - continue; - } +// for (ObjectModelAttribute attr : attributes) { +// ObjectModelAttribute reverse = attr.getReverseAttribute(); +// if (!attr.isNavigable() && +// !TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType( +// reverse, model)) { +// continue; +// } +// +// if (attr.hasAssociationClass()) { +// +// addInterfaceAssociationAttribute(output, attr); +// +// } else { +// //addInterfaceNoneAssociationAttribute(output, attr); +// } +// } - if (attr.hasAssociationClass()) { - addInterfaceAssociationAttribute(output, attr); - - } else { - //addInterfaceNoneAssociationAttribute(output, attr); - } - } - - //Méthodes d'accès aux attributs d'une classe d'associations if (input instanceof ObjectModelAssociationClass) { @@ -1010,19 +1087,19 @@ generateCompositeMethod(output, input); + // DONE +// for (ObjectModelAttribute attr : attributes) { +// ObjectModelAttribute reverse = attr.getReverseAttribute(); +// +// if (!(attr.isNavigable() +// || hasUnidirectionalRelationOnAbstractType(reverse, model))) { +// continue; +// } +// +// transformAttribute(output, attr, reverse); +// } - for (ObjectModelAttribute attr : attributes) { - ObjectModelAttribute reverse = attr.getReverseAttribute(); - if (!(attr.isNavigable() - || hasUnidirectionalRelationOnAbstractType(reverse, model))) { - continue; - } - - transformAttribute(output, attr, reverse); - } - - //Méthodes d'accès aux attributs d'une classe d'associations if (input instanceof ObjectModelAssociationClass) { @@ -1109,153 +1186,6 @@ } @Deprecated - protected void addInterfaceNoneAssociationAttribute(ObjectModelInterface output, - ObjectModelAttribute attr) { - String attrName = attr.getName(); - String attrType = attr.getType(); - ObjectModelOperation op; - ObjectModelParameter attr2; - - if (!GeneratorUtil.isNMultiplicity(attr)) { - // DONE - - // setXXX - - op = addOperation(output, - "set" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - setDocumentation(op, attr.getDocumentation()); - } - attr2 = addParameter(op, attrType, - GeneratorUtil.toLowerCaseFirstLetter(attrName)); - setDocumentation(attr2, "La valeur de l'attribut " + attrName - + " à positionner."); - - // getXXX - - // 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 - addSimpleGetOperation(attr, null); - - } else { - String collectionInterface = - TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); - - // addXXX : DONE - - op = addOperation(output, - "add" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - setDocumentation(op, attr.getDocumentation()); - } - attr2 = addParameter(op, attrType, - GeneratorUtil.toLowerCaseFirstLetter(attrName)); - setDocumentation(attr2, - "L'instance de " + attrName + " à ajouter"); - - // addAllXXX : DONE - - op = addOperation(output, - "addAll" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - setDocumentation(op, attr.getDocumentation()); - } - attr2 = addParameter(op, - collectionInterface + "<" + attrType + ">", - GeneratorUtil.toLowerCaseFirstLetter(attrName)); - setDocumentation(attr2, - "Les instances de " + attrName + " à ajouter"); - - // setXXX : DONE - - op = addOperation(output, - "set" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - setDocumentation(op, attr.getDocumentation()); - } - attr2 = addParameter(op, - collectionInterface + "<" + attrType + ">", - GeneratorUtil.toLowerCaseFirstLetter(attrName)); - setDocumentation(attr2, - "La Collection de " + attrName + " à ajouter"); - - // removeXXX : DONE - - op = addOperation(output, - "remove" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - setDocumentation(op, attr.getDocumentation()); - } - attr2 = addParameter(op, - attrType, - GeneratorUtil.toLowerCaseFirstLetter(attrName)); - setDocumentation(attr2, - "L'instance de " + attrName + " à retirer"); - - // clearXXX : DONE - - op = addOperation(output, - "clear" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - setDocumentation(op, attr.getDocumentation()); - } - setDocumentation(attr2, "Vide la Collection de " + attrName); - - // getXXX : DONE - - op = addOperation(output, - "get" + StringUtils.capitalize(attrName), - collectionInterface + "<" + attrType + ">", - ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - setDocumentation(op, "Retourne la collection."); - } - - if (!TopiaGeneratorUtil.isPrimitiveType(attr) && - !TopiaGeneratorUtil.isDateType(attr)) { - - // getXXXByTopiaId : DONE - - op = addOperation(output, - "get" + StringUtils.capitalize(attrName) + "ByTopiaId", - attrType, - ObjectModelModifier.PACKAGE); - setDocumentation(op, "Recupère l'attribut " + attrName + " à partir de son topiaId"); - attr2 = addParameter(op, String.class, "topiaId"); - setDocumentation(attr2, "le topia id de l'entité recherchée"); - } - - // sizeXXX : DONE - - op = addOperation(output, - "size" + StringUtils.capitalize(attrName), - int.class, - ObjectModelModifier.PACKAGE); - setDocumentation(op, "Retourne le nombre d'éléments de la collection " + attrName); - - // isXXXEmpty : DONE - - op = addOperation(output, - "is" + StringUtils.capitalize(attrName) + "Empty", - boolean.class, - ObjectModelModifier.PACKAGE); - setDocumentation(op, "Retourne {@code true} si la collection " + attrName + " est vide."); - } - } - protected void addInterfaceAssociationAttribute(ObjectModelInterface output, ObjectModelAttribute attr) { String attrName = attr.getName(); @@ -1267,7 +1197,8 @@ ObjectModelOperation op; ObjectModelParameter attr2; - if (!GeneratorUtil.isNMultiplicity(attr)) { + // Ok + if (!GeneratorUtil.isNMultiplicity(attr) && false) { // setXXX @@ -1285,7 +1216,7 @@ assocClassFQN, ObjectModelModifier.PACKAGE); - } else { + } else if (GeneratorUtil.isNMultiplicity(attr)) { String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); // addXXX @@ -1434,6 +1365,7 @@ // Abstract generation // ------------------------------------------------------------------------- + @Deprecated protected void transformAttribute(ObjectModelClass result, ObjectModelAttribute attr, ObjectModelAttribute reverse) { @@ -1446,7 +1378,8 @@ ObjectModelOperation op; - if (!GeneratorUtil.isNMultiplicity(attr)) { + // Ok for both + if (!GeneratorUtil.isNMultiplicity(attr) && false) { if (attr.hasAssociationClass()) { String assocAttrName = GeneratorUtil.getAssocAttrName(attr); @@ -1455,7 +1388,7 @@ assocClassFQN = TopiaGeneratorUtil.getSimpleName(assocClassFQN); String name = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName); - // setXXX + // setXXX : DONE op = addOperation(result, "set" + StringUtils.capitalize(assocAttrName), @@ -1471,7 +1404,7 @@ }*/ ); - // getXXX : getter for association attribute with unique multiplicity + // getXXX : getter for association attribute with unique multiplicity : DONE op = addOperation(result, "get" + StringUtils.capitalize(assocAttrName), @@ -1508,7 +1441,7 @@ // see {@link #addSimpleGetterOperation(ObjectModelAttribute, String)} } - } else { //NMultiplicity + } else if (GeneratorUtil.isNMultiplicity(attr)) { //NMultiplicity String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); String collectionObject = TopiaGeneratorUtil.getNMultiplicityObjectType(attr); addImport(result, collectionInterface);