Author: tchemit Date: 2009-04-13 11:34:36 +0000 (Mon, 13 Apr 2009) New Revision: 1422 Added: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java Log: add a simple Bean generator with pcs notifications Added: 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 (rev 0) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/BeanGenerator.java 2009-04-13 11:34:36 UTC (rev 1422) @@ -0,0 +1,401 @@ +/* *##% 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>. ##%*/ + +/* * +* EntityAbstractGenerator.java +* +* Created: 12 déc. 2005 +* +* @author Arnaud Thimel <thimel@codelutin.com> +* @version $Revision: 1334 $ +* +* Mise a jour: $Date: 2009-01-29 16:47:42 +0100 (jeu 29 jan 2009) $ +* par : $Author: thimel $ +*/ + +package org.codelutin.topia.generator; + +import static org.codelutin.topia.generator.TopiaGeneratorUtil.TAG_ANNOTATION; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.Iterator; + +import java.util.List; +import org.apache.commons.logging.Log; +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; + +/** + * DTO generator + */ +public class BeanGenerator extends ObjectModelGenerator { + + /** + * Logger for this class + */ + private static final Log log = LogFactory.getLog(BeanGenerator.class); + + public BeanGenerator() { + super(); + } + + @Override + public String getFilenameForClass(ObjectModelClass clazz) { + return clazz.getQualifiedName().replace('.', File.separatorChar) + ".java"; + } + + @Override + public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { + if (!clazz.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_BEAN)) { + return; + } + String copyright = TopiaGeneratorUtil.getCopyright(model); + if (TopiaGeneratorUtil.notEmpty(copyright)) { +/*{<%=copyright%> +}*/ + } + String clazzName = clazz.getName(); +/*{package <%=clazz.getPackageName()%>; + }*/ + boolean generateToString = TopiaGeneratorUtil.generateToString(clazz, model); + List<String> imports = TopiaGeneratorUtil.getImports(clazz, + java.beans.PropertyChangeListener.class.getName() + ); + if (generateToString) { + imports.add(org.apache.commons.lang.builder.ToStringBuilder.class.getName()); + } + if (log.isDebugEnabled()) { + log.debug("imports for class <" + clazzName + ">"); + } + for (String anImport : imports) { + if (log.isDebugEnabled()) { + log.debug("import " + anImport); + } +/*{import <%=anImport%>; +}*/ + } + +/** + * 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%> { + +}*/ + } else { + /*{ { + +}*/ + } + + 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*/ + + //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); + } + + public void addPropertyChangeListener(PropertyChangeListener listener) { + pcs.addPropertyChangeListener(listener); + } + + public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { + pcs.addPropertyChangeListener(propertyName, listener); + } + + public void removePropertyChangeListener(PropertyChangeListener listener) { + pcs.removePropertyChangeListener(listener); + } + + public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { + pcs.removePropertyChangeListener(propertyName, listener); + } + + protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { + pcs.firePropertyChange(propertyName, oldValue, newValue); + } +}*/ + + + /* + * Définition des getteurs et setteurs + */ + for (Object o : clazz.getAttributes()) { + ObjectModelAttribute attr = (ObjectModelAttribute) o; + ObjectModelAttribute reverse = attr.getReverseAttribute(); + + 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)%>() { + 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)%>; + } + +}*/ + } + } 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); + } + +}*/ + + //AddChild +/*{ public <%=attrType%> addChild(<%=attrType%> <%=attrName%>) { + this.<%=attrName%>.add(<%=attrName%>); + firePropertyChange("<%=attrName%>", null, <%=attrName%>); +}*/ + if (reverseNavigable) { +/*{ <%=attrName%>.set<%=GeneratorUtil.capitalize(reverseAttrName)%>(this); +}*/ + } +/*{ return <%=attrName%>; + } + +}*/ + //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 (!attr.hasAssociationClass()) { + String nMultType = getCollection(attr, attrType); +/*{ public <%=nMultType%> get<%=GeneratorUtil.capitalize(attrName)%>() { + return this.<%=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)%>; + } + +}*/ + } + } + } + + if (generateToString) { +/*{ + @Override + public String toString() { + String result = new ToStringBuilder(this). +}*/ + 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()); + } + String attrName = attr.getName(); +/*{ append("<%=attrName%>", this.<%=attrName%>). +}*/ + } +/*{ toString(); + return result; + } + }*/ + } +/*{ +} //<%=clazz.getName()%> +}*/ + } + + protected String getCollection(ObjectModelAttribute attr, String attrType) { + String nMultType; + if (attr.isOrdered()) { + nMultType = "List<"; + } else { + nMultType = "Collection<"; + } + nMultType += attrType; + nMultType += ">"; + return nMultType; + } +} //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-12 21:21:30 UTC (rev 1421) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/TopiaGeneratorUtil.java 2009-04-13 11:34:36 UTC (rev 1422) @@ -63,6 +63,8 @@ /** 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 objets devant être générées sous forme de bean */ + public static final String STEREOTYPE_BEAN = "bean"; /** * Stéréotype pour les interfaces devant être générées sous forme de * services @@ -302,11 +304,11 @@ ObjectModel model) { String value; value = model.getTagValue(TAG_NOT_GENERATE_TO_STRING); - if (value != null || value.trim().isEmpty()) { + if (value != null && !value.trim().isEmpty()) { return false; } value = clazz.getTagValue(TAG_NOT_GENERATE_TO_STRING); - if (value != null || value.trim().isEmpty()) { + if (value != null && !value.trim().isEmpty()) { return false; } return true;
participants (1)
-
tchemit@users.labs.libre-entreprise.org