Index: topia2/src/java/org/codelutin/topia/generator/DTOGenerator.java diff -u /dev/null topia2/src/java/org/codelutin/topia/generator/DTOGenerator.java:1.1 --- /dev/null Mon Jun 2 10:21:55 2008 +++ topia2/src/java/org/codelutin/topia/generator/DTOGenerator.java Mon Jun 2 10:21:48 2008 @@ -0,0 +1,319 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* 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 Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +/* * +* EntityAbstractGenerator.java +* +* Created: 12 déc. 2005 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +* +* Mise a jour: $Date: 2008-06-02 10:21:48 $ +* par : $Author: eore $ +*/ + +package org.codelutin.topia.generator; + +import static org.codelutin.topia.generator.GeneratorUtil.TAG_ANNOTATION; +import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.Iterator; + +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.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; + +/** + * Generateur d'entites abstraites. Il s'agit de l'implatation par defaut d'une + * entite. Les classes generees sont surchargees par un XXXImpl lorsque l'entite + * n'est pas abstraite. La surcharge peut etre ecrite par l'utilisateur. + */ +public class DTOGenerator extends ObjectModelGenerator { + + /** + * Logger for this class + */ + private static final Log log = LogFactory + .getLog(DTOGenerator.class); + + public DTOGenerator() { + super(); + } + + @Override + public String getFilenameForClass(ObjectModelClass clazz) { + return clazz.getQualifiedName().replace('.', File.separatorChar) + "DTO.java"; + } + + public boolean isDTO(String type) { + ObjectModelClassifier clazz = model.getClassifier(type); + if (clazz == null) { + return false; + } + return clazz.hasStereotype(GeneratorUtil.STEREOTYPE_DTO); + } + + @Override + public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { + if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_DTO)) { + return; + } + String copyright = GeneratorUtil.getCopyright(model); + if (GeneratorUtil.notEmpty(copyright)) { +/*{<%=copyright%> +}*/ + } +/*{package <%=clazz.getPackageName()%>; + +import org.apache.commons.lang.builder.ToStringBuilder; + +/** + * Implantation DTO pour <%=Util.capitalize(clazz.getName())%>. + *) +public class <%=clazz.getName()%>DTO }*/ + +/* + * Définition de la super classe : il ne doit y avoir qu'une + */ + + String extendClass = ""; + Iterator j = clazz.getSuperclasses().iterator(); + if (j.hasNext()) { + ObjectModelClassifier parent = j.next(); + if (parent.hasStereotype(GeneratorUtil.STEREOTYPE_DTO)) { + extendClass += parent.getName() + "DTO"; + } else { + extendClass += parent.getName(); + } + } + + if (extendClass.length() > 0) { + /*{extends <%=extendClass%> }*/ + } +/* + * Définition des interfaces + */ + /*{implements java.io.Serializable }*/ + String implInterface = ""; + for (Iterator i=clazz.getInterfaces().iterator(); i.hasNext();) { + ObjectModelClassifier parentInterface = i.next(); + if (parentInterface.hasStereotype(GeneratorUtil.STEREOTYPE_DTO)) { + implInterface += parentInterface.getName() + "DTO"; + } else { + implInterface += parentInterface.getName(); + } + + if (i.hasNext()) { + implInterface += ", "; + } + } + if (implInterface.length() > 0) { + /*{,<%=implInterface%> { + +}*/ + } else { + /*{ { + +}*/ + } + + String svUID = GeneratorUtil.findTagValue("dto-serialVersionUID", clazz, model); + 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 (GeneratorUtil.hasDocumentation(attr)) { +/*{ /** + * <%=attr.getDocumentation()%> + *) +}*/ + } + String annotation = attr.getTagValue(TAG_ANNOTATION); + if (annotation != null && annotation.length() > 0) { +/*{ <%=annotation%> +}*/ + } + if (!Util.isNMultiplicity(attr)) { + if (!attr.hasAssociationClass()) { +/*{ <%=attr.getVisibility()%> <%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> <%=attr.getName()%>; + +}*/ + } else { + String assocAttrName = GeneratorUtil.getAssocAttrName(attr); +/*{ <%=attr.getVisibility()%> <%=attr.getAssociationClass().getQualifiedName()%>DTO <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + +}*/ + } + } else { + if (!attr.hasAssociationClass()) { +/*{ <%=attr.getVisibility()%> <%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%>> <%=attr.getName()%>; + +}*/ + } else { + String assocAttrName = GeneratorUtil.getAssocAttrName(attr); +/*{ <%=attr.getVisibility()%> <%=((attr.isOrdered())?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>DTO> <%=Util.toLowerCaseFirstLetter(assocAttrName)%>; + +}*/ + } + } + } /* end for*/ + + //Déclaration des attributs d'une classe d'associations + if (clazz instanceof ObjectModelAssociationClass) { + ObjectModelAssociationClass assoc = (ObjectModelAssociationClass)clazz; + for (Iterator i = assoc.getParticipantsAttributes().iterator(); i.hasNext(); ) { + ObjectModelAttribute attr = (ObjectModelAttribute) i.next(); + if (attr != null) { +/*{ <%=attr.getVisibility()%> <%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>; + +}*/ + } + } + } + +/*{ /** + * Constructeur de <%=clazz.getName()%>DTO par défaut. + *) + public <%=clazz.getName()%>DTO() {} + +}*/ + /* + * Définition des getteurs et setteurs + */ + for (Iterator it = clazz.getAttributes().iterator(); it.hasNext();) { + ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + ObjectModelAttribute reverse = attr.getReverseAttribute(); + + if (!attr.isNavigable()) { + continue; + } + if (!Util.isNMultiplicity(attr)) { + if (!attr.hasAssociationClass()) { +/*{ public void set<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> value) { + this.<%=attr.getName()%> = value; + } + + public <%=attr.getType()%><%=(isDTO(attr.getType())?"DTO":"")%> get<%=Util.capitalize(attr.getName())%>() { + return <%=attr.getName()%>; + } + +}*/ + } else { + String assocAttrName = GeneratorUtil.getAssocAttrName(attr); + if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } +/*{ public void set<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%>DTO association) { + this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = association; + } + + public <%=attr.getAssociationClass().getQualifiedName()%>DTO get<%=Util.capitalize(assocAttrName)%>() { + return <%=Util.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) { + this.<%=attr.getName()%> = values; + } + +}*/ + } else { + String assocAttrName = GeneratorUtil.getAssocAttrName(attr); + 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) { + this.<%=Util.toLowerCaseFirstLetter(assocAttrName)%> = 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()%>; + } + +}*/ + } else { + String assocAttrName = GeneratorUtil.getAssocAttrName(attr); + 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)%>; + } + +}*/ + } + } + } + +/*{ /* (non-Javadoc) + * @see java.lang.Object#toString() + *) + public String toString() { + String result = new ToStringBuilder(this). +}*/ + for (Iterator it = clazz.getAttributes().iterator(); it.hasNext();) { + ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + 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)); + ObjectModelAttribute reverse = attr.getReverseAttribute(); + if ((isDTO && (reverse == null || !reverse.isNavigable()) && !attr.hasAssociationClass()) || (!isDTO)) { +/*{ append("<%=attr.getName()%>", this.<%=attr.getName()%>). +}*/ + } + } +/*{ toString(); + return result; + } + +} //<%=clazz.getName()%>DTO +}*/ + } + +} //EntityDTOGenerator