Index: topia2/src/java/org/codelutin/topia/generator/TopiaMetaGenerator.java diff -u topia2/src/java/org/codelutin/topia/generator/TopiaMetaGenerator.java:1.7 topia2/src/java/org/codelutin/topia/generator/TopiaMetaGenerator.java:1.8 --- topia2/src/java/org/codelutin/topia/generator/TopiaMetaGenerator.java:1.7 Wed Sep 20 12:28:47 2006 +++ topia2/src/java/org/codelutin/topia/generator/TopiaMetaGenerator.java Wed Apr 18 10:38:10 2007 @@ -24,9 +24,9 @@ * @author Grégoire DESSARD Copyright Code Lutin, Grégoire * Dessard * - * @version $Revision: 1.7 $ + * @version $Revision: 1.8 $ * - * Mise a jour: $Date: 2006/09/20 12:28:47 $ par : $Author: thimel $ + * Mise a jour: $Date: 2007/04/18 10:38:10 $ par : $Author: chatellier $ */ package org.codelutin.topia.generator; @@ -123,9 +123,13 @@ gen = new DAOAbstractGenerator(this); gen.generate(model, destDir); - // Génère du DAOHelper des entités + // Génère les DAOHelper des entités gen = new DAOHelperGenerator(this); gen.generate(model, destDir); + + // Génère les interfaces des services + gen = new ServiceInterfaceGenerator(this); + gen.generate(model, destDir); } Index: topia2/src/java/org/codelutin/topia/generator/ServiceInterfaceGenerator.java diff -u /dev/null topia2/src/java/org/codelutin/topia/generator/ServiceInterfaceGenerator.java:1.1 --- /dev/null Wed Apr 18 10:38:15 2007 +++ topia2/src/java/org/codelutin/topia/generator/ServiceInterfaceGenerator.java Wed Apr 18 10:38:10 2007 @@ -0,0 +1,399 @@ +/* *##% +* 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. +*##%*/ + +/* * +* EntityGenerator.java +* +* Created: 12 déc. 2005 +* +* @author Arnaud Thimel +* @version $Revision: 1.1 $ +* +* Mise a jour: $Date: 2007/04/18 10:38:10 $ +* par : $Author: chatellier $ +*/ + +package org.codelutin.topia.generator; + +import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.apache.commons.logging.Log; +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.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.generator.models.object.ObjectModelOperation; +import org.codelutin.generator.models.object.ObjectModelParameter; + +public class ServiceInterfaceGenerator extends ObjectModelGenerator { + + /** + * Logger for this class + */ + private static final Log log = LogFactory + .getLog(ServiceInterfaceGenerator.class); + + public ServiceInterfaceGenerator(Generator parent) { + super(parent); + } + + @Override + public String getFilenameForClass(ObjectModelClass clazz) { + return clazz.getQualifiedName().replace('.', File.separatorChar) + ".java"; + } + + @Override + public String getFilenameForInterface(ObjectModelInterface interfacez) { + return interfacez.getQualifiedName().replace('.', File.separatorChar) + ".java"; + } + + /* (non-Javadoc) + * @see org.codelutin.generator.ObjectModelGenerator#generateFromInterface(java.io.Writer, org.codelutin.generator.models.object.ObjectModelInterface) + */ + @Override + public void generateFromInterface(Writer output, ObjectModelInterface interfacez) throws IOException { + + if (!interfacez.hasStereotype(GeneratorUtil.STEREOTYPE_SERVICE)) { + return; + } + + generateInterfaceHeader(output, interfacez); + + generateInterfaceOperations(output, interfacez); + +/*{} //<%=interfacez.getName()%> +}*/ + } + + private String getStringRepresentation(List strings) { + StringBuffer result = new StringBuffer(); + String doubleQuote = "\""; + String comma = ","; + Iterator it = strings.iterator(); + while (it.hasNext()) { + result.append(doubleQuote); + result.append(it.next()); + result.append(doubleQuote); + if (it.hasNext()) { + result.append(comma); + } + } + return result.toString(); + } + + private void generateInterfaceHeader(Writer output, ObjectModelClassifier classifier) throws IOException { +/*{package <%=classifier.getPackageName()%>; + +}*/ + if (GeneratorUtil.hasDocumentation(classifier)) { +/*{ +/** + * <%=classifier.getDocumentation()%> + *) +}*/ + } +/*{public interface <%=classifier.getName()%> extends }*/ + String extendClass = ""; + for (Iterator i=classifier.getInterfaces().iterator(); i.hasNext();) { + ObjectModelClassifier parent = (ObjectModelClassifier)i.next(); + extendClass += parent.getQualifiedName(); + extendClass += ", "; + } + if (classifier instanceof ObjectModelClass) { + ObjectModelClass clazz = (ObjectModelClass)classifier; + for (Iterator i=clazz.getSuperclasses().iterator(); i.hasNext();) { + ObjectModelClassifier parent = (ObjectModelClassifier)i.next(); + if (parent.hasStereotype(GeneratorUtil.STEREOTYPE_SERVICE)) { + extendClass += parent.getQualifiedName(); + } + extendClass += ", "; + } + } + +/*{<%=extendClass%>org.codelutin.topia.service.TopiaApplicationService { + +}*/ + } + + @Override + public void generateFromClass(Writer output, ObjectModelClass clazz) throws IOException { + if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_SERVICE)) { + return; + } + generateInterfaceHeader(output, clazz); + for (Iterator it = clazz.getAttributes().iterator(); it.hasNext();) { + ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + ObjectModelAttribute reverse = attr.getReverseAttribute(); + if (!attr.isNavigable() + && !hasUnidirectionalRelationOnAbstractType(reverse, model)) { + continue; + } + if (!Util.isNMultiplicity(attr)) { + if (!attr.hasAssociationClass()) { +/*{ /** +}*/ + if (GeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> La valeur de l'attribut <%=attr.getName()%> à positionner. + *) + public void set<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + +}*/ +/*{ /** +}*/ + if (GeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * @return La valeur de l'attribut <%=attr.getName()%>. + *) + public <%=attr.getType()%> get<%=Util.capitalize(attr.getName())%>(); + +}*/ + } else { + String assocAttrName = GeneratorUtil.getAssocAttrName(attr); + if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } +/*{ /** + * @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 + if (!attr.hasAssociationClass()) { //Méthodes remplacées par des add/set sur les classes d'assoc +/*{ /** +}*/ + if (GeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> L'instance de <%=attr.getName()%> à ajouter. + *) + public void add<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + + /** +}*/ + if (GeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> Les instances de <%=attr.getName()%> à ajouter. + *) + public void addAll<%=Util.capitalize(attr.getName())%>(<%=(attr.isOrdered()?"java.util.List":"java.util.Collection")%><<%=attr.getType()%>> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + + /** +}*/ + if (GeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> La Collection de <%=attr.getName()%> à positionner. + *) + public void set<%=Util.capitalize(attr.getName())%>(<%=(attr.isOrdered()?"java.util.List":"java.util.Collection")%><<%=attr.getType()%>> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + + /** +}*/ + if (GeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * @param <%=Util.toLowerCaseFirstLetter(attr.getName())%> L'instance de <%=attr.getName()%> à retirer. + *) + public void remove<%=Util.capitalize(attr.getName())%>(<%=attr.getType()%> <%=Util.toLowerCaseFirstLetter(attr.getName())%>); + + /** +}*/ + if (GeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * Vide la Collection de <%=attr.getName()%>. + *) + public void clear<%=Util.capitalize(attr.getName())%>(); + +}*/ + } 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. + *) + public void add<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); + + /** + * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> Les instances de <%=attr.getAssociationClass().getName()%> à ajouter. + *) + public void addAll<%=Util.capitalize(assocAttrName)%>(<%=(attr.isOrdered()?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); + + /** + * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> La Collection de <%=attr.getAssociationClass().getName()%> à positionner. + *) + public void set<%=Util.capitalize(assocAttrName)%>(<%=(attr.isOrdered()?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); + + /** + * @param <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> L'instance de <%=attr.getAssociationClass().getName()%> à retirer. + *) + public void remove<%=Util.capitalize(assocAttrName)%>(<%=attr.getAssociationClass().getQualifiedName()%> <%=Util.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%>); + + /** + * Vide la Collection de <%=attr.getAssociationClass().getName()%>. + *) + public void clear<%=Util.capitalize(assocAttrName)%>(); + +}*/ + } + + if (!attr.hasAssociationClass()) { +/*{ /** +}*/ + if (GeneratorUtil.hasDocumentation(attr)) { +/*{ * <%=attr.getName()%> : <%=attr.getDocumentation()%> +}*/ + } +/*{ * @return La Liste de <%=attr.getName()%>. + *) + public <%=(attr.isOrdered()?"java.util.List":"java.util.Collection")%><<%=attr.getType()%>> get<%=Util.capitalize(attr.getName())%>(); + + /** + * @return Le nombre d'éléments de la collection <%=attr.getName()%>. + *) + public int size<%=Util.capitalize(attr.getName())%>(); + +}*/ + } else { + String assocAttrName = GeneratorUtil.getAssocAttrName(attr); + if (log.isTraceEnabled()) { log.trace("assocAttrName: " + assocAttrName); } +/*{ /** + * @return La liste des attributs <%=attr.getAssociationClass().getName()%>. + *) + public <%=(attr.isOrdered()?"java.util.List":"java.util.Collection")%><<%=attr.getAssociationClass().getQualifiedName()%>> get<%=Util.capitalize(assocAttrName)%>(); + + /** + * @return L'attribut <%=attr.getAssociationClass().getName()%> associé à la valeur value de l'attribut <%=attr.getName()%>. + *) + public <%=attr.getAssociationClass().getQualifiedName()%> get<%=Util.capitalize(assocAttrName)%>(<%=attr.getType()%> value); + + /** + * @return Le nombre d'éléments de la collection <%=attr.getName()%>. + *) + public int size<%=Util.capitalize(assocAttrName)%>(); + +}*/ + } + } + } + + //Méthodes d'accès aux 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) { + String type = attr.getType(); + String name = attr.getName(); + generateAssociationAccessors(output, name, type); + if (attr.getReverseAttribute() == null) { + type = ((ObjectModelClassifier)attr.getDeclaringElement()).getQualifiedName(); + name = attr.getDeclaringElement().getName(); + generateAssociationAccessors(output, name, type); + } + } + } + } + + generateInterfaceOperations(output, clazz); + +/*{} //<%=clazz.getName()%> +}*/ + } + + private void generateInterfaceOperations(Writer output, ObjectModelClassifier classifier) throws IOException { + for (Iterator it = classifier.getOperations().iterator(); it.hasNext();) { + ObjectModelOperation op = (ObjectModelOperation)it.next(); +/*{ /** +}*/ + if (GeneratorUtil.hasDocumentation(op)) { +/*{ * <%=op.getName()%> : <%=op.getDocumentation()%> +}*/ + } + Collection params = (Collection)op.getParameters(); + for(ObjectModelParameter param : params) { + if(log.isTraceEnabled()) {log.trace("Param" + param);} +/*{ * @param <%=param.getName()%> <%=param.getDocumentation()%> + }*/ + } +/*{ *) + <%=op.getVisibility()%> <%=op.getReturnType()%> <%=op.getName()%>(}*/ + String vir = ""; + for(ObjectModelParameter param : params){ + if(log.isTraceEnabled()) {log.trace("Param" + param + " vir" + vir);} +/*{<%=vir%><%=param.getType()%> <%=param.getName()%>}*/ + vir = ", "; + } +/*{)}*/ + Set exceptions = (Set)op.getExceptions(); + vir = " throws "; + for (String exception : exceptions) { + if(log.isTraceEnabled()) {log.trace("exception" + exception + " vir" + vir);} +/*{<%=vir%><%=exception%>}*/ + vir = ", "; + } +/*{; + +}*/ + } + } + + private void generateAssociationAccessors(Writer output, String name, String type) throws IOException { +/*{ /** + * @param value La valeur de l'attribut <%=name%> à positionner. + *) + public void set<%=Util.capitalize(name)%>(<%=type%> value); + + /** + * @return La valeur de l'attribut <%=name%>. + *) + public <%=type%> get<%=Util.capitalize(name)%>(); + +}*/ + } + +} //ServiceInterfaceGenerator