Author: fdesbois Date: 2010-06-27 13:13:04 +0200 (Sun, 27 Jun 2010) New Revision: 2037 Url: http://nuiton.org/repositories/revision/topia/2037 Log: Evo #609 : Refactor attribute generation + accept method 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 06:36:23 UTC (rev 2036) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/EntityTransformer.java 2010-06-27 11:13:04 UTC (rev 2037) @@ -56,7 +56,6 @@ import static org.nuiton.topia.generator.TopiaGeneratorUtil.TAG_ANNOTATION; import static org.nuiton.topia.generator.TopiaGeneratorUtil.TAG_DB_NAME; import static org.nuiton.topia.generator.TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType; -import static org.nuiton.topia.generator.TopiaGeneratorUtil.shouldBeAbstract; @@ -113,14 +112,12 @@ } if (log.isDebugEnabled()) { - log.debug("for entity : "+input.getQualifiedName()); - log.info("Will use classLoader "+ getClassLoader()); + log.debug("for entity : " + input.getQualifiedName()); + log.info("Will use classLoader " + getClassLoader()); } // fix once for all the constant prefix to use - String prefix = getConstantPrefix(input, ""); - if (StringUtils.isEmpty(prefix)) { // no specific prefix, so no prefix @@ -128,74 +125,65 @@ log.warn("[" + input.getName() + "] Will generate constants with NO prefix, not a good idea..."); } } - setConstantPrefix(prefix); - String clazzName = input.getName(); - String packageName = input.getPackageName(); + // Create Entity Interface and its header + createEntityInterface(input); - Collection<ObjectModelAttribute> attributes = input.getAttributes(); + // Create Entity Abstract class and its header + createEntityAbstractClass(input); - Collection<ObjectModelOperation> operations = input.getOperations(); + // Start util operations : create operations : accept, aggregate, composite, toString + startGenerateUtilOperations(input); - // Create classifiers : Interface, Abstract - outputInterface = createInterface(clazzName, packageName); - outputAbstract = createAbstractClass(clazzName + "Abstract", packageName); + // Add constant, attribute and operations for each property + generateProperties(input.getAttributes()); - generateHeaders(input); - - generateProperties(input); - // Case of association class : properties from participants/extremities // of the association class. if (input instanceof ObjectModelAssociationClass) { ObjectModelAssociationClass association = (ObjectModelAssociationClass)input; - generatePropertiesFromAssociation(association); + associationClass = true; + generateProperties(association.getParticipantsAttributes()); + associationClass = false; } + // Stop util operations : set each operation body + stopGenerateUtilOperations(); + + // Add extra constants (from uml dependency) generateExtraConstants(input); + // Add extra operations (defined on the entity) generateExtraOperations(input); - generateAbstract(input, outputAbstract, attributes, operations); + boolean generateImpl = isGenerateImpl(input, input.getOperations()); - boolean generateImpl = isGenerateImpl(input, operations); - if (generateImpl) { - String implName = clazzName + "Impl"; - if (isVerbose()) { - log.info("Will generate [" + implName + "]"); - } - - if (isAbstract(input)) { - outputImpl = createAbstractClass(implName, packageName); - } else { - outputImpl = createClass(implName, packageName); - } - // generate impl - generateImpl(input, outputImpl); + generateImpl(input); } // Clean data output after transformation clean(); } - protected void generateHeaders(ObjectModelClass input) { + protected void createEntityInterface(ObjectModelClass input) { - ////// For Interface - addImport(outputInterface, TopiaEntity.class); + outputInterface = createInterface(input.getName(), input.getPackageName()); + // Documentation if (TopiaGeneratorUtil.hasDocumentation(input)) { setDocumentation(outputInterface, input.getDocumentation()); } - // super interfaces + // Extends for (ObjectModelClassifier parent : input.getInterfaces()) { addInterface(outputInterface, parent.getQualifiedName()); } + // Extends from inheritance boolean needTopiaEntity = true; for (ObjectModelClassifier parent : input.getSuperclasses()) { if (parent.hasStereotype(STEREOTYPE_ENTITY)) { @@ -205,56 +193,244 @@ } } + // Extends TopiaEntity (only if hasn't parent entity) if (needTopiaEntity) { addInterface(outputInterface, TopiaEntity.class); } } + protected void createEntityAbstractClass(ObjectModelClass input) { + + outputAbstract = createAbstractClass(input.getName() + "Abstract", + 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); + + // Documentation + StringBuilder doc = new StringBuilder(); + doc.append("Implantation POJO pour l'entité {@link "); + doc.append(StringUtils.capitalize(outputInterface.getName())); + doc.append("}\n"); + + String dbName = input.getTagValue(TAG_DB_NAME); + if (dbName != null) { + doc.append("<p>Nom de l'entité en BD : "); + doc.append(dbName); + doc.append(".</p>"); + } + + setDocumentation(outputAbstract, doc.toString()); + + // Implements + addInterface(outputAbstract, outputInterface.getName()); + + // Extends + for (ObjectModelClass parent : input.getSuperclasses()) { + String extendClass = parent.getQualifiedName(); + //Si une des classes parentes définies des méthodes abstraites, son + // impl ne sera pas créé + boolean abstractParent = TopiaGeneratorUtil.shouldBeAbstract(parent); + if (parent.hasStereotype(STEREOTYPE_ENTITY)) { + if (abstractParent) { + extendClass += "Abstract"; + } else { + extendClass += "Impl"; + } + } + setSuperClass(outputAbstract, extendClass); + } + + // Extends TopiaEntityAbstract (only if hasn't parent entity) + if (outputAbstract.getSuperclasses().isEmpty()) { + setSuperClass(outputAbstract, TopiaEntityAbstract.class); + } + + // serialVersionUID + String svUID = + TopiaGeneratorUtil.findTagValue("serialVersionUID", input, model); + if (svUID != null) { + addConstant(outputInterface, "serialVersionUID", long.class, svUID, + ObjectModelModifier.PRIVATE); + } + } + + protected boolean isGenerateImpl(ObjectModelClass input, + Collection<ObjectModelOperation> operations) { + + String fqn = input.getQualifiedName() + "Impl"; + + URL fileLocation = getFileInClassPath(fqn); + + if (fileLocation != null) { + + // there is already a existing file in class-path, skip + if (isVerbose()) { + log.info("Will not generate [" + fqn + "], found existing file in class-path : " + fileLocation); + } + return false; + } + + // On ne génère pas le impl si l'entité a des opérations qui ne sont + // pas seulement pour le DAO + if (!operations.isEmpty()) { + + if (isVerbose()) { + log.info("Will not generate [" + fqn + "], there is some operations and not only DAO ones"); + } + return false; + } + + //De même, on ne génère pas le impl si il y a des opérations venant des + // superclasses non implémentées + for (ObjectModelOperation otherOp : input.getAllOtherOperations(false)) { + if (otherOp.isAbstract()) { + if (isVerbose()) { + log.info("Will not generate [" + fqn + "], there is a abstract operation [" + otherOp.getName() + "] in allOtherOperations."); + } + return false; + } + } + + return true; + } + + protected void generateImpl(ObjectModelClass input) { + + String implName = input.getName() + "Impl"; + String packageName = input.getPackageName(); + if (isVerbose()) { + log.info("Will generate [" + implName + "]"); + } + + if (isAbstract(input)) { + outputImpl = createAbstractClass(implName, packageName); + } else { + outputImpl = createClass(implName, packageName); + } + + setDocumentation(outputImpl, "Implantation des operations pour l'entité " + + input.getName() + "."); + setSuperClass(outputImpl, input.getQualifiedName() + "Abstract"); + } + + protected void startGenerateUtilOperations(ObjectModelClass input) { + + createAcceptOperation(); + + generateAggregateMethod(outputAbstract, input); + + generateCompositeMethod(outputAbstract, input); + + generateAbstractMethods(outputAbstract, input); + + boolean doGenerateToString = TopiaGeneratorUtil.generateToString(input, + model); + if (doGenerateToString) { + addImport(outputAbstract, ToStringBuilder.class); + generateToStringMethod(outputAbstract, input); + } + + String i18nPrefix = TopiaGeneratorUtil.getI18nPrefix(input, model); + if (!StringUtils.isEmpty(i18nPrefix)) { + // generate i18n prefix + generateI18n(outputAbstract, i18nPrefix, input); + } + } + + protected void stopGenerateUtilOperations() { + + closeAcceptOperation(); + } + /** - * Generate properties from {@code input} entity class. Generate - * constant and operations for each property. + * Generate extra constants if {@code input} has dependencies on + * enum used as constant injector. * - * @param input Input entity class + * @param input Entity class to treate */ - protected void generateProperties(ObjectModelClass input) { - for (ObjectModelAttribute attribute : input.getAttributes()) { + protected void generateExtraConstants(ObjectModelClass input) { + Set<String> constants = addConstantsFromDependency(input, outputInterface); - // FIXME-fdesbois-2010-06-25 : Stupid case, need to be removed - if (!attribute.isNavigable() && attribute.hasAssociationClass()) { - generatePropertyConstant(attribute); - } + if (log.isDebugEnabled()) { + log.debug("Add constants from dependency : " + constants); + } + } - if (!attribute.isNavigable() && - !TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType( - attribute.getReverseAttribute(), model)) { - continue; + 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; } - generatePropertyConstant(attribute); + String opName = operation.getName(); + String opType = operation.getReturnType(); - generatePropertyOperations(attribute); + ObjectModelOperation op2 = addOperation(outputInterface, opName, opType, ObjectModelModifier.PACKAGE); + if (TopiaGeneratorUtil.hasDocumentation(operation)) { + setDocumentation(op2, operation.getDocumentation()); + } + + 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()); + } + } + for (String exception : operation.getExceptions()) { + addException(op2, exception); + } } } /** - * Generate properties from {@code input} entity assocation class. Generate - * constant and operations for each property. + * Generate properties from {@code attributes}. Generate + * constant, attribute and operations for each property. * - * @param association Input entity association class + * @param attributes Input attributes */ - protected void generatePropertiesFromAssociation(ObjectModelAssociationClass association) { - associationClass = true; - for (ObjectModelAttribute attribute : association.getParticipantsAttributes()) { + protected void generateProperties(Collection<ObjectModelAttribute> attributes) { + for (ObjectModelAttribute attribute : attributes) { + if (!associationClass) { + + // FIXME-fdesbois-2010-06-25 : Strange behavior to keep those links + if (!attribute.isNavigable() && attribute.hasAssociationClass()) { + generatePropertyConstant(attribute); + + generatePropertyAttribute(attribute); + } + + if (!attribute.isNavigable() && + !TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType( + attribute.getReverseAttribute(), model)) { + continue; + } + } + + // constant generatePropertyConstant(attribute); - addSingleSetOperation(attribute); + // attribute + generatePropertyAttribute(attribute); - addSingleGetOperation(attribute, null); + // operations + generatePropertyOperations(attribute); } - associationClass = false; } + // ------------------------------------------------------------------------- + // Generate for property + // ------------------------------------------------------------------------- + /** * Generate constant in interface for {@code attribute}. * @@ -272,6 +448,49 @@ "\"" + attrName + "\""); } + protected void generatePropertyAttribute(ObjectModelAttribute attribute) { + + String attrName = getPropertyName(attribute); + String attrType = getPropertyType(attribute); + String collectionType = getCollectionType(attribute); + + if (collectionType != null) { + attrType = collectionType + "<" + attrType + ">"; + } + + //String attrVisibility = attr.getVisibility(); + + // Declaration + ObjectModelAttribute property = + addAttribute(outputAbstract, attrName, attrType, null, + //ObjectModelModifier.toValue(attrVisibility), + ObjectModelModifier.PROTECTED + ); + + // Documentation + StringBuilder buffer = new StringBuilder(); + if (TopiaGeneratorUtil.hasDocumentation(attribute)) { + String attrDocumentation = attribute.getDocumentation(); + buffer.append(attrDocumentation).append('\n'); + } + if (attribute.hasTagValue(TAG_DB_NAME)) { + String dbName = attribute.getTagValue(TAG_DB_NAME); + buffer.append("Nom de l'attribut en BD : ").append(dbName).append('\n'); + } + setDocumentation(property, buffer.toString()); + + // Annotation + if (attribute.hasTagValue(TAG_ANNOTATION)) { + String annotation = attribute.getTagValue(TAG_ANNOTATION); + //FIXME Make annotation works... + //TODO tchemit 20100513 Test it still works + addAnnotation(outputAbstract, property, annotation); + } + + // accept operation body + updateAcceptOperation(attribute); + } + /** * Generation operations for {@code attributes}. * One method exists for each operation to generate. Methods starting @@ -295,7 +514,7 @@ */ protected void generatePropertyOperations(ObjectModelAttribute attribute) { - if (attribute.getMaxMultiplicity() == 1) { + if (attribute.getMaxMultiplicity() == 1 || associationClass) { // setXXX addSingleSetOperation(attribute); @@ -358,91 +577,6 @@ } } - /** - * Generate extra constants if {@code input} has dependencies on - * enum used as constant injector. - * - * @param input Entity class to treate - */ - protected void generateExtraConstants(ObjectModelClass input) { - Set<String> constants = addConstantsFromDependency(input, outputInterface); - - if (log.isDebugEnabled()) { - log.debug("Add constants from dependency : " + constants); - } - } - - 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(); - - ObjectModelOperation op2 = addOperation(outputInterface, opName, opType, ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(operation)) { - setDocumentation(op2, operation.getDocumentation()); - } - - 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()); - } - } - for (String exception : operation.getExceptions()) { - addException(op2, exception); - } - } - } - - protected boolean isGenerateImpl(ObjectModelClass input, - Collection<ObjectModelOperation> operations) { - - String fqn = input.getQualifiedName() + "Impl"; - - URL fileLocation = getFileInClassPath(fqn); - - if (fileLocation != null) { - - // there is already a existing file in class-path, skip - if (isVerbose()) { - log.info("Will not generate [" + fqn + "], found existing file in class-path : " + fileLocation); - } - return false; - } - - // On ne génère pas le impl si l'entité a des opérations qui ne sont - // pas seulement pour le DAO - if (!operations.isEmpty()) { - - if (isVerbose()) { - log.info("Will not generate [" + fqn + "], there is some operations and not only DAO ones"); - } - return false; - } - - //De même, on ne génère pas le impl si il y a des opérations venant des - // superclasses non implémentées - for (ObjectModelOperation otherOp : input.getAllOtherOperations(false)) { - if (otherOp.isAbstract()) { - if (isVerbose()) { - log.info("Will not generate [" + fqn + "], there is a abstract operation [" + otherOp.getName() + "] in allOtherOperations."); - } - return false; - } - } - - return true; - } - protected void addSingleSetOperation(ObjectModelAttribute attribute) { String attrName = getPropertyName(attribute); @@ -459,8 +593,7 @@ attribute.getDocumentation()); // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); attrType = TopiaGeneratorUtil.getSimpleName(attrType); @@ -510,8 +643,7 @@ } // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); attrType = TopiaGeneratorUtil.getSimpleName(attrType); @@ -557,8 +689,7 @@ setDocumentation(param, "L'instance de " + attrType + " à ajouter"); // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); attrType = TopiaGeneratorUtil.getSimpleName(attrType); @@ -628,8 +759,7 @@ setDocumentation(param, "Les instances de " + attrType + " à ajouter"); // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); attrType = TopiaGeneratorUtil.getSimpleName(attrType); @@ -661,8 +791,7 @@ createPropertySetterSignature(outputInterface, attrType, attrName, attribute.getDocumentation()); - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); attrType = TopiaGeneratorUtil.getSimpleName(attrType); referenceType = TopiaGeneratorUtil.getSimpleName(referenceType); @@ -704,8 +833,7 @@ setDocumentation(param, "L'instance de " + attrType + " à retirer"); // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); attrType = TopiaGeneratorUtil.getSimpleName(attrType); @@ -770,8 +898,7 @@ } // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); attrType = TopiaGeneratorUtil.getSimpleName(attrType); @@ -837,8 +964,7 @@ setDocumentation(interfaceOperation, "Retourne la collection."); // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); setOperationBody(implOperation, "" /*{ @@ -870,8 +996,7 @@ setDocumentation(param, "le topia id de l'entité recherchée"); // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); addImport(outputAbstract, TopiaEntityHelper.class); @@ -908,8 +1033,7 @@ // no doc from previous code // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); attrType = TopiaGeneratorUtil.getSimpleName(attrType); @@ -945,8 +1069,7 @@ setDocumentation(interfaceOperation, "Retourne le nombre d'éléments de la collection " + attrName); // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); setOperationBody(implOperation, "" /*{ @@ -975,8 +1098,7 @@ setDocumentation(interfaceOperation, "Retourne {@code true} si la collection " + attrName + " est vide."); // Implementation - ObjectModelOperation implOperation = - cloneOperationSignature(interfaceOperation, outputAbstract); + ObjectModelOperation implOperation = createImplOperation(interfaceOperation); setOperationBody(implOperation, "" /*{ @@ -986,413 +1108,74 @@ ); } + // ------------------------------------------------------------------------- + // Generate util operations + // ------------------------------------------------------------------------- - ///////////////////////////////// OLD + private ObjectModelOperation acceptOperation; - @Deprecated - protected void generateInterface(ObjectModelClass input, - ObjectModelInterface output, - Collection<ObjectModelAttribute> attributes, - Collection<ObjectModelOperation> operations) { + private StringBuilder acceptOperationBody; - addImport(output, TopiaEntity.class); + protected void createAcceptOperation() { + // Interface operation + ObjectModelOperation interfaceOperation = + addOperation(outputInterface, "accept", void.class); + ObjectModelParameter param = + addParameter(interfaceOperation, EntityVisitor.class, "visitor"); - Set<String> constants = addConstantsFromDependency(input, output); + addException(interfaceOperation, TopiaException.class); - if (log.isDebugEnabled()) { - log.debug("Add constants from dependency : " + constants); - } + // Documentation + setDocumentation(interfaceOperation, "Envoi via les methodes du visitor l'ensemble des " + + "champs de l'entity\n" + + "avec leur nom, type et valeur."); + setDocumentation(param, "le visiteur de l'entite."); - if (TopiaGeneratorUtil.hasDocumentation(input)) { - setDocumentation(output,input.getDocumentation()); - } + // Implementation + acceptOperation = createImplOperation(interfaceOperation); - // super classes - - for (ObjectModelClassifier parent : input.getInterfaces()) { - addInterface(output,parent.getQualifiedName()); - } - - boolean needTopiaEntity = true; - for (ObjectModelClassifier parent : input.getSuperclasses()) { - if (parent.hasStereotype(STEREOTYPE_ENTITY)) { - addInterface(output,parent.getQualifiedName()); - needTopiaEntity = false; - break; - } - } - - if (needTopiaEntity) { - addInterface(output, TopiaEntity.class); - } - - generateInterfaceStaticColumnNames(input, output); - - //Méthodes d'accès aux attributs d'une classe d'associations - - if (input instanceof ObjectModelAssociationClass) { - ObjectModelAssociationClass assoc = - (ObjectModelAssociationClass) input; - for (ObjectModelAttribute attr : - assoc.getParticipantsAttributes()) { - if (attr != null) { - String type = attr.getType(); - String name = attr.getName(); - generateInterfaceAssociationAccessors(output, name, type); - if (attr.getReverseAttribute() == null) { - type = ((ObjectModelClassifier) - attr.getDeclaringElement()).getQualifiedName(); - name = attr.getDeclaringElement().getName(); - generateInterfaceAssociationAccessors(output, name, type); - } - } - } - } - - for (ObjectModelOperation operation : operations) { - generateInterfaceOperation(operation, output); - } + acceptOperationBody = new StringBuilder(); + acceptOperationBody.append("" +/*{ + visitor.start(this); +}*/ + ); } - @Deprecated - protected void generateAbstract(ObjectModelClass input, - ObjectModelClass output, - Collection<ObjectModelAttribute> attributes, - Collection<ObjectModelOperation> operations) { + protected void updateAcceptOperation(ObjectModelAttribute attribute) { + String attrName = + TopiaGeneratorUtil.getSimpleName(getPropertyName(attribute)); + String attrType = + TopiaGeneratorUtil.getSimpleName(getPropertyType(attribute)); + String collectionType = getCollectionType(attribute); - String clazzName = input.getName(); - String clazzFQN = TopiaGeneratorUtil.getSimpleName( - input.getQualifiedName()); - - addInterface(output, clazzName); - - addImport(output, ArrayList.class); - addImport(output, List.class); - addImport(output, TopiaEntity.class); - addImport(output, TopiaContextImplementor.class); - - // javadoc - - StringBuilder doc = new StringBuilder(); - doc.append("Implantation POJO pour l'entité {@link "); - doc.append(StringUtils.capitalize(clazzFQN)); - doc.append("}\n"); - - { - String dbName = input.getTagValue(TAG_DB_NAME); - if (dbName != null) { - doc.append("<p>Nom de l'entité en BD : "); - doc.append(dbName); - doc.append(".</p>"); - } - } - - setDocumentation(output, doc.toString()); - - // super classes - - for (ObjectModelClass parent : input.getSuperclasses()) { - String extendClass = parent.getQualifiedName(); - //Si une des classes parentes définies des méthodes abstraites, son - // impl ne sera pas créé - boolean abstractParent = shouldBeAbstract(parent); - if (parent.hasStereotype(STEREOTYPE_ENTITY)) { - if (abstractParent) { - extendClass += "Abstract"; - } else { - extendClass += "Impl"; - } - } - setSuperClass(output, extendClass); - } - - if (output.getSuperclasses().isEmpty()) { - setSuperClass(output, TopiaEntityAbstract.class); - } - - // serialVersionUID - - String svUID = TopiaGeneratorUtil.findTagValue("serialVersionUID", - input, model); - if (svUID != null) { - - addConstant(output, "serialVersionUID", long.class, svUID, - ObjectModelModifier.PRIVATE + if (collectionType != null) { + collectionType = TopiaGeneratorUtil.getSimpleName(collectionType); + acceptOperationBody.append("" +/*{ visitor.visit(this, <%=getConstantName(attrName)%>, <%=collectionType%>.class, <%=attrType%>.class, <%=attrName%>); +}*/ ); - } - - ObjectModelParameter attr2; - - for (ObjectModelAttribute attr : attributes) { - ObjectModelAttribute reverse = attr.getReverseAttribute(); - - // pour les asso quoi qu'il arrive il faut les lier des 2 cotes - // pour pouvoir supprimer en cascade l'asso lors de la suppression - // d'un des cotes - if (!(attr.isNavigable() - || hasUnidirectionalRelationOnAbstractType(reverse, model) - || attr.hasAssociationClass())) { - continue; - } - - String type; - String name; - - if (!attr.hasAssociationClass()) { - String attrName = attr.getName(); - name = attrName; - type = attr.getType(); - } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - //TODO THIMEL : Je pense que les - // GeneratorUtil.toLowerCaseFirstLetter sont inutiles - // ici, ou alors il faudrait le faire partout - name = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName); - type = attr.getAssociationClass().getQualifiedName(); - } - - if (GeneratorUtil.isNMultiplicity(attr)) { - String collectionType = - TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); - type = collectionType + '<' + type + '>'; - } - - String attrVisibility = attr.getVisibility(); - - attr2 = addAttribute(output, name, type, null, - ObjectModelModifier.toValue(attrVisibility), - ObjectModelModifier.PROTECTED + } else { + acceptOperationBody.append("" +/*{ visitor.visit(this, <%=getConstantName(attrName)%>, <%=attrType%>.class, <%=attrName%>); +}*/ ); - - doc = new StringBuilder(); - - if (TopiaGeneratorUtil.hasDocumentation(attr) || - attr.hasTagValue(TAG_DB_NAME)) { - if (TopiaGeneratorUtil.hasDocumentation(attr)) { - String attrDocumentation = attr.getDocumentation(); - doc.append(attrDocumentation).append('\n'); - } - if (attr.hasTagValue(TAG_DB_NAME)) { - String dbName = - attr.getTagValue(TAG_DB_NAME); - doc.append("Nom de l'attribut en BD : "); - doc.append(dbName); - doc.append('\n'); - } - } - - setDocumentation(attr2, doc.toString()); - - - if (attr.hasTagValue(TAG_ANNOTATION)) { - String annotation = attr.getTagValue(TAG_ANNOTATION); - //FIXME Make annotation works... - //TODO tchemit 20100513 Test it still works - addAnnotation(output, attr2, annotation); - } } + } - //Déclaration des attributs d'une classe d'associations - if (input instanceof ObjectModelAssociationClass) { - ObjectModelAssociationClass assoc = - (ObjectModelAssociationClass) input; - for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { - if (attr != null) { - String attrVisibility = attr.getVisibility(); - String attrType = attr.getType(); - String attrName = attr.getName(); - addAttribute(output, - GeneratorUtil.toLowerCaseFirstLetter(attrName), - attrType, null, - ObjectModelModifier.toValue(attrVisibility)); - } - } - } - - ObjectModelOperation op = addOperation(output, "update", "void", - ObjectModelModifier.PUBLIC); - addException(op, TopiaException.class); - setDocumentation(op,"@deprecated since 2.3.4, use the DAO api instead."); - addAnnotation(output, op, Deprecated.class.getSimpleName()); - addAnnotation(output, op, Override.class.getSimpleName()); - setOperationBody(op, "" -/*{ - ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=clazzName%>.class).update(this); + protected void closeAcceptOperation() { + acceptOperationBody.append("" +/*{ visitor.end(this); }*/ ); - - op = addOperation(output, "delete", "void", ObjectModelModifier.PUBLIC); - addException(op, TopiaException.class); - setDocumentation(op,"@deprecated since 2.3.4, use the DAO api instead."); - addAnnotation(output, op, Deprecated.class.getSimpleName()); - addAnnotation(output, op, Override.class.getSimpleName()); - setOperationBody(op, "" -/*{ - ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=clazzName%>.class).delete(this); -}*/ - ); - - generateAcceptMethod(output, input); - - generateAggregateMethod(output, input); - - generateCompositeMethod(output, input); - - // DONE -// for (ObjectModelAttribute attr : attributes) { -// ObjectModelAttribute reverse = attr.getReverseAttribute(); -// -// if (!(attr.isNavigable() -// || hasUnidirectionalRelationOnAbstractType(reverse, model))) { -// continue; -// } -// -// transformAttribute(output, attr, reverse); -// } - - // DONE - //Méthodes d'accès aux attributs d'une classe d'associations -// if (input instanceof ObjectModelAssociationClass) { -// -// for (ObjectModelAttribute attr : -// ((ObjectModelAssociationClass) input).getParticipantsAttributes()) { -// if (attr != null) { -// String attrType = TopiaGeneratorUtil.getSimpleName(attr.getType()); -// String attrName = attr.getName(); -// generateAssociationAccessors(output, attrName, attrType); -// } -// } -// } - - generateAbstractMethods(output, input); - - boolean doGenerateToString = TopiaGeneratorUtil.generateToString(input, - model); - if (doGenerateToString) { - addImport(output, ToStringBuilder.class); - generateToStringMethod(output, input); - } - - String i18nPrefix = TopiaGeneratorUtil.getI18nPrefix(input, model); - if (!StringUtils.isEmpty(i18nPrefix)) { - // generate i18n prefix - generateI18n(output, i18nPrefix, input); - } + setOperationBody(acceptOperation, acceptOperationBody.length() == 0 ? + " " : acceptOperationBody.toString()); } - protected void generateImpl(ObjectModelClass input, - ObjectModelClass output) { - setDocumentation(output, "Implantation des operations pour l'entité " + - input.getName() + "."); - setSuperClass(output, input.getQualifiedName() + "Abstract"); - } + ///////////////////////////////// OLD - // ------------------------------------------------------------------------- - // Interface generation - // ------------------------------------------------------------------------- - - @Deprecated - private void generateInterfaceStaticColumnNames(ObjectModelClass input, - ObjectModelInterface output) { - - for (ObjectModelAttribute attr : input.getAttributes()) { - ObjectModelAttribute reverse = attr.getReverseAttribute(); - if (!attr.isNavigable() && - !TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType( - reverse, model) - && !attr.hasAssociationClass()) { - continue; - } - String attrName; - if (!attr.hasAssociationClass()) { - attrName = attr.getName(); - } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - attrName = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName); - } - String attrColName = getConstantName(attrName); - addAttribute(output, - attrColName, - String.class, - "\"" + attrName + "\"", - ObjectModelModifier.PACKAGE); - } - - //Déclaration des noms des champs des attributs d'une classe d'associations - if (input instanceof ObjectModelAssociationClass) { - ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) input; - for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { - if (attr != null) { - String attrName = attr.getName(); - String attrColName = getConstantName(attrName); - addAttribute(output, - attrColName, - String.class, - "\"" + attrName + "\"", - ObjectModelModifier.PACKAGE); - } - } - } - } - - @Deprecated - private void generateInterfaceOperation(ObjectModelOperation op, - ObjectModelInterface output) { - - - String visibility = op.getVisibility(); - if (op.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 = op.getName(); - String opType = op.getReturnType(); - - ObjectModelOperation op2 = addOperation(output, opName, opType, ObjectModelModifier.PACKAGE); - if (TopiaGeneratorUtil.hasDocumentation(op)) { - setDocumentation(op2, op.getDocumentation()); - } - - for (ObjectModelParameter param : op.getParameters()) { - String paramName = param.getName(); - String paramType = param.getType(); - ObjectModelParameter param2 = addParameter(op2, paramType, paramName); - if (TopiaGeneratorUtil.hasDocumentation(param)) { - setDocumentation(param2, param.getDocumentation()); - } - } - for (String exception : op.getExceptions()) { - addException(op2, exception); - } - } - - @Deprecated - private void generateInterfaceAssociationAccessors(ObjectModelInterface output, - String attrName, - String attrType) { - - ObjectModelOperation op; - ObjectModelParameter param; - - op = addOperation(output, - "set" + StringUtils.capitalize(attrName), - "void", - ObjectModelModifier.PACKAGE); - param = addParameter(op, attrType, "value"); - setDocumentation(param, "La valeur de l'attribut " + attrName + " à positionner."); - - op = addOperation(output, - "get" + StringUtils.capitalize(attrName), - attrType, - ObjectModelModifier.PACKAGE); - setDocumentation(op, "Retourne la valeur de l'attribut " + attrName + "."); - } - - // ------------------------------------------------------------------------- // Abstract generation // ------------------------------------------------------------------------- @@ -1598,6 +1381,7 @@ setOperationBody(op, body.length() == 0 ? " " : body.toString()); } + @Deprecated protected void generateAcceptMethod(ObjectModelClass output, ObjectModelClass clazz) { @@ -1694,40 +1478,6 @@ setOperationBody(op, body.length() == 0 ? " " : body.toString()); } - - private void generateAssociationAccessors(ObjectModelClass output, - String name, String type) { - ObjectModelOperation op; - op = addOperation(output, - "set" + StringUtils.capitalize(name), - "void", - ObjectModelModifier.PUBLIC); - addAnnotation(output, op, Override.class.getSimpleName()); - ObjectModelParameter param = addParameter(op, type, "value"); - setDocumentation(param, "La valeur de l'attribut " + name + - " à positionner."); - setOperationBody(op, "" -/*{ - <%=type%> _oldValue = this.<%=GeneratorUtil.toLowerCaseFirstLetter(name)%>; - fireOnPreWrite("<%=name%>", _oldValue, value); - this.<%=GeneratorUtil.toLowerCaseFirstLetter(name)%> = value; - fireOnPostWrite("<%=name%>", _oldValue, value); -}*/ - ); - - op = addOperation(output, - "get" + StringUtils.capitalize(name), - type, - ObjectModelModifier.PUBLIC); - addAnnotation(output, op, Override.class.getSimpleName()); - setOperationBody(op, "" -/*{ - return <%=GeneratorUtil.toLowerCaseFirstLetter(name)%>; -}*/ - ); - - } - /** * Generate entity methods which have not a public visibility. In this case, they will not be * generated in the EntityInterface, so they will be generated here, in the EntityAbstract @@ -1780,9 +1530,8 @@ buffer.append("\");"); } - // ------------------------------------------------------------------------- - // Impl generation + // Helpers // ------------------------------------------------------------------------- protected boolean isAbstract(ObjectModelClass clazz) { @@ -1820,11 +1569,14 @@ return false; } + protected String getCollectionType(ObjectModelAttribute attribute) { + String result = null; + if (!associationClass && TopiaGeneratorUtil.isNMultiplicity(attribute)) { + result = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attribute); + } + return result; + } - // ------------------------------------------------------------------------- - // Helpers - // ------------------------------------------------------------------------- - protected String getPropertyName(ObjectModelAttribute attribute) { String propertyName = attribute.getName(); if (!associationClass && attribute.hasAssociationClass()) { @@ -1841,6 +1593,13 @@ return propertyType; } + protected ObjectModelOperation createImplOperation(ObjectModelOperation interfaceOperation) { + ObjectModelOperation implOperation = + cloneOperationSignature(interfaceOperation, outputAbstract); + addAnnotation(outputAbstract, implOperation, Override.class.getSimpleName()); + return implOperation; + } + /** * TODO-fdesbois-2010-06-25 : This method can be put in JavaBuilder or ObjectModelTransformerToJava *