r1423 - topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator
Author: tchemit Date: 2009-04-15 07:33:53 +0000 (Wed, 15 Apr 2009) New Revision: 1423 Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java Log: add a simple Bean generator with pcs notifications Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java 2009-04-13 11:34:36 UTC (rev 1422) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java 2009-04-15 07:33:53 UTC (rev 1423) @@ -34,6 +34,9 @@ import java.io.File; import java.io.IOException; import java.io.Writer; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; import java.util.Iterator; import java.util.List; @@ -41,11 +44,11 @@ import org.apache.commons.logging.LogFactory; import org.codelutin.generator.ObjectModelGenerator; 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 org.codelutin.generator.models.object.ObjectModelInterface; +import org.codelutin.topia.persistence.TopiaEntity; /** * DTO generator @@ -72,20 +75,67 @@ return; } String copyright = TopiaGeneratorUtil.getCopyright(model); - if (TopiaGeneratorUtil.notEmpty(copyright)) { -/*{<%=copyright%> -}*/ - } String clazzName = clazz.getName(); -/*{package <%=clazz.getPackageName()%>; - }*/ + String abstractStr = isAbstract(clazz)?"abstract ":" "; boolean generateToString = TopiaGeneratorUtil.generateToString(clazz, model); - List<String> imports = TopiaGeneratorUtil.getImports(clazz, + List<String> imports = TopiaGeneratorUtil.getImports(clazz, + List.class.getName(), + Collection.class.getName(), + TopiaEntity.class.getName(), + java.beans.PropertyChangeSupport.class.getName(), java.beans.PropertyChangeListener.class.getName() ); if (generateToString) { imports.add(org.apache.commons.lang.builder.ToStringBuilder.class.getName()); } + String extendClass = ""; + Iterator<ObjectModelClass> j = clazz.getSuperclasses().iterator(); + if (j.hasNext()) { + ObjectModelClassifier p = j.next(); + extendClass += p.getName(); + } + String implInterface = ""; + for (Iterator<ObjectModelInterface> i=clazz.getInterfaces().iterator(); i.hasNext();) { + ObjectModelClassifier parentInterface = i.next(); + implInterface += parentInterface.getName(); + if (i.hasNext()) { + implInterface += ", "; + } + } + String svUID = TopiaGeneratorUtil.findTagValue("dto-serialVersionUID", clazz, model); + + List<ObjectModelAttribute> attributes; + List<ObjectModelAttribute> multipleAttr; + attributes = new ArrayList<ObjectModelAttribute>(); + multipleAttr = new ArrayList<ObjectModelAttribute>(); + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attr.isNavigable()) { + attributes.add(attr); + if (GeneratorUtil.isNMultiplicity(attr)) { + multipleAttr.add(attr); + } + } + } + boolean sortAttribute = TopiaGeneratorUtil.sortAttribute(clazz, model); + if (sortAttribute) { + Comparator<ObjectModelAttribute> comp = new Comparator<ObjectModelAttribute>(){ + + @Override + public int compare(ObjectModelAttribute o1, ObjectModelAttribute o2) { + return o1.getName().compareTo(o2.getName()); + } + }; + java.util.Collections.sort(attributes,comp); + java.util.Collections.sort(multipleAttr,comp); + } + if (TopiaGeneratorUtil.notEmpty(copyright)) { +/*{<%=copyright%> +}*/ + } + +/*{package <%=clazz.getPackageName()%>; + }*/ + if (log.isDebugEnabled()) { log.debug("imports for class <" + clazzName + ">"); } @@ -96,34 +146,16 @@ /*{import <%=anImport%>; }*/ } +/*{ +public <%=abstractStr%>class <%=clazzName%>}*/ -/** - * DTO implantation for <%=GeneratorUtil.capitalize(clazzName)%> entity. - *) -public class <%=clazzName%>}*/ - /* * Définition de la super classe : il ne doit y avoir qu'une */ - - String extendClass = ""; - Iterator<ObjectModelClass> j = clazz.getSuperclasses().iterator(); - if (j.hasNext()) { - ObjectModelClassifier p = j.next(); - extendClass += p.getName(); - } - if (extendClass.length() > 0) { /*{ extends <%=extendClass%>}*/ } - String implInterface = ""; - for (Iterator<ObjectModelInterface> i=clazz.getInterfaces().iterator(); i.hasNext();) { - ObjectModelClassifier parentInterface = i.next(); - implInterface += parentInterface.getName(); - if (i.hasNext()) { - implInterface += ", "; - } - } + if (implInterface.length() > 0) { /*{ implements <%=implInterface%> { @@ -134,84 +166,21 @@ }*/ } - 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%>; }*/ } -/* - * Définition des attributs - */ - for (ObjectModelAttribute attr : clazz.getAttributes()) { - ObjectModelAttribute reverse = attr.getReverseAttribute(); - - if (!(attr.isNavigable() - || attr.hasAssociationClass())) { - continue; - } - - if (TopiaGeneratorUtil.hasDocumentation(attr)) { -/*{ /** - * <%=attr.getDocumentation()%> - *) -}*/ - } - String annotation = attr.getTagValue(TAG_ANNOTATION); - if (annotation != null && annotation.length() > 0) { -/*{ <%=annotation%> -}*/ - } - String attrName = attr.getName(); - String attrVisibility = attr.getVisibility(); - String attrType = attr.getType(); - if (!GeneratorUtil.isNMultiplicity(attr)) { - if (!attr.hasAssociationClass()) { -/*{ <%=attrVisibility%> <%=attrType%> <%=attrName%>; -}*/ - } else { - String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); -/*{ <%=attrVisibility%> <%=assocClassFQN%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; -}*/ - } - } else { - if (!attr.hasAssociationClass()) { - String nMultType = getCollection(attr, attrType); -/*{ <%=attrVisibility%> <%=nMultType%> <%=attrName%>; -}*/ - } else { - String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); - String nMultType = getCollection(attr, assocClassFQN); -/*{ <%=attrVisibility%> <%=nMultType%> <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; -}*/ - } - } - } /* end for*/ + generateAttributes(output, attributes); +/*{ protected final PropertyChangeSupport pcs; - //Déclaration des attributs d'une classe d'associations - if (clazz instanceof ObjectModelAssociationClass) { - ObjectModelAssociationClass assoc = (ObjectModelAssociationClass)clazz; - for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { - if (attr != null) { - String attrName = attr.getName(); - String attrVisibility = attr.getVisibility(); - String attrType = attr.getType(); -/*{ <%=attrVisibility%> <%=attrType%> <%=GeneratorUtil.toLowerCaseFirstLetter(attrName)%>; -}*/ - } - } - } -/*{ - protected java.beans.PropertyChangeSupport pcs; - /** * Default constructor of <%=clazzName%>. *) public <%=clazzName%>() { - pcs = new java.beans.PropertyChangeSupport(this); + pcs = new PropertyChangeSupport(this); } public void addPropertyChangeListener(PropertyChangeListener listener) { @@ -230,161 +199,254 @@ pcs.removePropertyChangeListener(propertyName, listener); } +}*/ + generateGetters(output, attributes); + generateSetters(output, attributes); + generateGetChild(output, multipleAttr); + generateAddChild(output, multipleAttr); + generateRemoveChild(output, multipleAttr); + if (generateToString) { + generateToString(output, clazz); + } + if (!multipleAttr.isEmpty()) { +/*{ protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { pcs.firePropertyChange(propertyName, oldValue, newValue); } + + protected <T> T getChild(Collection<T> childs, int index) { + if (childs != null) { + int i = 0; + for (T o : childs) { + if (index == i) { + return o; + } + i++; + } + } + return null; + } + + protected <T extends TopiaEntity> T getEntity(Collection<T> childs, String topiaId) { + if (childs != null) { + for (T o : childs) { + if (topiaId.equals(o.getTopiaId())) { + return o; + } + } + } + return null; + } + }*/ + } + +/*{ +} //<%=clazz.getName()%> }*/ + } + protected void generateAttributes(Writer output, List<ObjectModelAttribute> attributes) throws IOException { + + for (ObjectModelAttribute attr : attributes) { + + if (!(attr.isNavigable() + || attr.hasAssociationClass())) { + continue; + } + if (TopiaGeneratorUtil.hasDocumentation(attr)) { +/*{ /** + * <%=attr.getDocumentation()%> + *) +}*/ + } + String annotation = attr.getTagValue(TAG_ANNOTATION); + if (annotation != null && annotation.length() > 0) { +/*{ <%=annotation%> +}*/ + } + String attrName = attr.getName(); + String attrVisibility = attr.getVisibility(); + String attrType = attr.getType(); + if (attr.hasAssociationClass()) { + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + attrName = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName); + attrType = attr.getAssociationClass().getName(); + } + int dot = attrType.lastIndexOf("."); + if (dot>-1) { + attrType = attrType.substring(dot + 1); + } + if (GeneratorUtil.isNMultiplicity(attr)) { + attrType = getCollection(attr, attrType); + } + +/*{ <%=attrVisibility%> <%=attrType%> <%=attrName%>; +}*/ + } + } + + protected void generateGetters(Writer output, List<ObjectModelAttribute> attributes) throws IOException { /* * Définition des getteurs et setteurs */ - for (Object o : clazz.getAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; - ObjectModelAttribute reverse = attr.getReverseAttribute(); - + for (ObjectModelAttribute attr : attributes) { + if (!attr.isNavigable()) { continue; } String attrName = attr.getName(); String attrType = attr.getType(); - - if (!GeneratorUtil.isNMultiplicity(attr)) { - if (!attr.hasAssociationClass()) { -/*{ public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=attrType%> value) { - <%=attrType%> oldValue = this.<%=attrName%>; - this.<%=attrName%> = value; - firePropertyChange("<%=attrName%>", oldValue, value); - } - - public <%=attrType%> get<%=GeneratorUtil.capitalize(attrName)%>() { + if (attr.hasAssociationClass()) { + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + attrName = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName); + attrType = attr.getAssociationClass().getName(); + } + String attrNameCapitalized = GeneratorUtil.capitalize(attrName); + int dot = attrType.lastIndexOf("."); + if (dot>-1) { + attrType = attrType.substring(dot + 1); + } + if (GeneratorUtil.isNMultiplicity(attr)) { + attrType = getCollection(attr, attrType); + } +/*{ public <%=attrType%> get<%=attrNameCapitalized%>() { return <%=attrName%>; } }*/ - } else { - String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } -/*{ public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=assocClassFQN%> association) { - <%=assocClassFQN%> oldAssocation = this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; - this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = association; - p.firePropertyChange("<%=attrName%>", oldAssocation, assocation); + } } - public <%=assocClassFQN%> get<%=GeneratorUtil.capitalize(assocAttrName)%>() { - return <%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; + protected void generateSetters(Writer output, List<ObjectModelAttribute> attributes) throws IOException { + /* + * Définition des getteurs et setteurs + */ + for (ObjectModelAttribute attr : attributes) { + + if (!attr.isNavigable()) { + continue; + } + + String attrName = attr.getName(); + String attrType = attr.getType(); + + if (attr.hasAssociationClass()) { + String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); + attrName = GeneratorUtil.toLowerCaseFirstLetter(assocAttrName); + attrType = attr.getAssociationClass().getName(); + } + String attrNameCapitalized = GeneratorUtil.capitalize(attrName); + int dot = attrType.lastIndexOf("."); + if (dot>-1) { + attrType = attrType.substring(dot + 1); + } + if (GeneratorUtil.isNMultiplicity(attr)) { + attrType = getCollection(attr, attrType); + } +/*{ public void set<%=attrNameCapitalized%>(<%=attrType%> newValue) { + <%=attrType%> oldValue = get<%=attrNameCapitalized%>(); + this.<%=attrName%> = newValue; + firePropertyChange("<%=attrName%>", oldValue, newValue); } }*/ - } - } else { //NMultiplicity - if (!attr.hasAssociationClass()) { //Méthodes remplacées par des accesseurs sur les classes d'assoc - boolean reverseNavigable = reverse == null ? false : reverse.isNavigable(); - String reverseAttrName = reverse == null ? "" : reverse.getName(); - String nMultType = getCollection(attr, attrType); -/*{ public void set<%=GeneratorUtil.capitalize(attrName)%>(<%=nMultType%> values) { - <%=nMultType%> oldValues = this.<%=attrName%>; - this.<%=attrName%> = values; - firePropertyChange("<%=attrName%>", oldValues, values); + } } -}*/ + protected void generateGetChild(Writer output, List<ObjectModelAttribute> multipleAttr) throws IOException { + + for (ObjectModelAttribute attr : multipleAttr) { - //AddChild -/*{ public <%=attrType%> addChild(<%=attrType%> <%=attrName%>) { - this.<%=attrName%>.add(<%=attrName%>); - firePropertyChange("<%=attrName%>", null, <%=attrName%>); -}*/ - if (reverseNavigable) { -/*{ <%=attrName%>.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(this); -}*/ - } -/*{ return <%=attrName%>; + String attrName = attr.getName(); + String attrNameCapitalized = GeneratorUtil.capitalize(attrName); + String attrType = attr.getType(); + int dot = attrType.lastIndexOf("."); + if (dot>-1) { + attrType = attrType.substring(dot + 1); + } + ObjectModelClass attrEntity=null; + if (model.hasClass(attr.getType())) { + attrEntity = model.getClass(attr.getType()); + } + boolean isEntity = (attrEntity != null && attrEntity.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_ENTITY)); +/*{ public <%=attrType%> get<%=attrNameCapitalized%>(int index) { + <%=attrType%> o = getChild(<%=attrName%>, index); + return o; } - + }*/ - //RemoveChild -/*{ public void removeChild(<%=attrType%> <%=attrName%>) { - this.<%=attrName%>.remove(<%=attrName%>); - firePropertyChange("<%=attrName%>", <%=attrName%>, null); -}*/ - if (reverseNavigable) { -/*{ <%=attrName%>.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(null); -}*/ - } -/*{ } - -}*/ - } else { - String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); - String nMultType = getCollection(attr, assocClassFQN); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } -/*{ public void set<%=GeneratorUtil.capitalize(assocAttrName)%>(<%=nMultType%> values) { - <%=nMultType%> oldValues = this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; - this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%> = values; - firePropertyChange("<%=attrName%>", oldValues, values); + if (isEntity) { +/*{ public <%=attrType%> get<%=attrNameCapitalized%>(String topiaId) { + <%=attrType%> o = getEntity(<%=attrName%>, topiaId); + return o; } }*/ - } - if (!attr.hasAssociationClass()) { - String nMultType = getCollection(attr, attrType); -/*{ public <%=nMultType%> get<%=GeneratorUtil.capitalize(attrName)%>() { - return this.<%=attrName%>; + } + } } + protected void generateAddChild(Writer output, List<ObjectModelAttribute> multipleAttr) throws IOException { + for (ObjectModelAttribute attr : multipleAttr) { + + String attrName = attr.getName(); + String attrNameCapitalized = GeneratorUtil.capitalize(attrName); + String attrType = attr.getType(); + int dot = attrType.lastIndexOf("."); + if (dot>-1) { + attrType = attrType.substring(dot + 1); + } +/*{ public <%=attrType%> add<%=attrNameCapitalized%>(<%=attrType%> <%=attrName%>) { + get<%=attrNameCapitalized%>().add(<%=attrName%>); + firePropertyChange("<%=attrName%>", null, <%=attrName%>); + return <%=attrName%>; + } }*/ - } else { - String assocAttrName = TopiaGeneratorUtil.getAssocAttrName(attr); - String assocClassFQN = attr.getAssociationClass().getQualifiedName(); - String nMultType = getCollection(attr, assocClassFQN); - if (log.isTraceEnabled()) { - log.trace("assocAttrName: " + assocAttrName); - } -/*{ public <%=nMultType%> get<%=GeneratorUtil.capitalize(assocAttrName)%>() { - return this.<%=GeneratorUtil.toLowerCaseFirstLetter(assocAttrName)%>; + } } -}*/ - } + protected void generateRemoveChild(Writer output, List<ObjectModelAttribute> multipleAttr) throws IOException { + for (ObjectModelAttribute attr : multipleAttr) { + String attrName = attr.getName(); + String attrNameCapitalized = GeneratorUtil.capitalize(attrName); + String attrType = attr.getType(); + int dot = attrType.lastIndexOf("."); + if (dot>-1) { + attrType = attrType.substring(dot + 1); } +/*{ public boolean remove<%=attrNameCapitalized%>(<%=attrType%> <%=attrName%>) { + boolean removed = get<%=attrNameCapitalized%>().remove(<%=attrName%>); + if (removed) { + firePropertyChange("<%=attrName%>", <%=attrName%>, null); } + return removed; + } - if (generateToString) { +}*/ + } + } + + protected void generateToString(Writer output, ObjectModelClass clazz) throws IOException { /*{ @Override public String toString() { String result = new ToStringBuilder(this). }*/ for (ObjectModelAttribute attr : clazz.getAttributes()) { - if (!(attr.isNavigable() - || attr.hasAssociationClass())) { + 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()); - } String attrName = attr.getName(); /*{ append("<%=attrName%>", this.<%=attrName%>). -}*/ +}*/ } -/*{ toString(); +/*{ toString(); return result; } }*/ - } -/*{ -} //<%=clazz.getName()%> -}*/ } protected String getCollection(ObjectModelAttribute attr, String attrType) { @@ -398,4 +460,11 @@ nMultType += ">"; return nMultType; } + + protected boolean isAbstract(ObjectModelClass clazz) { + if (clazz.isAbstract()) { + return true; + } + return false; + } } //BeanGenerator Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java 2009-04-13 11:34:36 UTC (rev 1422) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java 2009-04-15 07:33:53 UTC (rev 1423) @@ -142,6 +142,9 @@ /** Tag pour specifier de ne pas generer la methode toString */ public static final String TAG_NOT_GENERATE_TO_STRING = "notGenerateToString"; + + /** Tag pour specifier de trier les attributs par nom lors de la generation */ + public static final String TAG_SORT_ATTRIBUTE = "sortAttribute"; /** Type de persistence Hibernate */ public static final String PERSISTENCE_TYPE_HIBERNATE = "hibernate"; @@ -313,7 +316,33 @@ } return true; } + /** + * 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 boolean sortAttribute(ObjectModelClass clazz, + ObjectModel model) { + String value; + value = clazz.getTagValue(TAG_SORT_ATTRIBUTE); + if (value == null || value.trim().isEmpty() || "false".equals(value.trim())) { + return false; + } + if (value != null && "true".equals(value.trim())) { + return true; + } + value = model.getTagValue(TAG_SORT_ATTRIBUTE); + if (value == null || value.trim().isEmpty() || "false".equals(value.trim())) { + return false; + } + if (value != null && "true".equals(value.trim())) { + return true; + } + return true; + } + /** * Detecte si un attribut fait partie d'une clef metier. *
participants (1)
-
tchemit@users.labs.libre-entreprise.org