Index: topia/src/java/org/codelutin/topia/generators/ObjectModelToEntityPersistenceServiceImplGenerator.java diff -u topia/src/java/org/codelutin/topia/generators/ObjectModelToEntityPersistenceServiceImplGenerator.java:1.16 topia/src/java/org/codelutin/topia/generators/ObjectModelToEntityPersistenceServiceImplGenerator.java:1.17 --- topia/src/java/org/codelutin/topia/generators/ObjectModelToEntityPersistenceServiceImplGenerator.java:1.16 Fri Aug 19 14:52:47 2005 +++ topia/src/java/org/codelutin/topia/generators/ObjectModelToEntityPersistenceServiceImplGenerator.java Fri Aug 26 17:53:35 2005 @@ -23,9 +23,9 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.16 $ + * @version $Revision: 1.17 $ * - * Mise a jour: $Date: 2005/08/19 14:52:47 $ + * Mise a jour: $Date: 2005/08/26 17:53:35 $ * par : $Author: bpoussin $ */ @@ -88,7 +88,7 @@ // if (i.hasNext()) { // ObjectModelClass superclass = (ObjectModelClass) i.next(); // /*{<%=superclass.getQualifiedName()%>PersistenceServiceImpl}*/ -// }else{ +// }else{1 /*{AbstractTopiaPersistenceService<<%=clazz.getName()%>>}*/ // } }else{ @@ -126,9 +126,14 @@ * @return an entity *) public <%=currentClazz.getName()%> findBy<%=Util.capitalize(attribute.getName())%>(<%=attribute.getType()%> value) throws TopiaException{ - // FIXME il faut une requete du genre TopiaQuery query = new TopiaQuery().select("A").from("asso").where("B=?"); - <%=attribute.getAssociationClass().getQualifiedName()%>PersistenceService ps = ((<%=getProperty("defaultPackage") + "." + model.getName()%>Context)getContext()).get<%=associationClassName%>PersistenceService(); - List result = ps.find(ps.newQuery().where("<%=attribute.getName()%> = ?").limit(0,1).addArg(value)); + TopiaQuery query = new TopiaQuery(); + query.select(<%=attribute.getReverseAttribute().getName()%>) + .from(<%=attribute.getAssociationClass().getQualifiedName()%>.class) + .where("<%=attribute.getName()%> = ?") + .limit(0,1) + .addArg(value)); + List result = getContext().getPersistenceHelper().find(query); + if(result.size() == 0){ return null;/*throw new TopiaNotFoundException("Can't find object with attribute <%=attribute.getName()%> == " + value);*) } @@ -138,10 +143,15 @@ /** * @return an unmodifiable collection *) - public List findAllBy<%=Util.capitalize(attribute.getName())%>(<%=attribute.getType()%> value) throws TopiaException{ - // FIXME idem findBy - <%=attribute.getAssociationClass().getQualifiedName()%>PersistenceService ps = ((<%=getProperty("defaultPackage") + "." + model.getName()%>Context)getContext()).get<%=associationClassName%>PersistenceService(); - return ps.find(ps.newQuery().where("<%=attribute.getName()%> = ?").addArg(value)); + public List<<%=currentClazz.getName()%>> findAllBy<%=Util.capitalize(attribute.getName())%>(<%=attribute.getType()%> value) throws TopiaException{ + TopiaQuery query = new TopiaQuery(); + query.select(<%=attribute.getReverseAttribute().getName()%>) + .from(<%=attribute.getAssociationClass().getQualifiedName()%>.class) + .where("<%=attribute.getName()%> = ?") + .addArg(value)); + List<<%=currentClazz.getName()%>> result = (List<<%=currentClazz.getName()%>>)getContext().getPersistenceHelper().find(query); + + return result; } }*/ } else { @@ -160,8 +170,8 @@ /** * @return an unmodifiable collection *) - public List findAllBy<%=Util.capitalize(attribute.getName())%>(<%=attribute.getType()%> value) throws TopiaException{ - return find(newQuery().where("<%=attribute.getName()%> = ?").addArg(value)); + public List<<%=currentClazz.getName()%>> findAllBy<%=Util.capitalize(attribute.getName())%>(<%=attribute.getType()%> value) throws TopiaException{ + return (List<<%=currentClazz.getName()%>>)find(newQuery().where("<%=attribute.getName()%> = ?").addArg(value)); } }*/ } @@ -189,8 +199,8 @@ /** * @return an unmodifiable collection *) - public List findAllBy<%=Util.capitalize(attribute.getName())%>(<%=attribute.getType()%> value) throws TopiaException{ - return find(newQuery().where("<%=attribute.getName()%> = ?").addArg(value)); + public List<<%=currentClazz.getName()%>> findAllBy<%=Util.capitalize(attribute.getName())%>(<%=attribute.getType()%> value) throws TopiaException{ + return (List<<%=currentClazz.getName()%>>)find(newQuery().where("<%=attribute.getName()%> = ?").addArg(value)); } }*/ } @@ -200,47 +210,12 @@ ////////////////////////////////////////////////////////////////////// public void generateOther(Writer output, ObjectModelClassifier clazzifier) throws IOException { - if(clazzifier instanceof ObjectModelClass){ - ObjectModelClass clazz = (ObjectModelClass)clazzifier; - generateAllOtherAttributeDeclaration(output, clazz); /*{ - /** - * Return new entity - *) - public <%=clazz.getName()%> create() throws TopiaException { - <% if(((ObjectModelClass)clazz).isAbstract()){ %> - throw new TopiaException("Abstract entity can't be instanciate"); - <% } else { %> - return (<%=clazz.getName()%>)super.create(); - <% } %> - } - -// public <%=clazz.getName()%> makePersistent(<%=clazz.getName()%> entity) throws TopiaException { -// if (! (entity instanceof <%=clazz.getName()%>)) { -// throw new TopiaException("Wrong persistence service. You should use the entity related persistence service"); -// } -// return (<%=clazz.getName()%>)super.makePersistent(entity); -// } -// -// public <%=clazz.getName()%> update(<%=clazz.getName()%> entity) throws TopiaException { -// if (! (entity instanceof <%=clazz.getName()%>)) { -// throw new TopiaException("Wrong persistence service. You should use the entity related persistence service"); -// } -// return (<%=clazz.getName()%>)super.update(entity); -// } -// -// public void delete(<%=clazz.getName()%> entity) throws TopiaException { -// if (! (entity instanceof <%=clazz.getName()%>)) { -// throw new TopiaException("Wrong persistence service. You should use the entity related persistence service"); -// } -// super.delete(entity); -// } - /** * Return entity Class managed by this service *) public Class getEntityClass(){ - return <%=clazz.getName()%>.class; + return <%=clazzifier.getName()%>.class; } /** @@ -250,6 +225,22 @@ <%=model.getName()%>Context context = (<%=model.getName()%>Context)getContext(); return context; } +}*/ + + if(clazzifier instanceof ObjectModelClass){ + ObjectModelClass clazz = (ObjectModelClass)clazzifier; + generateAllOtherAttributeDeclaration(output, clazz); +/*{ + /** + * Return new entity + *) + public <%=clazz.getName()%> create() throws TopiaException { + <% if(((ObjectModelClass)clazz).isAbstract()){ %> + throw new TopiaException("Abstract entity can't be instanciate"); + <% } else { %> + return (<%=clazz.getName()%>)super.create(); + <% } %> + } protected String [] fieldNames = new String[] { }*/ @@ -262,62 +253,23 @@ }*/ } /*{ }; - + /** + * @deprecated ne sert sans doute plus a rien maintenant qu'il y a les + * metainfo, il faut essayer de modifier le code qui l'utiliserait + *) public String [] getFieldNames(){ return fieldNames; } }*/ } else { - ObjectModelInterface interfacz = (ObjectModelInterface)clazzifier; /*{ - /** - * Return new entity + * @deprecated ne sert sans doute plus a rien maintenant qu'il y a les + * metainfo, il faut essayer de modifier le code qui l'utiliserait *) - public <%=interfacz.getName()%> create<%=interfacz.getName()%>() throws TopiaException { - throw new TopiaException("Interface entity can't be instanciate"); - } - - public <%=interfacz.getName()%> makePersistent(<%=interfacz.getName()%> entity) throws TopiaException { - if (! (entity instanceof <%=interfacz.getName()%>)) { - throw new TopiaException("Wrong persistence service. You should use the entity related persistence service"); - } - return (<%=interfacz.getName()%>)super.makePersistent(entity); - } - - public <%=interfacz.getName()%> update(<%=interfacz.getName()%> entity) throws TopiaException { - if (! (entity instanceof <%=interfacz.getName()%>)) { - throw new TopiaException("Wrong persistence service. You should use the entity related persistence service"); - } - return (<%=interfacz.getName()%>)super.update(entity); - } - - public void delete(<%=interfacz.getName()%> entity) throws TopiaException { - if (! (entity instanceof <%=interfacz.getName()%>)) { - throw new TopiaException("Wrong persistence service. You should use the entity related persistence service"); - } - super.delete(entity); - } - - /** - * Return entity Class managed by this service - *) - public Class getEntityClass(){ - return <%=interfacz.getName()%>.class; - } - - /** - * Retourne le context d'application associe a cette entite - *) - public <%=model.getName()%>Context getAppContext(){ - <%=model.getName()%>Context context = (<%=model.getName()%>Context)getContext(); - return context; - } - public String [] getFieldNames() throws TopiaException { throw new TopiaException("There is no field on a Interface entity !"); } - }*/ } Index: topia/src/java/org/codelutin/topia/generators/Util.java diff -u topia/src/java/org/codelutin/topia/generators/Util.java:1.15 topia/src/java/org/codelutin/topia/generators/Util.java:1.16 --- topia/src/java/org/codelutin/topia/generators/Util.java:1.15 Wed Aug 24 15:03:45 2005 +++ topia/src/java/org/codelutin/topia/generators/Util.java Fri Aug 26 17:53:35 2005 @@ -23,9 +23,9 @@ * * @author Benjamin Poussin * Copyright Code Lutin - * @version $Revision: 1.15 $ + * @version $Revision: 1.16 $ * - * Mise a jour: $Date: 2005/08/24 15:03:45 $ + * Mise a jour: $Date: 2005/08/26 17:53:35 $ * par : $Author: bpoussin $ */ @@ -34,15 +34,15 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; - -import org.codelutin.generator.models.object.ObjectModelParameter; +import java.util.LinkedList; +import java.util.List; +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 java.util.List; -import java.util.LinkedList; +import org.codelutin.generator.models.object.ObjectModelParameter; /** * Classe contenant des methodes utiles pour la generation @@ -316,18 +316,34 @@ * @return une String contenant les noms des champs sous la forme : '"a", "b", "c", [...], "k"' */ public static String getFieldList(ObjectModelClass clazz) { - String output = ""; + StringBuffer output = new StringBuffer(); Collection attributes = clazz.getAttributes(); if (attributes != null) { Iterator it = attributes.iterator(); while (it.hasNext()) { ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); - output += "\"" + attr.getName() + "\""; - if (it.hasNext()) - output += ", "; + output.append("\"" + attr.getName() + "\", "); + } + } + // BB 20050826 + // il faut aussi mettre dans les champs les attributs d'associations + if (clazz instanceof ObjectModelAssociationClass) { + ObjectModelAssociationClass assocClass = (ObjectModelAssociationClass)clazz; + List list = assocClass.getParticipantsAttributes(); + Iterator it = list.iterator(); + String id; + while (it.hasNext()) { + ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + if (attr != null) { + output.append("\"" + attr.getName() + "\", "); + } } } - return output; + // on supprime la virgule en trop + if(output.length() >= 2){ + output.setLength(output.length() - 2); + } + return output.toString(); } /** @@ -335,39 +351,56 @@ * textuelle d'un tableau de Class : * 'Class[] classes = {<%=getFieldTypes(clazz)%>}') * ATTENTION, les types primitifs sont tranformés en class de la classe - * associée, par exemple : int > Integer.class, float > Float.class + * associée, par exemple : int > Integer.class, float > Float.class * @param clazz la classe dont on souhaite lister le type des champs * @return une String contenant la liste des types des champs sous la forme : 'Integer.class, java.lang.String.class, mon.package.MaClasse.class, [...]' */ public static String getFieldClassTypes(ObjectModelClass clazz) { - String output = ""; + StringBuffer output = new StringBuffer(); Collection attributes = clazz.getAttributes(); if (attributes != null) { Iterator it = attributes.iterator(); while (it.hasNext()) { ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); - output += getFieldClassType(attr); - if (it.hasNext()) - output += ", "; + output.append(getFieldClassType(attr) + ", "); } } - return output; + // BB 20050826 + // il faut aussi mettre dans les champs les attributs d'associations + if (clazz instanceof ObjectModelAssociationClass) { + ObjectModelAssociationClass assocClass = (ObjectModelAssociationClass)clazz; + List list = assocClass.getParticipantsAttributes(); + Iterator it = list.iterator(); + String id; + while (it.hasNext()) { + ObjectModelAttribute attr = (ObjectModelAttribute)it.next(); + if (attr != null) { + output.append(getFieldClassType(attr) + ", "); + } + } + } + // on supprime la virgule en trop + if(output.length() >= 2){ + output.setLength(output.length() - 2); + } + return output.toString(); } /** * Retourne le type de l'attribut spécifié. * ATTENTION, les types primitifs sont tranformés en class de la classe - * associée, par exemple : int > Integer.class, float > Float.class + * associée, par exemple : int > Integer.class, float > Float.class * @param attr l'attribut dont on souhaite connaitre le type * @return le type de l'attribut */ public static String getFieldClassType(ObjectModelAttribute attr) { String output = ""; if (Util.isPrimitiveAttributeType(attr)) { - if (attr.getType().equals("int")) + if (attr.getType().equals("int")) { output += "Integer"; // Int > Integer - else + } else { output += Util.capitalize(attr.getType()); + } output += ".class"; } else output += attr.getType() + ".class";