r3091 - trunk/topia-templates/src/main/java/org/nuiton/topia/templates
Author: tchemit Date: 2014-05-01 15:15:46 +0200 (Thu, 01 May 2014) New Revision: 3091 Url: http://forge.nuiton.org/projects/topia/repository/revisions/3091 Log: fixes #3186 generates more methods on ordered (but not unique) entity attributes Modified: trunk/topia-templates/src/main/java/org/nuiton/topia/templates/EntityTransformer.java Modified: trunk/topia-templates/src/main/java/org/nuiton/topia/templates/EntityTransformer.java =================================================================== --- trunk/topia-templates/src/main/java/org/nuiton/topia/templates/EntityTransformer.java 2014-05-01 12:21:24 UTC (rev 3090) +++ trunk/topia-templates/src/main/java/org/nuiton/topia/templates/EntityTransformer.java 2014-05-01 13:15:46 UTC (rev 3091) @@ -28,6 +28,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.nuiton.eugene.EugeneStereoTypes; import org.nuiton.eugene.EugeneTagValues; import org.nuiton.eugene.java.JavaGeneratorUtil; import org.nuiton.eugene.java.ObjectModelTransformerToJava; @@ -718,9 +719,19 @@ collectionImpl = TopiaGeneratorUtil.getSimpleName(collectionImpl); + boolean ordered = TopiaGeneratorUtil.isOrdered(attribute); + boolean unique = EugeneStereoTypes.hasUniqueStereotype(attribute); + boolean entity = TopiaGeneratorUtil.isEntity(attribute, model); + // addXXX addMultipleAddOperation(attribute, collectionImpl); + if (ordered && !unique) { + + // addXXX(index) + addMultipleAddAtIndexOperation(attribute, collectionImpl); + } + // addAllXXX addMultipleAddAllOperation(attribute, collectionInterface); @@ -730,20 +741,27 @@ // removeXXX addMultipleRemoveOperation(attribute); + if (ordered && !unique) { + + // removeXXX(index) + addMultipleRemoveAtIndexOperation(attribute); + } + // clearXXX addMultipleClearOperation(attribute, collectionInterface, collectionImpl); // getXXX addMultipleGetOperation(attribute, collectionInterface); - if (TopiaGeneratorUtil.isOrdered(attribute)) { + if (ordered) { // getXXX(index) addMultipleGetByIndexOperation(attribute); } - if (TopiaGeneratorUtil.isEntity(attribute, model)) { + if (entity) { + // getXXXByTopiaId addMultipleGetByTopiaIdOperation(attribute); @@ -909,6 +927,79 @@ setOperationBody(implOperation, body.toString()); } + protected void addMultipleAddAtIndexOperation(ObjectModelAttribute attribute, + String collectionImpl) { + + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); + ObjectModelAttribute reverse = attribute.getReverseAttribute(); + + if (log.isDebugEnabled()) { + log.debug("Generate multiple 'add' operation for property : " + attrName + + " [" + attrType + "]"); + } + + String constantName = getConstantName(attrName); + + // Interface operation + ObjectModelOperation interfaceOperation = + addOperation(outputInterface, getJavaBeanMethodName("add", attrName), + void.class, ObjectModelJavaModifier.PACKAGE); + addParameter(interfaceOperation, int.class, "index"); + ObjectModelParameter param = + addParameter(interfaceOperation, attrType, attrName); + + // Implementation + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); + + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + + StringBuilder body = new StringBuilder(); + + body.append("" +/*{ + fireOnPreWrite(<%=constantName%>, null, <%=attrName%>); + if (this.<%=attrName%> == null) { + this.<%=attrName%> = new <%=collectionImpl%><<%=attrType%>>(); + } +}*/ + ); + + if (reverse != null && (reverse.isNavigable() || + hasUnidirectionalRelationOnAbstractType(attribute, model))) { + String getterName = getJavaBeanMethodName("get", reverse.getName()); + String setterName = getJavaBeanMethodName("set", reverse.getName()); + + // TODO brendan 15/04/14 remove FQN + String reverseAttrType = JavaGeneratorUtil.getAttributeImplementationType(reverse, true); + + if (!TopiaGeneratorUtil.isNMultiplicity(reverse)) { + body.append("" +/*{ + <%=attrName%>.<%=setterName%>(this); +}*/ + ); + // Don't manage reverse attribute add if attribute has associationClass + } else if (!attribute.hasAssociationClass()) { + body.append("" +/*{ + if (<%=attrName%>.<%=getterName%>() == null) { + <%=attrName%>.<%=setterName%>(new <%=reverseAttrType%>()); + } + <%=attrName%>.<%=getterName%>().add(index, this); +}*/ + ); + } + } + body.append("" +/*{ + this.<%=attrName%>.add(index, <%=attrName%>); + fireOnPostWrite(<%=constantName%>, index, null, <%=attrName%>); + }*/ + ); + setOperationBody(implOperation, body.toString()); + } + protected void addMultipleAddAllOperation(ObjectModelAttribute attribute, String collectionInterface) { @@ -1043,6 +1134,71 @@ setOperationBody(implOperation, body.toString()); } + protected void addMultipleRemoveAtIndexOperation(ObjectModelAttribute attribute) { + + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); + ObjectModelAttribute reverse = attribute.getReverseAttribute(); + String constantName = getConstantName(attrName); + + if (log.isDebugEnabled()) { + log.debug("Generate 'remove' operation for property : " + attrName + + " [" + attrType + "]"); + } + + // Interface operation + ObjectModelOperation interfaceOperation = + addOperation(outputInterface, getJavaBeanMethodName("remove", attrName), + void.class, ObjectModelJavaModifier.PACKAGE); + addParameter(interfaceOperation, int.class, "index"); + + // Implementation + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); + + attrType = TopiaGeneratorUtil.getSimpleName(attrType); + + StringBuilder body = new StringBuilder(); + + body.append("" +/*{ + fireOnPreWrite(<%=constantName%>, <%=attrName%>, null); + if (this.<%=attrName%> == null) { + throw new IllegalArgumentException("List does not contain given element"); + } + <%=attrType%> oldValue = this.<%=attrName%>.remove(index); + if (oldValue == null) { + throw new IllegalArgumentException("List does not contain given element"); + } +}*/ + ); + + if (reverse != null && (reverse.isNavigable() || + hasUnidirectionalRelationOnAbstractType(attribute, model))) { + String getterName = getJavaBeanMethodName("get", reverse.getName()); + String setterName = getJavaBeanMethodName("set", reverse.getName()); + if (!TopiaGeneratorUtil.isNMultiplicity(reverse)) { + body.append("" +/*{ + <%=attrName%>.<%=setterName%>(null); +}*/ + ); + // Don't manage reverse attribute remove if attribute has associationClass + } else if (!attribute.hasAssociationClass()) { + body.append("" +/*{ + <%=attrName%>.<%=getterName%>().remove(this); +}*/ + ); + } + } + body.append("" +/*{ + fireOnPostWrite(<%=constantName%>, index, oldValue, null); + }*/ + ); + setOperationBody(implOperation, body.toString()); + } + protected void addMultipleClearOperation(ObjectModelAttribute attribute, String collectionInterface, String collectionImpl) {
participants (1)
-
tchemit@users.nuiton.org