Author: fdesbois Date: 2010-06-25 15:12:59 +0200 (Fri, 25 Jun 2010) New Revision: 2033 Url: http://nuiton.org/repositories/revision/topia/2033 Log: Evo #609 : Remove old ugly code + add some javadoc 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:41:09 UTC (rev 2032) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2010-06-25 13:12:59 UTC (rev 2033) @@ -140,7 +140,8 @@ outputInterface = createInterface(clazzName, packageName); outputAbstract = createAbstractClass(clazzName + "Abstract", packageName); - //generateOperationsFromAttributes(input.getAttributes()); is executed in generateInterface + // Generate all methods needed for each entity attribute + generateOperationsFromAttributes(input.getAttributes()); generateInterface(input, outputInterface, attributes, operations); generateAbstract(input, outputAbstract, attributes, operations); @@ -208,6 +209,27 @@ return true; } + /** + * Generation operations for each attribute from {@code attributes} collection. + * One method is created for each operation to generate. Methods starting + * with 'addSingle' is for maxMultiplicity attribute = 1 and for collection + * case, methods start with 'addMultiple'. Other case are take care in each + * method (association class, reverse, entity reference, ...). + * + * @param attributes Input attributes to treate + * @see #addSingleGetOperation(ObjectModelAttribute, String) + * @see #addSingleSetOperation(ObjectModelAttribute) + * @see #addMultipleAddOperation(ObjectModelAttribute, String) + * @see #addMultipleAddAllOperation(ObjectModelAttribute, String) + * @see #addMultipleSetOperation(ObjectModelAttribute, String, String) + * @see #addMultipleRemoveOperation(ObjectModelAttribute) + * @see #addMultipleClearOperation(ObjectModelAttribute, String, String) + * @see #addMultipleGetOperation(ObjectModelAttribute, String) + * @see #addMultipleGetTopiaIdOperation(ObjectModelAttribute) + * @see #addMultipleGetOperationFromEntity(ObjectModelAttribute) + * @see #addMultipleSizeOperation(ObjectModelAttribute) + * @see #addMultipleIsEmptyOperation(ObjectModelAttribute) + */ protected void generateOperationsFromAttributes(Collection<ObjectModelAttribute> attributes) { for (ObjectModelAttribute attribute : attributes) { @@ -283,44 +305,6 @@ } } - 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, - String propertyName, - String operationDocumentation) { - // Operation - ObjectModelOperation operation = - addOperation(classifier, "set" + StringUtils.capitalize(propertyName), - void.class, ObjectModelModifier.PACKAGE); - - ObjectModelParameter param = - addParameter(operation, propertyType, propertyName); - - // Documentation - if (StringUtils.isNotEmpty(operationDocumentation)) { - setDocumentation(operation, operationDocumentation); - } - setDocumentation(param, "La valeur de l'attribut à positionner."); - - return operation; - } - protected void addSingleSetOperation(ObjectModelAttribute attribute) { String attrName = getPropertyName(attribute); @@ -677,10 +661,6 @@ attrType, ObjectModelModifier.PACKAGE); // Documentation -// if (TopiaGeneratorUtil.hasDocumentation(attribute)) { -// // ?? -// setDocumentation(interfaceOperation, "Retourne la collection."); -// } setDocumentation(interfaceOperation, "Retourne la collection."); // Implementation @@ -856,28 +836,6 @@ generateInterfaceStaticColumnNames(input, output); - // attributes - - generateOperationsFromAttributes(attributes); - -// 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); -// } -// } - - //Méthodes d'accès aux attributs d'une classe d'associations if (input instanceof ObjectModelAssociationClass) { @@ -1185,129 +1143,6 @@ } } - @Deprecated - protected void addInterfaceAssociationAttribute(ObjectModelInterface output, - ObjectModelAttribute attr) { - String attrName = attr.getName(); - String attrType = attr.getType(); - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); - String assocClassName = attr.getAssociationClass().getName(); - - ObjectModelOperation op; - ObjectModelParameter attr2; - - // Ok - if (!GeneratorUtil.isNMultiplicity(attr) && false) { - - // setXXX - - op = addOperation(output, - "set" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PACKAGE); - attr2 = addParameter(op, assocClassFQN, GeneratorUtil.toLowerCaseFirstLetter(assocClassName)); - setDocumentation(attr2, "La valeur de l'attribut " + assocClassName + " à positionner"); - - // getXXX : getter interface for association attribute with unique multiplicity - - addOperation(output, - "get" + StringUtils.capitalize(assocAttrName), - assocClassFQN, - ObjectModelModifier.PACKAGE); - - } else if (GeneratorUtil.isNMultiplicity(attr)) { - String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); - - // addXXX - - op = addOperation(output, - "add" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PACKAGE); - attr2 = addParameter(op, assocClassFQN, GeneratorUtil.toLowerCaseFirstLetter(assocClassName)); - setDocumentation(attr2, "L'instance de " + assocClassName + " à ajouter"); - - // addAllXXX - - op = addOperation(output, - "addAll" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PACKAGE); - attr2 = addParameter(op, collectionInterface + "<" + assocClassFQN + ">", GeneratorUtil.toLowerCaseFirstLetter(assocClassName)); - setDocumentation(attr2, "Les instances de " + assocClassName + " à ajouter"); - - // setXXX - - op = addOperation(output, - "set" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PACKAGE); - attr2 = addParameter(op, collectionInterface + "<" + assocClassFQN + ">", GeneratorUtil.toLowerCaseFirstLetter(assocClassName)); - setDocumentation(attr2, "La Collection de " + assocClassName + " à ajouter"); - - // removeXXX - - op = addOperation(output, - "remove" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PACKAGE); - attr2 = addParameter(op, assocClassFQN, GeneratorUtil.toLowerCaseFirstLetter(assocClassName)); - setDocumentation(attr2, "L'instance de " + assocClassName + " à retirer"); - - // clearXXX - - op = addOperation(output, - "clear" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PACKAGE); - setDocumentation(op, "Vide la Collection de " + assocClassName + " ."); - - // getXXX - - addOperation(output, - "get" + StringUtils.capitalize(assocAttrName), - collectionInterface + "<" + assocClassFQN + ">", - ObjectModelModifier.PACKAGE); - - if (!TopiaGeneratorUtil.isPrimitiveType(attr) && !TopiaGeneratorUtil.isDateType(attr)) { - - // getXXXByTopiaId - - op = addOperation(output, - "get" + StringUtils.capitalize(assocAttrName) + "ByTopiaId", - assocClassFQN, - 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"); - } - - // getXXX - - op = addOperation(output, - "get" + StringUtils.capitalize(assocAttrName), - assocClassFQN); - addParameter(op, attrType, "value"); - - - // sizeXXX - - addOperation(output, - "size" + StringUtils.capitalize(assocAttrName), - int.class, - ObjectModelModifier.PACKAGE); - - - // isXXXEmpty - - addOperation(output, - "is" + StringUtils.capitalize(assocAttrName) + "Empty", - boolean.class, - ObjectModelModifier.PACKAGE); - } - } - private void generateInterfaceOperation(ObjectModelOperation op, ObjectModelInterface output) { @@ -1365,566 +1200,6 @@ // Abstract generation // ------------------------------------------------------------------------- - @Deprecated - protected void transformAttribute(ObjectModelClass result, - ObjectModelAttribute attr, - ObjectModelAttribute reverse) { - - String attrName = attr.getName(); - String attrType = TopiaGeneratorUtil.getSimpleName(attr.getType()); - addImport(result, attrType); - - attrType = TopiaGeneratorUtil.getSimpleName(attrType); - - ObjectModelOperation op; - - // Ok for both - if (!GeneratorUtil.isNMultiplicity(attr) && false) { - - if (attr.hasAssociationClass()) { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); - addImport(result, assocClassFQN); - assocClassFQN = TopiaGeneratorUtil.getSimpleName(assocClassFQN); - String name = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName); - - // setXXX : DONE - - op = addOperation(result, - "set" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, assocClassFQN, "association"); - setOperationBody(op, "" -/*{ - <%=assocClassFQN%> _oldValue = this.<%=name%>; - fireOnPreWrite(<%=getConstantName(name)%>, _oldValue, association); - this.<%=name%> = association; - fireOnPostWrite(<%=getConstantName(name)%>, _oldValue, association); -}*/ - ); - - // getXXX : getter for association attribute with unique multiplicity : DONE - - op = addOperation(result, - "get" + StringUtils.capitalize(assocAttrName), - assocClassFQN, ObjectModelModifier.PUBLIC); - setOperationBody(op, "" -/*{ - return <%=name%>; -}*/ - ); - // Code is refactored and generated with correct methods, see generateOperationsFromAttributes - } else if (false) { - // DONE - - // setXXX - - op = addOperation(result, - "set" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, attrType, "value"); - setOperationBody(op, "" -/*{ - <%=attrType%> _oldValue = this.<%=attrName%>; - fireOnPreWrite(<%=getConstantName(attrName)%>, _oldValue, value); - this.<%=attrName%> = value; - fireOnPostWrite(<%=getConstantName(attrName)%>, _oldValue, value); -}*/ - - ); - - // getXXX : getter for simple attribute (as bean convention) - - // Getter is already set in abstract when added in interface - // see {@link #addSimpleGetterOperation(ObjectModelAttribute, String)} - - } - } else if (GeneratorUtil.isNMultiplicity(attr)) { //NMultiplicity - String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); - String collectionObject = TopiaGeneratorUtil.getNMultiplicityObjectType(attr); - addImport(result, collectionInterface); - addImport(result, collectionObject); - collectionInterface = TopiaGeneratorUtil.getSimpleName(collectionInterface); - collectionObject = TopiaGeneratorUtil.getSimpleName(collectionObject); - - // Code is refactored and generated with correct methods, see generateOperationsFromAttributes - if (!attr.hasAssociationClass() && false) { - //Méthodes remplacées par des accesseurs sur les classes d'assoc - - // addXXX : DONE - - op = addOperation(result, - "add" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, attrType, attrName); - StringBuilder body = new StringBuilder(); - - body.append("" -/*{ - fireOnPreWrite(<%=getConstantName(attrName)%>, null, <%=attrName%>); - if (this.<%=attrName%> == null) { - this.<%=attrName%> = new <%=collectionObject%><<%=attrType%>>(); - } -}*/ - ); - - if (reverse != null && (reverse.isNavigable() || - hasUnidirectionalRelationOnAbstractType(attr, model))) { - String reverseAttrName = reverse.getName(); - String reverseAttrType = TopiaGeneratorUtil.getSimpleName(reverse.getType()); - if (!GeneratorUtil.isNMultiplicity(reverse)) { - body.append("" -/*{ <%=attrName%>.set<%=StringUtils.capitalize(reverseAttrName)%>(this); -}*/ - ); - } else { - body.append("" -/*{ if (<%=attrName%>.get<%=StringUtils.capitalize(reverseAttrName)%>() == null) { - <%=attrName%>.set<%=StringUtils.capitalize(reverseAttrName)%>(new <%=collectionObject%><<%=reverseAttrType%>>()); - } - <%=attrName%>.get<%=StringUtils.capitalize(reverseAttrName)%>().add(this); -}*/ - ); - } - } - body.append("" -/*{ this.<%=attrName%>.add(<%=attrName%>); - fireOnPostWrite(<%=getConstantName(attrName)%>, this.<%=attrName%>.size(), null, <%=attrName%>); -}*/ - ); - setOperationBody(op, body.toString()); - - // addAllXXX : DONE - - op = addOperation(result, - "addAll" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, collectionInterface + '<' + attrType + '>', "values"); - - setOperationBody(op, "" -/*{ - if (values == null) { - return; - } - for (<%=attrType%> item : values) { - add<%=StringUtils.capitalize(attrName)%>(item); - } -}*/ - ); - - // getXXXByTopiaId : DONE - - if (!TopiaGeneratorUtil.isPrimitiveType(attr) && - !TopiaGeneratorUtil.isDateType(attr)) { - - op = addOperation(result, - "get" + StringUtils.capitalize(attrName) + "ByTopiaId", - attrType, - ObjectModelModifier.PUBLIC); - addParameter(op, String.class, "topiaId"); - setOperationBody(op, "" -/*{ - return org.nuiton.topia.persistence.util.TopiaEntityHelper.getEntityByTopiaId(<%=attrName%>, topiaId); - }*/ - ); - - } - - // setXXX : DONE - - op = addOperation(result, - "set" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, collectionInterface + '<' + attrType + '>', "values"); - - setOperationBody(op, "" -/*{ - <%=collectionInterface%><<%=attrType%>> _oldValue = <%=attrName%>; - fireOnPreWrite(<%=getConstantName(attrName)%>, _oldValue, values); - <%=attrName%> = values; - fireOnPostWrite(<%=getConstantName(attrName)%>, _oldValue, values); -}*/ - ); - - - // removeXXX : DONE - - - op = addOperation(result, - "remove" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, attrType, "value"); - body = new StringBuilder(); - - body.append("" -/*{ - fireOnPreWrite(<%=getConstantName(attrName)%>, value, null); - if ((this.<%=attrName%> == null) || (!this.<%=attrName%>.remove(value))) { - throw new IllegalArgumentException("List does not contain given element"); - } -}*/ - ); - - if (reverse != null && (reverse.isNavigable() || - hasUnidirectionalRelationOnAbstractType(attr, model))) { - String reverseAttrName = reverse.getName(); - if (!GeneratorUtil.isNMultiplicity(reverse)) { - body.append("" -/*{ value.set<%=StringUtils.capitalize(reverseAttrName)%>(null); -}*/ - ); - } else { - body.append("" -/*{ value.get<%=StringUtils.capitalize(reverseAttrName)%>().remove(this); -}*/ - ); - } - } - body.append("" -/*{ fireOnPostWrite(<%=getConstantName(attrName)%>, this.<%=attrName%>.size()+1, value, null); -}*/ - ); - setOperationBody(op, body.toString()); - - - // clearXXX : DONE - - op = addOperation(result, - "clear" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PUBLIC); - - body = new StringBuilder(); - - body.append("" -/*{ - if (this.<%=attrName%> == null) { - return; - } -}*/ - ); - - if (reverse != null && (reverse.isNavigable() || - hasUnidirectionalRelationOnAbstractType(attr, model))) { - String reverseAttrName = reverse.getName(); - body.append("" -/*{ for (<%=attrType%> item : this.<%=attrName%>) { -}*/ - ); - if (!GeneratorUtil.isNMultiplicity(reverse)) { - body.append("" -/*{ item.set<%=StringUtils.capitalize(reverseAttrName)%>(null); -}*/ - ); - } else { - body.append("" -/*{ item.get<%=StringUtils.capitalize(reverseAttrName)%>().remove(this); -}*/ - ); - } - body.append("" -/*{ } -}*/ - ); - } - body.append("" -/*{ <%=collectionInterface%><<%=attrType%>> _oldValue = new <%=collectionObject%><<%=attrType%>>(this.<%=attrName%>); - fireOnPreWrite(<%=getConstantName(attrName)%>, _oldValue, this.<%=attrName%>); - this.<%=attrName%>.clear(); - fireOnPostWrite(<%=getConstantName(attrName)%>, _oldValue, this.<%=attrName%>); -}*/ - ); - - setOperationBody(op, body.toString()); - - } else if (attr.hasAssociationClass()) { - - - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); -// String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); - - // addXXX - - op = addOperation(result, - "add" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, assocClassFQN, "value"); - - StringBuilder body = new StringBuilder(); - - body.append("" -/*{ - fireOnPreWrite(<%=getConstantName(GeneratorUtil.toLowerCaseFirstLetter(assocAttrName))%>, null, value); - if (this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) { - this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = new <%=collectionObject%><<%=assocClassFQN%>>(); - } -}*/ - ); - if (reverse != null && (reverse.isNavigable() || - hasUnidirectionalRelationOnAbstractType(attr, model))) { - String reverseAttrName = reverse.getName(); - body.append("" -/*{ value.set<%=StringUtils.capitalize(reverseAttrName)%>(this); -}*/ - ); - } - body.append("" -/*{ this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.add(value); - fireOnPostWrite(<%=getConstantName(GeneratorUtil.toLowerCaseFirstLetter(assocAttrName))%>, this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.size(), null, value); -}*/ - ); - setOperationBody(op, body.toString()); - - - if (!TopiaGeneratorUtil.isPrimitiveType(attr) && - !TopiaGeneratorUtil.isDateType(attr)) { - - // getXXXByTopiaId - - op = addOperation(result, - "get" + StringUtils.capitalize(assocAttrName) + "ByTopiaId", - assocClassFQN, - ObjectModelModifier.PUBLIC); - addParameter(op, String.class, "topiaId"); - setOperationBody(op, "" -/*{ - return org.nuiton.topia.persistence.util.TopiaEntityHelper.getEntityByTopiaId(<%=assocAttrName%>, topiaId); -}*/ - ); - - } - - // addAllXXX - - op = addOperation(result, - "addAll" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, collectionInterface + '<' + assocClassFQN + '>', "values"); - setOperationBody(op, "" -/*{ - if (values == null) { - return; - } - for (<%=assocClassFQN%> item : values) { - add<%=StringUtils.capitalize(assocAttrName)%>(item); - } -}*/ - ); - - // setXXX - - op = addOperation(result, - "set" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, collectionInterface + '<' + assocClassFQN + '>', "values"); - setOperationBody(op, "" -/*{ -// clear<%=StringUtils.capitalize(assocAttrName)%>(); -// addAll<%=StringUtils.capitalize(assocAttrName)%>(values); -// FIXME - <%=collectionInterface%><<%=assocClassFQN%>> _oldValue = <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; - fireOnPreWrite(<%=getConstantName(GeneratorUtil.toLowerCaseFirstLetter(assocAttrName))%>, _oldValue, values); - <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = values; - fireOnPostWrite(<%=getConstantName(GeneratorUtil.toLowerCaseFirstLetter(assocAttrName))%>, _oldValue, values); -}*/ - ); - - // removeXXX - - op = addOperation(result, - "remove" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PUBLIC); - addParameter(op, assocClassFQN, "value"); - - body = new StringBuilder(); - - body.append("" -/*{ - fireOnPreWrite(<%=getConstantName(GeneratorUtil.toLowerCaseFirstLetter(assocAttrName))%>, value, null); - if ((this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) || (!this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.remove(value))) { - throw new IllegalArgumentException("List does not contain given element"); - } -}*/ - ); - if (reverse != null && (reverse.isNavigable() || - hasUnidirectionalRelationOnAbstractType(attr, model))) { - String reverseAttrName = reverse.getName(); - body.append("" -/*{ value.set<%=StringUtils.capitalize(reverseAttrName)%>(null); -}*/ - ); - } - body.append("" -/*{ fireOnPostWrite(<%=getConstantName(GeneratorUtil.toLowerCaseFirstLetter(assocAttrName))%>, this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.size()+1, value, null); -}*/ - ); - - setOperationBody(op, body.toString()); - - // clearXXX - - op = addOperation(result, - "clear" + StringUtils.capitalize(assocAttrName), - "void", - ObjectModelModifier.PUBLIC); - - body = new StringBuilder(); - - body.append("" -/*{ - if (this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) { - return; - } -}*/ - ); - - if (reverse != null && (reverse.isNavigable() || - hasUnidirectionalRelationOnAbstractType(attr, model))) { - String reverseAttrName = reverse.getName(); - body.append("" -/*{ for (<%=assocClassFQN%> item : this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>) { - item.set<%=StringUtils.capitalize(reverseAttrName)%>(null); - } -}*/ - ); - } - body.append("" -/*{ <%=collectionInterface%><<%=assocClassFQN%>> _oldValue = new <%=collectionObject%><<%=assocClassFQN%>>(this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>); - fireOnPreWrite(<%=getConstantName(GeneratorUtil.toLowerCaseFirstLetter(assocAttrName))%>, _oldValue, null); - this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.clear(); - fireOnPostWrite(<%=getConstantName(GeneratorUtil.toLowerCaseFirstLetter(assocAttrName))%>, _oldValue, null); -}*/ - ); - setOperationBody(op, body.toString()); - } - - // Code is refactored and generated with correct methods, see generateOperationsFromAttributes - if (!attr.hasAssociationClass() && false) { - - // getXXX : getter for collection entity attribute (N multiplicity) : DONE - - op = addOperation(result, - "get" + StringUtils.capitalize(attrName), - collectionInterface + '<' + attrType + '>', - ObjectModelModifier.PUBLIC); - setOperationBody(op, "" -/*{ - return <%=attrName%>; -}*/ - ); - - // sizeXXX : DONE - - op = addOperation(result, - "size" + StringUtils.capitalize(attrName), - int.class, - ObjectModelModifier.PUBLIC); - setOperationBody(op, "" -/*{ - if (<%=attrName%> == null) { - return 0; - } - return <%=attrName%>.size(); -}*/ - ); - - // isXXXEmpty : DONE - - op = addOperation(result, - "is" + StringUtils.capitalize(attrName) + "Empty", - boolean.class, - ObjectModelModifier.PUBLIC); - setOperationBody(op, "" -/*{ - int size = size<%=StringUtils.capitalize(attrName)%>(); - return size == 0; -}*/ - ); - - } else if (attr.hasAssociationClass()) { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); -// String assocClassFQN = TopiaGeneratorUtil.getSimpleName(attr.getAssociationClass().getQualifiedName()); - - // getXXX : getter for association attribute with no parameter - // (return a collection) - - op = addOperation(result, - "get" + StringUtils.capitalize(assocAttrName), - collectionInterface + '<' + assocClassFQN + '>', - ObjectModelModifier.PUBLIC); - setOperationBody(op, "" -/*{ - return <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; -}*/ - ); - - // getXXX : getter for association attribute with one parameter - // (return a single element) - - op = addOperation(result, - "get" + StringUtils.capitalize(assocAttrName), - assocClassFQN, - ObjectModelModifier.PUBLIC); - addParameter(op, attrType, "value"); - setOperationBody(op, "" -/*{ - if (value == null || <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) { - return null; - } - for (<%=assocClassFQN%> item : <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>) { - if (value.equals(item.get<%=StringUtils.capitalize(attrName)%>())) { - return item; - } - } - return null; -}*/ - ); - - - // sizeXXX - - op = addOperation(result, - "size" + StringUtils.capitalize(assocAttrName), - int.class, - ObjectModelModifier.PUBLIC); - setOperationBody(op, "" -/*{ - if (<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) { - return 0; - } - return <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.size(); -}*/ - ); - - //isXXXEmpty - - op = addOperation(result, - "is" + StringUtils.capitalize(assocAttrName) + "Empty", - boolean.class, - ObjectModelModifier.PUBLIC); - setOperationBody(op, "" -/*{ - int size = size<%=StringUtils.capitalize(assocAttrName)%>(); - return size == 0; -}*/ - ); - - } - } - } - protected void generateToStringMethod(ObjectModelClass output, ObjectModelClass clazz) { if (log.isDebugEnabled()) { @@ -2349,4 +1624,61 @@ return false; } + + // ------------------------------------------------------------------------- + // Helpers + // ------------------------------------------------------------------------- + + 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 + * + * This method create an set operation in {@code classifier} with + * {@code propertyType} as return type and {@code propertyName} used for + * operation name ('set[propertyName]'). {@code operationDocument} can + * also be added to the operation created. Only signature with default + * visibility will be added. + * + * @param classifier Classifier where the operation will be added + * @param propertyType Type of the property (better if qualified name) + * @param propertyName Name of the property to set + * @param operationDocumentation Documentation for the operation + * @return the created operation + */ + protected ObjectModelOperation createPropertySetterSignature(ObjectModelClassifier classifier, + String propertyType, + String propertyName, + String operationDocumentation) { + // Operation + ObjectModelOperation operation = + addOperation(classifier, + "set" + StringUtils.capitalize(propertyName), void.class); + + ObjectModelParameter param = + addParameter(operation, propertyType, propertyName); + + // Documentation + if (StringUtils.isNotEmpty(operationDocumentation)) { + setDocumentation(operation, operationDocumentation); + } + setDocumentation(param, "La valeur de l'attribut à positionner."); + + return operation; + } + }