Index: topia2/src/java/org/codelutin/topia/generator/ServiceAbstractGenerator.java diff -u /dev/null topia2/src/java/org/codelutin/topia/generator/ServiceAbstractGenerator.java:1.1 --- /dev/null Tue Jun 19 15:43:20 2007 +++ topia2/src/java/org/codelutin/topia/generator/ServiceAbstractGenerator.java Tue Jun 19 15:43:15 2007 @@ -0,0 +1,322 @@ +/* *##% +* 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. +*##%*/ +package org.codelutin.topia.generator; + +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.Collection; +import java.util.Iterator; +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; +import org.codelutin.topia.service.TopiaApplicationServiceAbstract; + +/** +* ServiceAbstractGenerator.java +* +* Created: 19 juin 2007 +* +* @author dupont +* @version $Revision: 1.1 $ +* +* Le service abstrait etend TopiaApplicationServiceAbstract et implement +* l'interface du service. +* +* Genere l'implantation des methodes qui utilisent les methodes des DAO. +* +* @see ServiceInterfaceGenerator +* @see TopiaApplicationServiceAbstract +* +* Mise a jour: $Date: 2007/06/19 15:43:15 $ +* par : $Author: ndupont $ +*/ +public class ServiceAbstractGenerator extends ObjectModelGenerator { + + private static final String DAO = "dao"; + + private static final Log log = LogFactory + .getLog(ServiceInterfaceGenerator.class); + + public ServiceAbstractGenerator(Generator parent) { + super(parent); + } + + @Override + public String getFilenameForInterface(ObjectModelInterface clazz) { + return clazz.getQualifiedName().replace('.', File.separatorChar) + "Abstract.java"; + } + + @Override + public void generateFromInterface(Writer output, ObjectModelInterface interfacez) throws IOException { + + if (!interfacez.hasStereotype(GeneratorUtil.STEREOTYPE_SERVICE)) { + return; + } + + generateInterfaceHeader(output, interfacez); + + // generateInterfaceOperations(output, interfacez); + + // générer les méthodes étendues en modifiant les signatures pour éviter les doublons (findAllByAdresse héritée de Person et Employee) + generateInheritedInterfaceOperations(output, interfacez); + +/*{} //<%=interfacez.getName()%> +}*/ + } + + private void generateInterfaceHeader(Writer output, ObjectModelClassifier classifier) throws IOException { +/*{package <%=classifier.getPackageName()%>; + +import java.util.Arrays; +import java.util.List; +import org.codelutin.topia.TopiaException; +import org.codelutin.topia.service.TopiaApplicationServiceAbstract; +import org.codelutin.topia.framework.TopiaContextImplementor;}*/ + + // <%=clazz.getName()%>DAOAbstract + classifier.getInterfaces(); + // ajouter les imports des interfaces de DAO + for (Iterator i=classifier.getInterfaces().iterator(); i.hasNext();) { + ObjectModelClassifier parent = (ObjectModelClassifier)i.next(); + if(parent.hasStereotype(DAO)){ + // récupérer la classe de l'object model correspondant à la DAO + String className = parent.getQualifiedName().replace(DAO, ""); + String interfaceName = parent.getQualifiedName()+"Abstract"; +/*{ +import <%=interfaceName%>; +import <%=className%>;}*/ + } + } + + if (GeneratorUtil.hasDocumentation(classifier)) { + +/*{ +/** + * + * <%=classifier.getDocumentation()%> + *) +}*/ + } +/*{ + +public class <%=classifier.getName()+"Abstract"%> extends TopiaApplicationServiceAbstract implements <%=classifier.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 generateInheritedInterfaceOperations(Writer output, ObjectModelClassifier classifier) throws IOException { + + for (Iterator i=classifier.getInterfaces().iterator(); i.hasNext();) { + ObjectModelClassifier parent = (ObjectModelClassifier)i.next(); + // pour tous les DAOInterface + if(parent.hasStereotype(DAO)){ + + // récupérer la classe de l'object model correspondant à la DAO + String entityClassName = parent.getQualifiedName().replace(DAO, ""); + if(getModel().hasClass(entityClassName)){ + ObjectModelClass clazz = getModel().getClass(entityClassName); + + if (!clazz.hasStereotype(GeneratorUtil.STEREOTYPE_ENTITY)) { + return; + } +/*{ + /** + * Supprime l'entite <%=clazz.getName()%> passee en parametre + * @param entity l'entite a supprimer + *) + public void delete<%=clazz.getName()%>(<%=clazz.getQualifiedName()%> entity) throws TopiaException{ + TopiaContextImplementor usedContextImpl = (TopiaContextImplementor) topiaContext; + <%=clazz.getName()%>DAOAbstract dao = (<%=clazz.getName()%>DAOAbstract)usedContextImpl.getDAO(<%=clazz.getName()%>.class); + dao.delete(entity); + } +}*/ + for (Iterator it = clazz.getAttributes().iterator(); it.hasNext();) { + ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + if (!attr.isNavigable()) { + continue; + } + + if (!Util.isNMultiplicity(attr)) { + generateNoNMultiplicity(output, attr, clazz, false); + } else { + generateNMultiplicity(output, attr, clazz, false); + } + } + + if (clazz instanceof ObjectModelAssociationClass) { + ObjectModelAssociationClass assocClass = (ObjectModelAssociationClass)clazz; + Iterator it = assocClass.getParticipantsAttributes().iterator(); + while (it.hasNext()) { + ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + if (attr != null) { + if (!Util.isNMultiplicity(attr)) { + generateNoNMultiplicity(output, attr, clazz, true); + } else { + generateNMultiplicity(output, attr, clazz, true); + } + } + } + } + } + } + } + } + + protected void generateNoNMultiplicity(Writer output, ObjectModelAttribute attr, ObjectModelClass clazz, boolean isAssoc) throws IOException { + String propertyName = attr.getName(); + if (!isAssoc && attr.hasAssociationClass()) { + propertyName = GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName()) + "." + propertyName; + } +/*{ + /** + * Retourne le premier element trouve ayant comme valeur pour l'attribut + * <%=attr.getName()%> le parametre + * @param v la valeur que doit avoir <%=attr.getName()%> + * @return un element ou null + *) + public <%=clazz.getQualifiedName()%> find<%=clazz.getName()%>By<%=GeneratorUtil.capitalize(attr.getName())%>(<%=attr.getType()%> v) throws TopiaException{ + TopiaContextImplementor usedContextImpl = (TopiaContextImplementor) topiaContext; + <%=clazz.getName()%>DAOAbstract dao = (<%=clazz.getName()%>DAOAbstract)usedContextImpl.getDAO(<%=clazz.getName()%>.class); + <%=clazz.getName()%> entity = dao.findBy<%=GeneratorUtil.capitalize(attr.getName())%>(v); + return entity; + }; + + /** + * Retourne les elements ayant comme valeur pour l'attribut + * <%=attr.getName()%> le parametre + * @param v la valeur que doit avoir <%=attr.getName()%> + * @return une liste + *) + public List<<%=clazz.getQualifiedName()%>> findAll<%=clazz.getName()%>By<%=GeneratorUtil.capitalize(attr.getName())%>(<%=attr.getType()%> v) throws TopiaException{ + TopiaContextImplementor usedContextImpl = (TopiaContextImplementor) topiaContext; + <%=clazz.getName()%>DAOAbstract dao = (<%=clazz.getName()%>DAOAbstract)usedContextImpl.getDAO(<%=clazz.getName()%>.class); + List<<%=clazz.getName()%>> entityList = dao.findAllBy<%=GeneratorUtil.capitalize(attr.getName())%>(v); + return entityList; + }; +}*/ + if (attr.hasAssociationClass()) { +/*{ + /** + * Retourne le premier element trouve ayant comme valeur pour l'attribut + * <%=GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> le parametre + * @param value la valeur que doit avoir <%=GeneratorUtil.toLowerCaseFirstLetter(attr.getAssociationClass().getName())%> + * @return un element ou null + *) + public <%=clazz.getQualifiedName()%> find<%=clazz.getName()%>By<%=GeneratorUtil.capitalize(attr.getAssociationClass().getName())%>(<%=attr.getAssociationClass().getQualifiedName()%> value) throws TopiaException{ + TopiaContextImplementor usedContextImpl = (TopiaContextImplementor) topiaContext; + <%=clazz.getName()%>DAOAbstract dao = (<%=clazz.getName()%>DAOAbstract)usedContextImpl.getDAO(<%=clazz.getName()%>.class); + <%=clazz.getName()%> entity = dao.findBy<%=GeneratorUtil.capitalize(attr.getName())%>(v); + return entity; + }; + + /** + * Retourne les elements 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())%> + * @return une liste + *) + public List<<%=clazz.getQualifiedName()%>> findAll<%=clazz.getName()%>By<%=GeneratorUtil.capitalize(attr.getAssociationClass().getName())%>(<%=attr.getAssociationClass().getQualifiedName()%> value) throws TopiaException{ + TopiaContextImplementor usedContextImpl = (TopiaContextImplementor) topiaContext; + <%=clazz.getName()%>DAOAbstract dao = (<%=clazz.getName()%>DAOAbstract)usedContextImpl.getDAO(<%=clazz.getName()%>.class); + List<<%=clazz.getName()%>> entityList = dao.findAllBy<%=GeneratorUtil.capitalize(attr.getName())%>(v); + return entityList; + }; +}*/ + } + } + + protected void generateNMultiplicity(Writer output, ObjectModelAttribute attr, ObjectModelClass clazz, boolean isAssoc) throws IOException { +/*{ + /** + * Retourne le premier element trouve dont l'attribut + * <%=attr.getName()%> contient le parametre + * @param v la valeur que doit contenir <%=attr.getName()%> + * @return un element ou null + *) + public <%=clazz.getQualifiedName()%> find<%=clazz.getName()%>Contains<%=GeneratorUtil.capitalize(attr.getName())%>(<%=attr.getType()%> ... v) throws TopiaException{ + TopiaContextImplementor usedContextImpl = (TopiaContextImplementor) topiaContext; + <%=clazz.getName()%>DAOAbstract dao = (<%=clazz.getName()%>DAOAbstract)usedContextImpl.getDAO(<%=clazz.getName()%>.class); + <%=clazz.getName()%> entity = (<%=clazz.getName()%>) dao.findContainsProperties("<%=GeneratorUtil.toLowerCaseFirstLetter(attr.getName())%>", Arrays.asList(v)); + return entity; + }; + + /** + * Retourne les elements trouve dont l'attribut + * <%=attr.getName()%> contient le parametre + * @param v la valeur que doit contenir <%=attr.getName()%> + * @return une liste + *) + public List<<%=clazz.getQualifiedName()%>> findAll<%=clazz.getName()%>Contains<%=GeneratorUtil.capitalize(attr.getName())%>(<%=attr.getType()%> ... v) throws TopiaException{ + TopiaContextImplementor usedContextImpl = (TopiaContextImplementor) topiaContext; + <%=clazz.getName()%>DAOAbstract dao = (<%=clazz.getName()%>DAOAbstract)usedContextImpl.getDAO(<%=clazz.getName()%>.class); + List<<%=clazz.getName()%>> entityList = dao.findAllContainsProperties("<%=GeneratorUtil.toLowerCaseFirstLetter(attr.getName())%>", Arrays.asList(v)); + return entityList; + }; +}*/ + } + +} //ServiceInterfaceGenerator