r1326 - in topia/branches/generators-refactoring: . topia-persistence/src/main/java/org/codelutin/topia/generator
Author: thimel Date: 2009-01-29 10:42:29 +0000 (Thu, 29 Jan 2009) New Revision: 1326 Added: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java Removed: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/GeneratorUtil.java Modified: topia/branches/generators-refactoring/pom.xml topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOAbstractGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOHelperGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOImplGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DTOGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityAbstractGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityDTOGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityEnumGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityImplGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityProviderGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/InterfaceGenerator.java topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaRelationValidator.java Log: Full generators refactoring - part1 Modified: topia/branches/generators-refactoring/pom.xml =================================================================== --- topia/branches/generators-refactoring/pom.xml 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/pom.xml 2009-01-29 10:42:29 UTC (rev 1326) @@ -165,7 +165,7 @@ <labs.project>topia</labs.project> <!-- libs version --> - <generator.version>0.63</generator.version> + <generator.version>0.64-SNAPSHOT</generator.version> <processor.version>0.16</processor.version> <lutinutil.version>1.0</lutinutil.version> <xmlrpc.version>3.1</xmlrpc.version> Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOAbstractGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOAbstractGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOAbstractGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -37,8 +37,8 @@ import java.util.regex.Pattern; import org.codelutin.generator.Generator; +import org.codelutin.generator.GeneratorUtil; import org.codelutin.generator.ObjectModelGenerator; -import org.codelutin.generator.Util; import org.codelutin.generator.models.object.ObjectModelAssociationClass; import org.codelutin.generator.models.object.ObjectModelAttribute; import org.codelutin.generator.models.object.ObjectModelClass; @@ -71,11 +71,11 @@ @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { return; } - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } @@ -86,15 +86,14 @@ import org.codelutin.topia.TopiaException; import org.codelutin.topia.framework.TopiaContextImplementor; }*/ - String securityCreate = clazz.getTagValue("securityCreate"); - String securityLoad = clazz.getTagValue("securityLoad"); - String securityUpdate = clazz.getTagValue("securityUpdate"); - String securityDelete = clazz.getTagValue("securityDelete"); + boolean enableSecurity = ( + clazz.hasTagValue(TopiaGeneratorUtil.TAG_SECURITY_CREATE) || + clazz.hasTagValue(TopiaGeneratorUtil.TAG_SECURITY_LOAD) || + clazz.hasTagValue(TopiaGeneratorUtil.TAG_SECURITY_UPDATE) || + clazz.hasTagValue(TopiaGeneratorUtil.TAG_SECURITY_DELETE) + ); - if(GeneratorUtil.notEmpty(securityCreate) || - GeneratorUtil.notEmpty(securityLoad) || - GeneratorUtil.notEmpty(securityUpdate) || - GeneratorUtil.notEmpty(securityDelete)) { + if (enableSecurity) { /*{ import java.util.ArrayList; import java.security.Permission; @@ -107,19 +106,20 @@ import static org.codelutin.topia.taas.TaasUtil.UPDATE; }*/ } + String clazzName = clazz.getName(); /*{ /** - * Implantation DAO pour l'entité <%=Util.toUpperCaseFirstLetter(clazz.getName())%>. + * Implantation DAO pour l'entité <%=GeneratorUtil.toUpperCaseFirstLetter(clazz.getName())%>. * Cette classe contient une implantation de TopiaDAO a laquel elle peut * deleguer des traitements * *) -public abstract class <%=clazz.getName()%>DAOAbstract<E extends <%=clazz.getName()%>> extends }*/ +public abstract class <%=clazzName%>DAOAbstract<E extends <%=clazzName%>> extends }*/ String extendClass = ""; - for (Iterator i=clazz.getSuperclasses().iterator(); i.hasNext();) { - ObjectModelClassifier parent = (ObjectModelClassifier)i.next(); + for (Iterator<ObjectModelClass> i=clazz.getSuperclasses().iterator(); i.hasNext();) { + ObjectModelClassifier parent = i.next(); extendClass += parent.getQualifiedName(); - if (parent.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (parent.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { extendClass += "DAOImpl<E>"; } if (i.hasNext()) { @@ -133,44 +133,51 @@ /*{<%=extendClass%> implements org.codelutin.topia.persistence.TopiaDAO<E> { public Class<E> getEntityClass() { - return (Class<E>)<%=clazz.getName()%>.class; + return (Class<E>)<%=clazzName%>.class; } public void delete(E entity) throws TopiaException { }*/ String providerFQN = getProperty("defaultPackage") + "." + model.getName() + "EntityEnum.getImplementationClass"; for (ObjectModelAttribute attr : clazz.getAttributes()) { + String attrType = attr.getType(); + String reverseAttrName = attr.getReverseAttributeName(); ObjectModelAttribute reverse = attr.getReverseAttribute(); if (!attr.hasAssociationClass() && reverse != null && reverse.isNavigable() - && Util.isNMultiplicity(attr) && Util.isNMultiplicity(reverse)) { + && GeneratorUtil.isNMultiplicity(attr) && GeneratorUtil.isNMultiplicity(reverse)) { // On doit absolument supprimer pour les relations many-to-many // le this de la collection de l'autre cote + String attrDBName = TopiaGeneratorUtil.getDBName(attr); + String attrClassifierDBName = TopiaGeneratorUtil.getDBName(attr.getClassifier()); + String attrJoinTableName = TopiaGeneratorUtil.getManyToManyTableName(attr); + String attrReverseDBName = TopiaGeneratorUtil.getReverseDBName(attr); /*{ { - List<<%=attr.getType()%>> list = getContext().getHibernate().createSQLQuery( + List<<%=attrType%>> list = getContext().getHibernate().createSQLQuery( "SELECT {main.*} " + - "from <%=GeneratorUtil.getDBName(attr.getClassifier())%> main, <%=GeneratorUtil.getManyToManyTableName(attr)%> secondary " + - "where main.topiaid=secondary.<%=GeneratorUtil.getDBName(attr)%>" + - " and secondary.<%=GeneratorUtil.getReverseDBName(attr)%>='"+entity.getTopiaId()+"'") - .addEntity("main", <%=providerFQN%>(<%=attr.getType()%>.class)).list(); - for (<%=attr.getType()%> item : list) { - item.remove<%=Util.capitalize(attr.getReverseAttributeName())%>(entity); + "from <%=attrClassifierDBName%> main, <%=attrJoinTableName%> secondary " + + "where main.topiaid=secondary.<%=attrDBName%>" + + " and secondary.<%=attrReverseDBName%>='" + entity.getTopiaId() + "'") + .addEntity("main", <%=providerFQN%>(<%=attrType%>.class)).list(); + for (<%=attrType%> item : list) { + item.remove<%=TopiaGeneratorUtil.capitalize(reverseAttrName)%>(entity); } } }*/ } else if (!attr.hasAssociationClass() && reverse != null - && reverse.isNavigable() && !Util.isNMultiplicity(reverse)) { - // On doit mettre a null les attributs qui ont cette objet sur les + && reverse.isNavigable() && !GeneratorUtil.isNMultiplicity(reverse)) { + // On doit mettre a null les attributs qui ont cet objet sur les // autres entites en one-to-* // TODO peut-etre qu'hibernate est capable de faire ca tout seul ? + // THIMEL: J'ai remplacé reverse.getName() par reverseAttrName sans certitude /*{ { - List<<%=attr.getType()%>> list = getContext() - .getDAO(<%=attr.getType()%>.class) - .findAllByProperties("<%=reverse.getName()%>", entity); - for (<%=attr.getType()%> item : list) { - item.set<%=Util.capitalize(attr.getReverseAttributeName())%>(null); + List<<%=attrType%>> list = getContext() + .getDAO(<%=attrType%>.class) + .findAllByProperties("<%=reverseAttrName%>", entity); + for (<%=attrType%> item : list) { + item.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(null); }*/ if(attr.isAggregate()){ /*{ @@ -189,7 +196,7 @@ } /** - * Retourne tous les <%=clazz.getName()%> + * Retourne tous les <%=clazzName%> * @return une liste *) public List<E> findAll() throws TopiaException { @@ -197,42 +204,37 @@ return result; } }*/ - for (Iterator it = clazz.getAttributes().iterator(); it.hasNext();) { - ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + for (ObjectModelAttribute attr : clazz.getAttributes()) { if (!attr.isNavigable()) { continue; } + String attrName = attr.getName(); /*{ /** - * Recherche sur l'attribut <%=attr.getName()%> + * Recherche sur l'attribut <%=attrName%> *) }*/ - if (!Util.isNMultiplicity(attr)) { + if (!GeneratorUtil.isNMultiplicity(attr)) { generateNoNMultiplicity(output, attr, false); } else { - generateNMultiplicity(output, attr, false); + generateNMultiplicity(output, attr); } } if (clazz instanceof ObjectModelAssociationClass) { ObjectModelAssociationClass assocClass = (ObjectModelAssociationClass)clazz; - Iterator it = assocClass.getParticipantsAttributes().iterator(); - while (it.hasNext()) { - ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + for (ObjectModelAttribute attr : assocClass.getParticipantsAttributes()) { if (attr != null) { - if (!Util.isNMultiplicity(attr)) { + if (!GeneratorUtil.isNMultiplicity(attr)) { generateNoNMultiplicity(output, attr, true); } else { - generateNMultiplicity(output, attr, true); + generateNMultiplicity(output, attr); } } } } - if(GeneratorUtil.notEmpty(securityCreate) || - GeneratorUtil.notEmpty(securityLoad) || - GeneratorUtil.notEmpty(securityUpdate) || - GeneratorUtil.notEmpty(securityDelete)) { + if(enableSecurity) { /*{ /** * Retourne les permissions a verifier pour l'acces a l'entite pour le service Taas @@ -244,27 +246,30 @@ List<Permission> resultPermissions = new ArrayList<Permission>(); if ((actions & CREATE) == CREATE) { }*/ - generateSecurity(output, clazz, securityCreate); + generateSecurity(output, clazz, TopiaGeneratorUtil.TAG_SECURITY_CREATE); /*{ } if ((actions & LOAD) == LOAD) { }*/ - generateSecurity(output, clazz, securityLoad); + generateSecurity(output, clazz, TopiaGeneratorUtil.TAG_SECURITY_LOAD); /*{ } if ((actions & UPDATE) == UPDATE) { }*/ - generateSecurity(output, clazz, securityUpdate); + generateSecurity(output, clazz, TopiaGeneratorUtil.TAG_SECURITY_UPDATE); /*{ } if ((actions & DELETE) == DELETE) { }*/ - generateSecurity(output, clazz, securityDelete); + generateSecurity(output, clazz, TopiaGeneratorUtil.TAG_SECURITY_DELETE); /*{ } return resultPermissions; } - + +}*/ + // THIMEL : Le code suivant doit pouvoir être déplacé dans DAODelegator ? +/*{ /** * Retourne les permissions a verifier pour l'acces a l'entite pour le service Taas * @param topiaId topiaId d'une entite @@ -276,7 +281,7 @@ protected List<Permission> getRequestPermission(String topiaId, int actions, String query, Class daoClass) throws TopiaException { TopiaContextImplementor context = getContext(); List<String> result = context.find(query, "id", topiaId); - + List<Permission> resultPermissions = new ArrayList<Permission>(); for (String topiaIdPermission : result) { TopiaDAO dao = context.getDAO(daoClass); @@ -293,12 +298,13 @@ }*/ } /*{ -} //<%=clazz.getName()%>DAOAbstract +} // <%=clazz.getName()%>DAOAbstract }*/ } - private void generateSecurity(Writer output, ObjectModelClass clazz, String security) throws IOException { - if(security != null) { + private void generateSecurity(Writer output, ObjectModelClass clazz, String securityTagName) throws IOException { + if (clazz.hasTagValue(securityTagName)) { + String security = clazz.getTagValue(securityTagName); Pattern propertiesPattern = Pattern .compile("((?:[_a-zA-Z0-9]+\\.)+(?:_?[A-Z][_a-zA-Z0-9]*\\.)+)attribute\\.(?:([_a-z0-9][_a-zA-Z0-9]*))#(?:(create|load|update|delete))"); String[] valuesSecurity = security.split(":"); @@ -323,91 +329,99 @@ query = "select at.topiaId from " + className + " at inner join at." + attributeName + " cl where cl.topiaId = :id"; daoClass = className; } -/*{ resultPermissions.addAll(getRequestPermission(topiaId, +/*{ resultPermissions.addAll(getRequestPermission(topiaId, <%=actions%>, "<%=query%>", <%=daoClass%>.class)); }*/ } } else { -/*{ return null; +/*{ return null; }*/ } } protected void generateNoNMultiplicity(Writer output, ObjectModelAttribute attr, boolean isAssoc) throws IOException { - String propertyName = attr.getName(); + String attrName = attr.getName(); + String attrType = attr.getType(); + String propertyName = attrName; if (!isAssoc && attr.hasAssociationClass()) { - propertyName = GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName()) + "." + propertyName; + propertyName = TopiaGeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName()) + "." + propertyName; } /*{ /** * Retourne le premier élément trouvé ayant comme valeur pour l'attribut - * <%=attr.getName()%> le paramètre - * @param v la valeur que doit avoir <%=attr.getName()%> + * <%=attrName%> le paramètre + * @param v la valeur que doit avoir <%=attrName%> * @return un element ou null *) - public E findBy<%=GeneratorUtil.capitalize(attr.getName())%>(<%=attr.getType()%> v) throws TopiaException { + public E findBy<%=TopiaGeneratorUtil.capitalize(attrName)%>(<%=attrType%> v) throws TopiaException { E result = getParentDAO().findByProperty("<%=propertyName%>", v); return result; } + /** * Retourne les éléments ayant comme valeur pour l'attribut - * <%=attr.getName()%> le paramètre - * @param v la valeur que doit avoir <%=attr.getName()%> + * <%=attrName%> le paramètre + * @param v la valeur que doit avoir <%=attrName%> * @return une liste *) - public List<E> findAllBy<%=GeneratorUtil.capitalize(attr.getName())%>(<%=attr.getType()%> v) throws TopiaException { + public List<E> findAllBy<%=TopiaGeneratorUtil.capitalize(attrName)%>(<%=attrType%> v) throws TopiaException { List<E> result = getParentDAO().findAllByProperty("<%=propertyName%>", v); return result; } }*/ if (attr.hasAssociationClass()) { + String assocClassName = attr.getAssociationClass().getName(); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); /*{ /** * Retourne le premier élément trouvé ayant comme valeur pour l'attribut - * <%=GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> le paramètre - * @param value la valeur que doit avoir <%=GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> + * <%=TopiaGeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> le paramètre + * @param value la valeur que doit avoir <%=TopiaGeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> * @return un element ou null *) - public E findBy<%=GeneratorUtil.capitalize(attr.getAssociationClass().getName())%>(<%=attr.getAssociationClass().getQualifiedName()%> value) throws TopiaException { - E result = getParentDAO().findByProperty("<%=GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>", value); + public E findBy<%=TopiaGeneratorUtil.capitalize(assocClassName)%>(<%=assocClassFQN%> value) throws TopiaException { + E result = getParentDAO().findByProperty("<%=TopiaGeneratorUtil.toLowerCaseFirstLetter(assocClassName)%>", value); return result; } + /** * Retourne les éléments ayant comme valeur pour l'attribut - * <%=GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> le paramètre - * @param value la valeur que doit avoir <%=GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> + * <%=TopiaGeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> le paramètre + * @param value la valeur que doit avoir <%=TopiaGeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> * @return une liste *) - public List<E> findAllBy<%=GeneratorUtil.capitalize(attr.getAssociationClass().getName())%>(<%=attr.getAssociationClass().getQualifiedName()%> value) throws TopiaException { - List<E> result = getParentDAO().findAllByProperty("<%=GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>", value); + public List<E> findAllBy<%=TopiaGeneratorUtil.capitalize(assocClassName)%>(<%=assocClassFQN%> value) throws TopiaException { + List<E> result = getParentDAO().findAllByProperty("<%=TopiaGeneratorUtil.toLowerCaseFirstLetter(assocClassName)%>", value); return result; } }*/ } } - - protected void generateNMultiplicity(Writer output, ObjectModelAttribute attr, boolean isAssoc) throws IOException { + + protected void generateNMultiplicity(Writer output, ObjectModelAttribute attr) throws IOException { + String attrName = attr.getName(); + String attrType = attr.getType(); /*{ /** * Retourne le premier élément trouvé dont l'attribut - * <%=attr.getName()%> contient le paramètre - * @param v la valeur que doit contenir <%=attr.getName()%> + * <%=attrName%> contient le paramètre + * @param v la valeur que doit contenir <%=attrName%> * @return un element ou null *) - public E findContains<%=GeneratorUtil.capitalize(attr.getName())%>(<%=attr.getType()%> ... v) throws TopiaException { - E result = getParentDAO().findContainsProperties("<%=GeneratorUtil.toLowerCaseFirstLetter(attr.getName())%>", Arrays.asList(v)); + public E findContains<%=TopiaGeneratorUtil.capitalize(attrName)%>(<%=attrType%> ... v) throws TopiaException { + E result = getParentDAO().findContainsProperties("<%=TopiaGeneratorUtil.toLowerCaseFirstLetter(attrName)%>", Arrays.asList(v)); return result; } /** * Retourne les éléments trouvé dont l'attribut - * <%=attr.getName()%> contient le paramètre - * @param v la valeur que doit contenir <%=attr.getName()%> + * <%=attrName%> contient le paramètre + * @param v la valeur que doit contenir <%=attrName%> * @return une liste *) - public List<E> findAllContains<%=GeneratorUtil.capitalize(attr.getName())%>(<%=attr.getType()%> ... v) throws TopiaException { - List<E> results = getParentDAO().findAllContainsProperties("<%=GeneratorUtil.toLowerCaseFirstLetter(attr.getName())%>", Arrays.asList(v)); + public List<E> findAllContains<%=TopiaGeneratorUtil.capitalize(attrName)%>(<%=attrType%> ... v) throws TopiaException { + List<E> results = getParentDAO().findAllContainsProperties("<%=TopiaGeneratorUtil.toLowerCaseFirstLetter(attrName)%>", Arrays.asList(v)); return results; } }*/ Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -54,14 +54,16 @@ @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { return; } - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } + String clazzName = clazz.getName(); + String clazzFQN = clazz.getQualifiedName(); /*{package <%=clazz.getPackageName()%>; /** @@ -69,11 +71,11 @@ * Cette classe est marque finale car l'heritage entre les DAO se fait * sur les DOAImpl, c-a-d que DAOAbstract peut etendre le DAOImpl *) -public final class <%=clazz.getName()%>DAO extends <%=clazz.getQualifiedName()%>DAOImpl<<%=clazz.getName()%>> { +public final class <%=clazzName%>DAO extends <%=clazzFQN%>DAOImpl<<%=clazzName%>> { }*/ -/*{} //<%=clazz.getName()%>DAO +/*{} //<%=clazzName%>DAO }*/ } Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOHelperGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOHelperGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOHelperGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -71,22 +71,23 @@ @Override public void generateFromModel(Writer output, ObjectModel model) throws IOException { - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } + String modelName = model.getName(); /*{package <%=getProperty("defaultPackage")%>; import org.codelutin.topia.TopiaContext; import org.codelutin.topia.framework.TopiaContextImplementor; import org.codelutin.topia.TopiaException; -public class <%=model.getName()%>DAOHelper { +public class <%=modelName%>DAOHelper { }*/ String entitiesList = ""; - List<ObjectModelClass> classes = GeneratorUtil.getEntityClasses(model,true); + List<ObjectModelClass> classes = TopiaGeneratorUtil.getEntityClasses(model,true); if (classes.isEmpty()) { /*{ public static final String entitiesList = ""; @@ -96,10 +97,11 @@ public static final String entitiesList = "" + }*/ - for (Iterator i=classes.iterator(); i.hasNext();) { + for (Iterator<ObjectModelClass> i=classes.iterator(); i.hasNext();) { ObjectModelClass clazz = (ObjectModelClass)i.next(); - entitiesList += GeneratorUtil.getDOType(clazz, model) + (i.hasNext()?",":""); -/*{ "<%=GeneratorUtil.getDOType(clazz, model)%><%=(i.hasNext()?",\" +":"\";")%> + String doType = TopiaGeneratorUtil.getDOType(clazz, model); + entitiesList += doType + (i.hasNext()?",":""); +/*{ "<%=doType%><%=(i.hasNext()?",\" +":"\";")%> }*/ } } @@ -108,22 +110,23 @@ } /*{ /** *) - protected <%=model.getName()%>DAOHelper() { + protected <%=modelName%>DAOHelper() { } }*/ - for (ObjectModelClass objectModelClass : classes) { - ObjectModelClass clazz = objectModelClass; + for (ObjectModelClass clazz : classes) { + String clazzFQN = clazz.getQualifiedName(); + String clazzName = clazz.getName(); /*{ - static public <%=clazz.getQualifiedName()%>DAO get<%=clazz.getName()%>DAO(TopiaContext context) throws TopiaException { + static public <%=clazzFQN%>DAO get<%=clazzName%>DAO(TopiaContext context) throws TopiaException { TopiaContextImplementor ci = (TopiaContextImplementor)context; - <%=clazz.getQualifiedName()%>DAO result = (<%=clazz.getQualifiedName()%>DAO)ci.getDAO(<%=clazz.getQualifiedName()%>.class); + <%=clazzFQN%>DAO result = (<%=clazzFQN%>DAO)ci.getDAO(<%=clazzFQN%>.class); return result; } }*/ } /*{ -} +} //<%=modelName%>DAOHelper }*/ } - -} // DAOHelperGenerator + +} //DAOHelperGenerator Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOImplGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOImplGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DAOImplGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -54,28 +54,28 @@ @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { return; } - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } + String clazzName = clazz.getName(); + String clazzFQN = clazz.getQualifiedName(); /*{package <%=clazz.getPackageName()%>; /** - * Implantation du DAO pour l'entité <%=clazz.getName()%>. + * Implantation du DAO pour l'entité <%=clazzName%>. * L'utilisateur peut remplacer cette classe par la sienne en la mettant * simplement dans ces sources. Cette classe générée sera alors simplement - * écrasé. + * écrasée. *) -public class <%=clazz.getName()%>DAOImpl<E extends <%=clazz.getName()%>> extends <%=clazz.getQualifiedName()%>DAOAbstract<E> { +public class <%=clazzName%>DAOImpl<E extends <%=clazzName%>> extends <%=clazzFQN%>DAOAbstract<E> { +} //<%=clazzName%>DAO }*/ - -/*{} //<%=clazz.getName()%>DAO -}*/ } } //DAOGenerator Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DTOGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DTOGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/DTOGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -29,7 +29,7 @@ package org.codelutin.topia.generator; -import static org.codelutin.topia.generator.GeneratorUtil.TAG_ANNOTATION; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.TAG_ANNOTATION; import java.io.File; import java.io.IOException; @@ -39,7 +39,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.generator.ObjectModelGenerator; -import org.codelutin.generator.Util; +import org.codelutin.generator.GeneratorUtil; import org.codelutin.generator.models.object.ObjectModelAssociationClass; import org.codelutin.generator.models.object.ObjectModelAttribute; import org.codelutin.generator.models.object.ObjectModelClass; @@ -68,28 +68,31 @@ public boolean isDTO(String type) { ObjectModelClassifier clazz = model.getClassifier(type); - return clazz != null && clazz.hasStereotype(GeneratorUtil.STEREOTYPE_DTO); + return clazz != null && clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_DTO); } @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_DTO)) { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_DTO)) { return; } - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } + String clazzName = clazz.getName(); /*{package <%=clazz.getPackageName()%>; import org.apache.commons.lang.builder.ToStringBuilder; import java.beans.PropertyChangeListener; +import java.util.List; +import java.util.Collection; /** - * DTO implantation for <%=Util.capitalize(clazz.getName())%> entity. + * DTO implantation for <%=GeneratorUtil.capitalize(clazzName)%> entity. *) -public class <%=clazz.getName()%>DTO }*/ +public class <%=clazzName%>DTO}*/ /* * Définition de la super classe : il ne doit y avoir qu'une @@ -99,7 +102,7 @@ Iterator<ObjectModelClass> j = clazz.getSuperclasses().iterator(); if (j.hasNext()) { ObjectModelClassifier parent = j.next(); - if (parent.hasStereotype(GeneratorUtil.STEREOTYPE_DTO)) { + if (parent.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_DTO)) { extendClass += parent.getName() + "DTO"; } else { extendClass += parent.getName(); @@ -107,16 +110,16 @@ } if (extendClass.length() > 0) { - /*{extends <%=extendClass%> }*/ +/*{ extends <%=extendClass%>}*/ } /* * Définition des interfaces */ - /*{implements java.io.Serializable }*/ +/*{ implements java.io.Serializable}*/ String implInterface = ""; for (Iterator<ObjectModelInterface> i=clazz.getInterfaces().iterator(); i.hasNext();) { ObjectModelClassifier parentInterface = i.next(); - if (parentInterface.hasStereotype(GeneratorUtil.STEREOTYPE_DTO)) { + if (parentInterface.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_DTO)) { implInterface += parentInterface.getName() + "DTO"; } else { implInterface += parentInterface.getName(); @@ -127,7 +130,7 @@ } } if (implInterface.length() > 0) { - /*{,<%=implInterface%> { +/*{, <%=implInterface%> { }*/ } else { @@ -136,7 +139,8 @@ }*/ } - String svUID = GeneratorUtil.findTagValue("dto-serialVersionUID", clazz, model); + String svUID = TopiaGeneratorUtil.findTagValue("dto-serialVersionUID", clazz, model); + // TODO Calculer un serialVersionUID si il n'y en a pas if (svUID != null) { /*{ public static final long serialVersionUID = <%=svUID%>; @@ -153,7 +157,7 @@ continue; } - if (GeneratorUtil.hasDocumentation(attr)) { + if (TopiaGeneratorUtil.hasDocumentation(attr)) { /*{ /** * <%=attr.getDocumentation()%> *) @@ -164,22 +168,52 @@ /*{ <%=annotation%> }*/ } - if (!Util.isNMultiplicity(attr)) { + String attrName = attr.getName(); + String attrVisibility = attr.getVisibility(); + String attrType = attr.getType(); + if (!GeneratorUtil.isNMultiplicity(attr)) { if (!attr.hasAssociationClass()) { -/*{ <%=attr.getVisibility()%> <%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> <%=attr.getName()%>; + if (isDTO(attrType)) { + attrType += "DTO"; + } +/*{ <%=attrVisibility%> <%=attrType%> <%=attrName%>; }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ <%=attr.getVisibility()%> <%=attr.getAssociationClass().getQualifiedName()%>DTO <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); +/*{ <%=attrVisibility%> <%=assocClassFQN%>DTO <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; }*/ } } else { if (!attr.hasAssociationClass()) { -/*{ <%=attr.getVisibility()%> <%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%>> <%=attr.getName()%>; + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<"; + } else { + nMultType = "Collection<"; + } + nMultType += attrType; + if (isDTO(attrType)) { + nMultType += "DTO"; + } + nMultType += ">"; +/*{ <%=attrVisibility%> <%=nMultType%> <%=attrName%>; }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ <%=attr.getVisibility()%> <%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>DTO> <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<"; + } else { + nMultType = "Collection<"; + } + nMultType += assocClassFQN; + if (isDTO(attrType)) { + nMultType += "DTO"; + } + nMultType += ">"; +/*{ <%=attrVisibility%> <%=nMultType%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; }*/ } } @@ -188,34 +222,36 @@ //Déclaration des attributs d'une classe d'associations if (clazz instanceof ObjectModelAssociationClass) { ObjectModelAssociationClass assoc = (ObjectModelAssociationClass)clazz; - for (Object o : assoc.getParticipantsAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; + for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { -/*{ <%=attr.getVisibility()%> <%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>; + String attrName = attr.getName(); + String attrVisibility = attr.getVisibility(); + String attrType = attr.getType(); + if (isDTO(attrType)) { + attrType += "DTO"; + } +/*{ <%=attrVisibility%> <%=attrType%> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>; }*/ } } } /*{ - protected java.beans.PropertyChangeSupport p; + protected java.beans.PropertyChangeSupport p; -}*/ - -/*{ /** - * Default constructor of <%=clazz.getName()%>DTO. + /** + * Default constructor of <%=clazzName%>DTO. *) - public <%=clazz.getName()%>DTO() { p = new java.beans.PropertyChangeSupport(this); } + public <%=clazzName%>DTO() { + p = new java.beans.PropertyChangeSupport(this); + } -}*/ - -/*{ /** - * Constructor of <%=clazz.getName()%>DTO with all parameters. + /** + * Constructor of <%=clazzName%>DTO with all parameters. *) - public <%=clazz.getName()%>DTO(}*/ + public <%=clazzName%>DTO(}*/ boolean une_fois = true; - for (Object o : clazz.getAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; + for (ObjectModelAttribute attr : clazz.getAttributes()) { if (!(attr.isNavigable() || attr.hasAssociationClass())) { @@ -225,22 +261,43 @@ if (une_fois) { une_fois = false; } else { - /*{, }*/ +/*{, }*/ } - if (!Util.isNMultiplicity(attr)) { + String attrName = attr.getName(); + String attrVisibility = attr.getVisibility(); + String attrType = attr.getType(); + String attrTypeDTO = attr.getType(); + if (isDTO(attrType)) { + attrTypeDTO += "DTO"; + } + if (!GeneratorUtil.isNMultiplicity(attr)) { if (!attr.hasAssociationClass()) { -/*{<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> <%=attr.getName()%>}*/ +/*{<%=attrTypeDTO%> <%=attrName%>}*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{<%=attr.getAssociationClass().getQualifiedName()%>DTO <%=Util.toLowerCaseFirstLetter(assocAttrName)%>}*/ + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); +/*{<%=assocClassFQN%>DTO <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>}*/ } } else { if (!attr.hasAssociationClass()) { -/*{<%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%>> <%=attr.getName()%>}*/ + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<" + attrTypeDTO + ">"; + } else { + nMultType = "Collection<" + attrTypeDTO + ">"; + } +/*{<%=nMultType%> <%=attrName%>}*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{<%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>DTO> <%=Util.toLowerCaseFirstLetter(assocAttrName)%>}*/ + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<" + assocClassFQN + "DTO>"; + } else { + nMultType = "Collection<" + assocClassFQN + "DTO>"; + } +/*{<%=nMultType%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>}*/ } } @@ -248,34 +305,35 @@ } // end for /*{ ) { - this(); + this(); }*/ - for (Object o : clazz.getAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; + for (ObjectModelAttribute attr : clazz.getAttributes()) { if (!(attr.isNavigable() || attr.hasAssociationClass())) { continue; } - if (!Util.isNMultiplicity(attr)) { + String attrName = attr.getName(); + + if (!GeneratorUtil.isNMultiplicity(attr)) { if (!attr.hasAssociationClass()) { -/*{ this.<%=attr.getName()%> = <%=attr.getName()%>; +/*{ this.<%=attrName%> = <%=attrName%>; }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); +/*{ this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; }*/ } } else { if (!attr.hasAssociationClass()) { -/*{ this.<%=attr.getName()%> = <%=attr.getName()%>; +/*{ this.<%=attrName%> = <%=attrName%>; }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); +/*{ this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; }*/ } } @@ -311,95 +369,131 @@ if (!attr.isNavigable()) { continue; } - if (!Util.isNMultiplicity(attr)) { + + String attrName = attr.getName(); + String attrType = attr.getType(); + String attrTypeDTO = attr.getType(); + if (isDTO(attrType)) { + attrTypeDTO += "DTO"; + } + String reverseAttrName = reverse.getName(); + + if (!GeneratorUtil.isNMultiplicity(attr)) { if (!attr.hasAssociationClass()) { -/*{ public void set<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> value) { - <%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> oldValue = this.<%=attr.getName()%>; - this.<%=attr.getName()%> = value; - p.firePropertyChange("<%=attr.getName()%>", oldValue, value); +/*{ public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrTypeDTO%> value) { + <%=attrTypeDTO%> oldValue = this.<%=attrName%>; + this.<%=attrName%> = value; + p.firePropertyChange("<%=attrName%>", oldValue, value); } - public <%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> get<%=Util.capitalize(attr.getName())%>() { - return <%=attr.getName()%>; + public <%=attrTypeDTO%> get<%=GeneratorUtil.capitalize(attrName)%>() { + return <%=attrName%>; } }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } -/*{ public void set<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%>DTO association) { - <%=attr.getAssociationClass().getQualifiedName()%>DTO oldAssocation= this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>; - this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = association; - p.firePropertyChange("<%=attr.getName()%>", oldAssocation, assocation); +/*{ public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%>DTO association) { + <%=assocClassFQN%>DTO oldAssocation = this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; + this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = association; + p.firePropertyChange("<%=attrName%>", oldAssocation, assocation); } - public <%=attr.getAssociationClass().getQualifiedName()%>DTO get<%=Util.capitalize(assocAttrName)%>() { - return <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + public <%=assocClassFQN%>DTO get<%=GeneratorUtil.capitalize(assocAttrName)%>() { + return <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; } }*/ } } else { //NMultiplicity if (!attr.hasAssociationClass()) { //Méthodes remplacées par des accesseurs sur les classes d'assoc -/*{ public void set<%=Util.capitalize(attr.getName())%>(<%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%>> values) { - <%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%>> oldValues = this.<%=attr.getName()%>; - this.<%=attr.getName()%> = values; - p.firePropertyChange("<%=attr.getName()%>", oldValues, values); + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<" + attrTypeDTO + ">"; + } else { + nMultType = "Collection<" + attrTypeDTO + ">"; + } +/*{ public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=nMultType%> values) { + <%=nMultType%> oldValues = this.<%=attrName%>; + this.<%=attrName%> = values; + p.firePropertyChange("<%=attrName%>", oldValues, values); } }*/ //AddChild -/*{ public <%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> addChild(<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> <%=attr.getName()%>) { - this.<%=attr.getName()%>.add(<%=attr.getName()%>); +/*{ public <%=attrTypeDTO%> addChild(<%=attrTypeDTO%> <%=attrName%>) { + this.<%=attrName%>.add(<%=attrName%>); }*/ if (reverse.isNavigable()) { -/*{ <%=attr.getName()%>.set<%=Util.capitalize(reverse.getName())%>(this); +/*{ <%=attrName%>.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(this); }*/ } -/*{ return <%=attr.getName()%>; +/*{ return <%=attrName%>; } }*/ //RemoveChild -/*{ public void removeChild(<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> <%=attr.getName()%>) { - this.<%=attr.getName()%>.remove(<%=attr.getName()%>); +/*{ public void removeChild(<%=attrTypeDTO%> <%=attrName%>) { + this.<%=attrName%>.remove(<%=attrName%>); }*/ if (reverse.isNavigable()) { -/*{ <%=attr.getName()%>.set<%=Util.capitalize(reverse.getName())%>(null); +/*{ <%=attrName%>.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(null); }*/ } /*{ } }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<" + assocClassFQN + "DTO>"; + } else { + nMultType = "Collection<" + assocClassFQN + "DTO>"; + } if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } -/*{ public void set<%=Util.capitalize(assocAttrName)%>(<%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>DTO> values) { - <%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>DTO> oldValues =this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>; - this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = values; - p.firePropertyChange("<%=attr.getName()%>", oldValues, values); +/*{ public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=nMultType%> values) { + <%=nMultType%> oldValues = this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; + this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = values; + p.firePropertyChange("<%=attrName%>", oldValues, values); } }*/ } if (!attr.hasAssociationClass()) { -/*{ public <%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%>> get<%=Util.capitalize(attr.getName())%>() { - return this.<%=attr.getName()%>; + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<" + attrTypeDTO + ">"; + } else { + nMultType = "Collection<" + attrTypeDTO + ">"; + } +/*{ public <%=nMultType%> get<%=GeneratorUtil.capitalize(attrName)%>() { + return this.<%=attrName%>; } }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<" + assocClassFQN + "DTO>"; + } else { + nMultType = "Collection<" + assocClassFQN + "DTO>"; + } if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } -/*{ public <%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>DTO> get<%=Util.capitalize(assocAttrName)%>() { - return this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>; +/*{ public <%=nMultType%> get<%=GeneratorUtil.capitalize(assocAttrName)%>() { + return this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; } }*/ @@ -407,26 +501,27 @@ } } -/*{ +/*{ @Override public String toString() { String result = new ToStringBuilder(this). }*/ - for (Object o : clazz.getAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; + for (ObjectModelAttribute attr : clazz.getAttributes()) { if (!(attr.isNavigable() || attr.hasAssociationClass())) { continue; } + //FIXME possibilité de boucles (non directes) ObjectModelClass attrEntity = null; if (model.hasClass(attr.getType())) { attrEntity = model.getClass(attr.getType()); } - boolean isDTO = (attrEntity != null && attrEntity.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)); + boolean isDTO = (attrEntity != null && attrEntity.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)); //THIMEL : STEREOTYPE ENTITY ??? ObjectModelAttribute reverse = attr.getReverseAttribute(); if ((isDTO && (reverse == null || !reverse.isNavigable()) && !attr.hasAssociationClass()) || (!isDTO)) { -/*{ append("<%=attr.getName()%>", this.<%=attr.getName()%>). + String attrName = attr.getName(); +/*{ append("<%=attrName%>", this.<%=attrName%>). }*/ } } Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityAbstractGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityAbstractGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityAbstractGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -33,14 +33,12 @@ import org.apache.commons.logging.LogFactory; import org.codelutin.generator.Generator; import org.codelutin.generator.ObjectModelGenerator; -import org.codelutin.generator.Util; +import org.codelutin.generator.GeneratorUtil; import org.codelutin.generator.models.object.ObjectModelAssociationClass; import org.codelutin.generator.models.object.ObjectModelAttribute; import org.codelutin.generator.models.object.ObjectModelClass; -import org.codelutin.generator.models.object.ObjectModelClassifier; -import static org.codelutin.topia.generator.GeneratorUtil.TAG_ANNOTATION; -import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; -import static org.codelutin.topia.generator.GeneratorUtil.shouldBeAbstract; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.shouldBeAbstract; import org.codelutin.topia.persistence.TopiaEntityAbstract; import java.io.File; @@ -59,6 +57,14 @@ private static final Log log = LogFactory .getLog(EntityAbstractGenerator.class); + // TODO THIMEL : Je pense qu'il faudrait que l'EntityInterfaceGenerator + // hérite de ce générateur, avec generateBody qui renvoie false, et que dans + // ce générateur, on ne génère pas les corps de méthode, etc ... quand + // generateBoody() renvoie false + protected boolean generateBody() { + return true; + } + public EntityAbstractGenerator() { super(); } @@ -74,14 +80,16 @@ @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { return; } - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } + String clazzName = clazz.getName(); + String clazzFQN = clazz.getQualifiedName(); /*{package <%=clazz.getPackageName()%>; import org.apache.commons.lang.builder.ToStringBuilder; @@ -92,12 +100,13 @@ import java.util.List; import java.util.ArrayList; import java.util.Collection; +import java.io.Serializable; /** - * Implantation POJO pour l'entité <%=Util.capitalize(clazz.getName())%>. + * Implantation POJO pour l'entité <%=GeneratorUtil.capitalize(clazzName)%>. }*/ { - String dbName = clazz.getTagValue(GeneratorUtil.TAG_DB_NAME); + String dbName = clazz.getTagValue(TopiaGeneratorUtil.TAG_DB_NAME); if (dbName != null) { /*{ * * <p>Nom de l'entité en BD : <%=dbName%>.</p> @@ -105,10 +114,10 @@ } } /*{ *) -public abstract class <%=clazz.getName()%>Abstract extends }*/ +}*/ String extendClass = ""; - for (Iterator i = clazz.getSuperclasses().iterator(); i.hasNext();) { - ObjectModelClassifier parent = (ObjectModelClassifier) i.next(); + for (Iterator<ObjectModelClass> i = clazz.getSuperclasses().iterator(); i.hasNext();) { + ObjectModelClass parent = i.next(); extendClass += parent.getQualifiedName(); //Si une des classes parentes définies des méthodes abstraites, son // impl ne sera pas créé @@ -116,7 +125,7 @@ if (parent instanceof ObjectModelClass) { abstractParent = shouldBeAbstract((ObjectModelClass) parent); } - if (parent.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (parent.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { if (abstractParent) { extendClass += "Abstract"; } else { @@ -131,11 +140,11 @@ extendClass += TopiaEntityAbstract.class.getName(); } - /*{<%=extendClass%> implements java.io.Serializable, <%=clazz.getQualifiedName()%> { +/*{public abstract class <%=clazzName%>Abstract extends <%=extendClass%> implements Serializable, <%=clazzFQN%> { }*/ - String svUID = GeneratorUtil.findTagValue("serialVersionUID", clazz, model); + String svUID = TopiaGeneratorUtil.findTagValue("serialVersionUID", clazz, model); if (svUID != null) { /*{ public static final long serialVersionUID = <%=svUID%>; @@ -152,46 +161,56 @@ || attr.hasAssociationClass())) { continue; } - String dbName = attr.getTagValue(GeneratorUtil.TAG_DB_NAME); - if (GeneratorUtil.hasDocumentation(attr) || dbName != null) { + ; + if (TopiaGeneratorUtil.hasDocumentation(attr) || attr.hasTagValue(TopiaGeneratorUtil.TAG_DB_NAME)) { /*{ /** }*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(attr)) { + String attrDocumentation = attr.getDocumentation(); +/*{ * <%=attrDocumentation%> }*/ } - if (dbName != null) { + if (attr.hasTagValue(TopiaGeneratorUtil.TAG_DB_NAME)) { + String dbName = attr.getTagValue(TopiaGeneratorUtil.TAG_DB_NAME); /*{ * Nom de l'attribut en BD : <%=dbName%>. }*/ } /*{ *) }*/ } - String annotation = attr.getTagValue(TAG_ANNOTATION); - if (annotation != null && annotation.length() > 0) { + if (attr.hasTagValue(TopiaGeneratorUtil.TAG_ANNOTATION)) { + String annotation = attr.getTagValue(TopiaGeneratorUtil.TAG_ANNOTATION); /*{ <%=annotation%> }*/ } - if (!Util.isNMultiplicity(attr)) { - if (!attr.hasAssociationClass()) { -/*{ <%=attr.getVisibility()%> <%=attr.getType()%> <%=attr.getName()%>; + String attrVisibility = attr.getVisibility(); + + // In case attribute is NMultiplicity + String collectionType = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); + + if (!attr.hasAssociationClass()) { + String attrName = attr.getName(); + String attrType = attr.getType(); + if (!GeneratorUtil.isNMultiplicity(attr)) { +/*{ <%=attrVisibility%> <%=attrType%> <%=attrName%>; + }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ <%=attr.getVisibility()%> <%=attr.getAssociationClass().getQualifiedName()%> <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; +/*{ <%=attrVisibility%> <%=collectionType%><<%=attrType%>> <%=attrName%>; }*/ } } else { - String collectionType = GeneratorUtil.getNMultiplicityInterfaceType(attr); - if (!attr.hasAssociationClass()) { -/*{ <%=attr.getVisibility()%> <%=collectionType%><<%=attr.getType()%>> <%=attr.getName()%>; + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + if (!GeneratorUtil.isNMultiplicity(attr)) { + //TODO THIMEL : Je pense que les GeneratorUtil.toLowerCaseFirstLetter sont inutiles ici, ou alors il faudrait le faire partout +/*{ <%=attrVisibility%> <%=assocClassFQN%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ <%=attr.getVisibility()%> <%=collectionType%><<%=attr.getAssociationClass().getQualifiedName()%>> <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; +/*{ <%=attrVisibility%> <%=collectionType%><<%=assocClassFQN%>> <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; }*/ } @@ -203,7 +222,10 @@ ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) clazz; for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { -/*{ <%=attr.getVisibility()%> <%=attr.getType()%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>; + String attrVisibility = attr.getVisibility(); + String attrType = attr.getType(); + String attrName = attr.getName(); +/*{ <%=attrVisibility%> <%=attrType%> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>; }*/ } @@ -211,23 +233,19 @@ } /*{ /** - * Constructeur de <%=clazz.getName()%>Abstract par défaut. + * Constructeur de <%=clazzName%>Abstract par défaut. *) - public <%=clazz.getName()%>Abstract() { + public <%=clazzName%>Abstract() { } public void update() throws TopiaException { - ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=clazz.getName()%>.class).update(this); + ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=clazzName%>.class).update(this); } public void delete() throws TopiaException { - ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=clazz.getName()%>.class).delete(this); + ((TopiaContextImplementor)getTopiaContext()).getDAO(<%=clazzName%>.class).delete(this); } -}*/ - - -/*{ /** * Envoi via les methodes du visitor l'ensemble des champs de l'entity * avec leur type et leur valeur. @@ -250,27 +268,31 @@ continue; } - if (!Util.isNMultiplicity(attr)) { - if (!attr.hasAssociationClass()) { - // FIXME il faut encapsuler getType() pour retourner la version objet des types simples -/*{ visitor.visit(this, <%=attr.getType()%>.class, <%=attr.getName()%>); + if (!attr.hasAssociationClass()) { + String attrType = attr.getType(); + String attrName = attr.getName(); + if (!GeneratorUtil.isNMultiplicity(attr)) { +/*{ visitor.accept(this, <%=attrType%>.class, <%=attrName%>); }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ visitor.visit(this, <%=attr.getAssociationClass().getQualifiedName()%>.class, <%=Util.toLowerCaseFirstLetter(assocAttrName)%>); + String collectionType = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); +/*{ visitor.accept(this, <%=collectionType%>.class, <%=attrType%>.class, <%=attrName%>); }*/ } } else { - String collectionType = GeneratorUtil.getNMultiplicityInterfaceType(attr); - if (!attr.hasAssociationClass()) { -/*{ visitor.visit(this, <%=collectionType%>.class, <%=attr.getType()%>.class, <%=attr.getName()%>); + if (!GeneratorUtil.isNMultiplicity(attr)) { + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); +/*{ visitor.accept(this, <%=assocClassFQN%>.class, <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>); }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ visitor.visit(this, <%=collectionType%>.class, <%=attr.getAssociationClass().getQualifiedName()%>.class, <%=Util.toLowerCaseFirstLetter(assocAttrName)%>); + String collectionType = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); +/*{ visitor.accept(this, <%=collectionType%>.class, <%=assocClassFQN%>.class, <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>); }*/ } @@ -282,7 +304,9 @@ ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) clazz; for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { -/*{ visitor.visit(this, <%=attr.getType()%>.class, <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + String attrType = attr.getType(); + String attrName = attr.getName(); +/*{ visitor.accept(this, <%=attrType%>.class, <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>); }*/ } @@ -292,26 +316,21 @@ visitor.end(this); } -}*/ - - - -/*{ public List<TopiaEntity> getAggregate() throws TopiaException { 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 : clazz.getAttributes()) { - if (attr.referenceClassifier() && attr.getClassifier().hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (attr.referenceClassifier() && attr.getClassifier().hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { if (attr.isAggregate()) { - if (Util.isNMultiplicity(attr)) { -/*{ - tmp.addAll(get<%=Util.capitalize(attr.getName())%>()); + String attrName = attr.getName(); + if (GeneratorUtil.isNMultiplicity(attr)) { +/*{ tmp.addAll(get<%=GeneratorUtil.capitalize(attrName)%>()); }*/ } else { -/*{ - tmp.add(get<%=Util.capitalize(attr.getName())%>()); +/*{ tmp.add(get<%=GeneratorUtil.capitalize(attrName)%>()); }*/ } } @@ -329,36 +348,35 @@ return result; } -}*/ - -/*{ public List<TopiaEntity> getComposite() throws TopiaException { 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 : clazz.getAttributes()) { - ObjectModelAttribute reverse = attr.getReverseAttribute(); - if (attr.referenceClassifier() && attr.getClassifier().hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (attr.referenceClassifier() && attr.getClassifier().hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { if (attr.isComposite()) { - if (Util.isNMultiplicity(attr)) { -/*{ - tmp.addAll(get<%=Util.capitalize(attr.getName())%>()); + String attrName = attr.getName(); + if (GeneratorUtil.isNMultiplicity(attr)) { +/*{ tmp.addAll(get<%=GeneratorUtil.capitalize(attrName)%>()); }*/ } else { -/*{ - tmp.add(get<%=Util.capitalize(attr.getName())%>()); +/*{ tmp.add(get<%=GeneratorUtil.capitalize(attrName)%>()); }*/ } } else if (attr.hasAssociationClass()) { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (!Util.isNMultiplicity(attr)) { + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + if (!GeneratorUtil.isNMultiplicity(attr)) { /*{ - if (this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> != null) { - tmp.add(this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>); + if (this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> != null) { + tmp.add(this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>); } }*/ } 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 @@ -366,13 +384,13 @@ // 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 (this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> != null) { -// tmp.addAll(this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>); +// if (this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> != null) { +// tmp.addAll(this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>); // } /*{ tmp.addAll(((TopiaContextImplementor)getTopiaContext()) - .getDAO(<%=attr.getAssociationClass().getQualifiedName()%>.class) - .findAllByProperties("<%=reverse.getName()%>", this)); + .getDAO(<%=assocClassFQN%>.class) + .findAllByProperties("<%=reverseAttrName%>", this)); }*/ } } @@ -397,303 +415,306 @@ for (ObjectModelAttribute attr : clazz.getAttributes()) { ObjectModelAttribute reverse = attr.getReverseAttribute(); + String attrName = attr.getName(); + String attrType = attr.getType(); + if (!(attr.isNavigable() || hasUnidirectionalRelationOnAbstractType(reverse, model))) { continue; } - if (!Util.isNMultiplicity(attr)) { + if (!GeneratorUtil.isNMultiplicity(attr)) { if (!attr.hasAssociationClass()) { /*{ /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#set<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%>) + * @see <%=clazzFQN%>#set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%>) *) - public void set<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> value) { - <%=attr.getType()%> _oldValue = this.<%=attr.getName()%>; - fireOnPreWrite("<%=attr.getName()%>", _oldValue, value); - this.<%=attr.getName()%> = value; - fireOnPostWrite("<%=attr.getName()%>", _oldValue, value); + public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> value) { + <%=attrType%> _oldValue = this.<%=attrName%>; + fireOnPreWrite("<%=attrName%>", _oldValue, value); + this.<%=attrName%> = value; + fireOnPostWrite("<%=attrName%>", _oldValue, value); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#get<%=Util.capitalize(attr.getName())%>() + * @see <%=clazzFQN%>#get<%=GeneratorUtil.capitalize(attrName)%>() *) - public <%=attr.getType()%> get<%=Util.capitalize(attr.getName())%>() { - fireOnPreRead("<%=attr.getName()%>", <%=attr.getName()%>); - <%=attr.getType()%> result = this.<%=attr.getName()%>; - fireOnPostRead("<%=attr.getName()%>", <%=attr.getName()%>); + public <%=attrType%> get<%=GeneratorUtil.capitalize(attrName)%>() { + fireOnPreRead("<%=attrName%>", <%=attrName%>); + <%=attrType%> result = this.<%=attrName%>; + fireOnPostRead("<%=attrName%>", <%=attrName%>); return result; } }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); /*{ /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#set<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%>) + * @see <%=clazzFQN%>#set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%>) *) - public void set<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%> association) { - <%=attr.getAssociationClass().getQualifiedName()%> _oldValue = this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>; - fireOnPreWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, association); - this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = association; - fireOnPostWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, association); + public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%> association) { + <%=assocClassFQN%> _oldValue = this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; + fireOnPreWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, association); + this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = association; + fireOnPostWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, association); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#get<%=Util.capitalize(assocAttrName)%>() + * @see <%=clazzFQN%>#get<%=GeneratorUtil.capitalize(assocAttrName)%>() *) - public <%=attr.getAssociationClass().getQualifiedName()%> get<%=Util.capitalize(assocAttrName)%>() { - return <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + public <%=assocClassFQN%> get<%=GeneratorUtil.capitalize(assocAttrName)%>() { + return <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; } }*/ } } else { //NMultiplicity - String collectionInterface = GeneratorUtil.getNMultiplicityInterfaceType(attr); - String collectionObject = GeneratorUtil.getNMultiplicityObjectType(attr); + String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); + String collectionObject = TopiaGeneratorUtil.getNMultiplicityObjectType(attr); if (!attr.hasAssociationClass()) { //Méthodes remplacées par des accesseurs sur les classes d'assoc /*{ /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#add<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%>) + * @see <%=clazzFQN%>#add<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%>) *) - public void add<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>) { - fireOnPreWrite("<%=attr.getName()%>", null, <%=attr.getName()%>); - if (this.<%=attr.getName()%> == null) { - this.<%=attr.getName()%> = new <%=collectionObject%><<%=attr.getType()%>>(); + public void add<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> <%=attrName%>) { + fireOnPreWrite("<%=attrName%>", null, <%=attrName%>); + if (this.<%=attrName%> == null) { + this.<%=attrName%> = new <%=collectionObject%><<%=attrType%>>(); } }*/ if (reverse != null && (reverse.isNavigable() || hasUnidirectionalRelationOnAbstractType(attr, model))) { - if (!Util.isNMultiplicity(reverse)) { -/*{ <%=attr.getName()%>.set<%=Util.capitalize(reverse.getName())%>(this); + String reverseAttrName = reverse.getName(); + String reverseAttrType = reverse.getType(); + if (!GeneratorUtil.isNMultiplicity(reverse)) { +/*{ <%=attrName%>.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(this); }*/ } else { -/*{ if (<%=attr.getName()%>.get<%=Util.capitalize(reverse.getName())%>() == null) { - <%=attr.getName()%>.set<%=Util.capitalize(reverse.getName())%>(new <%=collectionObject%><<%=reverse.getType()%>>()); - } - <%=attr.getName()%>.get<%=Util.capitalize(reverse.getName())%>().add(this); +/*{ if (<%=attrName%>.get<%=GeneratorUtil.capitalize(reverseAttrName)%>() == null) { + <%=attrName%>.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(new <%=collectionObject%><<%=reverseAttrType%>>()); + } + <%=attrName%>.get<%=GeneratorUtil.capitalize(reverseAttrName)%>().add(this); }*/ } } -/*{ this.<%=attr.getName()%>.add(<%=attr.getName()%>); - fireOnPostWrite("<%=attr.getName()%>", this.<%=attr.getName()%>.size(), null, <%=attr.getName()%>); +/*{ this.<%=attrName%>.add(<%=attrName%>); + fireOnPostWrite("<%=attrName%>", this.<%=attrName%>.size(), null, <%=attrName%>); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#addAll<%=Util.capitalize(attr.getName())%>(<%=collectionInterface%><<%=attr.getType()%>>) + * @see <%=clazzFQN%>#addAll<%=GeneratorUtil.capitalize(attrName)%>(<%=collectionInterface%><<%=attrType%>>) *) - public void addAll<%=Util.capitalize(attr.getName())%>(<%=collectionInterface%><<%=attr.getType()%>> values) { + public void addAll<%=GeneratorUtil.capitalize(attrName)%>(<%=collectionInterface%><<%=attrType%>> values) { if (values == null) { return; } - for (<%=attr.getType()%> item : values) { - add<%=Util.capitalize(attr.getName())%>(item); + for (<%=attrType%> item : values) { + add<%=GeneratorUtil.capitalize(attrName)%>(item); } } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#get<%=Util.capitalize(attr.getName())%>ByTopiaId(String) + * @see <%=clazzFQN%>#get<%=GeneratorUtil.capitalize(attrName)%>ByTopiaId(String) *) - public <%=attr.getType()%> get<%=Util.capitalize(attr.getName())%>ByTopiaId(String topiaId) { - return org.codelutin.topia.persistence.util.TopiaEntityHelper.getEntityByTopiaId(<%=attr.getName()%>, topiaId); + public <%=attrType%> get<%=GeneratorUtil.capitalize(attrName)%>ByTopiaId(String topiaId) { + return org.codelutin.topia.persistence.util.TopiaEntityHelper.getEntityByTopiaId(<%=attrName%>, topiaId); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#set<%=Util.capitalize(attr.getName())%>(<%=collectionInterface%><<%=attr.getType()%>>) + * @see <%=clazzFQN%>#set<%=GeneratorUtil.capitalize(attrName)%>(<%=collectionInterface%><<%=attrType%>>) *) - public void set<%=Util.capitalize(attr.getName())%>(<%=collectionInterface%><<%=attr.getType()%>> values) { - <%=collectionInterface%><<%=attr.getType()%>> _oldValue = <%=attr.getName()%>; - fireOnPreWrite("<%=attr.getName()%>", _oldValue, values); - <%=attr.getName()%> = values; - fireOnPostWrite("<%=attr.getName()%>", _oldValue, values); + public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=collectionInterface%><<%=attrType%>> values) { + <%=collectionInterface%><<%=attrType%>> _oldValue = <%=attrName%>; + fireOnPreWrite("<%=attrName%>", _oldValue, values); + <%=attrName%> = values; + fireOnPostWrite("<%=attrName%>", _oldValue, values); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#remove<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%>) + * @see <%=clazzFQN%>#remove<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%>) *) - public void remove<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> value) { - fireOnPreWrite("<%=attr.getName()%>", value, null); - if ((this.<%=attr.getName()%> == null) || (!this.<%=attr.getName()%>.remove(value))) { + public void remove<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> value) { + fireOnPreWrite("<%=attrName%>", value, null); + if ((this.<%=attrName%> == null) || (!this.<%=attrName%>.remove(value))) { throw new IllegalArgumentException("List does not contain given element"); } }*/ if (reverse != null && (reverse.isNavigable() || hasUnidirectionalRelationOnAbstractType(attr, model))) { - if (!Util.isNMultiplicity(reverse)) { -/*{ value.set<%=Util.capitalize(reverse.getName())%>(null); + String reverseAttrName = reverse.getName(); + if (!GeneratorUtil.isNMultiplicity(reverse)) { +/*{ value.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(null); }*/ } else { -/*{ value.get<%=Util.capitalize(reverse.getName())%>().remove(this); +/*{ value.get<%=GeneratorUtil.capitalize(reverseAttrName)%>().remove(this); }*/ } } -/*{ fireOnPostWrite("<%=attr.getName()%>", this.<%=attr.getName()%>.size()+1, value, null); +/*{ fireOnPostWrite("<%=attrName%>", this.<%=attrName%>.size()+1, value, null); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#clear<%=Util.capitalize(attr.getName())%>() + * @see <%=clazzFQN%>#clear<%=GeneratorUtil.capitalize(attrName)%>() *) - public void clear<%=Util.capitalize(attr.getName())%>() { - if (this.<%=attr.getName()%> == null) { + public void clear<%=GeneratorUtil.capitalize(attrName)%>() { + if (this.<%=attrName%> == null) { return; } }*/ if (reverse != null && (reverse.isNavigable() || hasUnidirectionalRelationOnAbstractType(attr, model))) { - -/*{ for (<%=attr.getType()%> item : this.<%=attr.getName()%>) { + String reverseAttrName = reverse.getName(); +/*{ for (<%=attrType%> item : this.<%=attrName%>) { }*/ - if (!Util.isNMultiplicity(reverse)) { -/*{ item.set<%=Util.capitalize(reverse.getName())%>(null); + if (!GeneratorUtil.isNMultiplicity(reverse)) { +/*{ item.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(null); }*/ } else { -/*{ item.get<%=Util.capitalize(reverse.getName())%>().remove(this); +/*{ item.get<%=GeneratorUtil.capitalize(reverseAttrName)%>().remove(this); }*/ } /*{ } }*/ } -/*{ <%=collectionInterface%><<%=attr.getType()%>> _oldValue = new <%=collectionObject%><<%=attr.getType()%>>(this.<%=attr.getName()%>); - fireOnPreWrite("<%=attr.getName()%>", _oldValue, this.<%=attr.getName()%>); - this.<%=attr.getName()%>.clear(); - fireOnPostWrite("<%=attr.getName()%>", _oldValue, this.<%=attr.getName()%>); +/*{ <%=collectionInterface%><<%=attrType%>> _oldValue = new <%=collectionObject%><<%=attrType%>>(this.<%=attrName%>); + fireOnPreWrite("<%=attrName%>", _oldValue, this.<%=attrName%>); + this.<%=attrName%>.clear(); + fireOnPostWrite("<%=attrName%>", _oldValue, this.<%=attrName%>); } }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); /*{ /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#add<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%>) + * @see <%=clazzFQN%>#add<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%>) *) - public void add<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%> value) { - fireOnPreWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", null, value); - if (this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> == null) { - this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = new <%=collectionObject%><<%=attr.getAssociationClass().getQualifiedName()%>>(); + public void add<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%> value) { + fireOnPreWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", null, value); + if (this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) { + this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = new <%=collectionObject%><<%=assocClassFQN%>>(); } }*/ if (reverse != null && (reverse.isNavigable() || hasUnidirectionalRelationOnAbstractType(attr, model))) { -/*{ value.set<%=Util.capitalize(reverse.getName())%>(this); + String reverseAttrName = reverse.getName(); +/*{ value.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(this); }*/ } -/*{ this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>.add(value); - fireOnPostWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>.size(), null, value); +/*{ this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.add(value); + fireOnPostWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.size(), null, value); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#get<%=Util.capitalize(attr.getName())%>ByTopiaId(String) + * @see <%=clazzFQN%>#get<%=GeneratorUtil.capitalize(attrName)%>ByTopiaId(String) *) - public <%=attr.getAssociationClass().getQualifiedName()%> get<%=Util.capitalize(assocAttrName)%>ByTopiaId(String topiaId) { + public <%=assocClassFQN%> get<%=GeneratorUtil.capitalize(assocAttrName)%>ByTopiaId(String topiaId) { return org.codelutin.topia.persistence.util.TopiaEntityHelper.getEntityByTopiaId(<%=assocAttrName%>, topiaId); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#addAll<%=Util.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>>() + * @see <%=clazzFQN%>#addAll<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=assocClassFQN%>>() *) - public void addAll<%=Util.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>> values) { + public void addAll<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=assocClassFQN%>> values) { if (values == null) { return; } - for (<%=attr.getAssociationClass().getQualifiedName()%> item : values) { - add<%=Util.capitalize(assocAttrName)%>(item); + for (<%=assocClassFQN%> item : values) { + add<%=GeneratorUtil.capitalize(assocAttrName)%>(item); } } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#set<%=Util.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>>() + * @see <%=clazzFQN%>#set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=assocClassFQN%>>() *) - public void set<%=Util.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>> values) { -// clear<%=Util.capitalize(assocAttrName)%>(); -// addAll<%=Util.capitalize(assocAttrName)%>(values); + public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=assocClassFQN%>> values) { +// clear<%=GeneratorUtil.capitalize(assocAttrName)%>(); +// addAll<%=GeneratorUtil.capitalize(assocAttrName)%>(values); // FIXME - <%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>> _oldValue = <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; - fireOnPreWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, values); - <%=Util.toLowerCaseFirstLetter(assocAttrName)%> = values; - fireOnPostWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, values); + <%=collectionInterface%><<%=assocClassFQN%>> _oldValue = <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; + fireOnPreWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, values); + <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = values; + fireOnPostWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, values); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#remove<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%>) + * @see <%=clazzFQN%>#remove<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%>) *) - public void remove<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%> value) { - fireOnPreWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", value, null); - if ((this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> == null) || (!this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>.remove(value))) { + public void remove<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%> value) { + fireOnPreWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", value, null); + if ((this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) || (!this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.remove(value))) { throw new IllegalArgumentException("List does not contain given element"); } }*/ if (reverse != null && (reverse.isNavigable() || hasUnidirectionalRelationOnAbstractType(attr, model))) { -/*{ value.set<%=Util.capitalize(reverse.getName())%>(null); + String reverseAttrName = reverse.getName(); +/*{ value.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(null); }*/ } -/*{ fireOnPostWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>.size()+1, value, null); +/*{ fireOnPostWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.size()+1, value, null); } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#clear<%=Util.capitalize(assocAttrName)%>() + * @see <%=clazzFQN%>#clear<%=GeneratorUtil.capitalize(assocAttrName)%>() *) - public void clear<%=Util.capitalize(assocAttrName)%>() { - if (this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> == null) { + public void clear<%=GeneratorUtil.capitalize(assocAttrName)%>() { + if (this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) { return; } }*/ if (reverse != null && (reverse.isNavigable() || hasUnidirectionalRelationOnAbstractType(attr, model))) { -/*{ for (<%=attr.getAssociationClass().getQualifiedName()%> item : this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>) { - item.set<%=Util.capitalize(reverse.getName())%>(null); + String reverseAttrName = reverse.getName(); +/*{ for (<%=assocClassFQN%> item : this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>) { + item.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(null); } }*/ } -/*{ <%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>> _oldValue = new <%=collectionObject%><<%=attr.getAssociationClass().getQualifiedName()%>>(this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>); - fireOnPreWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, null); - this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>.clear(); - fireOnPostWrite("<%=Util.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, null); +/*{ <%=collectionInterface%><<%=assocClassFQN%>> _oldValue = new <%=collectionObject%><<%=assocClassFQN%>>(this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>); + fireOnPreWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, null); + this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.clear(); + fireOnPostWrite("<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>", _oldValue, null); } }*/ } if (!attr.hasAssociationClass()) { /*{ /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#get<%=Util.capitalize(attr.getName())%>() + * @see <%=clazzFQN%>#get<%=GeneratorUtil.capitalize(attrName)%>() *) - public <%=collectionInterface%><<%=attr.getType()%>> get<%=Util.capitalize(attr.getName())%>() { - return <%=attr.getName()%>; + public <%=collectionInterface%><<%=attrType%>> get<%=GeneratorUtil.capitalize(attrName)%>() { + return <%=attrName%>; } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#size<%=Util.capitalize(attr.getName())%>() + * @see <%=clazzFQN%>#size<%=GeneratorUtil.capitalize(attrName)%>() *) - public int size<%=Util.capitalize(attr.getName())%>() { - if (<%=attr.getName()%> == null) { + public int size<%=GeneratorUtil.capitalize(attrName)%>() { + if (<%=attrName%> == null) { return 0; } - return <%=attr.getName()%>.size(); + return <%=attrName%>.size(); } }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); /*{ /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#get<%=Util.capitalize(assocAttrName)%>() + * @see <%=clazzFQN%>#get<%=GeneratorUtil.capitalize(assocAttrName)%>() *) - public <%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>> get<%=Util.capitalize(assocAttrName)%>() { - return <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + public <%=collectionInterface%><<%=assocClassFQN%>> get<%=GeneratorUtil.capitalize(assocAttrName)%>() { + return <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#get<%=Util.capitalize(assocAttrName)%>(<%=attr.getType()%>) + * @see <%=clazzFQN%>#get<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=attrType%>) *) - public <%=attr.getAssociationClass().getQualifiedName()%> get<%=Util.capitalize(assocAttrName)%>(<%=attr.getType()%> value) { - if (value == null || <%=Util.toLowerCaseFirstLetter(assocAttrName)%> == null) { + public <%=assocClassFQN%> get<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=attrType%> value) { + if (value == null || <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) { return null; } - for (<%=attr.getAssociationClass().getQualifiedName()%> item : <%=Util.toLowerCaseFirstLetter(assocAttrName)%>) { - if (value.equals(item.get<%=Util.capitalize(attr.getName())%>())) { + for (<%=assocClassFQN%> item : <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>) { + if (value.equals(item.get<%=GeneratorUtil.capitalize(attrName)%>())) { return item; } } @@ -701,13 +722,13 @@ } /* (non-Javadoc) - * @see <%=clazz.getQualifiedName()%>#size<%=Util.capitalize(assocAttrName)%>() + * @see <%=clazzFQN%>#size<%=GeneratorUtil.capitalize(assocAttrName)%>() *) - public int size<%=Util.capitalize(assocAttrName)%>() { - if (<%=Util.toLowerCaseFirstLetter(assocAttrName)%> == null) { + public int size<%=GeneratorUtil.capitalize(assocAttrName)%>() { + if (<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> == null) { return 0; } - return <%=Util.toLowerCaseFirstLetter(assocAttrName)%>.size(); + return <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>.size(); } }*/ @@ -720,15 +741,15 @@ ObjectModelAssociationClass assoc = (ObjectModelAssociationClass) clazz; for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { - String type = attr.getType(); - String name = attr.getName(); - generateAssociationAccessors(output, name, type); + String attrType = attr.getType(); + String attrName = attr.getName(); + generateAssociationAccessors(output, attrName, attrType); // //Ne sert plus à rien normalement avec la navigabilité // ObjectModelAttribute reverse = attr.getReverseAttribute(); // if (reverse == null) { -// type = ((ObjectModelClassifier)attr.getDeclaringElement()).getQualifiedName(); -// name = attr.getDeclaringElement().getName(); -// generateAssociationAccessors(output, name, type); +// attrType = ((ObjectModelClassifier)attr.getDeclaringElement()).getQualifiedName(); +// attrName = attr.getDeclaringElement().getName(); +// generateAssociationAccessors(output, attrName, attrType); // } } } @@ -746,24 +767,25 @@ if (model.hasClass(attr.getType())) { attrEntity = model.getClass(attr.getType()); } - boolean isEntity = (attrEntity != null && attrEntity.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)); + boolean isEntity = (attrEntity != null && attrEntity.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)); ObjectModelAttribute reverse = attr.getReverseAttribute(); if ((isEntity && (reverse == null || !reverse.isNavigable()) && !attr.hasAssociationClass()) || (!isEntity)) { -/*{ append("<%=attr.getName()%>", this.<%=attr.getName()%>). + String attrName = attr.getName(); +/*{ append("<%=attrName%>", this.<%=attrName%>). }*/ } } -/*{ toString(); +/*{ toString(); return result; } }*/ - String i18nPrefix = GeneratorUtil.getI18nPrefix(clazz, model); + String i18nPrefix = TopiaGeneratorUtil.getI18nPrefix(clazz, model); if (i18nPrefix != null) { // generate i18n prefix generateI18n(output, i18nPrefix, clazz); } /*{ -} //<%=clazz.getName()%>Abstract +} //<%=clazzName%>Abstract }*/ } @@ -771,18 +793,18 @@ /*{ /** * @param value La valeur de l'attribut <%=name%> à positionner. *) - public void set<%=Util.capitalize(name)%>(<%=type%> value) { - <%=type%> _oldValue = this.<%=Util.toLowerCaseFirstLetter(name)%>; + public void set<%=GeneratorUtil.capitalize(name)%>(<%=type%> value) { + <%=type%> _oldValue = this.<%=GeneratorUtil.toLowerCaseFirstLetter(name)%>; fireOnPreWrite("<%=name%>", _oldValue, value); - this.<%=Util.toLowerCaseFirstLetter(name)%> = value; + this.<%=GeneratorUtil.toLowerCaseFirstLetter(name)%> = value; fireOnPostWrite("<%=name%>", _oldValue, value); } /** * @return La valeur de l'attribut <%=name%>. *) - public <%=type%> get<%=Util.capitalize(name)%>() { - return <%=Util.toLowerCaseFirstLetter(name)%>; + public <%=type%> get<%=GeneratorUtil.capitalize(name)%>() { + return <%=GeneratorUtil.toLowerCaseFirstLetter(name)%>; } }*/ Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityDTOGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityDTOGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityDTOGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -29,8 +29,8 @@ package org.codelutin.topia.generator; -import static org.codelutin.topia.generator.GeneratorUtil.TAG_ANNOTATION; -import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.TAG_ANNOTATION; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType; import java.io.File; import java.io.IOException; @@ -40,7 +40,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.generator.ObjectModelGenerator; -import org.codelutin.generator.Util; +import org.codelutin.generator.GeneratorUtil; import org.codelutin.generator.models.object.ObjectModelAssociationClass; import org.codelutin.generator.models.object.ObjectModelAttribute; import org.codelutin.generator.models.object.ObjectModelClass; @@ -70,45 +70,46 @@ public boolean isEntity(String type) { ObjectModelClassifier clazz = model.getClassifier(type); - return clazz != null && clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY); + return clazz != null && clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY); } @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { return; } - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } + String clazzName = clazz.getName(); /*{package <%=clazz.getPackageName()%>; import org.apache.commons.lang.builder.ToStringBuilder; import java.beans.PropertyChangeListener; /** - * Implantation DTO pour l'entité <%=Util.capitalize(clazz.getName())%>. + * Implantation DTO pour l'entité <%=GeneratorUtil.capitalize(clazzName)%>. *) -public class <%=clazz.getName()%>DTO }*/ +}*/ String extendClass = ""; - for (Iterator i=clazz.getSuperclasses().iterator(); i.hasNext();) { - ObjectModelClassifier parent = (ObjectModelClassifier)i.next(); + for (Iterator<ObjectModelClass> i=clazz.getSuperclasses().iterator(); i.hasNext();) { + ObjectModelClass parent = i.next(); extendClass += parent.getQualifiedName() + "DTO"; if (i.hasNext()) { extendClass += ", "; } } if (extendClass.length() > 0) { - /*{extends <%=extendClass%> }*/ + extendClass = "extends " + extendClass + " "; } - /*{implements java.io.Serializable { +/*{public class <%=clazzName%>DTO <%=extendClass%>implements java.io.Serializable { }*/ - String svUID = GeneratorUtil.findTagValue("dto-serialVersionUID", clazz, model); + String svUID = TopiaGeneratorUtil.findTagValue("dto-serialVersionUID", clazz, model); if (svUID != null) { /*{ public static final long serialVersionUID = <%=svUID%>; @@ -125,36 +126,43 @@ || attr.hasAssociationClass())) { continue; } - if (GeneratorUtil.hasDocumentation(attr)) { + if (TopiaGeneratorUtil.hasDocumentation(attr)) { /*{ /** * <%=attr.getDocumentation()%> *) }*/ } - String annotation = attr.getTagValue(TAG_ANNOTATION); - if (annotation != null && annotation.length() > 0) { + if (attr.hasTagValue(TAG_ANNOTATION)) { + String annotation = attr.getTagValue(TAG_ANNOTATION); /*{ <%=annotation%> }*/ } - if (!Util.isNMultiplicity(attr)) { - if (!attr.hasAssociationClass()) { -/*{ <%=attr.getVisibility()%> <%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%> <%=attr.getName()%>; + String attrVisibility = attr.getVisibility(); + if (!attr.hasAssociationClass()) { + String attrType = attr.getType(); + String attrName = attr.getName(); + if (isEntity(attrType)) { + attrType += "DTO"; + } + if (!GeneratorUtil.isNMultiplicity(attr)) { +/*{ <%=attrVisibility%> <%=attrType%> <%=attrName%>; + }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ <%=attr.getVisibility()%> <%=attr.getAssociationClass().getQualifiedName()%>DTO <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; +/*{ <%=attrVisibility%> <%=attrType%>[] <%=attrName%>; }*/ } } else { - if (!attr.hasAssociationClass()) { -/*{ <%=attr.getVisibility()%> <%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%>[] <%=attr.getName()%>; + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + if (!GeneratorUtil.isNMultiplicity(attr)) { +/*{ <%=attrVisibility%> <%=assocClassFQN%>DTO <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); -/*{ <%=attr.getVisibility()%> <%=attr.getAssociationClass().getQualifiedName()%>DTO[] <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; +/*{ <%=attrVisibility%> <%=assocClassFQN%>DTO[] <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; }*/ } @@ -164,10 +172,15 @@ //Déclaration des attributs d'une classe d'associations if (clazz instanceof ObjectModelAssociationClass) { ObjectModelAssociationClass assoc = (ObjectModelAssociationClass)clazz; - for (Object o : assoc.getParticipantsAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; + for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { -/*{ <%=attr.getVisibility()%> <%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>; + String attrVisibility = attr.getVisibility(); + String attrType = attr.getType(); + String attrName = attr.getName(); + if (isEntity(attrType)) { + attrType += "DTO"; + } +/*{ <%=attrVisibility%> <%=attrType%> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>; }*/ } @@ -180,9 +193,9 @@ }*/ /*{ /** - * Constructeur de <%=clazz.getName()%>DTO par défaut. + * Constructeur de <%=clazzName%>DTO par défaut. *) - public <%=clazz.getName()%>DTO() { p = new java.beans.PropertyChangeSupport(this); } + public <%=clazzName%>DTO() { p = new java.beans.PropertyChangeSupport(this); } public void addPropertyChangeListener(PropertyChangeListener listener) { p.addPropertyChangeListener(listener); @@ -202,84 +215,75 @@ }*/ - for (Object o : clazz.getAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; + for (ObjectModelAttribute attr : clazz.getAttributes()) { ObjectModelAttribute reverse = attr.getReverseAttribute(); if (!(attr.isNavigable() || hasUnidirectionalRelationOnAbstractType(reverse, model))) { continue; } - if (!Util.isNMultiplicity(attr)) { - if (!attr.hasAssociationClass()) { -/*{ public void set<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%> value) { - <%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%> oldValue = this.<%=attr.getName()%>; - this.<%=attr.getName()%> = value; - p.firePropertyChange("<%=attr.getName()%>", oldValue, value); + String attrName = attr.getName(); + + if (!attr.hasAssociationClass()) { + String attrType = attr.getType(); + if (isEntity(attrType)) { + attrType += "DTO"; + } + if (!GeneratorUtil.isNMultiplicity(attr)) { +/*{ public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> value) { + <%=attrType%> oldValue = this.<%=attrName%>; + this.<%=attrName%> = value; + p.firePropertyChange("<%=attrName%>", oldValue, value); } - public <%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%> get<%=Util.capitalize(attr.getName())%>() { - return <%=attr.getName()%>; + public <%=attrType%> get<%=GeneratorUtil.capitalize(attrName)%>() { + return <%=attrName%>; } }*/ - } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } -/*{ public void set<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%>DTO association) { - <%=attr.getAssociationClass().getQualifiedName()%>DTO oldAssocation= this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>; - this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = association; - p.firePropertyChange("<%=attr.getName()%>", oldAssocation, assocation); + } else { +/*{ public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%>[] values) { + <%=attrType%>[] oldValues = this.<%=attrName%>; + this.<%=attrName%> = values; + p.firePropertyChange("<%=attrName%>", oldValues, values); } - public <%=attr.getAssociationClass().getQualifiedName()%>DTO get<%=Util.capitalize(assocAttrName)%>() { - return <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + public <%=attrType%>[] get<%=GeneratorUtil.capitalize(attrName)%>() { + return this.<%=attrName%>; } }*/ - } - } else { //NMultiplicity - if (!attr.hasAssociationClass()) { //Méthodes remplacées par des accesseurs sur les classes d'assoc -/*{ public void set<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%>[] values) { - <%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%>[] oldValues = this.<%=attr.getName()%>; - this.<%=attr.getName()%> = values; - p.firePropertyChange("<%=attr.getName()%>", oldValues, values); + } + } else { + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + if (!GeneratorUtil.isNMultiplicity(attr)) { +/*{ public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%>DTO association) { + <%=assocClassFQN%>DTO oldAssocation= this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; + this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = association; + p.firePropertyChange("<%=attrName%>", oldAssocation, assocation); } -}*/ - } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } -/*{ public void set<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%>DTO[] values) { - <%=attr.getAssociationClass().getQualifiedName()%>DTO[] oldValues = this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>; - this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = values; - p.firePropertyChange("<%=attr.getName()%>", oldValues, values); + public <%=assocClassFQN%>DTO get<%=GeneratorUtil.capitalize(assocAttrName)%>() { + return <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; } }*/ - } - if (!attr.hasAssociationClass()) { -/*{ public <%=attr.getType()%><%=(isEntity(attr.getType())?"DTO":"")%>[] get<%=Util.capitalize(attr.getName())%>() { - return this.<%=attr.getName()%>; + } else { +/*{ public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%>DTO[] values) { + <%=assocClassFQN%>DTO[] oldValues = this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; + this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = values; + p.firePropertyChange("<%=attrName%>", oldValues, values); } -}*/ - } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } -/*{ public <%=attr.getAssociationClass().getQualifiedName()%>DTO[] get<%=Util.capitalize(assocAttrName)%>() { - return this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + public <%=assocClassFQN%>DTO[] get<%=GeneratorUtil.capitalize(assocAttrName)%>() { + return this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; } }*/ - } + } } + } /*{ @@ -294,10 +298,11 @@ if (model.hasClass(attr.getType())) { attrEntity = model.getClass(attr.getType()); } - boolean isEntity = (attrEntity != null && attrEntity.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)); + boolean isEntity = (attrEntity != null && attrEntity.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)); ObjectModelAttribute reverse = attr.getReverseAttribute(); if ((isEntity && (reverse == null || !reverse.isNavigable()) && !attr.hasAssociationClass()) || (!isEntity)) { -/*{ append("<%=attr.getName()%>", this.<%=attr.getName()%>). + String attrName = attr.getName(); +/*{ append("<%=attrName%>", this.<%=attrName%>). }*/ } } @@ -305,8 +310,23 @@ return result; } -} //<%=clazz.getName()%>DTO +} //<%=clazzName%>DTO }*/ } + //TODO Check wether this method could be used to generate getter and setters + protected void generateNormalGetterAndSetterWithPropertyChangeSupport(Writer output, String attrType, String attrName) throws IOException { +/*{ public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> value) { + <%=attrType%> _oldValue = this.<%=attrName%>; + this.<%=attrName%> = value; + p.firePropertyChange("<%=attrName%>", _oldValue, value); + } + + public <%=attrType%> get<%=GeneratorUtil.capitalize(attrName)%>() { + return this.<%=attrName%>; + } + +}*/ + } + } //EntityDTOGenerator Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityEnumGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityEnumGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityEnumGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -65,13 +65,13 @@ @Override public void generateFromModel(Writer output, ObjectModel model) throws IOException { - String copyright = GeneratorUtil.getCopyright(model); + String copyright = TopiaGeneratorUtil.getCopyright(model); String modelName = model.getName(); String className = modelName + "EntityEnum"; if (log.isDebugEnabled()) { log.debug("generating " + className); } - if (GeneratorUtil.notEmpty(copyright)) { + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } @@ -89,23 +89,23 @@ @SuppressWarnings({"unchecked"}) +public enum <%=className%> { }*/ - -/*{public enum <%=className%> { -}*/ - List<ObjectModelClass> classes = GeneratorUtil.getEntityClasses(model, true); + List<ObjectModelClass> classes = TopiaGeneratorUtil.getEntityClasses(model, true); if (classes.isEmpty()) { /*{ TopiaEntity(TopiaEntity.class); }*/ } else { - for (Iterator i = classes.iterator(); i.hasNext();) { - ObjectModelClass clazz = (ObjectModelClass) i.next(); + for (Iterator<ObjectModelClass> i = classes.iterator(); i.hasNext();) { + ObjectModelClass clazz = i.next(); + String clazzName = clazz.getName(); + String clazzFQN = clazz.getQualifiedName(); boolean hasNext = i.hasNext(); if (log.isDebugEnabled()) { - log.debug("generating entry " + clazz + " (hasNext:" + hasNext + ")"); + log.debug("generating entry " + clazzFQN + " (hasNext:" + hasNext + ")"); } -/*{ <%=clazz.getName()%>(<%=clazz.getQualifiedName()%>.class)<%=(hasNext?",":";")%> +/*{ <%=clazzName%>(<%=clazzFQN%>.class)<%=(hasNext?",":";")%> }*/ } } @@ -302,7 +302,7 @@ return entry.findAllContainsProperties(ctxt, propertyName, values, others); } -} +} //<%=className%> }*/ } Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -35,15 +35,14 @@ import org.apache.commons.logging.LogFactory; import org.codelutin.generator.Generator; import org.codelutin.generator.ObjectModelGenerator; -import org.codelutin.generator.Util; +import org.codelutin.generator.GeneratorUtil; import org.codelutin.generator.models.object.ObjectModelAssociationClass; import org.codelutin.generator.models.object.ObjectModelAttribute; import org.codelutin.generator.models.object.ObjectModelClass; -import static org.codelutin.topia.generator.GeneratorUtil.PERSISTENCE_TYPE_HIBERNATE; -import static org.codelutin.topia.generator.GeneratorUtil.STEREOTYPE_ENTITY; -import static org.codelutin.topia.generator.GeneratorUtil.TAG_ACCESS; -import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; -import static org.codelutin.topia.generator.GeneratorUtil.notEmpty; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.PERSISTENCE_TYPE_HIBERNATE; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.STEREOTYPE_ENTITY; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.TAG_ACCESS; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType; import java.io.File; import java.io.IOException; @@ -78,13 +77,13 @@ @Override public String getFilenameForClass(ObjectModelClass clazz) { - String DOName = GeneratorUtil.getDOType(clazz, model); + String DOName = TopiaGeneratorUtil.getDOType(clazz, model); return DOName.replace('.', File.separatorChar) + ".hbm.xml"; } @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - String persistenceType = GeneratorUtil.getPersistenceType(clazz); + String persistenceType = TopiaGeneratorUtil.getPersistenceType(clazz); if (!clazz.hasStereotype(STEREOTYPE_ENTITY) && PERSISTENCE_TYPE_HIBERNATE.equals(persistenceType)) { return; } @@ -97,43 +96,37 @@ List<ObjectModelAttribute> naturalAttributes = new ArrayList<ObjectModelAttribute>(); // la liste des autres attributs List<ObjectModelAttribute> noneNaturalAttributes = new ArrayList<ObjectModelAttribute>(); + + String clazzDOType = TopiaGeneratorUtil.getDOType(clazz, model); + String tableName = TopiaGeneratorUtil.getDBName(clazz); + String isAbstract = clazz.isAbstract()?"true":"false"; + String clazzFQN = clazz.getQualifiedName(); + + String optionalAttributes = ""; + String schema = TopiaGeneratorUtil.getSchemaName(clazz, model); + if (schema != null) { + optionalAttributes += "schema=\"" + schema + "\" "; + } + if (haveSuper) { ObjectModelClass superClass = clazz.getSuperclasses().iterator().next(); String superClassname = superClass.getQualifiedName(); - if (log.isDebugEnabled()) {log.debug("superClass for " + clazz.getQualifiedName() + " is " + superClassname);} -/*{ <union-subclass }*/ -/*{name="<%=GeneratorUtil.getDOType(clazz, model)%>" }*/ -/*{extends="<%=GeneratorUtil.getDOType(superClassname, model)%>" }*/ -/*{table="<%=GeneratorUtil.getDBName(clazz)%>" }*/ - String schema = GeneratorUtil.getSchemaName(clazz, model); - if (schema != null) { -/*{schema="<%=schema%>" }*/ + if (log.isDebugEnabled()) { + log.debug("superClass for " + clazz.getQualifiedName() + " is " + superClassname); } -/*{node="<%=GeneratorUtil.getDOType(clazz, model)%>" }*/ -/*{abstract="<%=(clazz.isAbstract()?"true":"false")%>" }*/ - //On précise au proxy de quelle interface hérite l'objet -/*{proxy="<%=clazz.getQualifiedName()%>" }*/ -/*{> + String superClassDOType = TopiaGeneratorUtil.getDOType(superClassname, model); + //On précise au proxy de quelle interface hérite l'objet + +/*{ <union-subclass name="<%=clazzDOType%>" extends="<%=superClassDOType%>" table="<%=tableName%>" node="<%=clazzDOType%>" abstract="<%=isAbstract%>" proxy="<%=clazzFQN%>" <%=optionalAttributes%>> <!--key column="topiaId"/--> }*/ } else { -/*{ <class }*/ -/*{name="<%=GeneratorUtil.getDOType(clazz, model)%>" }*/ -/*{table="<%=GeneratorUtil.getDBName(clazz)%>" }*/ - String schema = GeneratorUtil.getSchemaName(clazz, model); - if (schema != null) { -/*{schema="<%=schema%>" }*/ - } -/*{node="<%=GeneratorUtil.getDOType(clazz, model)%>" }*/ -/*{abstract="<%=(clazz.isAbstract()?"true":"false")%>" }*/ - //On précise au proxy de quelle interface hérite l'objet -/*{proxy="<%=clazz.getQualifiedName()%>" }*/ -/*{> +/*{ <class name="<%=clazzDOType%>" table="<%=tableName%>" node="<%=clazzDOType%>" abstract="<%=isAbstract%>" proxy="<%=clazzFQN%>" <%=optionalAttributes%>> <id name="topiaId" type="string" length="255" node="@topiaId"/> }*/ // on detecte les attributs des clef metiers for (ObjectModelAttribute attr : clazz.getAttributes()) { - if (GeneratorUtil.isNaturalId(attr)) { + if (TopiaGeneratorUtil.isNaturalId(attr)) { // attribut metier naturalAttributes.add(attr); } else { @@ -143,14 +136,14 @@ } if (!naturalAttributes.isEmpty()) { // generation de la clef metier - boolean mutable = GeneratorUtil.isNaturalIdMutable(clazz); + boolean mutable = TopiaGeneratorUtil.isNaturalIdMutable(clazz); String mutableStr = mutable ? " mutable=\"true\"" : ""; if (log.isDebugEnabled()) { log.debug("natural-id detected for class " + clazz.getName() + " (" + mutableStr + ") attributes : " + naturalAttributes); } /*{ <natural-id<%=mutableStr%>> }*/ - generateAttributes(output, clazz, naturalAttributes," "); + generateAttributes(output, clazz, naturalAttributes, " "); /*{ </natural-id> }*/ } @@ -158,8 +151,9 @@ <property name="topiaCreateDate" type="date" node="@topiaCreateDate"/> }*/ } - generateAttributes(output, clazz, noneNaturalAttributes,""); + generateAttributes(output, clazz, noneNaturalAttributes, ""); + if (haveSuper) { /*{ </union-subclass> }*/ @@ -171,7 +165,7 @@ }*/ } - protected void generateAttributes(Writer output, ObjectModelClass clazz, List<ObjectModelAttribute> attributes,String prefix) throws IOException { + protected void generateAttributes(Writer output, ObjectModelClass clazz, List<ObjectModelAttribute> attributes, String prefix) throws IOException { for (ObjectModelAttribute attr : attributes) { ObjectModelAttribute reverse = attr.getReverseAttribute(); @@ -181,9 +175,9 @@ if (attr.isNavigable() || hasUnidirectionalRelationOnAbstractType(reverse, model) || attr.hasAssociationClass()) { - if (!Util.isNMultiplicity(attr)) { + if (!GeneratorUtil.isNMultiplicity(attr)) { if (attr.getClassifier() != null && attr.getClassifier().hasStereotype(STEREOTYPE_ENTITY)) { - if (Util.isNMultiplicity(attr.getReverseMaxMultiplicity()) && !attr.hasAssociationClass()) { + if (GeneratorUtil.isNMultiplicity(attr.getReverseMaxMultiplicity()) && !attr.hasAssociationClass()) { generateHibernateManyToOne(output, attr, prefix); } else { generateHibernateOneToOne(output, attr, prefix); @@ -193,7 +187,7 @@ } } else { if (attr.getClassifier() != null && attr.getClassifier().hasStereotype(STEREOTYPE_ENTITY)) { - if (Util.isNMultiplicity(attr.getReverseMaxMultiplicity()) && !attr.hasAssociationClass()) { + if (GeneratorUtil.isNMultiplicity(attr.getReverseMaxMultiplicity()) && !attr.hasAssociationClass()) { generateHibernateManyToMany(output, attr, prefix); } else { generateHibernateOneToMany(output, attr, prefix); @@ -214,16 +208,14 @@ // Note(poussin) pour moi quoi qu'il arrive sur la classe d'association il faut // un many-to-one, sinon on a des problemes. // if ((!attr.getReverseAttribute().isNavigable()) || !Util.isNMultiplicity(attr.getReverseAttribute())) { -// / *{ <one-to-one name="<%=getName(attr, true)%>" class="<%=getType(attr, true)%>"<%=(GeneratorUtil.notEmpty(attr.getTagValue(GeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(GeneratorUtil.TAG_LENGTH) + "\""):"")%><%=(attr.isComposite()?" cascade=\"delete\"":"")%>/> +// / *{ <one-to-one name="<%=getName(attr, true)%>" class="<%=getType(attr, true)%>"<%=(TopiaGeneratorUtil.notEmpty(attr.getTagValue(TopiaGeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(TopiaGeneratorUtil.TAG_LENGTH) + "\""):"")%><%=(attr.isComposite()?" cascade=\"delete\"":"")%>/> // } */ // } else { - String notNull = attr.getTagValue(GeneratorUtil.TAG_NOT_NULL); - if (notEmpty(notNull)) { - notNull = " not-null=\""+notNull.trim()+"\""; - } else { - notNull=""; - } -/*{<%=prefix%> <many-to-one name="<%=getName(attr, true)%>" class="<%=getType(attr, true)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="<%=getName(attr, true)%>/@topiaId" embed-xml="false"<%=notNull%>/> + String notNull = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_NOT_NULL, "not-null"); + String attrName = getName(attr, true); + String attrType = getType(attr, true); + String attrColumn = TopiaGeneratorUtil.getDBName(attr); +/*{<%=prefix%> <many-to-one name="<%=attrName%>" class="<%=attrType%>" column="<%=attrColumn%>" node="<%=attrName%>/@topiaId" embed-xml="false"<%=notNull%>/> }*/ // } //Ne sert plus grâce à l'utilisation de la navigabilité @@ -242,16 +234,16 @@ } } } + } - } protected String getName(ObjectModelAttribute attr) { return getName(attr, false); } protected String getName(ObjectModelAttribute attr, boolean isAssoc) { - String result = Util.toLowerCaseFirstLetter(attr.getName()); + String result = GeneratorUtil.toLowerCaseFirstLetter(attr.getName()); if (attr.hasAssociationClass() && !isAssoc) { - result = GeneratorUtil.getAssocAttrName(attr); + result = TopiaGeneratorUtil.getAssocAttrName(attr); } return result; } @@ -262,7 +254,7 @@ protected String getType(ObjectModelAttribute attr, boolean isAssoc) { String type = attr.getType(); - if (GeneratorUtil.notEmpty(model.getTagValue(type))) { + if (TopiaGeneratorUtil.notEmpty(model.getTagValue(type))) { String typeString = model.getTagValue(type); int bracketIndex = typeString.indexOf('('); if (bracketIndex != -1) { @@ -282,58 +274,61 @@ if (attr.hasAssociationClass() && !isAssoc) { type = attr.getAssociationClass().getQualifiedName(); } - return GeneratorUtil.getDOType(type, model); + return TopiaGeneratorUtil.getDOType(type, model); } protected void generateHibernateProperty(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { - String type = getType(attr); - if (type.trim().endsWith("[]")) { - type = type.trim().substring(0, type.trim().length()-2); -/*{<%=prefix%> <primitive-array name="<%=getName(attr)%>" table="<%=GeneratorUtil.getDBName(attr.getDeclaringElement()) + "_" + getName(attr)%>" }*/ - String accessField = attr.getTagValue(TAG_ACCESS); - if (notEmpty(accessField)) { -/*{access="<%=accessField%>" }*/ - } else { -/*{access="field" }*/ - } - String schema = GeneratorUtil.getSchemaName(attr, model); + String attrType = getType(attr); + + String accessField = "field"; + if (attr.hasTagValue(TAG_ACCESS)) { + accessField = attr.getTagValue(TAG_ACCESS); + } + String attrName = attr.getName(); + String declaringElementDBName = TopiaGeneratorUtil.getDBName(attr.getDeclaringElement()); + String tableName = declaringElementDBName + "_" + attrName; + + if (attrType.trim().endsWith("[]")) { + attrType = attrType.trim().substring(0, attrType.trim().length()-2); + + String optionalAttributes = ""; + String schema = TopiaGeneratorUtil.getSchemaName(attr, model); if (schema != null) { -/*{schema="<%=schema%>" }*/ + optionalAttributes += "schema=\"" + schema + "\" "; } + if (attr.isIndexed()) { -/*{index="<%=GeneratorUtil.getDBName(attr.getDeclaringElement()) + "_" + getName(attr)%>_idx" }*/ + String indexName = tableName + "_idx"; + optionalAttributes += "index=\"" + indexName + "\" "; } -/*{> -<%=prefix%> <key column="<%=GeneratorUtil.getDBName(attr.getDeclaringElement())%>"/> -<%=prefix%> <list-index column="<%=getName(attr)%>_idx"/> -<%=prefix%> <element type="<%=type%>"/> + +/*{<%=prefix%> <primitive-array name="<%=attrName%>" table="<%=tableName%>" access="<%=accessField%>" <%=optionalAttributes%>> +<%=prefix%> <key column="<%=declaringElementDBName%>"/> +<%=prefix%> <list-index column="<%=attrName%>_idx"/> +<%=prefix%> <element type="<%=attrType%>"/> <%=prefix%> </primitive-array> }*/ } else { -/*{<%=prefix%> <property name="<%=getName(attr)%>" type="<%=type%>" }*/ - String accessField = attr.getTagValue(TAG_ACCESS); - if (notEmpty(accessField)) { -/*{access="<%=accessField%>" }*/ - } else { -/*{access="field" }*/ - } + String optionalAttributes = ""; if (attr.isIndexed()) { -/*{index="<%=GeneratorUtil.getDBName(attr.getDeclaringElement()) + "_" + getName(attr)%>_idx" }*/ + String indexName = tableName + "_idx"; + optionalAttributes += "index=\"" + indexName + "\" "; } - String notNull = attr.getTagValue(GeneratorUtil.TAG_NOT_NULL); - if (notNull != null) { -/*{ not-null="<%=notNull.trim()%>" }*/ - } - String[] columnNames = this.columnNamesMap.get(type); + optionalAttributes += generateFromTagValue(attr, TopiaGeneratorUtil.TAG_NOT_NULL, "not-null"); +/*{<%=prefix%> <property name="<%=attrName%>" type="<%=attrType%>" access="<%=accessField%>" }*/ + + String[] columnNames = this.columnNamesMap.get(attrType); if (columnNames == null || columnNames.length == 0) { -/*{column="<%=GeneratorUtil.getDBName(attr)%>"<%=(GeneratorUtil.notEmpty(attr.getTagValue(GeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(GeneratorUtil.TAG_LENGTH) + "\""):"")%> node="<%=getName(attr)%>"/> + optionalAttributes += generateFromTagValue(attr, TopiaGeneratorUtil.TAG_LENGTH, "length"); + String attrColumn = TopiaGeneratorUtil.getDBName(attr); +/*{column="<%=attrColumn%>" node="<%=attrName%>" <%=optionalAttributes%>/> }*/ } else { -/*{> +/*{<%=optionalAttributes%>> }*/ for (String columnName : columnNames) { - columnName = columnName.trim(); -/*{<%=prefix%> <column name="<%=getName(attr) + "_" + columnName%>"/> + columnName = attrName + "_" + columnName.trim(); +/*{<%=prefix%> <column name="<%=columnName%>"/> }*/ } /*{<%=prefix%> </property> @@ -345,7 +340,7 @@ protected void generateHibernateOneToOne(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { generateHibernateManyToOne(output, attr, true, prefix); // boolean accessField = hasUnidirectionalRelationOnAbstractType(attr.getReverseAttribute(), model); -/// *{ <one-to-one name="<%=getName(attr)%>" class="<%=getType(attr)%>"<%=(GeneratorUtil.notEmpty(attr.getTagValue(GeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(GeneratorUtil.TAG_LENGTH) + "\""):"")%><%=((attr.isComposite() || attr.hasAssociationClass())?" cascade=\"delete\"":"")%><%=((accessField)?" access=\"field\"":"")%> node="<%=getName(attr)%>/@topiaId" embed-xml="false"/> +/// *{ <one-to-one name="<%=getName(attr)%>" class="<%=getType(attr)%>"<%=(TopiaGeneratorUtil.notEmpty(attr.getTagValue(GeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(GeneratorUtil.TAG_LENGTH) + "\""):"")%><%=((attr.isComposite() || attr.hasAssociationClass())?" cascade=\"delete\"":"")%><%=((accessField)?" access=\"field\"":"")%> node="<%=getName(attr)%>/@topiaId" embed-xml="false"/> //} */ } @@ -353,68 +348,81 @@ boolean needsIndex = attr.isIndexed(); boolean isInverse = attr.getReverseAttribute().isNavigable(); isInverse |= hasUnidirectionalRelationOnAbstractType(attr, model); - String orderBy = attr.getTagValue(GeneratorUtil.TAG_ORDER_BY); - if (orderBy == null) { - orderBy = ""; - } else { - orderBy = " order-by=\"" + orderBy + "\""; - } + String attrName = getName(attr); + String attrType = getType(attr); + String reverseAttrDBName = TopiaGeneratorUtil.getReverseDBName(attr); + String orderBy = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_ORDER_BY, "order-by"); + String cascade = ""; if (attr.isComposite() || attr.hasAssociationClass()) { - cascade += " cascade=\"all,delete-orphan\""; + cascade += "cascade=\"all,delete-orphan\" "; } - String lazy = " lazy=\""; - if (notEmpty(attr.getTagValue(GeneratorUtil.TAG_LAZY))){ - lazy += attr.getTagValue(GeneratorUtil.TAG_LAZY); - } - else { - lazy += "true"; - } - lazy += "\""; + String lazy = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_LAZY, "lazy", "true"); - String fetch = ""; - if (notEmpty(attr.getTagValue(GeneratorUtil.TAG_FETCH))){ - fetch = " fetch=\"" + attr.getTagValue(GeneratorUtil.TAG_FETCH) + "\""; + String fetch = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_FETCH, "fetch"); + + String collType = TopiaGeneratorUtil.getNMultiplicityHibernateType(attr); + String inverse = ""; + if (isInverse) { + inverse = "inverse=\"true\" "; } - - String collType = GeneratorUtil.getNMultiplicityHibernateType(attr); - if (!needsIndex) { - //fixme pour le moment, on ne calcule pas si on doit autoriser le embed-xml à true - // on le positionne manuellement - //TC-20090115 embed-xml wasat true but nobody could tellme why -/*{<%=prefix%> <<%=collType%> name="<%=getName(attr)%>"<%=orderBy%><%=((!isInverse)?"":" inverse=\"true\"")%><%=fetch%><%=lazy%><%=cascade%> node="<%=getName(attr)%>" embed-xml="false"> -<%=prefix%> <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> -<%=prefix%> <one-to-many class="<%=getType(attr)%>" node="topiaId" embed-xml="false"/> + if (needsIndex) { +/*{<%=prefix%> <<%=collType%> name="<%=attrName%>" <%=inverse%><%=lazy%><%=cascade%>node="<%=attrName%>" embed-xml="false"> +<%=prefix%> <key column="<%=reverseAttrDBName%>"/> +<%=prefix%> <list-index column="<%=reverseAttrDBName%>_idx"/> +<%=prefix%> <one-to-many class="<%=attrType%>" node="topiaId" embed-xml="false"/> <%=prefix%> </<%=collType%>> }*/ }else { -/*{<%=prefix%> <<%=collType%> name="<%=getName(attr)%>"<%=((!isInverse)?"":" inverse=\"true\"")%><%=lazy%><%=cascade%> node="<%=getName(attr)%>" embed-xml="false"> -<%=prefix%> <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> -<%=prefix%> <list-index column="<%=GeneratorUtil.getReverseDBName(attr)%>_idx"/> -<%=prefix%> <one-to-many class="<%=getType(attr)%>" node="topiaId" embed-xml="false"/> +//fixme pour le moment, on ne calcule pas si on doit autoriser le embed-xml à true +// on le positionne manuellement +//TC-20090115 embed-xml wasat true but nobody could tellme why +/*{<%=prefix%> <<%=collType%> name="<%=attrName%>" <%=inverse%><%=orderBy%><%=fetch%><%=lazy%><%=cascade%>node="<%=attrName%>" embed-xml="false"> +<%=prefix%> <key column="<%=reverseAttrDBName%>"/> +<%=prefix%> <one-to-many class="<%=attrType%>" node="topiaId" embed-xml="false"/> <%=prefix%> </<%=collType%>> }*/ } } - protected void generateHibernateMany(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { + private String generateFromTagValue(ObjectModelAttribute attr, + String tagName, String attributeName) { + return generateFromTagValue(attr, tagName, attributeName, null); + } + + private String generateFromTagValue(ObjectModelAttribute attr, + String tagName, String attributeName, String defaultValue) { + String result = ""; + if (attr.hasTagValue(tagName) || defaultValue != null) { + result+= attributeName + "=\""; + if (attr.hasTagValue(tagName)) { + result += attr.getTagValue(tagName); + } else { + result += defaultValue; + } + result += "\" "; + } + return result; + } + + protected void generateHibernateMany(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { boolean needsIndex = attr.isIndexed(); - String collType = GeneratorUtil.getNMultiplicityHibernateType(attr); - String lazy = ""; - if (attr.getTagValue(GeneratorUtil.TAG_LAZY) != null) { - lazy = " lazy=\"" + attr.getTagValue(GeneratorUtil.TAG_LAZY) + "\""; - } + String attrName = getName(attr); + String attrType = getType(attr); + String collType = TopiaGeneratorUtil.getNMultiplicityHibernateType(attr); + String lazy = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_LAZY, "lazy"); + String attrColumn = TopiaGeneratorUtil.getDBName(attr); -/*{<%=prefix%> <<%=collType%> name="<%=getName(attr)%>" node="<%=getName(attr)%>" embed-xml="true"<%=lazy%>> +/*{<%=prefix%> <<%=collType%> name="<%=attrName%>" node="<%=attrName%>" embed-xml="true"<%=lazy%>> <%=prefix%> <key column="OWNER"/> }*/ if (needsIndex) { /*{<%=prefix%> <list-index/> }*/ } -/*{<%=prefix%> <element type="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="id"/> +/*{<%=prefix%> <element type="<%=attrType%>" column="<%=attrColumn%>" node="id"/> <%=prefix%> </<%=collType%>> }*/ } @@ -424,7 +432,10 @@ } protected void generateHibernateManyToOne(Writer output, ObjectModelAttribute attr, boolean isUnique, String prefix) throws IOException { -/*{<%=prefix%> <many-to-one name="<%=getName(attr)%>" class="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>"}*/ + String attrName = getName(attr); + String attrType = getType(attr); + String attrColumn = TopiaGeneratorUtil.getDBName(attr); +/*{<%=prefix%> <many-to-one name="<%=attrName%>" class="<%=attrType%>" column="<%=attrColumn%>"}*/ if (attr.isComposite() || attr.hasAssociationClass()) { /*{ cascade="delete"}*/ } @@ -437,16 +448,13 @@ if (isUnique) { /*{ unique="true"}*/ } -/*{ node="<%=getName(attr)%>/@topiaId" embed-xml="false"}*/ +/*{ node="<%=attrName%>/@topiaId" embed-xml="false"}*/ // vérifier si le tag lazy est defini par defaut dans le fichier de proprietes - if (attr.getTagValue(GeneratorUtil.TAG_LAZY) != null){ -/*{ lazy="<%=attr.getTagValue("lazy")%>"}*/ - } - String notNull = attr.getTagValue(GeneratorUtil.TAG_NOT_NULL); - if (notNull != null) { -/*{ not-null="<%=notNull.trim()%>"}*/ - } + String lazy = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_LAZY, "lazy"); +/*{<%=lazy%>}*/ + String notNull = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_NOT_NULL, "not-null"); +/*{<%=notNull%>}*/ /*{/> }*/ } @@ -458,36 +466,34 @@ boolean isInverse = attr.isNavigable() && attr.getReverseAttribute().isNavigable(); //isInverse |= !Util.isFirstAttribute(attr); //isInverse = false; // 20070117 poussin: pour du many, jamais de inverse - isInverse &= Util.isFirstAttribute(attr); + isInverse &= GeneratorUtil.isFirstAttribute(attr); boolean needsIndex = attr.isIndexed(); String cascade = ""; if (attr.isComposite() || attr.hasAssociationClass()) { - // a quoi ca sert ? de concatener "" avec autre chose ??? - //cascade += "cascade=\"delete,delete-orphan\""; cascade = " cascade=\"delete,delete-orphan\""; } - // a quoi ca sert ? :) - //cascade += ""; - String lazy = " lazy=\""; - if (attr.getTagValue(GeneratorUtil.TAG_LAZY) != null){ - lazy += attr.getTagValue(GeneratorUtil.TAG_LAZY); + String attrType = getType(attr); + String attrName = getName(attr); + String attrColumn = TopiaGeneratorUtil.getDBName(attr); + String lazy = generateFromTagValue(attr, TopiaGeneratorUtil.TAG_LAZY, "lazy", "true"); + String collType = TopiaGeneratorUtil.getNMultiplicityHibernateType(attr); + String tableName = TopiaGeneratorUtil.getManyToManyTableName(attr); + String inverse = ""; + if (isInverse) { + inverse = "inverse=\"true\" "; } - else { - lazy += "true"; - } - lazy += "\""; - String collType = GeneratorUtil.getNMultiplicityHibernateType(attr); + String reverseAttrDBName = TopiaGeneratorUtil.getReverseDBName(attr); -/*{<%=prefix%> <<%=collType%> name="<%=getName(attr)%>" table="<%=GeneratorUtil.getManyToManyTableName(attr)%>"<%=(isInverse?" inverse=\"true\" ":"")%><%=lazy%><%=cascade%> node="<%=getName(attr)%>" embed-xml="true"> -<%=prefix%> <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> +/*{<%=prefix%> <<%=collType%> name="<%=attrName%>" table="<%=tableName%>" <%=inverse%><%=lazy%><%=cascade%> node="<%=attrName%>" embed-xml="true"> +<%=prefix%> <key column="<%=reverseAttrDBName%>"/> }*/ if (needsIndex) { -/*{<%=prefix%> <list-index column="<%=GeneratorUtil.getReverseDBName(attr)%>_idx"/> +/*{<%=prefix%> <list-index column="<%=reverseAttrDBName%>_idx"/> }*/ } -/*{<%=prefix%> <many-to-many class="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="topiaId"/> +/*{<%=prefix%> <many-to-many class="<%=attrType%>" column="<%=attrColumn%>" node="topiaId"/> <%=prefix%> </<%=collType%>> }*/ } Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityImplGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityImplGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityImplGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -60,7 +60,7 @@ @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY) || clazz.getOperations().size() > 0) { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY) || clazz.getOperations().size() > 0) { return; } //De même, on ne génère pas le impl si il y a des opérations venant des @@ -70,44 +70,29 @@ return; } } - - boolean isAbstract = isAbstract(clazz); - //boolean isAbstract = clazz.isAbstract(); - //Une classe peut être abstraite si elle a des méthodes définies dans - // ses superinterface et non implantées dans ses superclasses - /*if (!isAbstract) { - for (Iterator operations = clazz.getAllInterfaceOperations(true).iterator(); (!isAbstract) && operations.hasNext(); ) { - ObjectModelOperation op = (ObjectModelOperation)operations.next(); - boolean implementationFound = false; - for (Iterator superClasses = clazz.getSuperclasses().iterator(); (!implementationFound) && superClasses.hasNext(); ) { - ObjectModelClass superClazz = (ObjectModelClass)superClasses.next(); - for (Iterator matchingOps = superClazz.getOperations(op.getName()).iterator(); (!implementationFound) && matchingOps.hasNext(); ) { - ObjectModelOperation matchingOp = (ObjectModelOperation)matchingOps.next(); - implementationFound = (op.equals(matchingOp) && !matchingOp.isAbstract()); - } - } - isAbstract = !implementationFound; - } - }*/ - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } + String clazzName = clazz.getName(); + String clazzFQN = clazz.getQualifiedName(); + String abstractStr = isAbstract(clazz)?"abstract ":" "; /*{package <%=clazz.getPackageName()%>; import java.io.Serializable; /** - * Implantation des operations pour l'entité <%=GeneratorUtil.capitalize(clazz.getName())%>. + * Implantation des operations pour l'entité <%=TopiaGeneratorUtil.capitalize(clazz.getName())%>. *) -public <%=(isAbstract?"abstract ":" ")%>class <%=clazz.getName()%>Impl extends <%=clazz.getQualifiedName()%>Abstract implements Serializable, <%=clazz.getQualifiedName()%> { +public <%=abstractStr%>class <%=clazzName%>Impl extends <%=clazzFQN%>Abstract implements Serializable, <%=clazzFQN%> { private static final long serialVersionUID = 1L; }*/ -/*{} //<%=clazz.getName()%>Impl +/*{} //<%=clazzName%>Impl }*/ } @@ -142,4 +127,4 @@ return false; } -} //EntityPOJOGenerator +} //EntityImplGenerator Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityInterfaceGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -33,7 +33,7 @@ import org.apache.commons.logging.LogFactory; import org.codelutin.generator.Generator; import org.codelutin.generator.ObjectModelGenerator; -import org.codelutin.generator.Util; +import org.codelutin.generator.GeneratorUtil; import org.codelutin.generator.models.object.ObjectModelAssociationClass; import org.codelutin.generator.models.object.ObjectModelAttribute; import org.codelutin.generator.models.object.ObjectModelClass; @@ -41,11 +41,11 @@ import org.codelutin.generator.models.object.ObjectModelInterface; import org.codelutin.generator.models.object.ObjectModelOperation; import org.codelutin.generator.models.object.ObjectModelParameter; -import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; -import static org.codelutin.topia.generator.GeneratorUtil.isBooleanType; -import static org.codelutin.topia.generator.GeneratorUtil.isDateType; -import static org.codelutin.topia.generator.GeneratorUtil.isNumericType; -import static org.codelutin.topia.generator.GeneratorUtil.isTextType; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.isBooleanType; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.isDateType; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.isNumericType; +import static org.codelutin.topia.generator.TopiaGeneratorUtil.isTextType; import java.io.File; import java.io.IOException; @@ -85,7 +85,7 @@ */ @Override public void generateFromInterface(Writer output, ObjectModelInterface interfacez) throws IOException { - if (!interfacez.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (!interfacez.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { return; } @@ -93,7 +93,7 @@ log.debug("Generating interface for : " + interfacez.getName()); } - generateInterfaceHeader(output, interfacez); + generateInterfaceHeaderFromClassifier(output, interfacez); generateInterfaceOperations(output, interfacez); @@ -127,7 +127,7 @@ allAttrs.addAll(clazz.getAllOtherAttributes()); boolean needAnnotation = false; for (ObjectModelAttribute attr : allAttrs) { - String name = Util.toLowerCaseFirstLetter(attr.getName()); + String name = GeneratorUtil.toLowerCaseFirstLetter(attr.getName()); if (isTextType(attr)) { txtFields.add(name); needAnnotation = true; @@ -173,28 +173,32 @@ }*/ } - private void generateInterfaceHeader(Writer output, ObjectModelClassifier classifier) throws IOException { - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + private void generateInterfaceHeaderFromClassifier(Writer output, ObjectModelClassifier classifier) throws IOException { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } /*{package <%=classifier.getPackageName()%>; import org.codelutin.topia.persistence.SearchFields; +import org.codelutin.topia.persistence.TopiaEntity; }*/ - if (GeneratorUtil.hasDocumentation(classifier)) { + if (TopiaGeneratorUtil.hasDocumentation(classifier)) { + String documentation = classifier.getDocumentation(); /*{ /** - * <%=classifier.getDocumentation()%> + * <%=documentation%> *) }*/ } + // if (classifier instanceof ObjectModelClass) { generateSearchFields(output, (ObjectModelClass) classifier); } -/*{public interface <%=classifier.getName()%> extends }*/ + String classifierName = classifier.getName(); +/*{public interface <%=classifierName%> extends }*/ String extendClass = ""; for (ObjectModelClassifier parent : classifier.getInterfaces()) { extendClass += parent.getQualifiedName(); @@ -203,214 +207,208 @@ if (classifier instanceof ObjectModelClass) { ObjectModelClass clazz = (ObjectModelClass) classifier; for (ObjectModelClassifier parent : clazz.getSuperclasses()) { - if (parent.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (parent.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { extendClass += parent.getQualifiedName(); } extendClass += ", "; } } -/*{<%=extendClass%>org.codelutin.topia.persistence.TopiaEntity { +/*{<%=extendClass%>TopiaEntity { }*/ } @Override public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { - if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)) { return; } - generateInterfaceHeader(output, clazz); + String clazzName = clazz.getName(); + generateInterfaceHeaderFromClassifier(output, clazz); for (ObjectModelAttribute attr : clazz.getAttributes()) { ObjectModelAttribute reverse = attr.getReverseAttribute(); if (!attr.isNavigable() && !hasUnidirectionalRelationOnAbstractType(reverse, model)) { continue; } - if (!Util.isNMultiplicity(attr)) { - if (!attr.hasAssociationClass()) { + + String attrName = attr.getName(); + String attrType = attr.getType(); + if (!attr.hasAssociationClass()) { + if (!GeneratorUtil.isNMultiplicity(attr)) { /*{ /** }*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attrName%> : <%=attr.getDocumentation()%> }*/ } -/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> La valeur de l'attribut <%=attr.getName()%> à positionner. +/*{ * @param <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%> La valeur de l'attribut <%=attrName%> à positionner. *) - public void set<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>); }*/ /*{ /** }*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attrName%> : <%=attr.getDocumentation()%> }*/ } -/*{ * @return La valeur de l'attribut <%=attr.getName()%>. +/*{ * @return La valeur de l'attribut <%=attrName%>. *) - public <%=attr.getType()%> get<%=Util.capitalize(attr.getName())%>(); + public <%=attrType%> get<%=GeneratorUtil.capitalize(attrName)%>(); }*/ } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } + String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); /*{ /** - * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> La valeur de l'attribut <%=attr.getAssociationClass().getName()%> à positionner. - *) - public void set<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); - - /** - * @return La valeur de l'attribut <%=attr.getAssociationClass().getName()%>. - *) - public <%=attr.getAssociationClass().getQualifiedName()%> get<%=Util.capitalize(assocAttrName)%>(); - }*/ - } - } else { //NMultiplicity - String collectionInterface = GeneratorUtil.getNMultiplicityInterfaceType(attr); - if (!attr.hasAssociationClass()) { //Méthodes remplacées par des add/set sur les classes d'assoc -/*{ /** + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attrName%> : <%=attr.getDocumentation()%> }*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> -}*/ } -/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> L'instance de <%=attr.getName()%> à ajouter. +/*{ * @param <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%> L'instance de <%=attrName%> à ajouter. *) - public void add<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + public void add<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>); /** }*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attrName%> : <%=attr.getDocumentation()%> }*/ } -/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> Les instances de <%=attr.getName()%> à ajouter. +/*{ * @param <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%> Les instances de <%=attrName%> à ajouter. *) - public void addAll<%=Util.capitalize(attr.getName())%>(<%=collectionInterface%><<%=attr.getType()%>> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + public void addAll<%=GeneratorUtil.capitalize(attrName)%>(<%=collectionInterface%><<%=attrType%>> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>); /** }*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attrName%> : <%=attr.getDocumentation()%> }*/ } -/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> La Collection de <%=attr.getName()%> à positionner. +/*{ * @param <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%> La Collection de <%=attrName%> à positionner. *) - public void set<%=Util.capitalize(attr.getName())%>(<%=collectionInterface%><<%=attr.getType()%>> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=collectionInterface%><<%=attrType%>> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>); /** }*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attrName%> : <%=attr.getDocumentation()%> }*/ } -/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> L'instance de <%=attr.getName()%> à retirer. +/*{ * @param <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%> L'instance de <%=attrName%> à retirer. *) - public void remove<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + public void remove<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>); /** }*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attrName%> : <%=attr.getDocumentation()%> }*/ } -/*{ * Vide la Collection de <%=attr.getName()%>. +/*{ * Vide la Collection de <%=attrName%>. *) - public void clear<%=Util.capitalize(attr.getName())%>(); + public void clear<%=GeneratorUtil.capitalize(attrName)%>(); }*/ - } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } + /*{ /** - * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> L'instance de <%=attr.getAssociationClass().getName()%> à ajouter. +}*/ + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attrName%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * @return La Liste de <%=attrName%>. *) - public void add<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); + public <%=collectionInterface%><<%=attrType%>> get<%=GeneratorUtil.capitalize(attrName)%>(); /** - * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> Les instances de <%=attr.getAssociationClass().getName()%> à ajouter. + * Recupère l'attribut <%=attrName%> à partir de son topiaId. + * + * @param topiaId le topia id de l'entité recherchée + * + * @return l'attribut recherché, ou <code>null</code> s'il n'existe pas. *) - public void addAll<%=Util.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); + public <%=attrType%> get<%=GeneratorUtil.capitalize(attrName)%>ByTopiaId(String topiaId); /** - * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> La Collection de <%=attr.getAssociationClass().getName()%> à positionner. + * @return Le nombre d'éléments de la collection <%=attrName%>. *) - public void set<%=Util.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); + public int size<%=GeneratorUtil.capitalize(attrName)%>(); - /** - * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> L'instance de <%=attr.getAssociationClass().getName()%> à retirer. +}*/ + + } + } else { + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + String assocClassFQN = attr.getAssociationClass().getQualifiedName(); + String assocClassName = attr.getAssociationClass().getName(); + if (!GeneratorUtil.isNMultiplicity(attr)) { +/*{ /** + * @param <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> La valeur de l'attribut <%=assocClassName%> à positionner. *) - public void remove<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); + public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%>); /** - * Vide la Collection de <%=attr.getAssociationClass().getName()%>. + * @return La valeur de l'attribut <%=assocClassName%>. *) - public void clear<%=Util.capitalize(assocAttrName)%>(); + public <%=assocClassFQN%> get<%=GeneratorUtil.capitalize(assocAttrName)%>(); }*/ - } - - if (!attr.hasAssociationClass()) { + } else { + String collectionInterface = TopiaGeneratorUtil.getNMultiplicityInterfaceType(attr); /*{ /** -}*/ - if (GeneratorUtil.hasDocumentation(attr)) { -/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> -}*/ - } -/*{ * @return La Liste de <%=attr.getName()%>. + * @param <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> L'instance de <%=assocClassName%> à ajouter. *) - public <%=collectionInterface%><<%=attr.getType()%>> get<%=Util.capitalize(attr.getName())%>(); + public void add<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%>); /** - * Recupère l'attribut <%=attr.getName()%> à partir de son topiaId. - * - * @param topiaId le topia id de l'entité recherchée - * - * @return l'attribut recherché, ou <code>null</code> s'il n'existe pas. + * @param <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> Les instances de <%=assocClassName%> à ajouter. *) - public <%=attr.getType()%> get<%=Util.capitalize(attr.getName())%>ByTopiaId(String topiaId); - + public void addAll<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=assocClassFQN%>> <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%>); + /** - * @return Le nombre d'éléments de la collection <%=attr.getName()%>. + * @param <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> La Collection de <%=assocClassName%> à positionner. *) - public int size<%=Util.capitalize(attr.getName())%>(); + public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=collectionInterface%><<%=assocClassFQN%>> <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%>); -}*/ - } else { - String assocAttrName = GeneratorUtil.getAssocAttrName(attr); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } -/*{ /** - * @return La liste des attributs <%=attr.getAssociationClass().getName()%>. + /** + * @param <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%> L'instance de <%=assocClassName%> à retirer. *) - public <%=collectionInterface%><<%=attr.getAssociationClass().getQualifiedName()%>> get<%=Util.capitalize(assocAttrName)%>(); + public void remove<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocClassName)%>); /** - * Recupère l'attribut <%=attr.getName()%> à partir de son topiaId. + * Vide la Collection de <%=assocClassName%>. + *) + public void clear<%=GeneratorUtil.capitalize(assocAttrName)%>(); + + /** + * @return La liste des attributs <%=assocClassName%>. + *) + public <%=collectionInterface%><<%=assocClassFQN%>> get<%=GeneratorUtil.capitalize(assocAttrName)%>(); + + /** + * Recupère l'attribut <%=attrName%> à partir de son topiaId. * * @param topiaId le topia id de l'attribut recherchée * * @return l'attribut recherché, ou <code>null</code> s'il n'existe pas. *) - public <%=attr.getAssociationClass().getQualifiedName()%> get<%=Util.capitalize(assocAttrName)%>ByTopiaId(String topiaId); + public <%=assocClassFQN%> get<%=GeneratorUtil.capitalize(assocAttrName)%>ByTopiaId(String topiaId); /** - * @return L'attribut <%=attr.getAssociationClass().getName()%> associé à la valeur <code>value</code> de l'attribut <%=attr.getName()%>. + * @return L'attribut <%=assocClassName%> associé à la valeur <code>value</code> de l'attribut <%=attrName%>. *) - public <%=attr.getAssociationClass().getQualifiedName()%> get<%=Util.capitalize(assocAttrName)%>(<%=attr.getType()%> value); + public <%=assocClassFQN%> get<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=attrType%> value); /** - * @return Le nombre d'éléments de la collection <%=attr.getName()%>. + * @return Le nombre d'éléments de la collection <%=attrName%>. *) - public int size<%=Util.capitalize(assocAttrName)%>(); + public int size<%=GeneratorUtil.capitalize(assocAttrName)%>(); }*/ + } } } @@ -434,45 +432,44 @@ generateInterfaceOperations(output, clazz); -/*{} //<%=clazz.getName()%> +/*{} //<%=clazzName%> }*/ } private void generateInterfaceOperations(Writer output, ObjectModelClassifier classifier) throws IOException { for (ObjectModelOperation op : classifier.getOperations()) { + String opName = op.getName(); /*{ /** }*/ - if (GeneratorUtil.hasDocumentation(op)) { -/*{ * <%=op.getName()%> : <%=op.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(op)) { + String opDocumentation = op.getDocumentation(); +/*{ * <%=opName%> : <%=opDocumentation%> }*/ } Collection<ObjectModelParameter> params = op.getParameters(); for (ObjectModelParameter param : params) { - if (log.isTraceEnabled()) { - log.trace("Param" + param); - } -/*{ * @param <%=param.getName()%> <%=param.getDocumentation()%> + String paramName = param.getName(); + String paramDocumentation = param.getDocumentation(); +/*{ * @param <%=paramName%> <%=paramDocumentation%> }*/ } + String opVisibility = op.getVisibility(); + String opType = op.getReturnType(); /*{ *) - <%=op.getVisibility()%> <%=op.getReturnType()%> <%=op.getName()%>(}*/ - String vir = ""; + <%=opVisibility%> <%=opType%> <%=opName%>(}*/ + String comma = ""; for (ObjectModelParameter param : params) { - if (log.isTraceEnabled()) { - log.trace("Param" + param + " vir" + vir); - } -/*{<%=vir%><%=param.getType()%> <%=param.getName()%>}*/ - vir = ", "; + String paramName = param.getName(); + String paramType = param.getType(); +/*{<%=comma%><%=paramType%> <%=paramName%>}*/ + comma = ", "; } /*{)}*/ Set<String> exceptions = op.getExceptions(); - vir = " throws "; + comma = " throws "; for (String exception : exceptions) { - if (log.isTraceEnabled()) { - log.trace("exception" + exception + " vir" + vir); - } -/*{<%=vir%><%=exception%>}*/ - vir = ", "; +/*{<%=comma%><%=exception%>}*/ + comma = ", "; } /*{; @@ -480,16 +477,16 @@ } } - private void generateAssociationAccessors(Writer output, String name, String type) throws IOException { + private void generateAssociationAccessors(Writer output, String attrName, String attrType) throws IOException { /*{ /** - * @param value La valeur de l'attribut <%=name%> à positionner. + * @param value La valeur de l'attribut <%=attrName%> à positionner. *) - public void set<%=Util.capitalize(name)%>(<%=type%> value); + public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> value); /** - * @return La valeur de l'attribut <%=name%>. + * @return La valeur de l'attribut <%=attrName%>. *) - public <%=type%> get<%=Util.capitalize(name)%>(); + public <%=attrType%> get<%=GeneratorUtil.capitalize(attrName)%>(); }*/ } Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityProviderGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityProviderGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityProviderGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -67,22 +67,26 @@ @Override public void generateFromModel(Writer output, ObjectModel model) throws IOException { - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } + String modelName = model.getName(); /*{package <%=getProperty("defaultPackage")%>; import org.codelutin.topia.persistence.TopiaEntity; +import java.util.Map; +import java.util.HashMap; +import java.util.Collections; @SuppressWarnings({"unchecked"}) -public class <%=model.getName()%>EntityProvider { +public class <%=modelName%>EntityProvider { - protected static java.util.Map<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>> cache; + protected static Map<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>> cache; }*/ - List<ObjectModelClass> classes = GeneratorUtil.getEntityClasses(model,true); + List<ObjectModelClass> classes = TopiaGeneratorUtil.getEntityClasses(model, true); if (classes.isEmpty()) { /*{ protected static Class<? extends TopiaEntity>[] entitiesClass = new Class[]{}; @@ -92,17 +96,18 @@ protected static Class<? extends TopiaEntity>[] entitiesClass = new Class[]{ }*/ } - for (Iterator i=classes.iterator(); i.hasNext();) { - ObjectModelClass clazz = (ObjectModelClass)i.next(); -/*{ <%=clazz.getQualifiedName()%>.class<%=(i.hasNext()?", ":"\n};")%> -}*/ + for (Iterator<ObjectModelClass> i=classes.iterator(); i.hasNext();) { + ObjectModelClass clazz = i.next(); + String clazzFQN = clazz.getQualifiedName(); +/*{ <%=clazzFQN%>.class<%=(i.hasNext()?", ":"\n};")%> +}*/ } /*{ public static <E extends TopiaEntity> Class<E> getImpl(Class<E> klazz) { return (Class<E>) getCache().get(klazz); } - public static java.util.Map<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>> getCache() { + public static Map<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>> getCache() { if (cache == null) { initCache(); } @@ -110,7 +115,7 @@ } private static void initCache() { - java.util.Map<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>> cache = new java.util.HashMap<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>>(); + Map<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>> cache = new HashMap<Class<? extends TopiaEntity>, Class<? extends TopiaEntity>>(); for (Class<? extends TopiaEntity> entitiesClas : entitiesClass) { String implFQN = entitiesClas.getName() + "Impl"; try { @@ -120,15 +125,16 @@ throw new RuntimeException("could not find entity implementation class " + implFQN); } } - <%=model.getName()%>EntityProvider.cache = java.util.Collections.unmodifiableMap(cache); + <%=modelName%>EntityProvider.cache = Collections.unmodifiableMap(cache); } /** * should have no instance *) - protected <%=model.getName()%>EntityProvider() { + protected <%=modelName%>EntityProvider() { } -} + +} //<%=modelName%>EntityProvider }*/ } Deleted: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/GeneratorUtil.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/GeneratorUtil.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/GeneratorUtil.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -1,685 +0,0 @@ -/* *##% ToPIA - Tools for Portable and Independent Architecture - * Copyright (C) 2004 - 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%*/ - -/******************************************************************************* - * GeneratorUtil.java - * - * Created: 13 déc. 2005 - * - * @author Arnaud Thimel <thimel@codelutin.com> - * - * @version $Revision$ - * - * Mise a jour: $Date$ par : $Author$ - */ - -package org.codelutin.topia.generator; - -import org.apache.commons.lang.StringUtils; -import org.codelutin.generator.Generator; -import org.codelutin.generator.Util; -import org.codelutin.generator.models.Model; -import org.codelutin.generator.models.object.ObjectModel; -import org.codelutin.generator.models.object.ObjectModelAttribute; -import org.codelutin.generator.models.object.ObjectModelClass; -import org.codelutin.generator.models.object.ObjectModelElement; -import org.codelutin.generator.models.object.ObjectModelInterface; -import org.codelutin.generator.models.object.ObjectModelOperation; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; - -/** Classe regroupant divers méthodes utiles pour la génération des entités */ -public class GeneratorUtil extends Util { - - /** Stéréotype pour les interfaces devant être générées sous forme de facades */ - public final static String STEREOTYPE_FACADE = "facade"; - - /** Stéréotype pour les objets devant être générées sous forme d'entités */ - public static final String STEREOTYPE_ENTITY = "entity"; - - /** Stéréotype pour les objets devant être générées sous forme de DTO */ - public static final String STEREOTYPE_DTO = "dto"; - - /** - * Stéréotype pour les interfaces devant être générées sous forme de - * services - */ - public static final String STEREOTYPE_SERVICE = "service"; - - /** Stéréotype pour les interfaces devant être générées sous forme de DAO */ - public static final String STEREOTYPE_DAO = "dao"; - - /** Stéréotype pour les attributs à indexer en base */ - public static final String STEREOTYPE_INDEXED = "indexed"; - - /** Stéréotype pour les collections avec unicité */ - public static final String STEREOTYPE_UNIQUE = "unique"; - - /** Stéréotype pour les attributs étant des clés primaires */ - public static final String STEREOTYPE_PRIMARYKAY = "primaryKey"; - - /** Tag pour le type de persistence */ - public static final String TAG_PERSISTENCE_TYPE = "persistenceType"; - - /** Tag pour le nom du champ / entité en BD */ - public static final String TAG_DB_NAME = "dbName"; - - /** Tag pour le nom du schema en BD */ - public static final String TAG_SCHEMA_NAME = "dbSchema"; - - /** Tag pour la taille du champ en BD */ - public static final String TAG_LENGTH = "length"; - - /** Tag pour ajouter une annotation à un champ */ - public static final String TAG_ANNOTATION = "annotation"; - - /** Tag pour ajouter specifier le copyright d'un fichier */ - public static final String TAG_COPYRIGHT = "copyright"; - - /** Tag pour specfier le type d'acces a un champ */ - public static final String TAG_ACCESS = "access"; - - /** Tag pour specfier si on doit générer i18n */ - public static final String TAG_I18N_PREFIX = "i18n"; - - /** Tag pour ajouter un attribut dans une clef métier */ - public static final String TAG_NATURAL_ID = "naturalId"; - - /** Tag pour specifier si une clef metier est mutable */ - public static final String TAG_NATURAL_ID_MUTABLE = "naturalIdMutable"; - - /** Tag pour spécifier la caractèrelazy d'une association multiple */ - public static final String TAG_LAZY = "lazy"; - - /** Tag pour spécifier la caractère fetch d'une association multiple */ - public static final String TAG_FETCH = "fetch"; - - /** Tag pour spécifier la caractère order-by d'une association multiple */ - public static final String TAG_ORDER_BY = "orderBy"; - - /** Tag pour spécifier la caractère not-null d'un attribut */ - public static final String TAG_NOT_NULL = "notNull"; - - /** Tag pour spécifier la caractère embed-xml d'une association */ - public static final String TAG_EMBED_XML = "embedXml"; - - /** Type de persistence Hibernate */ - public static final String PERSISTENCE_TYPE_HIBERNATE = "hibernate"; - - /** Type de persistence LDAP */ - public static final String PERSISTENCE_TYPE_LDAP = "ldap"; - - /** Type de persistence par défaut (si aucun précisé) */ - public static final String PERSISTENCE_TYPE_DEFAULT = PERSISTENCE_TYPE_HIBERNATE; - - /** Propriété des générateurs indiquant le package par défaut */ - public static final String PROPERTY_DEFAULT_PACKAGE = "defaultPackage"; - - /** Le package par défaut si aucun n'est spécifié */ - public static final String DEFAULT_PACKAGE = "org.codelutin.malo"; - - /** - * Renvoie le package par défaut pour le générateur donné - * - * @param generator le générateur donné - * @return le package par défaut du générator donné - */ - public static String getDefaultPackage(Generator generator) { - String packageName = generator.getProperty(PROPERTY_DEFAULT_PACKAGE); - if (packageName == null || "".equals(packageName)) { - packageName = DEFAULT_PACKAGE; - } - return packageName; - } - - /** - * Indique si l'élément spécifié dispose de documentation - * - * @param element l'élément à tester - * @return true s'il y a documentation, false sinon - */ - public static boolean hasDocumentation(ObjectModelElement element) { - return notEmpty(element.getDocumentation()); - } - - /** - * Indique si la chaine de caratère n'est pas vide (null ou "") - * - * @param s la chaine de caractères à tester - * @return true si <code>s</code> n'est pas vide - */ - public static boolean notEmpty(String s) { - return (s != null && !"".equals(s)); - } - - /** - * Renvoie l'interface DAO associée à la classe passée en paramètre - * - * @param clazz la classe à tester - * @param model le modele utilisé - * @return l'interface trouvée ou null sinon - */ - public static ObjectModelInterface getDAOInterface(ObjectModelClass clazz, - ObjectModel model) { - for (Object o : model.getInterfaces()) { - ObjectModelInterface daoInterface = (ObjectModelInterface) o; - if (daoInterface.getName().equals(clazz.getName() + "DAO")) { - if (daoInterface.hasStereotype(STEREOTYPE_DAO)) { - return daoInterface; - } - } - } - return null; - } - - /** - * Renvoie le type de persistence pour l'élément donné. Si aucun n'est - * trouvé, le type par défaut est utilisé - * - * @param element l'élément à tester - * @return le type de persitence pour l'élément donné. - */ - public static String getPersistenceType(ObjectModelElement element) { - String tag = element.getTagValue(TAG_PERSISTENCE_TYPE); - if (tag == null) { - tag = PERSISTENCE_TYPE_DEFAULT; - } - return tag; - } - - public static String getReverseDBName(ObjectModelAttribute attr) { - if (attr.getReverseAttribute() != null) { - return getDBName(attr.getReverseAttribute()); - } else { - return getDBName(attr) + "_id"; - } - } - - /** - * Renvoie le nom BD de l'élement passé en paramètre. Elle se base sur le - * tag associé si il existe, sinon sur le nom de l'élément - * - * @param element l'élément à tester - * @return le nom de table - */ - public static String getDBName(ObjectModelElement element) { - if (element == null) { - return null; - } - if (notEmpty(element.getTagValue(TAG_DB_NAME))) { - return element.getTagValue(TAG_DB_NAME); - } - return toLowerCaseFirstLetter(element.getName()); - } - - /** - * Cherche et renvoie le schema a utiliser sur cet element, sinon sur le model. - * - * @param element l'élément à tester - * @param model le modele utilisé - * @return le nom du schema ou null - */ - public static String getSchemaName(ObjectModelElement element, - ObjectModel model) { - return findTagValue(TAG_SCHEMA_NAME, element, model); - } - - /** - * Cherche et renvoie le prefixe i18n à utiliser sur cet element, sinon sur le model. - * - * @param element l'élément à tester - * @param model le modele utilisé - * @return le prefix i18n ou <code>null</code> si non spécifié - */ - public static String getI18nPrefix(ObjectModelElement element, - ObjectModel model) { - return findTagValue(TAG_I18N_PREFIX, element, model); - } - - /** - * Cherche et renvoie la liste des attributs constituant la clef metier d'une classe. - * - * @param clazz la classe à tester - * @return la liste des attributs de la clef métier ou null si pas de clef métier. - */ - public static List<String> getNaturalId(ObjectModelClass clazz) { - String value = clazz.getTagValue(TAG_NATURAL_ID); - if (value == null || value.trim().isEmpty()) { - return java.util.Collections.emptyList(); - } - List<String> result = new ArrayList<String>(); - for (String attribute : value.split(",")) { - result.add(attribute.trim()); - } - return result; - } - - /** - * Detecte si un attribut fait partie d'une clef metier. - * - * @param attribute l'attribut à tester - * @return <code>true</code> si l'attribut fait partie d'une clef metier, <code>false</cdoe> sinon. - */ - public static boolean isNaturalId(ObjectModelAttribute attribute) { - String value = attribute.getTagValue(TAG_NATURAL_ID); - if (!notEmpty(value)) { - // valeur null, donc pas positionnee - return false; - } - try { - return Boolean.valueOf(value.trim()); - } catch (Exception e) { - // on a pas reussi a convertir en boolean. - //todo peut-être declancher une exception ? - return false; - } - } - - /** - * Cherches et renvoie le copyright a utiliser sur le model. - * - * @param model le modele utilisé - * @return le texte du copyright ou null - */ - public static String getCopyright(Model model) { - return findTagValue(TAG_COPYRIGHT, null, model); - } - - /** - * Cherches et renvoie la valeur du tagvalue indique sur cet element, - * sinon sur le model. - * - * @param tagName le nom du tag - * @param element l'élément à tester - * @param model le modele utilisé - * @return la valeur du tagValue ou null - */ - public static String findTagValue(String tagName, - ObjectModelElement element, Model model) { - if (element == null) { - if (model != null) { - if (notEmpty(model.getTagValue(tagName))) { - return model.getTagValue(tagName); - } - } - return null; - } - if (notEmpty(element.getTagValue(tagName))) { - return element.getTagValue(tagName); - } - //On va chercher sur l'element declarant - return findTagValue(tagName, element.getDeclaringElement(), model); - } - - public static <Type extends ObjectModelElement> Collection<Type> getElementsWithStereotype( - Collection<Type> elements, String... stereotypes) { - Collection<Type> result = new ArrayList<Type>(); - for (Type element : elements) { - if (hasStereotypes(element, stereotypes)) { - result.add(element); - } - } - return result; - } - - public static boolean hasStereotypes(ObjectModelElement element, - String... stereotypes) { - for (String stereotype : stereotypes) { - if (!element.hasStereotype(stereotype)) { - return false; - } - } - return true; - } - - public static String getPrimaryKeyAttributesListDeclaration( - ObjectModelClass clazz, boolean includeName) { - String attributes = ""; - for (ObjectModelAttribute attr : getElementsWithStereotype(clazz - .getAttributes(), STEREOTYPE_PRIMARYKAY)) { - attributes += attr.getType(); - if (includeName) { - attributes += " " + attr.getName(); - } - attributes += ", "; - } - if (attributes.length() > 0) { - attributes = attributes.substring(0, attributes.length() - 2); - } - return attributes; - } - - public static String capitalize(String s) { - return StringUtils.capitalize(s); - } - - public static boolean isAssociationClassDoublon(ObjectModelAttribute attr) { - return (attr.getReverseAttribute() != null) - && (attr.getDeclaringElement().equals(attr - .getReverseAttribute().getDeclaringElement())) - && (!Util.isFirstAttribute(attr)); - } - - /** - * Renvoie le nom de l'attribut de classe d'association en fonction des cas: - * Si l'attribut porte le même nom que le type (extrémité inverse de - * l'association), on lui ajoute le nom de la classe d'association - * - * @param attr l'attribut a traiter - * @return le nom de l'attribut de classe d'association - */ - public static String getAssocAttrName(ObjectModelAttribute attr) { - String typeName = attr.getType().substring( - attr.getType().lastIndexOf(".") + 1); - String result = attr.getName(); - if (attr.getName().equalsIgnoreCase(typeName)) { - result += GeneratorUtil.capitalize(attr.getAssociationClass() - .getName()); - } - return result; - } - - public static String getDOType(ObjectModelElement elem, ObjectModel model) { - String type = elem.getName(); - if (elem instanceof ObjectModelAttribute) { - type = ((ObjectModelAttribute) elem).getType(); - } - if (elem instanceof ObjectModelClass) { - type = ((ObjectModelClass) elem).getQualifiedName(); - } - return getDOType(type, model); - } - - public static String getDOType(String type, ObjectModel model) { - if (!model.hasClass(type)) { - return type; - } - ObjectModelClass clazz = model.getClass(type); - if (clazz.hasStereotype(STEREOTYPE_ENTITY)) { - if (shouldBeAbstract(clazz)) { - type += "Abstract"; - } else { - type += "Impl"; - } - } - return type; - } - - private static Set<String> numberTypes = new HashSet<String>(); - - static { - numberTypes.add("byte"); - numberTypes.add("java.lang.Byte"); - numberTypes.add("short"); - numberTypes.add("java.lang.Short"); - numberTypes.add("int"); - numberTypes.add("java.lang.Integer"); - numberTypes.add("long"); - numberTypes.add("java.lang.Long"); - numberTypes.add("float"); - numberTypes.add("java.lang.Float"); - numberTypes.add("double"); - numberTypes.add("java.lang.Double"); - } - - public static boolean isNumericType(ObjectModelAttribute attr) { - return numberTypes.contains(attr.getType()); - } - - private static Set<String> textTypes = new HashSet<String>(); - - static { - textTypes.add("char"); - textTypes.add("java.lang.Char"); - textTypes.add("java.lang.String"); - } - - public static boolean isTextType(ObjectModelAttribute attr) { - return textTypes.contains(attr.getType()); - } - - public static boolean isDateType(ObjectModelAttribute attr) { - return "java.util.Date".equals(attr.getType()); - } - - public static boolean isBooleanType(ObjectModelAttribute attr) { - return ("boolean".equals(attr.getType()) || "java.lang.Boolean" - .equals(attr.getType())); - } - - /** - * Indique si la classe specifiee n'a aucune ou que des methodes abstraites - * - * @param clazz l'instance de ObjectModelClass - * @return true si la classe n'a que des operations abstraite ou aucune - * operation - */ - public static boolean hasNothingOrAbstractMethods(ObjectModelClass clazz) { - boolean result = true; - Iterator operations = clazz.getOperations().iterator(); - while (result && operations.hasNext()) { - ObjectModelOperation op = (ObjectModelOperation) operations.next(); - result = op.isAbstract(); - } - return result; - } - - /** - * Indique si la classe specifiee devrait etre abstraite - * - * @param clazz l'instance de ObjectModelClass - * @return true dans ce cas, false sinon - */ - public static boolean shouldBeAbstract(ObjectModelClass clazz) { - return clazz != null - && (clazz.isAbstract() && hasNothingOrAbstractMethods(clazz)); - } - - /** - * <p> - * Cette méthode permet de détecter si - * - l'attribut représente une relation 1-n - * - cette relation est unidirectionnelle - * - le type de l'attribut représente un entité - * - cette entité a des sous-classes dans le modèle - * <p/> - * Ce cas correspond à une incompatibilité d'Hibernate qui nous oblige a - * adopter un comportement particulier. - * </p> - * - * @param attr l'attribut a tester - * @param model le model - * @return true si et seulement si il s'agit bien de ce type de relation - */ - public static boolean hasUnidirectionalRelationOnAbstractType( - ObjectModelAttribute attr, ObjectModel model) { - ObjectModelAttribute reverse = attr.getReverseAttribute(); - //relation 1-n - if (reverse != null && isNMultiplicity(attr) - && !isNMultiplicity(reverse)) { - //Pas de navigabilité - if (!reverse.isNavigable()) { - //Il s'agit d'une entity - ObjectModelClass clazz = model.getClass(attr.getType()); - if (clazz != null && clazz.hasStereotype(STEREOTYPE_ENTITY)) { - //Cette classe a des sous-classes dans le modèle - for (ObjectModelClass subClass : model.getClasses()) { - if (subClass.getSuperclasses().contains(clazz)) { - return true; - } - } - } - } - } - return false; - } - - /** - * Renvoie le nom unique de table pour une relation ManyToMany en fonction - * de l'attribut <code>attr</code> - * <p/> - * Plusieurs cas de figure: - * <li> - * - * @param attr l'attribut servant de base au calcul du nom - * @return le nom de la table - */ - public static String getManyToManyTableName(ObjectModelAttribute attr) { - String result; - - if (attr.hasAssociationClass()) { - result = GeneratorUtil.getDBName(attr.getAssociationClass()); - } else { - String name = attr.getName(); - String revers = attr.getReverseAttributeName(); - - if (name.compareToIgnoreCase(revers) < 0) { - result = name + "_" + revers; - } else { - result = revers + "_" + name; - } - } - // String result; - // if (!Util.isFirstAttribute(attr)) { - // result = attr.getDeclaringElement().getName() + "_" + attr.getReverseAttribute().getDeclaringElement().getName(); - // } else { - // result = attr.getReverseAttribute().getDeclaringElement().getName() + "_" + attr.getDeclaringElement().getName(); - // } - return result.toLowerCase(); - } - - /** - * Renvoie le type d'interface à utiliser en fonction de l'attribut - * - * @param attr l'attribut a traiter - * @return String - */ - public static String getNMultiplicityInterfaceType(ObjectModelAttribute attr) { - if (attr.hasStereotype(STEREOTYPE_UNIQUE)) { - return Set.class.getName(); - } else if (attr.isIndexed() || attr.isOrdered()) { - return List.class.getName(); - } - return Collection.class.getName(); - } - - /** - * Renvoie le type d'objet (instance) à utiliser en fonction de l'attribut - * - * @param attr l'attribut a traiter - * @return String - */ - public static String getNMultiplicityObjectType(ObjectModelAttribute attr) { - if (attr.hasStereotype(STEREOTYPE_UNIQUE)) { - return HashSet.class.getName(); - } else if (attr.isIndexed() || attr.isOrdered()) { - //On considère qu'on ne sait pas traiter vraiment l'attribut "ordered" - // puisqu'on va conserver l'ordre d'insertion, et non un ordre en - // fonction d'un élément donné. Donc on renvoi une ArrayList - return ArrayList.class.getName(); - } - LinkedList.class.getName(); - return ArrayList.class.getName(); - } - - /** - * Renvoie le type d'interface à utiliser en fonction de l'attribut - * - * @param attr l'attribut a traiter - * @return String - */ - public static String getNMultiplicityHibernateType(ObjectModelAttribute attr) { - if (attr.hasStereotype(STEREOTYPE_UNIQUE)) { - return "set"; - } else if (attr.isIndexed()) { - return "list"; - } - //attr.isOrdered() - On génère le ordered en bag - return "bag"; - } - - /** - * Obtain the list of entities classes with the possibility to sort the result. - * - * @param model the current model to scan - * @param sort flag to allow sort the result - * @return the list of filtred classes by their stereotype - */ - public static List<ObjectModelClass> getEntityClasses(ObjectModel model, - boolean sort) { - return getClassesByStereotype(STEREOTYPE_ENTITY, model, sort); - } - - /** - * Obtain the list of classes for a given stereotype with the possibility to sort the result. - * - * @param stereotype filter stereotype - * @param model the current model to scan - * @param sort flag to allow sort the result - * @return the list of filtred classes by their stereotype - */ - public static List<ObjectModelClass> getClassesByStereotype( - String stereotype, ObjectModel model, boolean sort) { - List<ObjectModelClass> classes = new ArrayList<ObjectModelClass>(); - for (ObjectModelClass clazz : model.getClasses()) { - if (clazz.hasStereotype(stereotype)) { - classes.add(clazz); - } - } - if (sort && !classes.isEmpty()) { - java.util.Collections.sort(classes, - new java.util.Comparator<ObjectModelClass>() { - public int compare(ObjectModelClass o1, - ObjectModelClass o2) { - return o1.getQualifiedName().compareTo( - o2.getQualifiedName()); - } - }); - } - return classes; - } - - /** - * Detecte si la clef metier d'une classe est mutable ou pas. - * <p/> - * On respecte la valeur par defaut d'hibernate, à savoir que par default une clef metier est non mutable. - * - * @param clazz la classe a tester - * @return <code>true</code> si le tag value a ete positionne sur la classe via le tag - * {@link #TAG_NATURAL_ID_MUTABLE}, , <code>false</code> sinon. - */ - public static boolean isNaturalIdMutable(ObjectModelClass clazz) { - String value = clazz.getTagValue(TAG_NATURAL_ID_MUTABLE); - if (!notEmpty(value)) { - // valeur null, donc par default positionnee - return false; - } - try { - return Boolean.valueOf(value.trim()); - } catch (Exception e) { - // on a pas reussi a convertir en boolean. - //todo peut-être declancher une exception ? - return false; - } - } -} // GeneratorUtil Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/InterfaceGenerator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/InterfaceGenerator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/InterfaceGenerator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -80,34 +80,38 @@ log.debug("Generating interface for : " + interfacez.getName()); } + String interfacezName = interfacez.getName(); + generateInterfaceHeader(output, interfacez); generateInterfaceOperations(output, interfacez); -/*{} //<%=interfacez.getName()%> +/*{} //<%=interfacezName%> }*/ } - private void generateInterfaceHeader(Writer output, ObjectModelClassifier classifier) throws IOException { - String copyright = GeneratorUtil.getCopyright(model); - if (GeneratorUtil.notEmpty(copyright)) { + private void generateInterfaceHeader(Writer output, ObjectModelInterface interfacez) throws IOException { + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { /*{<%=copyright%> }*/ } -/*{package <%=classifier.getPackageName()%>; +/*{package <%=interfacez.getPackageName()%>; }*/ - if (GeneratorUtil.hasDocumentation(classifier)) { + if (TopiaGeneratorUtil.hasDocumentation(interfacez)) { + String documentation = interfacez.getDocumentation(); /*{ /** - * <%=classifier.getDocumentation()%> + * <%=documentation%> *) }*/ } -/*{public interface <%=classifier.getName()%> }*/ + String interfacezName = interfacez.getName(); +/*{public interface <%=interfacezName%> }*/ String extendClass = ""; - if (!classifier.getInterfaces().isEmpty()) { - for (ObjectModelClassifier parent : classifier.getInterfaces()) { + if (!interfacez.getInterfaces().isEmpty()) { + for (ObjectModelClassifier parent : interfacez.getInterfaces()) { extendClass += parent.getQualifiedName(); extendClass += ", "; } @@ -127,8 +131,9 @@ for (ObjectModelOperation op : classifier.getOperations()) { /*{ /** }*/ - if (GeneratorUtil.hasDocumentation(op)) { -/*{ * <%=op.getName()%> : <%=op.getDocumentation()%> + if (TopiaGeneratorUtil.hasDocumentation(op)) { + String documentation = op.getDocumentation(); +/*{ * <%=documentation%> }*/ } Collection<ObjectModelParameter> params = op.getParameters(); @@ -136,28 +141,35 @@ if (log.isTraceEnabled()) { log.trace("Param" + param); } -/*{ * @param <%=param.getName()%> <%=param.getDocumentation()%> + String paramName = param.getName(); + String paramDocumentation = param.getDocumentation(); +/*{ * @param <%=paramName%> <%=paramDocumentation%> }*/ } + String opVisibility = op.getVisibility(); + String opType = op.getReturnType(); + String opName = op.getName(); /*{ *) - <%=op.getVisibility()%> <%=op.getReturnType()%> <%=op.getName()%>(}*/ - String vir = ""; + <%=opVisibility%> <%=opType%> <%=opName%>(}*/ + String comma = ""; for (ObjectModelParameter param : params) { if (log.isTraceEnabled()) { - log.trace("Param" + param + " vir" + vir); + log.trace("Param" + param + " comma" + comma); } -/*{<%=vir%><%=param.getType()%> <%=param.getName()%>}*/ - vir = ", "; + String paramName = param.getName(); + String paramType = param.getType(); +/*{<%=comma%><%=paramType%> <%=paramName%>}*/ + comma = ", "; } /*{)}*/ Set<String> exceptions = op.getExceptions(); - vir = " throws "; + comma = " throws "; for (String exception : exceptions) { if (log.isTraceEnabled()) { - log.trace("exception" + exception + " vir" + vir); + log.trace("exception" + exception + " vir" + comma); } -/*{<%=vir%><%=exception%>}*/ - vir = ", "; +/*{<%=comma%><%=exception%>}*/ + comma = ", "; } /*{; Added: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java (rev 0) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -0,0 +1,697 @@ +/* *##% ToPIA - Tools for Portable and Independent Architecture + * Copyright (C) 2004 - 2008 CodeLutin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%*/ + +/******************************************************************************* + * GeneratorUtil.java + * + * Created: 13 déc. 2005 + * + * @author Arnaud Thimel <thimel@codelutin.com> + * + * @version $Revision: 1298 $ + * + * Mise a jour: $Date: 2009-01-15 00:01:45 +0100 (jeu 15 jan 2009) $ par : $Author: tchemit $ + */ + +package org.codelutin.topia.generator; + +import org.apache.commons.lang.StringUtils; +import org.codelutin.generator.Generator; +import org.codelutin.generator.GeneratorUtil; +import org.codelutin.generator.models.Model; +import org.codelutin.generator.models.object.ObjectModel; +import org.codelutin.generator.models.object.ObjectModelAttribute; +import org.codelutin.generator.models.object.ObjectModelClass; +import org.codelutin.generator.models.object.ObjectModelElement; +import org.codelutin.generator.models.object.ObjectModelInterface; +import org.codelutin.generator.models.object.ObjectModelOperation; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +/** Classe regroupant divers méthodes utiles pour la génération des entités */ +public class TopiaGeneratorUtil extends GeneratorUtil { + + /** Stéréotype pour les interfaces devant être générées sous forme de facades */ + public final static String STEREOTYPE_FACADE = "facade"; + + /** Stéréotype pour les objets devant être générées sous forme d'entités */ + public static final String STEREOTYPE_ENTITY = "entity"; + + /** Stéréotype pour les objets devant être générées sous forme de DTO */ + public static final String STEREOTYPE_DTO = "dto"; + + /** + * Stéréotype pour les interfaces devant être générées sous forme de + * services + */ + public static final String STEREOTYPE_SERVICE = "service"; + + /** Stéréotype pour les interfaces devant être générées sous forme de DAO */ + public static final String STEREOTYPE_DAO = "dao"; + + /** Stéréotype pour les attributs à indexer en base */ + public static final String STEREOTYPE_INDEXED = "indexed"; + + /** Stéréotype pour les collections avec unicité */ + public static final String STEREOTYPE_UNIQUE = "unique"; + + /** Stéréotype pour les attributs étant des clés primaires */ + public static final String STEREOTYPE_PRIMARYKAY = "primaryKey"; + + /** Tag pour le type de persistence */ + public static final String TAG_PERSISTENCE_TYPE = "persistenceType"; + + /** Tag pour le nom du champ / entité en BD */ + public static final String TAG_DB_NAME = "dbName"; + + /** Tag pour le nom du schema en BD */ + public static final String TAG_SCHEMA_NAME = "dbSchema"; + + /** Tag pour la taille du champ en BD */ + public static final String TAG_LENGTH = "length"; + + /** Tag pour ajouter une annotation à un champ */ + public static final String TAG_ANNOTATION = "annotation"; + + /** Tag pour ajouter specifier le copyright d'un fichier */ + public static final String TAG_COPYRIGHT = "copyright"; + + /** Tag pour specfier le type d'acces a un champ */ + public static final String TAG_ACCESS = "access"; + + /** Tag pour specfier si on doit générer i18n */ + public static final String TAG_I18N_PREFIX = "i18n"; + + /** Tag pour ajouter un attribut dans une clef métier */ + public static final String TAG_NATURAL_ID = "naturalId"; + + /** Tag pour specifier si une clef metier est mutable */ + public static final String TAG_NATURAL_ID_MUTABLE = "naturalIdMutable"; + + /** Tag pour spécifier la caractèrelazy d'une association multiple */ + public static final String TAG_LAZY = "lazy"; + + /** Tag pour spécifier la caractère fetch d'une association multiple */ + public static final String TAG_FETCH = "fetch"; + + /** Tag pour spécifier la caractère order-by d'une association multiple */ + public static final String TAG_ORDER_BY = "orderBy"; + + /** Tag pour spécifier la caractère not-null d'un attribut */ + public static final String TAG_NOT_NULL = "notNull"; + + /** Tag pour spécifier la caractère embed-xml d'une association */ + public static final String TAG_EMBED_XML = "embedXml"; + + /** Tag pour spécifier le permissions à la création */ + public static final String TAG_SECURITY_CREATE = "securityCreate"; + + /** Tag pour spécifier le permissions au chargement */ + public static final String TAG_SECURITY_LOAD = "securityLoad"; + + /** Tag pour spécifier le permissions à la mise à jour */ + public static final String TAG_SECURITY_UPDATE = "securityUpdate"; + + /** Tag pour spécifier le permissions à la suppression */ + public static final String TAG_SECURITY_DELETE = "securityDelete"; + + /** Type de persistence Hibernate */ + public static final String PERSISTENCE_TYPE_HIBERNATE = "hibernate"; + + /** Type de persistence LDAP */ + public static final String PERSISTENCE_TYPE_LDAP = "ldap"; + + /** Type de persistence par défaut (si aucun précisé) */ + public static final String PERSISTENCE_TYPE_DEFAULT = PERSISTENCE_TYPE_HIBERNATE; + + /** Propriété des générateurs indiquant le package par défaut */ + public static final String PROPERTY_DEFAULT_PACKAGE = "defaultPackage"; + + /** Le package par défaut si aucun n'est spécifié */ + public static final String DEFAULT_PACKAGE = "org.codelutin.malo"; + + /** + * Renvoie le package par défaut pour le générateur donné + * + * @param generator le générateur donné + * @return le package par défaut du générator donné + */ + public static String getDefaultPackage(Generator generator) { + String packageName = generator.getProperty(PROPERTY_DEFAULT_PACKAGE); + if (packageName == null || "".equals(packageName)) { + packageName = DEFAULT_PACKAGE; + } + return packageName; + } + + /** + * Indique si l'élément spécifié dispose de documentation + * + * @param element l'élément à tester + * @return true s'il y a documentation, false sinon + */ + public static boolean hasDocumentation(ObjectModelElement element) { + return notEmpty(element.getDocumentation()); + } + + /** + * Indique si la chaine de caratère n'est pas vide (null ou "") + * + * @param s la chaine de caractères à tester + * @return true si <code>s</code> n'est pas vide + */ + public static boolean notEmpty(String s) { + return (s != null && !"".equals(s)); + } + + /** + * Renvoie l'interface DAO associée à la classe passée en paramètre + * + * @param clazz la classe à tester + * @param model le modele utilisé + * @return l'interface trouvée ou null sinon + */ + public static ObjectModelInterface getDAOInterface(ObjectModelClass clazz, + ObjectModel model) { + for (Object o : model.getInterfaces()) { + ObjectModelInterface daoInterface = (ObjectModelInterface) o; + if (daoInterface.getName().equals(clazz.getName() + "DAO")) { + if (daoInterface.hasStereotype(STEREOTYPE_DAO)) { + return daoInterface; + } + } + } + return null; + } + + /** + * Renvoie le type de persistence pour l'élément donné. Si aucun n'est + * trouvé, le type par défaut est utilisé + * + * @param element l'élément à tester + * @return le type de persitence pour l'élément donné. + */ + public static String getPersistenceType(ObjectModelElement element) { + String tag = element.getTagValue(TAG_PERSISTENCE_TYPE); + if (tag == null) { + tag = PERSISTENCE_TYPE_DEFAULT; + } + return tag; + } + + public static String getReverseDBName(ObjectModelAttribute attr) { + if (attr.getReverseAttribute() != null) { + return getDBName(attr.getReverseAttribute()); + } else { + return getDBName(attr) + "_id"; + } + } + + /** + * Renvoie le nom BD de l'élement passé en paramètre. Elle se base sur le + * tag associé si il existe, sinon sur le nom de l'élément + * + * @param element l'élément à tester + * @return le nom de table + */ + public static String getDBName(ObjectModelElement element) { + if (element == null) { + return null; + } + if (notEmpty(element.getTagValue(TAG_DB_NAME))) { + return element.getTagValue(TAG_DB_NAME); + } + return toLowerCaseFirstLetter(element.getName()); + } + + /** + * Cherche et renvoie le schema a utiliser sur cet element, sinon sur le model. + * + * @param element l'élément à tester + * @param model le modele utilisé + * @return le nom du schema ou null + */ + public static String getSchemaName(ObjectModelElement element, + ObjectModel model) { + return findTagValue(TAG_SCHEMA_NAME, element, model); + } + + /** + * Cherche et renvoie le prefixe i18n à utiliser sur cet element, sinon sur le model. + * + * @param element l'élément à tester + * @param model le modele utilisé + * @return le prefix i18n ou <code>null</code> si non spécifié + */ + public static String getI18nPrefix(ObjectModelElement element, + ObjectModel model) { + return findTagValue(TAG_I18N_PREFIX, element, model); + } + + /** + * Cherche et renvoie la liste des attributs constituant la clef metier d'une classe. + * + * @param clazz la classe à tester + * @return la liste des attributs de la clef métier ou null si pas de clef métier. + */ + public static List<String> getNaturalId(ObjectModelClass clazz) { + String value = clazz.getTagValue(TAG_NATURAL_ID); + if (value == null || value.trim().isEmpty()) { + return java.util.Collections.emptyList(); + } + List<String> result = new ArrayList<String>(); + for (String attribute : value.split(",")) { + result.add(attribute.trim()); + } + return result; + } + + /** + * Detecte si un attribut fait partie d'une clef metier. + * + * @param attribute l'attribut à tester + * @return <code>true</code> si l'attribut fait partie d'une clef metier, <code>false</cdoe> sinon. + */ + public static boolean isNaturalId(ObjectModelAttribute attribute) { + String value = attribute.getTagValue(TAG_NATURAL_ID); + if (!notEmpty(value)) { + // valeur null, donc pas positionnee + return false; + } + try { + return Boolean.valueOf(value.trim()); + } catch (Exception e) { + // on a pas reussi a convertir en boolean. + //todo peut-être declancher une exception ? + return false; + } + } + + /** + * Cherches et renvoie le copyright a utiliser sur le model. + * + * @param model le modele utilisé + * @return le texte du copyright ou null + */ + public static String getCopyright(Model model) { + return findTagValue(TAG_COPYRIGHT, null, model); + } + + /** + * Cherches et renvoie la valeur du tagvalue indique sur cet element, + * sinon sur le model. + * + * @param tagName le nom du tag + * @param element l'élément à tester + * @param model le modele utilisé + * @return la valeur du tagValue ou null + */ + public static String findTagValue(String tagName, + ObjectModelElement element, Model model) { + if (element == null) { + if (model != null) { + if (notEmpty(model.getTagValue(tagName))) { + return model.getTagValue(tagName); + } + } + return null; + } + if (notEmpty(element.getTagValue(tagName))) { + return element.getTagValue(tagName); + } + //On va chercher sur l'element declarant + return findTagValue(tagName, element.getDeclaringElement(), model); + } + + public static <Type extends ObjectModelElement> Collection<Type> getElementsWithStereotype( + Collection<Type> elements, String... stereotypes) { + Collection<Type> result = new ArrayList<Type>(); + for (Type element : elements) { + if (hasStereotypes(element, stereotypes)) { + result.add(element); + } + } + return result; + } + + public static boolean hasStereotypes(ObjectModelElement element, + String... stereotypes) { + for (String stereotype : stereotypes) { + if (!element.hasStereotype(stereotype)) { + return false; + } + } + return true; + } + + public static String getPrimaryKeyAttributesListDeclaration( + ObjectModelClass clazz, boolean includeName) { + String attributes = ""; + for (ObjectModelAttribute attr : getElementsWithStereotype(clazz + .getAttributes(), STEREOTYPE_PRIMARYKAY)) { + attributes += attr.getType(); + if (includeName) { + attributes += " " + attr.getName(); + } + attributes += ", "; + } + if (attributes.length() > 0) { + attributes = attributes.substring(0, attributes.length() - 2); + } + return attributes; + } + + public static String capitalize(String s) { + return StringUtils.capitalize(s); + } + + public static boolean isAssociationClassDoublon(ObjectModelAttribute attr) { + return (attr.getReverseAttribute() != null) + && (attr.getDeclaringElement().equals(attr + .getReverseAttribute().getDeclaringElement())) + && (!GeneratorUtil.isFirstAttribute(attr)); + } + + /** + * Renvoie le nom de l'attribut de classe d'association en fonction des cas: + * Si l'attribut porte le même nom que le type (extrémité inverse de + * l'association), on lui ajoute le nom de la classe d'association + * + * @param attr l'attribut a traiter + * @return le nom de l'attribut de classe d'association + */ + public static String getAssocAttrName(ObjectModelAttribute attr) { + String typeName = attr.getType().substring( + attr.getType().lastIndexOf(".") + 1); + String result = attr.getName(); + if (attr.getName().equalsIgnoreCase(typeName)) { + result += TopiaGeneratorUtil.capitalize(attr.getAssociationClass() + .getName()); + } + return result; + } + + public static String getDOType(ObjectModelElement elem, ObjectModel model) { + String type = elem.getName(); + if (elem instanceof ObjectModelAttribute) { + type = ((ObjectModelAttribute) elem).getType(); + } + if (elem instanceof ObjectModelClass) { + type = ((ObjectModelClass) elem).getQualifiedName(); + } + return getDOType(type, model); + } + + public static String getDOType(String type, ObjectModel model) { + if (!model.hasClass(type)) { + return type; + } + ObjectModelClass clazz = model.getClass(type); + if (clazz.hasStereotype(STEREOTYPE_ENTITY)) { + if (shouldBeAbstract(clazz)) { + type += "Abstract"; + } else { + type += "Impl"; + } + } + return type; + } + + private static Set<String> numberTypes = new HashSet<String>(); + + static { + numberTypes.add("byte"); + numberTypes.add("java.lang.Byte"); + numberTypes.add("short"); + numberTypes.add("java.lang.Short"); + numberTypes.add("int"); + numberTypes.add("java.lang.Integer"); + numberTypes.add("long"); + numberTypes.add("java.lang.Long"); + numberTypes.add("float"); + numberTypes.add("java.lang.Float"); + numberTypes.add("double"); + numberTypes.add("java.lang.Double"); + } + + public static boolean isNumericType(ObjectModelAttribute attr) { + return numberTypes.contains(attr.getType()); + } + + private static Set<String> textTypes = new HashSet<String>(); + + static { + textTypes.add("char"); + textTypes.add("java.lang.Char"); + textTypes.add("java.lang.String"); + } + + public static boolean isTextType(ObjectModelAttribute attr) { + return textTypes.contains(attr.getType()); + } + + public static boolean isDateType(ObjectModelAttribute attr) { + return "java.util.Date".equals(attr.getType()); + } + + public static boolean isBooleanType(ObjectModelAttribute attr) { + return ("boolean".equals(attr.getType()) || "java.lang.Boolean" + .equals(attr.getType())); + } + + /** + * Indique si la classe specifiee n'a aucune ou que des methodes abstraites + * + * @param clazz l'instance de ObjectModelClass + * @return true si la classe n'a que des operations abstraite ou aucune + * operation + */ + public static boolean hasNothingOrAbstractMethods(ObjectModelClass clazz) { + boolean result = true; + Iterator operations = clazz.getOperations().iterator(); + while (result && operations.hasNext()) { + ObjectModelOperation op = (ObjectModelOperation) operations.next(); + result = op.isAbstract(); + } + return result; + } + + /** + * Indique si la classe specifiee devrait etre abstraite + * + * @param clazz l'instance de ObjectModelClass + * @return true dans ce cas, false sinon + */ + public static boolean shouldBeAbstract(ObjectModelClass clazz) { + return clazz != null + && (clazz.isAbstract() && hasNothingOrAbstractMethods(clazz)); + } + + /** + * <p> + * Cette méthode permet de détecter si + * - l'attribut représente une relation 1-n + * - cette relation est unidirectionnelle + * - le type de l'attribut représente un entité + * - cette entité a des sous-classes dans le modèle + * <p/> + * Ce cas correspond à une incompatibilité d'Hibernate qui nous oblige a + * adopter un comportement particulier. + * </p> + * + * @param attr l'attribut a tester + * @param model le model + * @return true si et seulement si il s'agit bien de ce type de relation + */ + public static boolean hasUnidirectionalRelationOnAbstractType( + ObjectModelAttribute attr, ObjectModel model) { + ObjectModelAttribute reverse = attr.getReverseAttribute(); + //relation 1-n + if (reverse != null && isNMultiplicity(attr) + && !isNMultiplicity(reverse)) { + //Pas de navigabilité + if (!reverse.isNavigable()) { + //Il s'agit d'une entity + ObjectModelClass clazz = model.getClass(attr.getType()); + if (clazz != null && clazz.hasStereotype(STEREOTYPE_ENTITY)) { + //Cette classe a des sous-classes dans le modèle + for (ObjectModelClass subClass : model.getClasses()) { + if (subClass.getSuperclasses().contains(clazz)) { + return true; + } + } + } + } + } + return false; + } + + /** + * Renvoie le nom unique de table pour une relation ManyToMany en fonction + * de l'attribut <code>attr</code> + * <p/> + * Plusieurs cas de figure: + * <li> + * + * @param attr l'attribut servant de base au calcul du nom + * @return le nom de la table + */ + public static String getManyToManyTableName(ObjectModelAttribute attr) { + String result; + + if (attr.hasAssociationClass()) { + result = TopiaGeneratorUtil.getDBName(attr.getAssociationClass()); + } else { + String name = attr.getName(); + String revers = attr.getReverseAttributeName(); + + if (name.compareToIgnoreCase(revers) < 0) { + result = name + "_" + revers; + } else { + result = revers + "_" + name; + } + } + // String result; + // if (!Util.isFirstAttribute(attr)) { + // result = attr.getDeclaringElement().getName() + "_" + attr.getReverseAttribute().getDeclaringElement().getName(); + // } else { + // result = attr.getReverseAttribute().getDeclaringElement().getName() + "_" + attr.getDeclaringElement().getName(); + // } + return result.toLowerCase(); + } + + /** + * Renvoie le type d'interface à utiliser en fonction de l'attribut + * + * @param attr l'attribut a traiter + * @return String + */ + public static String getNMultiplicityInterfaceType(ObjectModelAttribute attr) { + if (attr.hasStereotype(STEREOTYPE_UNIQUE)) { + return Set.class.getName(); + } else if (attr.isIndexed() || attr.isOrdered()) { + return List.class.getName(); + } + return Collection.class.getName(); + } + + /** + * Renvoie le type d'objet (instance) à utiliser en fonction de l'attribut + * + * @param attr l'attribut a traiter + * @return String + */ + public static String getNMultiplicityObjectType(ObjectModelAttribute attr) { + if (attr.hasStereotype(STEREOTYPE_UNIQUE)) { + return HashSet.class.getName(); + } else if (attr.isIndexed() || attr.isOrdered()) { + //On considère qu'on ne sait pas traiter vraiment l'attribut "ordered" + // puisqu'on va conserver l'ordre d'insertion, et non un ordre en + // fonction d'un élément donné. Donc on renvoi une ArrayList + return ArrayList.class.getName(); + } + LinkedList.class.getName(); + return ArrayList.class.getName(); + } + + /** + * Renvoie le type d'interface à utiliser en fonction de l'attribut + * + * @param attr l'attribut a traiter + * @return String + */ + public static String getNMultiplicityHibernateType(ObjectModelAttribute attr) { + if (attr.hasStereotype(STEREOTYPE_UNIQUE)) { + return "set"; + } else if (attr.isIndexed()) { + return "list"; + } + //attr.isOrdered() - On génère le ordered en bag + return "bag"; + } + + /** + * Obtain the list of entities classes with the possibility to sort the result. + * + * @param model the current model to scan + * @param sort flag to allow sort the result + * @return the list of filtred classes by their stereotype + */ + public static List<ObjectModelClass> getEntityClasses(ObjectModel model, + boolean sort) { + return getClassesByStereotype(STEREOTYPE_ENTITY, model, sort); + } + + /** + * Obtain the list of classes for a given stereotype with the possibility to sort the result. + * + * @param stereotype filter stereotype + * @param model the current model to scan + * @param sort flag to allow sort the result + * @return the list of filtred classes by their stereotype + */ + public static List<ObjectModelClass> getClassesByStereotype( + String stereotype, ObjectModel model, boolean sort) { + List<ObjectModelClass> classes = new ArrayList<ObjectModelClass>(); + for (ObjectModelClass clazz : model.getClasses()) { + if (clazz.hasStereotype(stereotype)) { + classes.add(clazz); + } + } + if (sort && !classes.isEmpty()) { + java.util.Collections.sort(classes, + new java.util.Comparator<ObjectModelClass>() { + public int compare(ObjectModelClass o1, + ObjectModelClass o2) { + return o1.getQualifiedName().compareTo( + o2.getQualifiedName()); + } + }); + } + return classes; + } + + /** + * Detecte si la clef metier d'une classe est mutable ou pas. + * <p/> + * On respecte la valeur par defaut d'hibernate, à savoir que par default une clef metier est non mutable. + * + * @param clazz la classe a tester + * @return <code>true</code> si le tag value a ete positionne sur la classe via le tag + * {@link #TAG_NATURAL_ID_MUTABLE}, , <code>false</code> sinon. + */ + public static boolean isNaturalIdMutable(ObjectModelClass clazz) { + String value = clazz.getTagValue(TAG_NATURAL_ID_MUTABLE); + if (!notEmpty(value)) { + // valeur null, donc par default positionnee + return false; + } + try { + return Boolean.valueOf(value.trim()); + } catch (Exception e) { + // on a pas reussi a convertir en boolean. + //todo peut-être declancher une exception ? + return false; + } + } +} // GeneratorUtil Modified: topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaRelationValidator.java =================================================================== --- topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaRelationValidator.java 2009-01-29 10:33:30 UTC (rev 1325) +++ topia/branches/generators-refactoring/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaRelationValidator.java 2009-01-29 10:42:29 UTC (rev 1326) @@ -29,9 +29,6 @@ package org.codelutin.topia.generator; -import static org.codelutin.generator.Util.isFirstAttribute; -import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; - import org.codelutin.generator.models.object.ObjectModel; import org.codelutin.generator.models.object.ObjectModelAttribute; import org.codelutin.generator.models.object.validator.ObjectModelValidator; @@ -68,7 +65,7 @@ /* Relation navigabilité */ //Pour ne pas avoir de doublons, on ne vérifie que sur le premier //attribut par ordre alphabétique - if (isFirstAttribute(attr)) { + if (TopiaGeneratorUtil.isFirstAttribute(attr)) { if (!attr.isNavigable() && !reverse.isNavigable()) { addError(attr, "La relation entre " + "\"" + reverse.getType() + "\"[" + attr.getName() + "] et " + "\"" @@ -79,7 +76,7 @@ } /* Relation héritage */ - if (hasUnidirectionalRelationOnAbstractType(attr, model)) { + if (TopiaGeneratorUtil.hasUnidirectionalRelationOnAbstractType(attr, model)) { isValid = false; addError( attr,
participants (1)
-
thimel@users.labs.libre-entreprise.org