Author: tchemit Date: 2012-05-17 00:44:49 +0200 (Thu, 17 May 2012) New Revision: 2488 Url: http://nuiton.org/repositories/revision/topia/2488 Log: refs #2078: Remove the TopiaContext from TopiaEntityAbstract (deprecate getComposite and getAggregate from TopiaEntity and rewritte them in DAO) Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityDAOTransformer.java trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntity.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityDAOTransformer.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityDAOTransformer.java 2012-05-16 22:41:20 UTC (rev 2487) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityDAOTransformer.java 2012-05-16 22:44:49 UTC (rev 2488) @@ -27,6 +27,8 @@ /*{generator option: parentheses = false}*/ /*{generator option: writeString = +}*/ +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -46,8 +48,10 @@ import org.nuiton.topia.framework.TopiaContextImplementor; import org.nuiton.topia.persistence.TopiaDAO; import org.nuiton.topia.persistence.TopiaEntity; +import org.nuiton.topia.persistence.TopiaPersistenceUtil; import org.nuiton.util.StringUtil; +import java.io.IOException; import java.security.Permission; import java.util.ArrayList; import java.util.Collection; @@ -413,6 +417,10 @@ generateNotNull(daoAbstractClass, clazz); + generateCompositeOperation(daoAbstractClass, clazz); + + generateAggregateOperation(daoAbstractClass, clazz); + for (ObjectModelAttribute attr : clazz.getAttributes()) { if (!attr.isNavigable()) { continue; @@ -620,18 +628,18 @@ String setName = getJavaBeanMethodName("set", reverseAttrName); body.append("" - /*{ - { - List<<%=attrSimpleType%>> list = getContext() - .getDAO(<%=attrSimpleType%>.class) - .findAllByProperties(<%=attrSimpleType%>.<%=getConstantName(reverseAttrName)%>, entity); - for (<%=attrSimpleType%> item : list) { +/*{ + { + List<<%=attrSimpleType%>> list = getContext() + .getDAO(<%=attrSimpleType%>.class) + .findAllByProperties(<%=attrSimpleType%>.<%=getConstantName(reverseAttrName)%>, entity); + for (<%=attrSimpleType%> item : list) { - // sletellier : Set null only if target is concerned by deletion - if (entity.equals(item.<%=getName%>())) { - item.<%=setName%>(null); - } - }*/ + // sletellier : Set null only if target is concerned by deletion + if (entity.equals(item.<%=getName%>())) { + item.<%=setName%>(null); + } +}*/ ); if (attr.isAggregate()) { body.append("" @@ -1178,4 +1186,205 @@ ); } } + + protected void generateCompositeOperation(ObjectModelClass result, + ObjectModelClass input) { + + ObjectModelOperation operation = + addOperation(result, + "getComposite", + Multimap.class.getName() + "<Class<? extends TopiaEntity>, TopiaEntity>"); + + addParameter(operation, "E", "entity"); + addException(operation, TopiaException.class); + addAnnotation(result, operation, Override.class.getSimpleName()); + + addImport(result, ArrayList.class); + addImport(result, List.class); + addImport(result, Multimap.class); + addImport(result, ArrayListMultimap.class); + + StringBuilder body = new StringBuilder("" +/*{ + Multimap<Class<? extends TopiaEntity>, TopiaEntity> tmp = + ArrayListMultimap.create(); + + // pour tous les attributs rechecher les composites et les class d'asso + // on les ajoute dans tmp +}*/ + ); + Collection<ObjectModelAttribute> compositeAttributes = + TopiaGeneratorUtil.getEntityAttributes( + input, model, + TopiaGeneratorUtil.IS_ATTRIBUTE_REFERENCE_CLASSIFIER, + TopiaGeneratorUtil.IS_ATTRIBUTE_COMPOSITE) + ; + + // add composition attributes + + for (ObjectModelAttribute attr : compositeAttributes ) { + + String type = attr.getType(); + addImport(result, type); + type = TopiaGeneratorUtil.getSimpleName(type); + String attrName = attr.getName(); + String getterName = getJavaBeanMethodName("get", attrName); + if (TopiaGeneratorUtil.isNMultiplicity(attr)) { + addImport(result, Collection.class); + body.append("" +/*{ Collection<<%=type%>> ee = entity.<%=getterName%>(); + if (ee != null) { + tmp.putAll(<%=type%>.class, ee); + } +}*/ + ); + } else { + body.append("" +/*{ <%=type%> ee = entity.<%=getterName%>(); + if (ee != null) { + tmp.put(<%=type%>.class, ee); + } +}*/ + ); + } + } + + // add association attributes + + Collection<ObjectModelAttribute> associationAttributes = + TopiaGeneratorUtil.getEntityAttributes( + input, model, + TopiaGeneratorUtil.IS_ATTRIBUTE_REFERENCE_CLASSIFIER, + TopiaGeneratorUtil.IS_ATTRIBUTE_HAS_ASSOCIATION_CLASS); + for (ObjectModelAttribute attr : associationAttributes ) { + + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + addImport(result, assocClassFQN); + assocClassFQN = TopiaGeneratorUtil.getSimpleName(assocClassFQN); + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName( + attr); + String getterName = getJavaBeanMethodName("get", TopiaGeneratorUtil.toLowerCaseFirstLetter( + assocAttrName)); + if (!TopiaGeneratorUtil.isNMultiplicity(attr)) { + body.append("" +/*{ <%=assocClassFQN%> ee = entity.<%=getterName%>(); + if (ee != null) { + tmp.put(<%=assocClassFQN%>.class, ee); + } +}*/ + ); + } 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%>); +// } + + addImport(result, TopiaDAO.class); + body.append("" +/*{ { + TopiaDAO<<%=assocClassFQN%>> dao = getContext().getDAO(<%=assocClassFQN%>.class); + List<<%=assocClassFQN%>> eee = dao.findAllByProperties("<%=reverseAttrName%>", entity); + if (eee != null) { + tmp.putAll(<%=assocClassFQN%>.class, eee); + } + } +}*/ + ); + } + } + + // merge all results + + body.append("" +/*{ + // on refait un tour sur chaque entity de tmp pour recuperer leur + // composite + Multimap<Class<? extends TopiaEntity>, TopiaEntity> result = + fillComposite(tmp); + return result; + }*/ + ); + + setOperationBody(operation, body.length() == 0 ? " " : body.toString()); + } + + protected void generateAggregateOperation(ObjectModelClass result, + ObjectModelClass input) { + + ObjectModelOperation operation = + addOperation(result, "getAggregate", + Multimap.class.getName() + "<Class<? extends TopiaEntity>, TopiaEntity>"); + + addParameter(operation, "E", "entity"); + addException(operation, TopiaException.class); + addAnnotation(result, operation, Override.class.getSimpleName()); + + addImport(result, ArrayList.class); + addImport(result, List.class); + + StringBuilder body = new StringBuilder("" +/*{ + Multimap<Class<? extends TopiaEntity>, TopiaEntity> tmp = + ArrayListMultimap.create(); + + // pour tous les attributs rechecher les aggregates + // on les ajoute dans tmp +}*/ + ); + + Collection<ObjectModelAttribute> attributes = + TopiaGeneratorUtil.getEntityAttributes( + input, model, + TopiaGeneratorUtil.IS_ATTRIBUTE_REFERENCE_CLASSIFIER, + TopiaGeneratorUtil.IS_ATTRIBUTE_AGGREGATE); + + + for (ObjectModelAttribute attr : attributes) { + + String attrName = attr.getName(); + String getterName = getJavaBeanMethodName("get", attrName); + String type = attr.getType(); + addImport(result, type); + type = TopiaGeneratorUtil.getSimpleName(type); + if (TopiaGeneratorUtil.isNMultiplicity(attr)) { + body.append("" +/*{ <%=type%> ee = entity.<%=getterName%>(); + if (ee != null) { + tmp.put(<%=type%>.class, ee); + } +}*/ + ); + } else { + body.append("" +/*{ Collection<<%=type%>> ee = entity.<%=getterName%>(); + if (ee != null) { + tmp.putAll(<%=type%>.class, ee); + } +}*/ + ); + } + } + body.append("" +/*{ + // on refait un tour sur chaque entity de tmp pour recuperer leur + // aggregate + Multimap<Class<? extends TopiaEntity>, TopiaEntity> result = + fillAggregate(tmp); + return result; + }*/ + ); + + setOperationBody(operation, body.toString()); + } } 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-16 22:41:20 UTC (rev 2487) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2012-05-16 22:44:49 UTC (rev 2488) @@ -1286,6 +1286,7 @@ addException(operation, TopiaException.class); addAnnotation(outputAbstract, operation, Override.class.getSimpleName()); + addAnnotation(outputAbstract, operation, Deprecated.class.getSimpleName()); addImport(outputAbstract, ArrayList.class); addImport(outputAbstract, List.class); @@ -1390,6 +1391,7 @@ addException(operation, TopiaException.class); addAnnotation(outputAbstract, operation, Override.class.getSimpleName()); + addAnnotation(outputAbstract, operation, Deprecated.class.getSimpleName()); addImport(outputAbstract, ArrayList.class); addImport(outputAbstract, List.class); Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java 2012-05-16 22:41:20 UTC (rev 2487) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java 2012-05-16 22:44:49 UTC (rev 2488) @@ -25,6 +25,10 @@ package org.nuiton.topia.generator; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.Collections2; +import com.google.common.collect.Lists; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; @@ -44,6 +48,7 @@ import org.nuiton.eugene.models.object.ObjectModelParameter; import org.nuiton.topia.persistence.TopiaDAOImpl; +import javax.annotation.Nullable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -1068,13 +1073,14 @@ */ public static boolean isEntity(ObjectModelAttribute attribute, ObjectModel model) { - if (isPrimitiveType(attribute)) { - return false; + boolean result = false; + if (!isPrimitiveType(attribute)) { + String attributeType = attribute.getType(); + ObjectModelClassifier typeclassifier = + model.getClassifier(attributeType); + result = typeclassifier != null && isEntity(typeclassifier); } - String attributeType = attribute.getType(); - ObjectModelClassifier typeclassifier = - model.getClassifier(attributeType); - return typeclassifier != null && isEntity(typeclassifier); + return result; } /** @@ -1706,5 +1712,62 @@ return generate; } + public static Collection<ObjectModelAttribute> getEntityAttributes(ObjectModelClassifier clazz, + ObjectModel model, + Predicate<ObjectModelAttribute>... extraPredicate) { + + Predicate<ObjectModelAttribute> predicate = + new AttributeIsEntity(model); + if (extraPredicate != null && extraPredicate.length > 0) { + predicate = Predicates.and(Lists.asList(predicate, extraPredicate)); + } + Collection<ObjectModelAttribute> result = Collections2.filter( + clazz.getAttributes(), predicate); + return result; + } + + public static final Predicate<ObjectModelAttribute> IS_ATTRIBUTE_COMPOSITE = new Predicate<ObjectModelAttribute>() { + @Override + public boolean apply( ObjectModelAttribute input) { + return input.isComposite(); + } + }; + + public static final Predicate<ObjectModelAttribute> IS_ATTRIBUTE_AGGREGATE = new Predicate<ObjectModelAttribute>() { + @Override + public boolean apply( ObjectModelAttribute input) { + return input.isAggregate(); + } + }; + + public static final Predicate<ObjectModelAttribute> IS_ATTRIBUTE_REFERENCE_CLASSIFIER= new Predicate<ObjectModelAttribute>() { + @Override + public boolean apply( ObjectModelAttribute input) { + return input.referenceClassifier(); + } + }; + + public static final Predicate<ObjectModelAttribute> IS_ATTRIBUTE_HAS_ASSOCIATION_CLASS= new Predicate<ObjectModelAttribute>() { + @Override + public boolean apply( ObjectModelAttribute input) { + return input.hasAssociationClass(); + } + }; + + public static class AttributeIsEntity implements Predicate<ObjectModelAttribute> { + + private final ObjectModel model; + + public AttributeIsEntity(ObjectModel model) { + this.model = model; + } + + @Override + public boolean apply(ObjectModelAttribute input) { + boolean result = isEntity(input, model); + return result; + } + } + } // TopiaGeneratorUtil Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2012-05-16 22:41:20 UTC (rev 2487) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaDAO.java 2012-05-16 22:44:49 UTC (rev 2488) @@ -36,6 +36,7 @@ */ package org.nuiton.topia.persistence; +import com.google.common.collect.Multimap; import org.nuiton.topia.TopiaContext; import org.nuiton.topia.TopiaException; import org.nuiton.topia.event.TopiaEntityListener; @@ -466,5 +467,33 @@ */ E findByNaturalIds(Object... k) throws TopiaException; + /** + * Obtain from a given entity all objects which will be deleted if + * entity is deleted (such objects are called composite). + * + * <strong>Note:</strong> result is given as a Multimap indexed their type. + * + * @param entity entity to scan + * @return all object that must be deleted if this object is deleted + * @throws TopiaException if any pb + * @since 3.0 + */ + Multimap<Class<? extends TopiaEntity>, TopiaEntity> getComposite(E entity) throws TopiaException; + + /** + * Obtains from a given entity all objects used by this entity but will not + * be removed when deleting given entity (such objects are called + * aggregate). + * + * <strong>Note:</strong> result is given as a Multimap indexed their type. + * + * @param entity entity to scan + * @return all object that are aggregate with the given entity, + * aggreate object are not removed automaticaly + * @throws TopiaException if any pb + * @since 3.0 + */ + Multimap<Class<? extends TopiaEntity>, TopiaEntity> getAggregate(E entity) throws TopiaException; + } //TopiaDAO 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 2012-05-16 22:41:20 UTC (rev 2487) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/TopiaEntity.java 2012-05-16 22:44:49 UTC (rev 2488) @@ -73,8 +73,10 @@ String TOPIA_VERSION = "topiaVersion"; + @Deprecated String COMPOSITE = "composite"; - + + @Deprecated String AGGREGATE = "aggregate"; /** @@ -133,14 +135,18 @@ /** * @return all object that must be deleted if this object is deleted * @throws TopiaException if any pb + * @deprecated since 3.0, prefer use the dao method {@link TopiaDAO#getComposite(TopiaEntity)}. */ + @Deprecated List<TopiaEntity> getComposite() throws TopiaException; /** * @return all object that are aggregate with this instance, aggreate object * are not removed automaticaly * @throws TopiaException if any pb + * @deprecated since 3.0, prefer use the dao method {@link TopiaDAO#getAggregate(TopiaEntity)}. */ + @Deprecated List<TopiaEntity> getAggregate() throws TopiaException; /**
participants (1)
-
tchemit@users.nuiton.org