Index: topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java diff -u topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java:1.8 topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java:1.9 --- topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java:1.8 Tue Jun 28 14:08:34 2005 +++ topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java Tue Jul 19 13:15:13 2005 @@ -23,9 +23,9 @@ * Created: Aug 1, 2004 * * @author Cédric Pineau - * @version $Revision: 1.8 $ + * @version $Revision: 1.9 $ * - * Last update : $Date: 2005/06/28 14:08:34 $ + * Last update : $Date: 2005/07/19 13:15:13 $ * by : $Author: bpoussin $ */ @@ -43,49 +43,40 @@ */ public interface PersistenceHelper { - public TopiaEntity create(Class entityClass) throws TopiaException; + public TopiaContext getContext(); - public TopiaEntity makePersistent(TopiaEntity entity) - throws TopiaException; + /** + * Permet de creer une nouvelle entité + */ + public A create(Class entityClass) throws TopiaException; + + /** + * Permet de preparer une entité qui viendrait d'un autre PersistenceHelper + * à etre sauver dans ce PersistenceHelper. Il n'est pas nécessaire d'appeler + * cette méthode après un create. + * Après l'appel de cette méthode, il ne faut plus utilisé l'entité passé + * en paramètre, mais celle retournée. + */ + public A makePersistent(A entity) throws TopiaException; - public TopiaEntity update(TopiaEntity entity) - throws TopiaException; + /** + * Permet d'indiquer de prendre en compte les modifications faites sur + * l'entité passée en paramètre. L'entity doit provenir du create de ce + * PersistenceHelper ou alors doit-être passé par la méthode makePersistent + * Après l'appel de cette méthode, il ne faut plus utilisé l'entité passé + * en paramètre, mais celle retournée. + */ + public A update(A entity) throws TopiaException; public void delete(TopiaEntity entity) throws TopiaException; /** * Permet de récupéré une liste d'objet par rapport a une requete. * - * @param query - * la requete a executer - * @return une List d'objets - */ - public List find(TopiaQuery query) throws TopiaException; - - /** - * Permet de récupéré de manière asynchrone une liste d'objet par rapport a une requete. * @param query la requete a executer - * @return a JDOAsynchronousLoader - */ - public AsynchronousLoader findAsynchronously(TopiaQuery query) throws TopiaException; - - /** - * Get a list of objects within a range for the given query. - * @param query request to execute - * @param startIndex index of the first element to return - * @param endIndex index of the last element to return - * @return a list og Objects - */ - public List findInRange(TopiaQuery query, int startIndex, int endIndex) throws TopiaException; - - /** - * Get an asynchronous loader of objects within a range for the given query. - * @param query request to execute - * @param startIndex index of the first element to return - * @param endIndex index of the last element to return - * @return a JDOAsynchronousLoader + * @return une List d'objets */ - public AsynchronousLoader findInRangeAsynchronously(TopiaQuery query, int startIndex, int endIndex) throws TopiaException; + public List find(TopiaQuery query) throws TopiaException; public int size(TopiaQuery query) throws TopiaException; @@ -105,8 +96,6 @@ * Recherche un objet par son identifiant */ public TopiaEntity findByTopiaId(String id) throws TopiaException; - - public void destroy(); /** * Ouvre une transaction pour le thread courant Index: topia/src/java/org/codelutin/topia/persistence/PersistenceStorage.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/PersistenceStorage.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/PersistenceStorage.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,117 @@ +/* *##% + * Copyright (C) 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. + *##%*/ + +/* * + * PersistenceStorage.java + * + * Created: 16 juillet 2005 23:40:21 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.Properties; +import org.codelutin.topia.TopiaQuery; + +/** +* L'implantation du storage doit prendre en compte qu'il n'y aura qu'une +* instance pour un même ensemble de propriété. Cela veut dire que l'instance +* peut-etre solicitée par plusieurs thread en même temps. +*

+* L'implantation doit avoir un constructeur prenant en paramètre un objet +* java.util.Properties +*/ +public interface PersistenceStorage { // PersistenceStorage + + /** + * Demande la sauvegarde de l'entité dans le context de la transaction + */ + public void store(TopiaTransaction tt, TopiaPersistenceObject tpo); + + /** + * Demande la restauration de l'entité dans le context de la transaction + */ + public void restore(TopiaTransaction tt, TopiaPersistenceObject tpo); + + /** + * Retourne tous les id de tous les objets encore existant dans le + * context de la transaction + */ + public Collection getAllId(TopiaTransaction tt); + + /** + * Permet d'indiquer au storage le debut d'une transaction + */ + public void beginTransaction(TopiaTransaction tt); + + /** + * Indique au storage de commiter la transaction passé en paramètre. + * @return la nouvelle transaction utilisable pour de futurs appels au storage + * car l'ancienne transaction n'est plus valide + */ + public TopiaTransaction commitTransaction(TopiaTransaction tt); + + /** + * Indique au storage de faire un rollback de la transaction passé en paramètre. + * @return la nouvelle transaction utilisable pour de futurs appels au storage + * car l'ancienne transaction n'est plus valide + */ + public TopiaTransaction rollbackTransaction(TopiaTransaction tt); + + /** + * Indique si le storage implante une methode de recherche rapide + * @return vrai si la methode find est implanté + */ + public boolean haveFindImplemented(); + + /** + * Permet de faire une recherche dans le storage dans le context de la + * transaction passée en argument. Si cette méthode n'est pas implantée + * il faut que haveFindImplemented return false, et que cette méthode + * leve l'exception UnsupportedOperationException + */ + public List find(TopiaTransaction tt, TopiaQuery query); + + /** + * Retourne l'historique de l'objet ayant l'id passé en paramètre + */ + public List getHistory(TopiaTransaction tt, String id); + + /** + * Nettoie l'historique pour ne conserver qu'une profondeur d'historique + * egal à depth. Doit prendre en compte le fait que toutes les transactions + * en cours doit avoir toujours au moins la vision convenable de toutes + * les entités. + */ + public void cleanHistory(int depth); + + /** + * Permet de savoir si un id existe encore dans le context de la transaction + */ + public boolean exists(TopiaTransaction tt, String id); + +} // PersistenceStorage + Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,63 @@ +/* *##% + * Copyright (C) 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. + *##%*/ + +/* * + * TopiaPersistenceCache.java + * + * Created: 16 juillet 2005 23:42:14 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.Properties; +import org.codelutin.topia.TopiaEntity; + +public class TopiaPersistenceCache { // TopiaPersistenceCache + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.persistence.TopiaPersistenceCache"); + + protected HashMap cache = new HashMap(); + + public TopiaPersistenceCache(Properties config){ + + } + + /** + * @return null si l'objet n'est pas dans le cache + */ + public TopiaEntity get(String id){ + return cache.get(id); + } + + public void put(String id, TopiaEntity e){ + cache.put(id, e); + } + +} // TopiaPersistenceCache + Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,157 @@ +/* *##% + * Copyright (C) 2002, 2003, 2004 + * 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. + *##%*/ + +/* * + * TopiaPersistenceHelper.java + * + * Created: Jul 16, 2005 + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update : $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.lang.IllegalStateException; +import java.util.Properties; +import org.codelutin.topia.TopiaConst; +import org.codelutin.topia.TopiaContext; +import org.codelutin.topia.TopiaEntity; +import org.codelutin.topia.TopiaException; + +public class TopiaPersistenceHelper implements PersistenceHelper { // TopiaPersistenceHelper + + /** + * Le context a partir duquel on a recupere ce AbstractPersistenceHelper + */ + protected TopiaContext context = null; + protected TopiaPersistenceCache = null; + protected PersistenceStorage storage = null; + + /** + * Les proprietes pour ce AbstractPersistenceHelper + */ + protected Properties properties = null; + + public TopiaContext getContext(){ + return context; + } + + public Properties getProperties(){ + return properties; + } + + public TopiaPersistenceHelper(TopiaContext context, Properties properties) { + if(context.getTransaction() == null){ + throw new IllegalStateException("Context must be in transaction to used TopiaPersistenceHelper"); + } + this.context = context; + this.properties = properties; + } + + protected PersistenceStorage getStorage(){ + if(storage == null){ + storage = getContext().getContextHelper().getHelper(getProperties(), + TopiaConst.PERSISTENCE_STORAGE_CLASS, true); + } + return storage; + } + + protected TopiaPersistenceCache getCache(){ + if(cache == null){ + cache = getContext().getContextHelper().getHelper(getProperties(), + TopiaConst.PERSISTENCE_CACHE_CLASS, false); + } + return cache; + } + + public void beginTransaction() throws TopiaException { + getStorage().beginTransaction(getContext().getTransaction()); + } + + public void commitTransaction() throws TopiaException { + getStorage().commitTransaction(getContext().getTransaction()); + } + + public void rollbackTransaction() throws TopiaException { + getStorage().rollbackTransaction(getContext().getTransaction()); + } + + public A create(Class entityClass) throws TopiaException { + // FIXME creer un nouveau proxy + } + + public A makePersistent(A entity) throws TopiaException { + // TODO créer un nouveau proxy a partir des informations de l'entité + // passée en parametre et retourner le proxy. + return entity; + } + + public A update(A entity) throws TopiaException{ + // FIXME recuperer l'objet encapsulé dans le proxy et faire un store + // getStorage().store(getContext().getTransaction(), entity); + return entity; + } + + public void delete(TopiaEntity entity) throws TopiaException { + // FIXME, recuperer l'objet encapsulé dans le proxy et le marquer + // supprimé. + // appeler le store + } + + public List find(TopiaQuery query) throws TopiaException { + List result = null; + if(getStorage().haveFindImplemented()){ + result = new TopiaPersistenceLazyList(this, getStorage().find(query)); + } else { + // TODO faire la recherche + // getStorage().getAllId(); + } + return result; + } + + public int size(TopiaQuery query) throws TopiaException { + List result = null; + if(getStorage().haveFindImplemented()){ + result = getStorage().find(query)); + } else { + result = find(query); + } + return result.size(); + } + + public void importXML(String xml) throws TopiaException { + // TODO + throw new UnsupportedOperationException(); + } + + public String exportXML() throws TopiaException { + // TODO + throw new UnsupportedOperationException(); + } + + public TopiaEntity findByTopiaId(String id) throws TopiaException { + // FIXME + } + +} // TopiaPersistenceHelper + Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObject.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObject.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObject.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,91 @@ +/* *##% + * Copyright (C) 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. + *##%*/ + +/* * + * TopiaPersistenceObject.java + * + * Created: 16 juillet 2005 23:44:18 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.io.Serializable; +import java.util.HashSet; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.Set; + +public class TopiaPersistenceObject implements Serializable { // TopiaPersistenceObject + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.persistence.TopiaPersistenceObject"); + + protected TopiaPersistenceObjectData data = null; + protected TopiaPersistenceObjectManagementData management = null; + protected Set modifiedFields = new HashSet; + + public TopiaPersistenceObject(){} + + public TopiaPersistenceObjectData getData(){ + return data; + } + + public TopiaPersistenceObjectManagementData getManagement(){ + return management; + } + + public Set getModifiedFields(){ + return modifiedFields; + } + + /** + * 2 objet sont egaux si leur id sont egaux et qu'ils ont la meme date + */ + public boolean equals(Object o){ + boolean result = false; + if(o instanceof TopiaPersistenceObject){ + TopiaPersistenceObjectManagementData md1 = getManagement(); + TopiaPersistenceObjectManagementData md2 = + ((TopiaPersistenceObject)o).getManagement(); + result = md1.getId().equals(md2.getId()) + && md1.getDate() == md2.getDate(); + } + return result; + } + + public String getId(){ + return getManagement().getId(); + } + + public Object getField(String fieldName){ + return getData().getField(fieldName); + } + + public void setField(String fieldName, Object value){ + getData().setField(fieldName, value); + } + +} // TopiaPersistenceObject + Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectData.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectData.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectData.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,46 @@ +/* *##% + * Copyright (C) 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. + *##%*/ + +/* * + * TopiaPersistenceObjectData.java + * + * Created: 16 juillet 2005 23:45:07 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.util.HashMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class TopiaPersistenceObjectData extends HashMap { // TopiaPersistenceObjectData + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.persistence.TopiaPersistenceObjectData"); + + public TopiaPersistenceObjectData(){} + +} // TopiaPersistenceObjectData + Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectManagementData.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectManagementData.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectManagementData.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,60 @@ +/* *##% + * Copyright (C) 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. + *##%*/ + +/* * + * TopiaPersistenceObjectManagementData.java + * + * Created: 16 juillet 2005 23:46:51 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.io.Serializable; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class TopiaPersistenceObjectManagementData implements Serializable { // TopiaPersistenceObjectManagementData + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.persistence.TopiaPersistenceObjectManagementData"); + + protected String id = null; + + public TopiaPersistenceObjectManagementData(String id){ + this.id = id; + } + + public String getId(){ + return id; + } + public boolean isNew(){} + public boolean isDeleted(){} + public boolean isStored(){} + public boolean Unloaded(){} + public long getDate(){} + public long getSchemaVersion(){} + +} // TopiaPersistenceObjectManagementData + Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,183 @@ +/* *##% +* Copyright (C) 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. +*##%*/ + +/* * +* TopiaPersistenceProxy.java +* +* Created: 16 juillet 2005 23:52:35 CEST +* +* @author Benjamin POUSSIN +* @version $Revision: 1.1 $ +* +* Last update: $Date: 2005/07/19 13:15:13 $ +* by : $Author: bpoussin $ +*/ + +package org.codelutin.topia.persistence; + +import java.io.Serializable; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.apache.commons.beanutils.MethodUtils; +import org.apache.commons.lang.ObjectUtils; +import org.codelutin.topia.TopiaEntityOperation; +import org.codelutin.util.ArrayUtil; + +public class TopiaPersistenceProxy implements InvocationHandler { // TopiaPersistenceProxy + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.persistence.TopiaPersistenceProxy"); + + static public TopiaEntity newProxy(PersistenceHelper ph, TopiaPersistenceObject o) throws TopiaException { + Class clazz = TopiaId.getClassName(o.getId()); + TopiaEntity result = (TopiaEntity) Proxy.newProxyInstance(TopiaPersistenceProxy.class.getClassLoader(), + new Class[] { clazz }, new TopiaPersistenceProxy(ph, o)); + return result; + } + + protected PersistenceHelper ph = null; + protected Class interfacez = null; + protected TopiaPersistenceObject o = null; + + protected TopiaPersistenceProxy(PersistenceHelper tph, Class interfacez, TopiaPersistenceObject o){ + this.tph = tph; + this.interfacez = interfacez; + this.o = o; + } + + public TopiaPersistenceObject getObject(){ + return o; + } + + public Object invoke(Object proxy, Method method, Object[] args) + throws Throwable { + String methodName = method.getName(); + TopiaEntityOperation op = ph.getContext().getEntityOperation(interfacez); + + Class [] argsType = ArrayUtil.concat([interfacez], + method.getParameterTypes()); + Method m = MethodUtils.getAccessibleMethod(op.getClass(), methodName, + argsType); + if(m != null){ + // on a retrouvé la méthode sur les operations, on peut donc l'appeler + Object [] argsObject = ArrayUtil.concat([o], args); + return m.invoke(op, argsObject); + } else { + if(methodName.equals("toString") + && method.getParameterTypes().length == 0){ + EntitiesHelper entitiesHelper = ph.getContext().getEntitiesHelper(); + Class entitiesHelperClass = entitiesHelper.getClass(); + Method m = MethodUtils.getAccessibleMethod(entitiesHelperClass, "toString", interfacez); + if(m != null){ + // on essai d'invoker la methode exact si possible + return m.invoke(entitiesHelper, this); + } else { + return MethodUtils.invokeMethod( + ph.getContext().getEntitiesHelper(), "toString", this); + } + }else if(methodName.equals("hashCode") + && method.getParameterTypes().length == 0){ + return getObject().hashCode(); + } else if(methodName.equals("equals") + && method.getParameterTypes().length == 1){ + if(args[0] instanceof TopiaEntity){ + TopiaEntity entity = (TopiaEntity)args[0]; + if(Proxy.isProxyClass(args[0].getClass())){ + TopiaPersistenceProxy tpp = (TopiaPersistenceProxy)Proxy.getInvocationHandler(entity); + TopiaPersistenceObject obis = tpp.getObject(); + return o.equals(obis); + } else { + return o.getId().equals(entity.get_topiaId_()); + } + } else { + return Boolean.FALSE; + } + + // la méthode n'est pas une operation, c donc une méthode d'acces + // aux attributs ou equals ou + }else if(methodName.startsWith("get") || methodName.startsWith("is")){ + int prefix = methodName.startsWith("get")?3:2; + String fieldName = + StringUtil.uncapitalize(methodName.substring(prefix)); + + Object result = o.getField(fieldName); + if(result == o.UNLOADED_FIELD){ + if(o.isNew()){ + // TODO retourner la valeur par defaut defini dans le meta + + // si pas de valeur par defaut dans le meta, on retourne + // la valeur par defaut Java + Class returnType = method.getReturnType(); + result = getDefaultValue(returnType); + } else { + tph.getStorage().restore(o); + result = o.getField(fieldName); + } + } + if(result instanceof String && TopiaId.isValidId((String)result)){ + // TODO peut-etre verifier que l'objet pointer par l'id + // existe reelement et si ce n'est pas le cas, mettre + // a null le result et afficher un message WARNING + result = tph.findByTopiaId((String)result); + } + return result; + } else if(methodName.startsWith("set")){ + String fieldName = StringUtil.uncapitalize(methodName.substring(3)); + Object value = args[0]; + if(value instanceof TopiaEntity){ + value = ((TopiaEntity)value).get_topiaId_(); + } + o.setField(fieldName, value); + // on vient de modifier au moins un champs donc l'objet doit + // rentrer dans la transaction + o.getManagement().setDate(ph.getContext().getTransaction().getId()); + o.getModifiedFields().add(fieldName); + } else{ + log.severe("Can't find called method: " + method + " methodName: " + method.getName() + " methodArgType: " + Arrays.toString(method.getParameterTypes())); + // FIXME gérer les appels de méthode spécifique d'attribut (add, remove, ...) + } + + } + } + + protected Object getDefaultValue(Class type){ + Object result = null; + if(returnType.equals(Boolean.TYPE)){ + result = Boolean.FALSE; + } else if(returnType.equals(Character.TYPE)){ + result = Character.valueOf(Character.MIN_VALUE); + } else if(returnType.equals(Byte.TYPE)){ + result = Byte.valueOf((byte)0); + } else if(returnType.equals(Short.TYPE)){ + result = Short.valueOf((short)0); + } else if(returnType.equals(Integer.TYPE)){ + result = Integer.valueOf(0); + } else if(returnType.equals(Long.TYPE)){ + result = Long.valueOf(0); + } else if(returnType.equals(Float.TYPE)){ + result = Float.valueOf(0); + } else if(returnType.equals(Double.TYPE)){ + result = Double.valueOf(0); + } + return result; + } +} // TopiaPersistenceProxy + Index: topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,112 @@ +/* *##% + * Copyright (C) 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. + *##%*/ + +/* * + * TopiaTransaction.java + * + * Created: 16 juillet 2005 22:54:16 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class TopiaTransaction implements Serializable, Comparable { // TopiaTransaction + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.persistence.TopiaTransaction"); + + protected TopiaTransactionHelper tth = null; + protected TopiaTransaction parent = null; + protected List childs = new ArrayList(); + protected long id; + + public TopiaTransaction(TopiaTransactionHelper tth){ + this.tth = tth; + this.id = - tth.getUniqueDate(); + } + + public TopiaTransaction(TopiaTransaction parent){ + this.parent = parent; + this.tth = parent.tth; + this.id = - tth.getUniqueDate(); + } + + public long getId(){ + return id; + } + + public TopiaTransaction getParent(){ + return parent; + } + + public List getChilds(){ + return childs; + } + + public TopiaTransaction newSubTransaction(){ + TopiaTransaction result = new TopiaTransaction(this); + childs.add(result); + return result; + } + + public TopiaTransaction regenerateTransaction(){ + TopiaTransaction result = null; + if(getParent() == null){ + result = tth.newTopiaTranstaction(); + } else { + getParent().getChilds().remove(this); + result = getParent().newSubTransaction(); + } + return result; + } + + + public int compareTo(TopiaTransaction o){ + if(getId() < o.getId()){ + return -1; + } else if(getId() > o.getId()){ + return 1; + } else { + return 0; + } + } + + public int hashCode(){ + return (int)(getId() ^ (getId() >>> 32)); + } + + public boolean equals(Object o){ + return o instanceof TopiaTransaction + && getId() == ((TopiaTransaction)o).getId(); + } + + +} // TopiaTransaction + Index: topia/src/java/org/codelutin/topia/persistence/TopiaTransactionHelper.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TopiaTransactionHelper.java:1.1 --- /dev/null Tue Jul 19 13:15:18 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaTransactionHelper.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,76 @@ +/* *##% + * Copyright (C) 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. + *##%*/ + +/* * + * TopiaTransactionHelper.java + * + * Created: 16 juillet 2005 23:27:18 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.util.logging.Level; +import java.util.logging.Logger; + +public class TopiaTransactionHelper { // TopiaTransactionHelper + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private Logger log = Logger.getLogger("org.codelutin.topia.persistence.TopiaTransactionHelper"); + + public TopiaTransactionHelper(){} + + public TopiaTransaction newTranstaction(){ + return new TopiaTransaction(this); + } + + /** la derniere date retourné */ + protected long lastDate = 0; + + /** + * Permet de modifier la valeur de la derniere date retrouné. Cette méthode + * est utilisé par le framework lors de l'initialisation. + */ + public void setLastDate(long date){ + lastDate = date; + } + + /** + * Permet d'avoir une date unique par rapport au temps systeme. + * Utilisé aussi pour les dates de modif des objets qui doivent + * aussi etre unique pour le meme id + */ + public synchronized long getDate(){ + long result = System.currentTimeMillis(); + // la derniere date retourné doit toujours etre unique + // et superieur a la precedente en valeur absolue + if(result <= lastDate){ + result = lastDate + 1; + } + lastDate = result; + return result; + } + +} // TopiaTransactionHelper + Index: topia/src/java/org/codelutin/topia/persistence/TransactionHelper.java diff -u /dev/null topia/src/java/org/codelutin/topia/persistence/TransactionHelper.java:1.1 --- /dev/null Tue Jul 19 13:15:19 2005 +++ topia/src/java/org/codelutin/topia/persistence/TransactionHelper.java Tue Jul 19 13:15:13 2005 @@ -0,0 +1,58 @@ +/* *##% + * Copyright (C) 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. + *##%*/ + +/* * + * TransactionHelper.java + * + * Created: 16 juillet 2005 23:33:39 CEST + * + * @author Benjamin POUSSIN + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2005/07/19 13:15:13 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.topia.persistence; + +import java.util.logging.Level; +import java.util.logging.Logger; + +public interface TransactionHelper { // TransactionHelper + + /** + * Retourne une nouvelle transaction unique + */ + public TopiaTransaction newTranstaction(); + + /** + * Permet de modifier la valeur de la derniere date retrouné. Cette méthode + * est utilisé par le framework lors de l'initialisation. + */ + public void setLastDate(long date); + + /** + * Permet d'avoir une date unique par rapport au temps systeme. + * Utilisé aussi pour les dates de modif des objets qui doivent + * aussi etre unique pour le meme id + */ + public long getDate(); + +} // TransactionHelper +