Index: topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java:1.4 topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java:1.5 --- topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java:1.4 Fri Aug 5 17:04:40 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaTransaction.java Thu Aug 11 16:42:27 2005 @@ -23,9 +23,9 @@ * Created: 16 juillet 2005 22:54:16 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ * - * Last update: $Date: 2005/08/05 17:04:40 $ + * Last update: $Date: 2005/08/11 16:42:27 $ * by : $Author: thimel $ */ @@ -85,7 +85,7 @@ TopiaTransaction tt = this; int i = 0; result[i++] = tt.getId(); - while(getParent() != null){ + while(tt.getParent() != null){ tt = tt.getParent(); result[i++] = tt.getId(); } @@ -116,6 +116,10 @@ return parent; } + protected void setParent(TopiaTransaction newParent) { + this.parent = newParent; + } + public List getChilds(){ return childs; } @@ -136,6 +140,10 @@ } // si on a des enfants, il faut aussi les garder :) result.getChilds().addAll(getChilds()); + // pour dire aux enfants qu'ils changent de père + for (TopiaTransaction tt : result.getChilds()) { + tt.setParent(result); + } return result; } Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.7 topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.8 --- topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java:1.7 Thu Aug 4 20:59:31 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceHelper.java Thu Aug 11 16:42:27 2005 @@ -23,10 +23,10 @@ * Created: Jul 16, 2005 * * @author Benjamin POUSSIN - * @version $Revision: 1.7 $ + * @version $Revision: 1.8 $ * - * Last update : $Date: 2005/08/04 20:59:31 $ - * by : $Author: bpoussin $ + * Last update : $Date: 2005/08/11 16:42:27 $ + * by : $Author: thimel $ */ package org.codelutin.topia.persistence; @@ -48,6 +48,7 @@ import org.codelutin.topia.TopiaId; import org.codelutin.topia.TopiaNotFoundException; import org.codelutin.topia.TopiaQuery; +import org.codelutin.topia.annotation.ClassInfoHelper; public class TopiaPersistenceHelper implements PersistenceHelper { // TopiaPersistenceHelper @@ -79,7 +80,7 @@ this.properties = properties; } - protected PersistenceStorage getStorage() throws TopiaNotFoundException { + /*protected TODO Pour les tests !!!!!!!!!*/ public PersistenceStorage getStorage() throws TopiaNotFoundException { if(storage == null){ storage = (PersistenceStorage)getContext().getContextHelper().getHelper(getProperties(), TopiaConst.PERSISTENCE_STORAGE_CLASS, true); @@ -132,11 +133,10 @@ } /** - * Recupere la version de schema par introspection sur les MetaInfos + * Recupere la version de schema sur les MetaInfos */ protected long getSchemaVersion(Class entityClass){ - // TODO implanter getSchemaVersion - return 0; + return ClassInfoHelper.getSchemaVersion(entityClass); } public A create(Class entityClass) throws TopiaException { @@ -287,11 +287,15 @@ throw new UnsupportedOperationException(); } + public boolean exists(String id) throws TopiaException { + return getStorage().exists(getContext(), id); + } + public TopiaEntity findByTopiaId(String id) throws TopiaException { if(id == null){ return null; } - if(!getStorage().exists(getContext(), id)){ + if(!exists(id)){ throw new TopiaException("This id don't exist in Storage: " + id); } TopiaEntity result = getCache().get(id); Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectManagementData.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectManagementData.java:1.3 topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectManagementData.java:1.4 --- topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectManagementData.java:1.3 Thu Jul 21 16:51:39 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceObjectManagementData.java Thu Aug 11 16:42:27 2005 @@ -23,10 +23,10 @@ * Created: 16 juillet 2005 23:46:51 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ * - * Last update: $Date: 2005/07/21 16:51:39 $ - * by : $Author: bpoussin $ + * Last update: $Date: 2005/08/11 16:42:27 $ + * by : $Author: thimel $ */ package org.codelutin.topia.persistence; @@ -37,6 +37,9 @@ import java.util.logging.Logger; import java.util.Set; +import org.codelutin.topia.TopiaId; +import org.codelutin.topia.TopiaNotFoundException; + public class TopiaPersistenceObjectManagementData implements Serializable { // TopiaPersistenceObjectManagementData /** to use log facility, just put in your code: log.info(\"...\"); */ @@ -48,12 +51,14 @@ /** l'objet vient d'etre creer mais n'a jamais ete sauvé */ NEW, /** l'objet a deja ete sauvé */ - STORED; + STORED, + /** l'objet a été rollbacké */ + ROLLBACKED; } protected String id = null; protected EnumSet state = EnumSet.noneOf(State.class); - protected long date = 0; + protected long date = 0; //Correspond à la dernière date de modification (transaction en cours ou dernière trasaction commitée) protected long schemaVersion = 0; public TopiaPersistenceObjectManagementData(){} @@ -120,5 +125,26 @@ this.schemaVersion = schemaVersion; } -} // TopiaPersistenceObjectManagementData + public Class getEntityClass() { + try { + return TopiaId.getClassName(id); + } catch (TopiaNotFoundException e) { + // Ca n'est pas censé arriver + log.log(Level.SEVERE, e.getMessage()); + return Void.class; + } + } + public void setRollbacked(boolean v) { + if (v) { + state.add(State.ROLLBACKED); + } else { + state.remove(State.ROLLBACKED); + } + } + + public boolean isRollbacked() { + return state.contains(State.ROLLBACKED); + } + +} // TopiaPersistenceObjectManagementData Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java:1.4 topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java:1.5 --- topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java:1.4 Thu Aug 4 20:59:31 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceCache.java Thu Aug 11 16:42:27 2005 @@ -23,14 +23,15 @@ * Created: 16 juillet 2005 23:42:14 CEST * * @author Benjamin POUSSIN - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ * - * Last update: $Date: 2005/08/04 20:59:31 $ - * by : $Author: bpoussin $ + * Last update: $Date: 2005/08/11 16:42:27 $ + * by : $Author: thimel $ */ package org.codelutin.topia.persistence; +import java.lang.reflect.Proxy; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; @@ -71,7 +72,17 @@ } public void clean(){ - cache.clear(); +// cache.clear(); + for (String id: cache.keySet()) { + TopiaEntity entity = cache.get(id); + if (entity != null) { + TopiaPersistenceProxy tpp = + (TopiaPersistenceProxy)Proxy.getInvocationHandler(entity); + TopiaPersistenceObject tpo = tpp.getObject(); + tpo.getManagement().setRollbacked(true); + tpo.getData().clear(); + } + } } public void putAll(TopiaPersistenceCache cache){ Index: topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java diff -u topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java:1.4 topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java:1.5 --- topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java:1.4 Tue Aug 2 15:00:11 2005 +++ topia/src/java/org/codelutin/topia/persistence/TopiaPersistenceProxy.java Thu Aug 11 16:42:27 2005 @@ -23,10 +23,10 @@ * Created: 16 juillet 2005 23:52:35 CEST * * @author Benjamin POUSSIN -* @version $Revision: 1.4 $ +* @version $Revision: 1.5 $ * -* Last update: $Date: 2005/08/02 15:00:11 $ -* by : $Author: bpoussin $ +* Last update: $Date: 2005/08/11 16:42:27 $ +* by : $Author: thimel $ */ package org.codelutin.topia.persistence; @@ -39,6 +39,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -137,12 +138,20 @@ // toujours pas de valeur, soit parce que l'objet est nouveau // soit parce qu'on a jamais assigné de valeur a l'objet - // 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 - result = getDefaultValue(fieldType); + Object[] values = ClassInfoHelper.fieldValues(entity.getEntityClass()); + String[] fields = ClassInfoHelper.fields(entity.getEntityClass()); + if (values == null || values.length == 0 || fields == null || fields.length != values.length) { + // si pas de valeur par defaut dans le meta, + // on retourne la valeur par defaut Java + result = getDefaultValue(fieldType); + } else { + for (int i = 0; i < values.length; i++) { + if (fields[i].equals(fieldName)) { + result = values[i]; + break; + } + } + } } } return result; @@ -187,18 +196,26 @@ } } + TopiaEntity r; + //On veut faire un entity.setXXX(value) +// if(assoPS.getEntityClass() != entity.getEntityClass()){ // recuperation de l'objet a modifier s'il existe - TopiaEntity r = (TopiaEntity)MethodUtils.invokeMethod( - assoPS, - "findBy" + StringUtils.capitalize(otherField), - entity); - if(r == null){ - r = assoPS.create(); + r = (TopiaEntity)MethodUtils.invokeMethod( + assoPS, + "findBy" + StringUtils.capitalize(otherField), + entity); + if(r == null){ + r = assoPS.create(); + MethodUtils.invokeMethod(r, + "set" + StringUtils.capitalize(otherField), entity); + } MethodUtils.invokeMethod(r, - "set" + StringUtils.capitalize(otherField), entity); - } - MethodUtils.invokeMethod(r, - "set" + StringUtils.capitalize(thisField), value); + "set" + StringUtils.capitalize(thisField), value); +// } else { +// r = entity; +// setField(thisField, value); +// } + //TODO // QUESTION: peut-etre ne pas faire le update // ici mais lorsque l'on fait le update sur // l'objet a partir duquel on a modifier la @@ -240,7 +257,7 @@ "set" + StringUtils.capitalize(thisField), value); } } - + //TODO // QUESTION: peut-etre ne pas faire le update // ici mais lorsque l'on fait le update sur // l'objet a partir duquel on a modifier la @@ -280,7 +297,7 @@ } } - protected Collection getFieldAssociationCardinalityN( + protected List getFieldAssociationCardinalityN( TopiaPersistenceService assoPS, String thisField, String otherField, @@ -289,13 +306,13 @@ assoPS, "findAllBy" + StringUtils.capitalize(otherField), entity); - Collection result = new ArrayList(col.size()); + List result = new ArrayList(col.size()); for(TopiaEntity r: col){ TopiaEntity e = (TopiaEntity)MethodUtils.invokeExactMethod(r, "get" + StringUtils.capitalize(thisField), null); result.add(e); } - return Collections.unmodifiableCollection(result); + return Collections.unmodifiableList(result); } public Object invoke(Object proxy, Method method, Object[] args) @@ -304,10 +321,36 @@ String methodName = method.getName(); // on ne permet pas la surcharge des methodes utiles au framework - if("get_topiaId_".equals(methodName)){ + if ("getEntityClass".equals(methodName)) { + return getObject().getManagement().getEntityClass(); + } else if("get_topiaId_".equals(methodName)){ return getObject().getManagement().getId(); - } - // TODO mettre ici les autres methodes declarees sur TopiaEntity + } else if ("get_topiaVersion_".equals(methodName)) { + return getObject().getManagement().getSchemaVersion(); +// } else if ("get_topiaCreationDate_".equals(methodName)) { +// return getObject().getManagement().getCreationDate(); + } else if ("get_topiaLastUpdateDate_".equals(methodName)) { + return new Date(getObject().getManagement().getDate()); +// } else if ("get_topiaLastUpdateUser_".equals(methodName)) { +// return getObject().getManagement().getLastUpdateUser(); + } else if ("set_topiaId_".equals(methodName)) { + getObject().getManagement().setId((String)args[0]); + return null; + } else if ("set_topiaVersion_".equals(methodName)) { + getObject().getManagement().setSchemaVersion((Long)args[0]); + return null; +// } else if ("set_topiaCreationDate_".equals(methodName)) { +// getObject().getManagement().setCreationDate((Long)args[0]); +// return null; + } else if ("set_topiaLastUpdateDate_".equals(methodName)) { + getObject().getManagement().setDate((Long)args[0]); + return null; +// } else if ("set_topiaLastUpdateUser_".equals(methodName)) { +// getObject().getManagement().setLastUpdateUser((TopiaUser)args[0]); +// return null; + } + + // TODO voir si les autres methodes declarees sur TopiaEntity ont une utilité et si oui comment les gérer // on commence par regarder si l'utilisateur n'a pas implanté la // méthode dans l'objet des operations @@ -355,7 +398,7 @@ if(ClassInfoHelper.type(fieldType) == ClassType.ENTITY){ // le champs pointe sur une autre entite Class assoClass = MethodInfoHelper.associationClass(method); - if(assoClass == null){ + if(assoClass == null || assoClass.equals( entity.getEntityClass() )){ // on pointe vers une class sans qu'il y ait de classe // d'association pour conserver la relation, on // fait donc juste une conversion en id @@ -367,10 +410,14 @@ } else { // methode pour acceder au champs Object result = getField(entity, fieldName, fieldType); - // 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 = ph.findByTopiaId((String)result); + if (!ph.exists((String)result)) { + log.log(Level.WARNING, "L'objet d'id \"" + + result + "\" n'existe pas. Le champ \"" + + fieldName + "\" est mis à null."); + result = null; + } else { + result = ph.findByTopiaId((String)result); + } return result; } } else { Index: topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java diff -u topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java:1.12 topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java:1.13 --- topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java:1.12 Wed Aug 3 16:17:10 2005 +++ topia/src/java/org/codelutin/topia/persistence/PersistenceHelper.java Thu Aug 11 16:42:27 2005 @@ -23,9 +23,9 @@ * Created: Aug 1, 2004 * * @author Cédric Pineau - * @version $Revision: 1.12 $ + * @version $Revision: 1.13 $ * - * Last update : $Date: 2005/08/03 16:17:10 $ + * Last update : $Date: 2005/08/11 16:42:27 $ * by : $Author: thimel $ */ @@ -35,6 +35,7 @@ import org.codelutin.topia.TopiaContext; import org.codelutin.topia.TopiaEntity; import org.codelutin.topia.TopiaException; +import org.codelutin.topia.TopiaNotFoundException; import org.codelutin.topia.TopiaQuery; import org.codelutin.topia.AsynchronousLoader; @@ -112,7 +113,12 @@ /** * Recherche un objet par son identifiant */ - public A findByTopiaId(String id) throws TopiaException; + public TopiaEntity findByTopiaId(String id) throws TopiaException; + + /** + * Renvoie si l'id correspond à une entité existante + */ + public boolean exists(String id) throws TopiaException; /** * Ouvre une transaction pour le thread courant @@ -131,4 +137,6 @@ * @throws TopiaException si aucune transaction ouverte pour le thread courant */ public void rollbackTransaction() throws TopiaException; + + public PersistenceStorage getStorage() throws TopiaNotFoundException ; }