Index: topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java:1.2 topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java:1.3 --- topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java:1.2 Wed Jan 4 13:21:51 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaDAO.java Fri Jan 6 12:15:20 2006 @@ -23,9 +23,9 @@ * Created: 30 déc. 2005 03:00:57 * * @author poussin - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * - * Last update: $Date: 2006/01/04 13:21:51 $ + * Last update: $Date: 2006/01/06 12:15:20 $ * by : $Author: bpoussin $ */ @@ -86,10 +86,29 @@ * les arguments vont par paire (propertyName, value) * @return un nouvel objet * @throws TopiaException si un problème est rencontré durant l'instanciation - * @throws IllegalArgumentException Si le nombre on le type des arguments n'est pas bon + * @throws IllegalArgumentException Si le nombre on le type des arguments + * n'est pas bon ou que le type ou le nom d'une propriété est fausse */ public abstract Entity create(Object ... properties) throws TopiaException; + /** + * Construit une nouvelle instance de l'objet géré par ce DAO + * @param properties la liste des propriétés que doit avoir l'objet créé + * @return un nouvel objet + * @throws TopiaException si un problème est rencontré durant l'instanciation + * @throws IllegalArgumentException Si le nombre on le type des arguments + * n'est pas bon ou que le type ou le nom d'une propriété est fausse + */ + public Entity create(Map properties) throws TopiaException; + + /** + * Permet d'ajouter ou de mettre a jour un objet. Cela permet d'ajouter + * par exemple un objet provenant d'un autre context mais du meme type + * de DAO + * @param e l'entite a ajouter ou mettre a jour + * @return l'entity passé en paramètre. + * @throws TopiaException + */ public abstract Entity update(Entity e) throws TopiaException; public abstract void delete(Entity e) throws TopiaException; Index: topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java:1.3 topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java:1.4 --- topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java:1.3 Thu Jan 5 04:50:47 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaDAOAbstract.java Fri Jan 6 12:15:20 2006 @@ -23,14 +23,15 @@ * Created: 31 déc. 2005 13:10:34 * * @author poussin - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ * - * Last update: $Date: 2006/01/05 04:50:47 $ + * Last update: $Date: 2006/01/06 12:15:20 $ * by : $Author: bpoussin $ */ package org.codelutin.topia.persistence; +import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; @@ -44,6 +45,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.codelutin.topia.TopiaException; +import org.codelutin.topia.TopiaNotFoundException; import org.codelutin.topia.event.TopiaEntityListener; import org.codelutin.topia.framework.TopiaContextImplementor; import org.codelutin.topia.persistence.hibernate.TopiaDAOHibernate; @@ -76,6 +78,53 @@ protected TopiaContextImplementor context = null; /** + * Retourne l'id de l'entity + * @param e l'entity + * @return l'id de l'entity ou null si pas trouvé + * @throws TopiaException Si une erreur survient durant la recherche + */ + protected Serializable getId(Entity e) throws TopiaException { + ClassMetadata meta = getContext().getHibernateFactory() + .getClassMetadata(entityClass); + String idPropName = meta.getIdentifierPropertyName(); + + Serializable result; + try { + result = (Serializable)PropertyUtils.getSimpleProperty(e, idPropName); + } catch (IllegalAccessException eee) { + throw new TopiaException("Impossible de récuperer l'identifiant " + + idPropName + " de l'entite: " + e); + } catch (InvocationTargetException eee) { + throw new TopiaException("Impossible de récuperer l'identifiant " + + idPropName + " de l'entite: " + e); + } catch (NoSuchMethodException eee) { + throw new TopiaException("Impossible de récuperer l'identifiant " + + idPropName + " de l'entite: " + e); + } + return result; + } + + /** + * Retourne l'id de l'entity representer comme une map + * @param map l'entity en representation map + * @return l'id de l'entity ou null si pas trouvé + * @throws TopiaException Si une erreur survient durant la recherche + */ + protected Serializable getId(Map map) throws TopiaException { + try { + ClassMetadata meta = getContext().getHibernateFactory() + .getClassMetadata(entityClass); + String idPropName = meta.getIdentifierPropertyName(); + + Serializable id = (Serializable)map.get(idPropName); + return id; + } catch (HibernateException eee) { + throw new TopiaException(eee); + } + } + + + /** * When TopiaContextImpl create the TopiaDAOHibernate, it must call this method just * after * @@ -155,22 +204,14 @@ * @see org.codelutin.topia.TopiaDAO#create(java.lang.String, java.lang.Object, java.lang.Object) */ public Entity create(Object ... properties) throws TopiaException{ - Entity result = instanciateNew(); + Map map = new HashMap(); Object propertyName = null; Object value = null; try { for(int i=0; i properties) throws TopiaException { + Serializable id = getId(properties); + getContext().fireVetoableCreate(entityClass, id); + + Entity result = instanciateNew(); + try { + for(Map.Entry e : properties.entrySet()) { + String propertyName = e.getKey(); + Object value = e.getValue(); + PropertyUtils.setProperty(result, (String)propertyName, value); + } + } catch (IllegalAccessException eee) { + throw new IllegalArgumentException("Can't put properties on new Object", eee); + } catch (InvocationTargetException eee) { + throw new IllegalArgumentException("Can't put properties on new Object", eee); + } catch (NoSuchMethodException eee) { + throw new IllegalArgumentException("Can't put properties on new Object", eee); + } + + getContext().fireOnCreated(entityClass, id, result); + + return result; + } + /* (non-Javadoc) * @see org.codelutin.topia.TopiaDAO#size() */ Index: topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java:1.2 topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java:1.3 --- topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java:1.2 Wed Jan 4 13:21:51 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaDAODelegator.java Fri Jan 6 12:15:20 2006 @@ -23,9 +23,9 @@ * Created: 30 déc. 2005 22:28:48 * * @author poussin - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * - * Last update: $Date: 2006/01/04 13:21:51 $ + * Last update: $Date: 2006/01/06 12:15:20 $ * by : $Author: bpoussin $ */ @@ -109,6 +109,13 @@ } /* (non-Javadoc) + * @see org.codelutin.topia.persistence.TopiaDAO#create(java.util.Map) + */ + public Entity create(Map properties) throws TopiaException { + return getParentDAO().create(properties); + } + + /* (non-Javadoc) * @see org.codelutin.topia.TopiaDAO#update(Entity) */ public Entity update(Entity e) throws TopiaException { Index: topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java:1.2 topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java:1.3 --- topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java:1.2 Thu Jan 5 04:50:47 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaEntity.java Fri Jan 6 12:15:20 2006 @@ -23,14 +23,15 @@ * Created: 28 déc. 2005 22:48:10 * * @author poussin - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * - * Last update: $Date: 2006/01/05 04:50:47 $ + * Last update: $Date: 2006/01/06 12:15:20 $ * by : $Author: bpoussin $ */ package org.codelutin.topia.persistence; +import java.io.Serializable; import java.util.Date; import org.codelutin.topia.TopiaContext; @@ -44,8 +45,8 @@ */ public interface TopiaEntity { - public String getTopiaId(); - public void setTopiaId(String v); + public Serializable getTopiaId(); + public void setTopiaId(Serializable v); public long getTopiaVersion(); public void setTopiaVersion(long v); Index: topia2/src/java/org/codelutin/topia/persistence/TopiaEntityAbstract.java diff -u topia2/src/java/org/codelutin/topia/persistence/TopiaEntityAbstract.java:1.2 topia2/src/java/org/codelutin/topia/persistence/TopiaEntityAbstract.java:1.3 --- topia2/src/java/org/codelutin/topia/persistence/TopiaEntityAbstract.java:1.2 Thu Jan 5 04:50:47 2006 +++ topia2/src/java/org/codelutin/topia/persistence/TopiaEntityAbstract.java Fri Jan 6 12:15:20 2006 @@ -23,13 +23,14 @@ * * @author poussin * - * @version $Revision: 1.2 $ + * @version $Revision: 1.3 $ * - * Last update: $Date: 2006/01/05 04:50:47 $ by : $Author: bpoussin $ + * Last update: $Date: 2006/01/06 12:15:20 $ by : $Author: bpoussin $ */ package org.codelutin.topia.persistence; +import java.io.Serializable; import java.util.Date; import org.codelutin.topia.TopiaContext; @@ -45,7 +46,7 @@ */ public abstract class TopiaEntityAbstract implements TopiaEntity { - protected String topiaId; + protected Serializable topiaId; protected long topiaVersion; @@ -58,7 +59,7 @@ * * @see org.codelutin.topia.persistence.TopiaEntity#getTopiaId() */ - public String getTopiaId() { + public Serializable getTopiaId() { return topiaId; } @@ -67,7 +68,7 @@ * * @see org.codelutin.topia.persistence.TopiaEntity#setTopiaId(java.lang.String) */ - public void setTopiaId(String v) { + public void setTopiaId(Serializable v) { this.topiaId = v; }