Author: tchemit Date: 2014-01-22 14:59:19 +0100 (Wed, 22 Jan 2014) New Revision: 2986 Url: http://nuiton.org/projects/topia/repository/revisions/2986 Log: fixes #3009: clean TopiaEntity contract Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntity.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntityContextable.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/internal/AbstractTopiaEntity.java trunk/topia-templates/src/main/java/org/nuiton/topia/templates/EntityTransformer.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntity.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntity.java 2014-01-20 08:36:03 UTC (rev 2985) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntity.java 2014-01-22 13:59:19 UTC (rev 2986) @@ -28,7 +28,6 @@ import java.beans.VetoableChangeListener; import java.io.Serializable; import java.util.Date; -import java.util.List; /** * The TopiaEntity is the main interface for each generated entity. @@ -144,20 +143,6 @@ void notifyDeleted(); /** - * @return all objects that must be deleted if this object is deleted - * @deprecated from 3.0, method will be moved to entity's generated Dao(cf http://nuiton.org/issues/2776) - */ - @Deprecated - List<TopiaEntity> getComposite(); - - /** - * @return all objects that are aggregate with this instance, aggregate object are not removed automatically - * @deprecated from 3.0, method will be moved to entity's generated Dao (cf http://nuiton.org/issues/2776) - */ - @Deprecated - List<TopiaEntity> getAggregate(); - - /** * Route the entity using a {@code visitor}. * * @param visitor to used Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntityContextable.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntityContextable.java 2014-01-20 08:36:03 UTC (rev 2985) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntityContextable.java 2014-01-22 13:59:19 UTC (rev 2986) @@ -24,8 +24,10 @@ * #L% */ +import java.util.List; + /** - * {@link TopiaEntity} with {@link org.nuiton.topia.persistence.TopiaDaoSupplier} support (injected by + * {@link TopiaEntity} with {@link TopiaDaoSupplier} support (injected by * {@link org.nuiton.topia.persistence.internal.AbstractTopiaDao} into entities). * <p/> * WARNING, it is not recommended to use this mechanism as it breaks the POJO's Hibernate approach. @@ -35,6 +37,16 @@ public interface TopiaEntityContextable extends TopiaEntity { /** + * @since 3.0 + */ + String PROPERTY_COMPOSITE = "composite"; + + /** + * @since 3.0 + */ + String PROPERTY_AGGREGATE = "aggregate"; + + /** * Update entity in persistence context. */ void update(); @@ -45,16 +57,30 @@ void delete(); /** - * Injects into this entity the given {@link org.nuiton.topia.persistence.TopiaDaoSupplier}, which can be used to + * Injects into this entity the given {@link TopiaDaoSupplier}, which can be used to * get any Dao instance. This is used to make {@link #update()} and {@link #delete()} implementation possible. * - * @param topiaDaoSupplier an initialized {@link org.nuiton.topia.persistence.TopiaDaoSupplier} instance + * @param topiaDaoSupplier an initialized {@link TopiaDaoSupplier} instance */ void setTopiaDaoSupplier(TopiaDaoSupplier topiaDaoSupplier); /** - * @return the currently injected {@link org.nuiton.topia.persistence.TopiaDaoSupplier} + * @return the currently injected {@link TopiaDaoSupplier} */ TopiaDaoSupplier getTopiaDaoSupplier(); + /** + * @return all objects that must be deleted if this object is deleted + * @deprecated from 3.0, method will be moved to entity's generated Dao(cf http://nuiton.org/issues/2776) + */ + @Deprecated + List<TopiaEntity> getComposite(); + + /** + * @return all objects that are aggregate with this instance, aggregate object are not removed automatically + * @deprecated from 3.0, method will be moved to entity's generated Dao (cf http://nuiton.org/issues/2776) + */ + @Deprecated + List<TopiaEntity> getAggregate(); + } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/internal/AbstractTopiaEntity.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/internal/AbstractTopiaEntity.java 2014-01-20 08:36:03 UTC (rev 2985) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/internal/AbstractTopiaEntity.java 2014-01-22 13:59:19 UTC (rev 2986) @@ -158,16 +158,16 @@ deleted = true; } - @Override - public List<TopiaEntity> getComposite() throws TopiaException { - throw new UnsupportedOperationException(); - } +// @Override +// public List<TopiaEntity> getComposite() throws TopiaException { +// throw new UnsupportedOperationException(); +// } +// +// @Override +// public List<TopiaEntity> getAggregate() throws TopiaException { +// throw new UnsupportedOperationException(); +// } - @Override - public List<TopiaEntity> getAggregate() throws TopiaException { - throw new UnsupportedOperationException(); - } - /** * On utilise la date de creation comme hash code, cette date ne varie pas * au cours du temps 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-01-20 08:36:03 UTC (rev 2985) +++ trunk/topia-templates/src/main/java/org/nuiton/topia/templates/EntityTransformer.java 2014-01-22 13:59:19 UTC (rev 2986) @@ -39,6 +39,7 @@ import org.nuiton.eugene.models.object.ObjectModelModifier; import org.nuiton.eugene.models.object.ObjectModelOperation; import org.nuiton.eugene.models.object.ObjectModelParameter; +import org.nuiton.topia.persistence.TopiaDao; import org.nuiton.topia.persistence.TopiaEntityVisitor; import org.nuiton.topia.persistence.internal.AbstractTopiaEntity; import org.nuiton.topia.persistence.TopiaDaoSupplier; @@ -182,10 +183,6 @@ // Add extra operations (defined on the entity) generateExtraOperations(input); - // Implement aggregate and composite operations - generateAggregateOperation(input); - generateCompositeOperation(input); - // Implement toString operation if (TopiaGeneratorUtil.generateToString(input, model)) { generateToStringOperation(input); @@ -200,8 +197,6 @@ generateSerialVersionUID(input, outputImpl); } - - // Clean data output after transformation clean(); } @@ -327,7 +322,9 @@ setSuperClass(outputAbstract, superClassName); } - addContextableMethods(input, outputAbstract); + if (TopiaGeneratorUtil.isContextable(input)) { + addContextableMethods(input, outputAbstract); + } } /** @@ -339,70 +336,100 @@ protected void addContextableMethods(ObjectModelClass input, ObjectModelClass outputAbstract) { - if (TopiaGeneratorUtil.isContextable(input)) { + addImport(outputAbstract, TopiaDaoSupplier.class); - addImport(outputAbstract, TopiaDaoSupplier.class); + // topiaContext attribute + ObjectModelAttribute topiaContextAttribute = addAttribute( + outputAbstract, "topiaDaoSupplier", TopiaDaoSupplier.class, null, + ObjectModelJavaModifier.PROTECTED, ObjectModelJavaModifier.TRANSIENT); + setDocumentation(topiaContextAttribute, + "TopiaDAO instance associated with the current \n" + + "instance. For internal usage only"); - // topiaContext attribute - ObjectModelAttribute topiaContextAttribute = addAttribute( - outputAbstract, "topiaDaoSupplier", TopiaDaoSupplier.class, null, - ObjectModelJavaModifier.PROTECTED, ObjectModelJavaModifier.TRANSIENT); - setDocumentation(topiaContextAttribute, - "TopiaDAO instance associated with the current \n" + - "instance. For internal usage only"); + ObjectModelOperation op; - ObjectModelOperation op = addOperation(outputAbstract, - "getTopiaDaoSupplier", TopiaDaoSupplier.class, - ObjectModelJavaModifier.PUBLIC); - setDocumentation(op,"@since 3.0"); - addAnnotation(outputAbstract, op, Override.class); - setOperationBody(op, "" + op = addOperation(outputAbstract, + "getTopiaDaoSupplier", TopiaDaoSupplier.class, + ObjectModelJavaModifier.PUBLIC); + setDocumentation(op,"@since 3.0"); + addAnnotation(outputAbstract, op, Override.class); + setOperationBody(op, "" /*{ return topiaDaoSupplier; }*/ - ); + ); - op = addOperation(outputAbstract, - "setTopiaDaoSupplier", "void", - ObjectModelJavaModifier.PUBLIC); - addException(op, TopiaException.class); - addParameter(op, TopiaDaoSupplier.class, "topiaDaoSupplier"); - setDocumentation(op, -// "@param context The context to set.\n" + -// "@throws TopiaException if trying to replace a context\n" + - "@since 3.0"); - addAnnotation(outputAbstract, op, Override.class); - setOperationBody(op, "" + op = addOperation(outputAbstract, + "setTopiaDaoSupplier", "void", + ObjectModelJavaModifier.PUBLIC); + addException(op, TopiaException.class); + addParameter(op, TopiaDaoSupplier.class, "topiaDaoSupplier"); + setDocumentation(op, + "@since 3.0"); + addAnnotation(outputAbstract, op, Override.class); + setOperationBody(op, "" /*{ if (this.topiaDaoSupplier == null) { this.topiaDaoSupplier = topiaDaoSupplier; -// } else { -// throw new TopiaException("TopiaContext replacement is forbidden"); } }*/ - ); + ); - op = addOperation(outputAbstract, "update", "void", - ObjectModelJavaModifier.PUBLIC); - addException(op, TopiaException.class); -// setDocumentation(op,"@since 2.5.3"); - addAnnotation(outputAbstract, op, Override.class); - setOperationBody(op, "" + String daoClassName = TopiaGeneratorUtil.getConcreteDaoName(input); + op = addOperation(outputAbstract, + "getInternalDao", daoClassName, + ObjectModelJavaModifier.PROTECTED); + setDocumentation(op,"@since 3.0"); + addAnnotation(outputAbstract, op, Override.class); + setOperationBody(op, "" /*{ - getTopiaDaoSupplier().getDao(<%=input.getName()%>.class).update(this); + <%=daoClassName%> result = getTopiaDaoSupplier().getDao(<%=input.getName()%>.class, <%=daoClassName%>); + return result; }*/ - ); + ); - op = addOperation(outputAbstract, "delete", "void", ObjectModelJavaModifier.PUBLIC); - addException(op, TopiaException.class); -// setDocumentation(op,"@since 2.5.3"); - addAnnotation(outputAbstract, op, Override.class); - setOperationBody(op, "" + op = addOperation(outputAbstract, "update", "void", + ObjectModelJavaModifier.PUBLIC); + addException(op, TopiaException.class); + addAnnotation(outputAbstract, op, Override.class); + setOperationBody(op, "" /*{ - getTopiaDaoSupplier().getDao(<%=input.getName()%>.class).delete(this); + getInternalDao().update(this); }*/ - ); - } + ); + + op = addOperation(outputAbstract, "delete", "void", ObjectModelJavaModifier.PUBLIC); + addException(op, TopiaException.class); + addAnnotation(outputAbstract, op, Override.class); + setOperationBody(op, "" +/*{ + getInternalDao().delete(this); + }*/ + ); + + op = addOperation(outputAbstract, "getComposite", + List.class.getName() + '<' + TopiaEntity.class.getName() + '>', + ObjectModelJavaModifier.PUBLIC); + addException(op, TopiaException.class); + addAnnotation(outputAbstract, op, Override.class); + setOperationBody(op, "" +/*{ + List<TopiaEntity> result = getInternalDao().getComposite(this); + return result; + }*/ + ); + + op = addOperation(outputAbstract, "getAggregate", + List.class.getName() + '<' + TopiaEntity.class.getName() + '>', + ObjectModelJavaModifier.PUBLIC); + addException(op, TopiaException.class); + addAnnotation(outputAbstract, op, Override.class); + setOperationBody(op, "" +/*{ + List<TopiaEntity> result = getInternalDao().getAggregate(this); + return result; + }*/ + ); } protected boolean isGenerateInterface(ObjectModelClass input) { @@ -1149,6 +1176,7 @@ addOperation(outputInterface, getJavaBeanMethodName("get", attrName) + "TopiaIds", collectionInterface + "<String>", ObjectModelJavaModifier.PACKAGE); + addImport(outputAbstract, TopiaEntity.class); // Implementation ObjectModelOperation implOperation = createImplOperation(interfaceOperation); @@ -1386,184 +1414,6 @@ } - protected void generateCompositeOperation(ObjectModelClass input) { - - ObjectModelOperation operation = - addOperation(outputAbstract, "getComposite", - List.class.getName() + '<' + TopiaEntity.class.getName() + '>'); - - addException(operation, TopiaException.class); - addAnnotation(outputAbstract, operation, Override.class); - - StringBuilder body; - if (!TopiaGeneratorUtil.isContextable(input)) { - body = new StringBuilder("" -/*{ - throw new UnsupportedOperationException("Since ToPIA 3.0, 'getComposite' is only available for contextable entities"); - }*/ - ); - } else { - addImport(outputAbstract, ArrayList.class); - addImport(outputAbstract, List.class); - - body = new StringBuilder("" -/*{ - List<TopiaEntity> tmp = new ArrayList<TopiaEntity>(); - - // pour tous les attributs rechecher les composites et les class d'asso - // on les ajoute dans tmp -}*/ - ); - for (ObjectModelAttribute attr : input.getAttributes()) { - - if (attr.referenceClassifier() && - TopiaGeneratorUtil.isEntity(attr.getClassifier())) { - - if (attr.isComposite()) { - String attrName = attr.getName(); - String getterName = getJavaBeanMethodName("get", attrName); - if (TopiaGeneratorUtil.isNMultiplicity(attr)) { - body.append("" -/*{ if (<%=getterName%>() != null) { - tmp.addAll(<%=getterName%>()); - } -}*/ - ); - } else { - body.append("" -/*{ tmp.add(<%=getterName%>()); -}*/ - ); - } - } else if (attr.hasAssociationClass()) { - String assocAttrName = TopiaGeneratorUtil.getAssocAttrName( - attr); - String assocClassFQN = TopiaGeneratorUtil.getSimpleName( - attr.getAssociationClass().getQualifiedName()); - String ref = "this." + TopiaGeneratorUtil.toLowerCaseFirstLetter( - assocAttrName); - if (!TopiaGeneratorUtil.isNMultiplicity(attr)) { - body.append("" -/*{ - if (<%=ref%> != null) { - tmp.add(<%=ref%>); - } -}*/ - ); - } else { - ObjectModelAttribute reverse = attr.getReverseAttribute(); - String reverseAttrName = reverse.getName(); - // On utilise pas l'attribut car il est potentiellement - // pas a jour, car pour les asso avec cardinalité - // personne ne fait de add. Ce qui est normal, mais - // pour pouvoir faire tout de meme des delete en cascade - // sur les asso, le champs est dans le mapping - // hibernate et donc il le faut aussi dans la classe - // sinon hibernate rale lorsqu'il charge l'objet - // if (<%=ref%> != null) { - // tmp.addAll(<%=ref%>); - // } - - body.append("" -/*{ - { - org.nuiton.topia.persistence.TopiaDAO<<%=assocClassFQN%>> dao = getTopiaContext().getDao(<%=assocClassFQN%>.class); - List<<%=assocClassFQN%>> findAllByProperties = dao.findAllByProperties("<%=reverseAttrName%>", this); - if (findAllByProperties != null) { - tmp.addAll(findAllByProperties); - } - } -}*/ - ); - } - } - } - } - body.append("" -/*{ - // on refait un tour sur chaque entity de tmp pour recuperer leur - // composite - List<TopiaEntity> result = new ArrayList<TopiaEntity>(); - for (TopiaEntity entity : tmp) { - if (entity != null) { - result.add(entity); - result.addAll(entity.getComposite()); - } - } - return result; - }*/ - ); - } - - setOperationBody(operation, body.length() == 0 ? " " : body.toString()); - } - - protected void generateAggregateOperation(ObjectModelClass input) { - - ObjectModelOperation operation = - addOperation(outputAbstract, "getAggregate", - List.class.getName() + '<' + TopiaEntity.class.getName() + '>'); - - addException(operation, TopiaException.class); - addAnnotation(outputAbstract, operation, Override.class); - - - StringBuilder body = new StringBuilder(); - if (!TopiaGeneratorUtil.isContextable(input)) { - body = new StringBuilder("" -/*{ - throw new UnsupportedOperationException("Since ToPIA 3.0, 'getComposite' is only available for contextable entities"); - }*/ - ); - } else { - - addImport(outputAbstract, ArrayList.class); - addImport(outputAbstract, List.class); - body .append("" -/*{ - List<TopiaEntity> tmp = new ArrayList<TopiaEntity>(); - - // pour tous les attributs rechecher les composites et les class d'asso - // on les ajoute dans tmp -}*/ - ); - for (ObjectModelAttribute attr : input.getAttributes()) { - - if (attr.referenceClassifier() && - TopiaGeneratorUtil.isEntity(attr.getClassifier()) && - attr.isAggregate()) { - - String attrName = attr.getName(); - String getterName = getJavaBeanMethodName("get", attrName); - if (TopiaGeneratorUtil.isNMultiplicity(attr)) { - body.append("" -/*{ tmp.addAll(<%=getterName%>()); -}*/ - ); - } else { - body.append("" -/*{ tmp.add(<%=getterName%>()); -}*/ - ); - } - } - } - body.append("" -/*{ - // on refait un tour sur chaque entity de tmp pour recuperer leur - // composite - List<TopiaEntity> result = new ArrayList<TopiaEntity>(); - for (TopiaEntity entity : tmp) { - result.add(entity); - result.addAll(entity.getAggregate()); - } - return result; - }*/ - ); - } - setOperationBody(operation, body.length() == 0 ? " " : body.toString()); - } - // ------------------------------------------------------------------------- // Helpers // -------------------------------------------------------------------------