Author: tchemit Date: 2009-01-07 08:11:19 +0000 (Wed, 07 Jan 2009) New Revision: 1285 Modified: topia/trunk/topia-persistence/changelog.txt topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/GeneratorUtil.java topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/util/BeanPropertyLoador.java topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/util/Loador.java Log: - am?\195?\169lioration du g?\195?\169n?\195?\169rateur de mapping hibernate : - g?\195?\169n?\195?\169ration des clefs metier dans le mapping hibernate via la tag value naturalId - mise en constantes des tagValues utiliser dans le g?\195?\169n?\195?\169ratuer du mapping hibernate - ajout du tagValue notNull pour le mapping hibernate - changement du tagValue order-by en orderBy car sinon on r?\195?\169cup?\195?\168re order au lieu de order-by - ajout de la m?\195?\169thode obtainProgperties sur la classe Loador pour recuperer la map des propri?\195?\169t?\195?\169s ?\195?\160 binder (car pour les clef m?\195?\169tiers on doit les ajouter ?\195?\160 la cr?\195?\169ation sinon hibernate pleure...) Modified: topia/trunk/topia-persistence/changelog.txt =================================================================== --- topia/trunk/topia-persistence/changelog.txt 2009-01-05 08:40:44 UTC (rev 1284) +++ topia/trunk/topia-persistence/changelog.txt 2009-01-07 08:11:19 UTC (rev 1285) @@ -1,4 +1,11 @@ 2.1.2 ??? 200901?? +* 20090106 [chemit] - amélioration du générateur de mapping hibernate : + - génération des clefs metier dans le mapping hibernate via la tag value naturalId + - mise en constantes des tagValues utiliser dans le génératuer du mapping hibernate + - ajout du tagValue notNull pour le mapping hibernate + - changement du tagValue order-by en orderBy car sinon on récupère order au lieu de order-by + - ajout de la méthode obtainProgperties sur la classe Loador pour recuperer la map des propriétés à binder + (car pour les clef métiers on doit les ajouter à la création sinon hibernate pleure...) * 20090104 [chemit] - utilisation foreach dans les générateurs - ajout de méthode getXXXByTopiaId pour les attributs à multiplicité dans les entités (interface et abstract) - fix generic dans les méthodes générées Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java 2009-01-05 08:40:44 UTC (rev 1284) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/EntityHibernateMappingGenerator.java 2009-01-07 08:11:19 UTC (rev 1285) @@ -31,12 +31,6 @@ package org.codelutin.topia.generator; -import java.io.File; -import java.io.IOException; -import java.io.Writer; -import java.util.HashMap; -import java.util.Map; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.generator.Generator; @@ -45,13 +39,20 @@ import org.codelutin.generator.models.object.ObjectModelAssociationClass; import org.codelutin.generator.models.object.ObjectModelAttribute; import org.codelutin.generator.models.object.ObjectModelClass; - -import static org.codelutin.topia.generator.GeneratorUtil.notEmpty; +import static org.codelutin.topia.generator.GeneratorUtil.PERSISTENCE_TYPE_HIBERNATE; import static org.codelutin.topia.generator.GeneratorUtil.STEREOTYPE_ENTITY; import static org.codelutin.topia.generator.GeneratorUtil.TAG_ACCESS; -import static org.codelutin.topia.generator.GeneratorUtil.PERSISTENCE_TYPE_HIBERNATE; import static org.codelutin.topia.generator.GeneratorUtil.hasUnidirectionalRelationOnAbstractType; +import static org.codelutin.topia.generator.GeneratorUtil.notEmpty; +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * FIXME mettre les attributs node="..." sur tous les attributs * @author poussin @@ -92,7 +93,7 @@ <hibernate-mapping default-access="field" auto-import="true" package="<%=clazz.getPackageName()%>"> }*/ boolean haveSuper = clazz.getSuperclasses().size() > 0; - + List<ObjectModelAttribute> noneNaturalAttributes = new ArrayList<ObjectModelAttribute >(); if (haveSuper) { ObjectModelClass superClass = clazz.getSuperclasses().iterator().next(); String superClassname = superClass.getQualifiedName(); @@ -111,7 +112,7 @@ /*{proxy="<%=clazz.getQualifiedName()%>" }*/ /*{> <!--key column="topiaId"/--> -}*/ +}*/ } else { /*{ <class }*/ /*{name="<%=GeneratorUtil.getDOType(clazz, model)%>" }*/ @@ -126,13 +127,51 @@ /*{proxy="<%=clazz.getQualifiedName()%>" }*/ /*{> <id name="topiaId" type="string" length="255" node="@topiaId"/> - <version name="topiaVersion" type="long" node="@topiaVersion"/> +}*/ + List<String> attributes = GeneratorUtil.getNaturalId(clazz); + if (log.isDebugEnabled()) { + log.debug("natural-id for class "+clazz.getName()+" : "+attributes); + } + + if (attributes.isEmpty()) { + // pas de clef metiers sur la classe + noneNaturalAttributes.addAll(clazz.getAttributes()); + } else { + // une clef métier sur la classe a ete detectee + List<ObjectModelAttribute> naturalAttributes = new ArrayList<ObjectModelAttribute >(attributes.size()); + for (ObjectModelAttribute attr : clazz.getAttributes()) { + if (attributes.contains(attr.getName())) { + naturalAttributes.add(attr); + } else { + noneNaturalAttributes.add(attr); + } + } +/*{ <natural-id> +}*/ + generateAttributes(output, clazz, naturalAttributes," "); +/*{ </natural-id> +}*/ + } + +/*{ <version name="topiaVersion" type="long" node="@topiaVersion"/> <property name="topiaCreateDate" type="date" node="@topiaCreateDate"/> }*/ } + generateAttributes(output, clazz, noneNaturalAttributes,""); - for (Object o : clazz.getAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; + if (haveSuper) { +/*{ </union-subclass> +}*/ + } else { +/*{ </class> +}*/ + } +/*{</hibernate-mapping> +}*/ + } + + protected void generateAttributes(Writer output, ObjectModelClass clazz, List<ObjectModelAttribute> attributes,String prefix) throws IOException { + for (ObjectModelAttribute attr : attributes) { ObjectModelAttribute reverse = attr.getReverseAttribute(); // pour les asso quoi qu'il arrive il faut les lier des 2 cotes @@ -144,22 +183,22 @@ if (!Util.isNMultiplicity(attr)) { if (attr.getClassifier() != null && attr.getClassifier().hasStereotype(STEREOTYPE_ENTITY)) { if (Util.isNMultiplicity(attr.getReverseMaxMultiplicity()) && !attr.hasAssociationClass()) { - generateHibernateManyToOne(output, attr); + generateHibernateManyToOne(output, attr, prefix); } else { - generateHibernateOneToOne(output, attr); + generateHibernateOneToOne(output, attr, prefix); } } else { - generateHibernateProperty(output, attr); + generateHibernateProperty(output, attr, prefix); } } else { if (attr.getClassifier() != null && attr.getClassifier().hasStereotype(STEREOTYPE_ENTITY)) { if (Util.isNMultiplicity(attr.getReverseMaxMultiplicity()) && !attr.hasAssociationClass()) { - generateHibernateManyToMany(output, attr); + generateHibernateManyToMany(output, attr, prefix); } else { - generateHibernateOneToMany(output, attr); + generateHibernateOneToMany(output, attr, prefix); } } else { - generateHibernateMany(output, attr); + generateHibernateMany(output, attr, prefix); } } } @@ -168,18 +207,20 @@ //Attributs pour les classes d'association if (clazz instanceof ObjectModelAssociationClass) { ObjectModelAssociationClass assoc = (ObjectModelAssociationClass)clazz; - for (Object o : assoc.getParticipantsAttributes()) { - ObjectModelAttribute attr = (ObjectModelAttribute) o; + for (ObjectModelAttribute attr : assoc.getParticipantsAttributes()) { if (attr != null) { // Note(poussin) pour moi quoi qu'il arrive sur la classe d'association il faut -// un many-to-one, sinon on a des problemes. +// un many-to-one, sinon on a des problemes. // if ((!attr.getReverseAttribute().isNavigable()) || !Util.isNMultiplicity(attr.getReverseAttribute())) { // / *{ <one-to-one name="<%=getName(attr, true)%>" class="<%=getType(attr, true)%>"<%=(GeneratorUtil.notEmpty(attr.getTagValue(GeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(GeneratorUtil.TAG_LENGTH) + "\""):"")%><%=(attr.isComposite()?" cascade=\"delete\"":"")%>/> // } */ // } else { - -/*{ <many-to-one name="<%=getName(attr, true)%>" class="<%=getType(attr, true)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="<%=getName(attr, true)%>/@topiaId" embed-xml="false"/> + String notNull = attr.getTagValue(GeneratorUtil.TAG_NOT_NULL); + if (notNull!=null) { + notNull = "not-null=\""+notNull.trim()+"\""; + } +/*{<%=prefix%> <many-to-one name="<%=getName(attr, true)%>" class="<%=getType(attr, true)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="<%=getName(attr, true)%>/@topiaId" embed-xml="false" <%=notNull%> /> }*/ // } //Ne sert plus grâce à l'utilisation de la navigabilité @@ -199,17 +240,7 @@ } } - if (haveSuper) { -/*{ </union-subclass> -}*/ - } else { -/*{ </class> -}*/ - } -/*{</hibernate-mapping> -}*/ } - protected String getName(ObjectModelAttribute attr) { return getName(attr, false); } @@ -251,11 +282,11 @@ return GeneratorUtil.getDOType(type, model); } - protected void generateHibernateProperty(Writer output, ObjectModelAttribute attr) throws IOException { + protected void generateHibernateProperty(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { String type = getType(attr); if (type.trim().endsWith("[]")) { type = type.trim().substring(0, type.trim().length()-2); -/*{ <primitive-array name="<%=getName(attr)%>" table="<%=GeneratorUtil.getDBName(attr.getDeclaringElement()) + "_" + getName(attr)%>" }*/ +/*{<%=prefix%> <primitive-array name="<%=getName(attr)%>" table="<%=GeneratorUtil.getDBName(attr.getDeclaringElement()) + "_" + getName(attr)%>" }*/ String accessField = attr.getTagValue(TAG_ACCESS); if (notEmpty(accessField)) { /*{access="<%=accessField%>" }*/ @@ -270,13 +301,13 @@ /*{index="<%=GeneratorUtil.getDBName(attr.getDeclaringElement()) + "_" + getName(attr)%>_idx" }*/ } /*{> - <key column="<%=GeneratorUtil.getDBName(attr.getDeclaringElement())%>"/> - <list-index column="<%=getName(attr)%>_idx"/> - <element type="<%=type%>"/> - </primitive-array> +<%=prefix%> <key column="<%=GeneratorUtil.getDBName(attr.getDeclaringElement())%>"/> +<%=prefix%> <list-index column="<%=getName(attr)%>_idx"/> +<%=prefix%> <element type="<%=type%>"/> +<%=prefix%> </primitive-array> }*/ } else { -/*{ <property name="<%=getName(attr)%>" type="<%=type%>" }*/ +/*{<%=prefix%> <property name="<%=getName(attr)%>" type="<%=type%>" }*/ String accessField = attr.getTagValue(TAG_ACCESS); if (notEmpty(accessField)) { /*{access="<%=accessField%>" }*/ @@ -286,6 +317,10 @@ if (attr.isIndexed()) { /*{index="<%=GeneratorUtil.getDBName(attr.getDeclaringElement()) + "_" + getName(attr)%>_idx" }*/ } + String notNull = attr.getTagValue(GeneratorUtil.TAG_NOT_NULL); + if (notNull != null) { +/*{ not-null="<%=notNull.trim()%>" }*/ + } String[] columnNames = this.columnNamesMap.get(type); if (columnNames == null || columnNames.length == 0) { /*{column="<%=GeneratorUtil.getDBName(attr)%>"<%=(GeneratorUtil.notEmpty(attr.getTagValue(GeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(GeneratorUtil.TAG_LENGTH) + "\""):"")%> node="<%=getName(attr)%>"/> @@ -295,27 +330,27 @@ }*/ for (String columnName : columnNames) { columnName = columnName.trim(); -/*{ <column name="<%=getName(attr) + "_" + columnName%>"/> +/*{<%=prefix%> <column name="<%=getName(attr) + "_" + columnName%>"/> }*/ } -/*{ </property> +/*{<%=prefix%> </property> }*/ } } } - protected void generateHibernateOneToOne(Writer output, ObjectModelAttribute attr) throws IOException { - generateHibernateManyToOne(output, attr, true); + protected void generateHibernateOneToOne(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { + generateHibernateManyToOne(output, attr, true, prefix); // boolean accessField = hasUnidirectionalRelationOnAbstractType(attr.getReverseAttribute(), model); /// *{ <one-to-one name="<%=getName(attr)%>" class="<%=getType(attr)%>"<%=(GeneratorUtil.notEmpty(attr.getTagValue(GeneratorUtil.TAG_LENGTH))?(" length=\"" + attr.getTagValue(GeneratorUtil.TAG_LENGTH) + "\""):"")%><%=((attr.isComposite() || attr.hasAssociationClass())?" cascade=\"delete\"":"")%><%=((accessField)?" access=\"field\"":"")%> node="<%=getName(attr)%>/@topiaId" embed-xml="false"/> //} */ } - protected void generateHibernateOneToMany(Writer output, ObjectModelAttribute attr) throws IOException { + protected void generateHibernateOneToMany(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { boolean needsIndex = attr.isIndexed(); boolean isInverse = attr.getReverseAttribute().isNavigable(); isInverse |= hasUnidirectionalRelationOnAbstractType(attr, model); - String orderBy = attr.getTagValue("order-by"); + String orderBy = attr.getTagValue(GeneratorUtil.TAG_ORDER_BY); if (orderBy == null) { orderBy = ""; } else { @@ -328,8 +363,8 @@ } String lazy = "lazy=\""; - if (attr.getTagValue("lazy") != null){ - lazy += attr.getTagValue("lazy"); + if (attr.getTagValue(GeneratorUtil.TAG_LAZY) != null){ + lazy += attr.getTagValue(GeneratorUtil.TAG_LAZY); } else { lazy += "true"; @@ -337,53 +372,53 @@ lazy += "\""; String fetch = ""; - if (attr.getTagValue("fetch") != null){ - fetch = "fetch=\"" + attr.getTagValue("fetch") + "\""; + if (attr.getTagValue(GeneratorUtil.TAG_FETCH) != null){ + fetch = "fetch=\"" + attr.getTagValue(GeneratorUtil.TAG_FETCH) + "\""; } - + String collType = GeneratorUtil.getNMultiplicityHibernateType(attr); if (!needsIndex) { -/*{ <<%=collType%> name="<%=getName(attr)%>" <%=orderBy%> <%=((!isInverse)?"":"inverse=\"true\"")%> <%=fetch%> <%=lazy%> <%=cascade%> node="<%=getName(attr)%>" embed-xml="true"> - <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> - <one-to-many class="<%=getType(attr)%>" node="topiaId" embed-xml="false"/> - </<%=collType%>> +/*{<%=prefix%> <<%=collType%> name="<%=getName(attr)%>" <%=orderBy%> <%=((!isInverse)?"":"inverse=\"true\"")%> <%=fetch%> <%=lazy%> <%=cascade%> node="<%=getName(attr)%>" embed-xml="true"> +<%=prefix%> <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> +<%=prefix%> <one-to-many class="<%=getType(attr)%>" node="topiaId" embed-xml="false"/> +<%=prefix%> </<%=collType%>> }*/ }else { -/*{ <<%=collType%> name="<%=getName(attr)%>"<%=((!isInverse)?"":" inverse=\"true\"")%> <%=lazy%> <%=cascade%> node="<%=getName(attr)%>" embed-xml="false"> - <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> - <list-index column="<%=GeneratorUtil.getReverseDBName(attr)%>_idx"/> - <one-to-many class="<%=getType(attr)%>" node="topiaId" embed-xml="false"/> - </<%=collType%>> +/*{<%=prefix%> <<%=collType%> name="<%=getName(attr)%>"<%=((!isInverse)?"":" inverse=\"true\"")%> <%=lazy%> <%=cascade%> node="<%=getName(attr)%>" embed-xml="false"> +<%=prefix%> <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> +<%=prefix%> <list-index column="<%=GeneratorUtil.getReverseDBName(attr)%>_idx"/> +<%=prefix%> <one-to-many class="<%=getType(attr)%>" node="topiaId" embed-xml="false"/> +<%=prefix%> </<%=collType%>> }*/ } } - protected void generateHibernateMany(Writer output, ObjectModelAttribute attr) throws IOException { + protected void generateHibernateMany(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { boolean needsIndex = attr.isIndexed(); String collType = GeneratorUtil.getNMultiplicityHibernateType(attr); String lazy = ""; - if (attr.getTagValue("lazy") != null) { - lazy = "lazy=\"" + attr.getTagValue("lazy") + "\""; + if (attr.getTagValue(GeneratorUtil.TAG_LAZY) != null) { + lazy = "lazy=\"" + attr.getTagValue(GeneratorUtil.TAG_LAZY) + "\""; } - -/*{ <<%=collType%> name="<%=getName(attr)%>" node="<%=getName(attr)%>" embed-xml="true" <%=lazy%>> - <key column="OWNER"/> + +/*{<%=prefix%> <<%=collType%> name="<%=getName(attr)%>" node="<%=getName(attr)%>" embed-xml="true" <%=lazy%>> +<%=prefix%> <key column="OWNER"/> }*/ if (needsIndex) { -/*{ <list-index/> +/*{<%=prefix%> <list-index/> }*/ } -/*{ <element type="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="id"/> - </<%=collType%>> +/*{<%=prefix%> <element type="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="id"/> +<%=prefix%> </<%=collType%>> }*/ } - protected void generateHibernateManyToOne(Writer output, ObjectModelAttribute attr) throws IOException { - generateHibernateManyToOne(output, attr, false); + protected void generateHibernateManyToOne(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { + generateHibernateManyToOne(output, attr, false, prefix); } - protected void generateHibernateManyToOne(Writer output, ObjectModelAttribute attr, boolean isUnique) throws IOException { -/*{ <many-to-one name="<%=getName(attr)%>" class="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>"}*/ + protected void generateHibernateManyToOne(Writer output, ObjectModelAttribute attr, boolean isUnique, String prefix) throws IOException { +/*{<%=prefix%> <many-to-one name="<%=getName(attr)%>" class="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>"}*/ if (attr.isComposite() || attr.hasAssociationClass()) { /*{ cascade="delete"}*/ } @@ -397,16 +432,20 @@ /*{ unique="true"}*/ } /*{ node="<%=getName(attr)%>/@topiaId" embed-xml="false"}*/ - + // vérifier si le tag lazy est defini par defaut dans le fichier de proprietes if (attr.getTagValue("lazy") != null){ -/*{ lazy="<%=attr.getTagValue("lazy")%>"}*/ +/*{ lazy="<%=attr.getTagValue("lazy")%>"}*/ } + String notNull = attr.getTagValue(GeneratorUtil.TAG_NOT_NULL); + if (notNull != null) { +/*{ not-null="<%=notNull.trim()%>"}*/ + } /*{/> }*/ } - protected void generateHibernateManyToMany(Writer output, ObjectModelAttribute attr) throws IOException { + protected void generateHibernateManyToMany(Writer output, ObjectModelAttribute attr, String prefix) throws IOException { // On ne met le inverse="true" uniquement pour un seul coté de la relation. // Dans le cas contraire, les modifications dans la relation ne seront // pas sauvegardées. Ceci n'est vrai que si les deux coté sont navigable @@ -414,7 +453,7 @@ //isInverse |= !Util.isFirstAttribute(attr); //isInverse = false; // 20070117 poussin: pour du many, jamais de inverse isInverse &= Util.isFirstAttribute(attr); - + boolean needsIndex = attr.isIndexed(); String cascade = ""; if (attr.isComposite() || attr.hasAssociationClass()) { @@ -432,15 +471,15 @@ lazy += "\""; String collType = GeneratorUtil.getNMultiplicityHibernateType(attr); -/*{ <<%=collType%> name="<%=getName(attr)%>" table="<%=GeneratorUtil.getManyToManyTableName(attr)%>" <%=(isInverse?"inverse=\"true\" ":"")%> <%=lazy%> <%=cascade%> node="<%=getName(attr)%>" embed-xml="true"> - <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> +/*{<%=prefix%> <<%=collType%> name="<%=getName(attr)%>" table="<%=GeneratorUtil.getManyToManyTableName(attr)%>" <%=(isInverse?"inverse=\"true\" ":"")%> <%=lazy%> <%=cascade%> node="<%=getName(attr)%>" embed-xml="true"> +<%=prefix%> <key column="<%=GeneratorUtil.getReverseDBName(attr)%>"/> }*/ if (needsIndex) { -/*{ <list-index column="<%=GeneratorUtil.getReverseDBName(attr)%>_idx"/> +/*{<%=prefix%> <list-index column="<%=GeneratorUtil.getReverseDBName(attr)%>_idx"/> }*/ } -/*{ <many-to-many class="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="topiaId"/> - </<%=collType%>> +/*{<%=prefix%> <many-to-many class="<%=getType(attr)%>" column="<%=GeneratorUtil.getDBName(attr)%>" node="topiaId"/> +<%=prefix%> </<%=collType%>> }*/ } Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/GeneratorUtil.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/GeneratorUtil.java 2009-01-05 08:40:44 UTC (rev 1284) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/generator/GeneratorUtil.java 2009-01-07 08:11:19 UTC (rev 1285) @@ -102,6 +102,21 @@ /** Tag pour specfier si on doit générer i18n */ public static final String TAG_I18N_PREFIX = "i18n"; + /** Tag pour ajouter un attribut dans une clef métier */ + public static final String TAG_NATURAL_ID = "naturalId"; + + /** Tag pour spécifier la caractèrelazy d'une association multiple */ + public static final String TAG_LAZY = "lazy"; + + /** Tag pour spécifier la caractère fetch d'une association multiple */ + public static final String TAG_FETCH = "fetch"; + + /** Tag pour spécifier la caractère order-by d'une association multiple */ + public static final String TAG_ORDER_BY = "orderBy"; + + /** Tag pour spécifier la caractère not-null d'un attribut */ + public static final String TAG_NOT_NULL = "notNull"; + /** Type de persistence Hibernate */ public static final String PERSISTENCE_TYPE_HIBERNATE = "hibernate"; @@ -134,7 +149,7 @@ /** * Indique si l'élément spécifié dispose de documentation * - * @param element l'élément à tester + * @param element l'élément à tester * @return true s'il y a documentation, false sinon */ public static boolean hasDocumentation(ObjectModelElement element) { @@ -159,7 +174,7 @@ * @return l'interface trouvée ou null sinon */ public static ObjectModelInterface getDAOInterface(ObjectModelClass clazz, - ObjectModel model) { + ObjectModel model) { for (Object o : model.getInterfaces()) { ObjectModelInterface daoInterface = (ObjectModelInterface) o; if (daoInterface.getName().equals(clazz.getName() + "DAO")) { @@ -215,11 +230,11 @@ * Cherche et renvoie le schema a utiliser sur cet element, sinon sur le model. * * @param element l'élément à tester - * @param model le modele utilisé + * @param model le modele utilisé * @return le nom du schema ou null */ public static String getSchemaName(ObjectModelElement element, - ObjectModel model) { + ObjectModel model) { return findTagValue(TAG_SCHEMA_NAME, element, model); } @@ -227,15 +242,34 @@ * Cherche et renvoie le prefixe i18n à utiliser sur cet element, sinon sur le model. * * @param element l'élément à tester - * @param model le modele utilisé + * @param model le modele utilisé * @return le prefix i18n ou <code>null</code> si non spécifié */ public static String getI18nPrefix(ObjectModelElement element, - ObjectModel model) { + ObjectModel model) { return findTagValue(TAG_I18N_PREFIX, element, model); } /** + * Cherche et renvoie la liste des attributs constituant la clef metier d'une classe. + * + * @param clazz la classe à tester + * @return la liste des attributs de la clef métier ou null si pas de clef métier. + */ + public static List<String> getNaturalId(ObjectModelClass clazz) { + + String value = clazz.getTagValue(TAG_NATURAL_ID); + if (value == null || value.trim().isEmpty()) { + return java.util.Collections.emptyList(); + } + List<String> result = new ArrayList<String>(); + for (String attribute : value.split(",")) { + result.add(attribute.trim()); + } + return result; + } + + /** * Cherches et renvoie le copyright a utiliser sur le model. * * @param model le modele utilisé @@ -251,11 +285,11 @@ * * @param tagName le nom du tag * @param element l'élément à tester - * @param model le modele utilisé + * @param model le modele utilisé * @return la valeur du tagValue ou null */ public static String findTagValue(String tagName, - ObjectModelElement element, Model model) { + ObjectModelElement element, Model model) { if (element == null) { if (model != null) { if (notEmpty(model.getTagValue(tagName))) { @@ -283,7 +317,7 @@ } public static boolean hasStereotypes(ObjectModelElement element, - String... stereotypes) { + String... stereotypes) { for (String stereotype : stereotypes) { if (!element.hasStereotype(stereotype)) { return false; @@ -316,7 +350,7 @@ public static boolean isAssociationClassDoublon(ObjectModelAttribute attr) { return (attr.getReverseAttribute() != null) && (attr.getDeclaringElement().equals(attr - .getReverseAttribute().getDeclaringElement())) + .getReverseAttribute().getDeclaringElement())) && (!Util.isFirstAttribute(attr)); } @@ -566,7 +600,7 @@ * @return the list of filtred classes by their stereotype */ public static List<ObjectModelClass> getEntityClasses(ObjectModel model, - boolean sort) { + boolean sort) { return getClassesByStereotype(STEREOTYPE_ENTITY, model, sort); } @@ -590,7 +624,7 @@ java.util.Collections.sort(classes, new java.util.Comparator<ObjectModelClass>() { public int compare(ObjectModelClass o1, - ObjectModelClass o2) { + ObjectModelClass o2) { return o1.getQualifiedName().compareTo( o2.getQualifiedName()); } Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/util/BeanPropertyLoador.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/util/BeanPropertyLoador.java 2009-01-05 08:40:44 UTC (rev 1284) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/util/BeanPropertyLoador.java 2009-01-07 08:11:19 UTC (rev 1285) @@ -7,7 +7,9 @@ import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** @author chemit */ public class BeanPropertyLoador<E> implements Loador<E> { @@ -54,6 +56,28 @@ return descriptors; } + public Map<String, Object> obtainProgperties(E from) { + if (from == null) { + return java.util.Collections.emptyMap(); + } + Map<String, Object> result = new HashMap<String, Object>(); + for (PropertyDescriptor descriptor : getDescriptors()) { + Object read = null; + try { + read = descriptor.getReadMethod().invoke(from); + if (read != null) { + result.put(descriptor.getName(), read); + } + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } + + } + return result; + } + @Override public void load(E from, E dst, boolean tech) { if (from == null) { Modified: topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/util/Loador.java =================================================================== --- topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/util/Loador.java 2009-01-05 08:40:44 UTC (rev 1284) +++ topia/trunk/topia-persistence/src/main/java/org/codelutin/topia/persistence/util/Loador.java 2009-01-07 08:11:19 UTC (rev 1285) @@ -1,7 +1,11 @@ package org.codelutin.topia.persistence.util; +import java.util.Map; + /** @author chemit */ public interface Loador<E> extends java.io.Serializable { + Map<String, Object> obtainProgperties(E from); + void load(E from, E dst, boolean tech); }