Author: fdesbois Date: 2010-06-27 14:51:25 +0200 (Sun, 27 Jun 2010) New Revision: 2038 Url: http://nuiton.org/repositories/revision/topia/2038 Log: Evo #609 : Refactor aggregate and composite operations + abstract methods from entity 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-27 11:13:04 UTC (rev 2037) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2010-06-27 12:51:25 UTC (rev 2038) @@ -205,8 +205,6 @@ input.getPackageName()); // These import will be removed (will be automatically added if needed) - addImport(outputAbstract, ArrayList.class); - addImport(outputAbstract, List.class); // addImport(output, TopiaEntity.class); addImport(outputAbstract, TopiaContextImplementor.class); @@ -321,11 +319,13 @@ createAcceptOperation(); - generateAggregateMethod(outputAbstract, input); + // Imports needed for aggregate and composite operations + addImport(outputAbstract, ArrayList.class); + addImport(outputAbstract, List.class); - generateCompositeMethod(outputAbstract, input); + createAggregateOperation(); - generateAbstractMethods(outputAbstract, input); + createCompositeOperation(); boolean doGenerateToString = TopiaGeneratorUtil.generateToString(input, model); @@ -344,6 +344,10 @@ protected void stopGenerateUtilOperations() { closeAcceptOperation(); + + closeAggregateOperation(); + + closeCompositeOperation(); } /** @@ -362,32 +366,35 @@ protected void generateExtraOperations(ObjectModelClass input) { for (ObjectModelOperation operation : input.getOperations()) { - String visibility = operation.getVisibility(); - if (operation.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_DAO) || - !visibility.equals(ObjectModelModifier.PUBLIC.toString())) { - // Pas de génération des signatures de méthodes pour celles à intégrer au DAO de l'entité - return; - } String opName = operation.getName(); String opType = operation.getReturnType(); + ObjectModelModifier visibility = + ObjectModelModifier.toValue(operation.getVisibility()); - ObjectModelOperation op2 = addOperation(outputInterface, opName, opType, ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(operation)) { - setDocumentation(op2, operation.getDocumentation()); + if (isVerbose()) { + log.info("Extra operation for : " + input.getQualifiedName() + + " - method : " + opName + + " - returnType : " + opType + + " - visibility : " + visibility); } - for (ObjectModelParameter param : operation.getParameters()) { - String paramName = param.getName(); - String paramType = param.getType(); - ObjectModelParameter param2 = addParameter(op2, paramType, paramName); - if (TopiaGeneratorUtil.hasDocumentation(param)) { - setDocumentation(param2, param.getDocumentation()); - } + // Deprecated from 2.3.4 + // Pas de génération des signatures de méthodes pour celles à intégrer au DAO de l'entité + if (operation.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_DAO)) { + return; + + // Generate entity methods which have not a public visibility. + // Only in abstract entity class as abstract operation. + } else if (!visibility.equals(ObjectModelModifier.PUBLIC)) { + addOperation(outputAbstract, opName, opType, visibility, + ObjectModelModifier.ABSTRACT); + + // Other operations, only in entity interface, implementations + // need to be done in implementation class created by developper + } else { + cloneOperationSignature(operation, outputInterface, true); } - for (String exception : operation.getExceptions()) { - addException(op2, exception); - } } } @@ -402,7 +409,7 @@ if (!associationClass) { - // FIXME-fdesbois-2010-06-25 : Strange behavior to keep those links + // FIXME-fdesbois-2010-06-25 : Strange behavior to keep those links, will break hibernate, may be a problem in mapping if (!attribute.isNavigable() && attribute.hasAssociationClass()) { generatePropertyConstant(attribute); @@ -487,8 +494,10 @@ addAnnotation(outputAbstract, property, annotation); } - // accept operation body + // Update util operations body updateAcceptOperation(attribute); + updateAggregateOperation(attribute); + updateCompositeOperation(attribute); } /** @@ -1116,6 +1125,14 @@ private StringBuilder acceptOperationBody; + private ObjectModelOperation aggregateOperation; + + private StringBuilder aggregateOperationBody; + + private ObjectModelOperation compositeOperation; + + private StringBuilder compositeOperationBody; + protected void createAcceptOperation() { // Interface operation ObjectModelOperation interfaceOperation = @@ -1172,7 +1189,174 @@ " " : acceptOperationBody.toString()); } + protected void createAggregateOperation() { + // Interface operation + ObjectModelOperation interfaceOperation = + addOperation(outputInterface, "getAggregate", + List.class.getName() + '<' + TopiaEntity.class.getName() + '>'); + + addException(interfaceOperation, TopiaException.class); + + // Implementation + aggregateOperation = createImplOperation(interfaceOperation); + + aggregateOperationBody = 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 +}*/ + ); + } + + protected void updateAggregateOperation(ObjectModelAttribute attribute) { + if (attribute.isAggregate() && + attribute.referenceClassifier() && + attribute.getClassifier().hasStereotype(STEREOTYPE_ENTITY)) { + + String attrName = attribute.getName(); + String getterName = "get" + StringUtils.capitalize(attrName); + if (GeneratorUtil.isNMultiplicity(attribute)) { + aggregateOperationBody.append("" +/*{ tmp.addAll(<%=getterName%>()); +}*/ + ); + } else { + aggregateOperationBody.append("" +/*{ tmp.add(<%=getterName%>()); +}*/ + ); + } + } + } + + protected void closeAggregateOperation() { + aggregateOperationBody.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(aggregateOperation, aggregateOperationBody.length() == 0 ? + " " : aggregateOperationBody.toString()); + } + + protected void createCompositeOperation() { + + // Interface operation + ObjectModelOperation interfaceOperation = + addOperation(outputInterface, "getComposite", + List.class.getName() + '<' + TopiaEntity.class.getName() + '>'); + + addException(interfaceOperation, TopiaException.class); + + // Implementation + compositeOperation = createImplOperation(interfaceOperation); + + compositeOperationBody = 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 +}*/ + ); + } + + protected void updateCompositeOperation(ObjectModelAttribute attribute) { + if (attribute.referenceClassifier() && + attribute.getClassifier().hasStereotype(STEREOTYPE_ENTITY)) { + if (attribute.isComposite()) { + String attrName = attribute.getName(); + String getterName = "get" + StringUtils.capitalize(attrName); + if (GeneratorUtil.isNMultiplicity(attribute)) { + compositeOperationBody.append("" +/*{ if (<%=getterName%>() != null) { + tmp.addAll(<%=getterName%>()); + } +}*/ + ); + } else { + compositeOperationBody.append("" +/*{ tmp.add(<%=getterName%>()); +}*/ + ); + } + } else if (attribute.hasAssociationClass()) { + String assocAttrName = GeneratorUtil.getAssocAttrName( + attribute); + String assocClassFQN = TopiaGeneratorUtil.getSimpleName( + attribute.getAssociationClass().getQualifiedName()); + String ref = "this." + GeneratorUtil.toLowerCaseFirstLetter( + assocAttrName); + if (!GeneratorUtil.isNMultiplicity(attribute)) { + compositeOperationBody.append("" +/*{ + if (<%=ref%> != null) { + tmp.add(<%=ref%>); + } +}*/ + ); + } else { + ObjectModelAttribute reverse = attribute.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%>); +// } + compositeOperationBody.append("" +/*{ + { + org.nuiton.topia.persistence.TopiaDAO<<%=assocClassFQN%>> dao = ((TopiaContextImplementor) getTopiaContext()).getDAO(<%=assocClassFQN%>.class); + List<<%=assocClassFQN%>> findAllByProperties = dao.findAllByProperties("<%=reverseAttrName%>", this); + if (findAllByProperties != null) { + tmp.addAll(findAllByProperties); + } + } +}*/ + ); + } + } + } + } + + protected void closeCompositeOperation() { + compositeOperationBody.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(compositeOperation, compositeOperationBody.length() == 0 ? + " " : compositeOperationBody.toString()); + } + + ///////////////////////////////// OLD // ------------------------------------------------------------------------- @@ -1225,6 +1409,7 @@ } + @Deprecated protected void generateCompositeMethod(ObjectModelClass output, ObjectModelClass clazz) { @@ -1325,6 +1510,7 @@ setOperationBody(op, body.length() == 0 ? " " : body.toString()); } + @Deprecated protected void generateAggregateMethod(ObjectModelClass output, ObjectModelClass clazz) { @@ -1485,6 +1671,7 @@ * @param output ObjectModelClass result corresponding to the EntityAbstract * @param input ObjectModelClass source from ObjectModel */ + @Deprecated private void generateAbstractMethods(ObjectModelClass output, ObjectModelClass input) { for (ObjectModelOperation op : input.getOperations()) { @@ -1508,6 +1695,8 @@ private void generateI18n(ObjectModelClass result, String i18nPrefix, ObjectModelClass clazz) { + ObjectModelOperation block = addBlock(result, ObjectModelModifier.STATIC); + StringBuilder buffer = new StringBuilder(300); addI18n(buffer, i18nPrefix, Introspector.decapitalize(clazz.getName())); for (ObjectModelAttribute attr : clazz.getAttributes()) { @@ -1516,10 +1705,7 @@ addI18n(buffer, i18nPrefix, Introspector.decapitalize(attr.getName())); } } - - //FIXME : use a block extension for java - ObjectModelOperation op = addBlock(result, ObjectModelModifier.STATIC); - setOperationBody(op, buffer.toString()); + setOperationBody(block, buffer.toString()); } private void addI18n(StringBuilder buffer, String i18nPrefix, @@ -1595,7 +1781,7 @@ protected ObjectModelOperation createImplOperation(ObjectModelOperation interfaceOperation) { ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + cloneOperationSignature(interfaceOperation, outputAbstract, false); addAnnotation(outputAbstract, implOperation, Override.class.getSimpleName()); return implOperation; } @@ -1635,5 +1821,4 @@ return operation; } - }