Index: topia/src/java/org/codelutin/topia/jdo/AbstractTopiaJDOPersistenceService.java diff -u topia/src/java/org/codelutin/topia/jdo/AbstractTopiaJDOPersistenceService.java:1.6 topia/src/java/org/codelutin/topia/jdo/AbstractTopiaJDOPersistenceService.java:1.7 --- topia/src/java/org/codelutin/topia/jdo/AbstractTopiaJDOPersistenceService.java:1.6 Fri Jun 4 18:43:54 2004 +++ topia/src/java/org/codelutin/topia/jdo/AbstractTopiaJDOPersistenceService.java Wed Jun 16 09:41:26 2004 @@ -23,9 +23,9 @@ * * @author Cédric Pineau * Copyright Code Lutin -* @version $Revision: 1.6 $ +* @version $Revision: 1.7 $ * -* Last update : $Date: 2004/06/04 18:43:54 $ +* Last update : $Date: 2004/06/16 09:41:26 $ * by : $Author: bpoussin $ */ package org.codelutin.topia.jdo; @@ -53,6 +53,44 @@ extends AbstractTopiaPersistenceService { /** + * Returns the TopiaEntity DataObject corresponding to the given TopiaEntity + * TransferObject. The DO is updated to represente the same data that TO + * The change is commited to the data store + * @param topiaEntityTO - the TopiaEntity TransferObject whose DataObject + * is wanted. + * @see topiaEntityTO + * @see topiaEntityDO + * + * @return the corresponding TopiaEntity DataObject. + */ + protected TopiaEntityDO toDO(TopiaEntityTO _to) throws TopiaException { + if (_to == null) return null; + TopiaEntityDO _do; + Object objectTopiaId = _to.get_topiaId_(); + //TODO mettre a jour les info lastModificationDate, ... + + PersistenceManager persistenceManager = ((TopiaJDOPersistenceHelper) getContext().getPersistenceHelper()).getPersistenceManager(); + Transaction transaction = persistenceManager.currentTransaction(); + transaction.begin(); + try{ + if (objectTopiaId != null) { + _do = findDOByTopiaId(objectTopiaId); + _do.set_allProperties_(_to); + } else { + _do = getContext().createEntityDO(getEntityClass()); + _do.set_allProperties_(_to); + persistenceManager.makePersistent(_do); + } + transaction.commit(); + } catch (Throwable eee) { + transaction.rollback(); + throw (new TopiaException("Cannot make persistent !", eee)); + } + + return _do; + } + + /** * Make persistent the given TopiaEntity. * This operation returns either a copy or the given TopiaEntity itself (depending on the current context) feeded with a valid topiaId. * @param topiaEntity - the TopiaEntity to persist. @@ -60,7 +98,7 @@ * @return the persistent TopiaEntity. */ public TopiaEntity makePersistent(TopiaEntity topiaEntity) throws TopiaException { - if (topiaEntity._getTopiaId() != null) { + if (topiaEntity.get_topiaId_() != null) { throw new TopiaException("Can't persist "+topiaEntity+" has it seems to exist, please use update instead !"); } TopiaEntityTO _to = (TopiaEntityTO) Util.toTO(topiaEntity); @@ -83,20 +121,20 @@ * @param topiaEntity - the TopiaEntity whose changes must be made persistent. */ public TopiaEntity update(TopiaEntity topiaEntity) throws TopiaException { - if (topiaEntity._getTopiaId() == null) { + if (topiaEntity.get_topiaId_() == null) { throw new TopiaException("Can't update "+topiaEntity+" has it seems not to exist, please use makePersistent instead !"); } TopiaEntityTO _to = (TopiaEntityTO) Util.toTO(topiaEntity); - PersistenceManager persistenceManager = ((TopiaJDOPersistenceHelper) getContext().getPersistenceHelper()).getPersistenceManager(); - Transaction transaction = persistenceManager.currentTransaction(); - transaction.begin(); - try { - toUpdatedDO(_to); - } catch (TopiaException e) { - transaction.rollback(); - throw (e); - } - transaction.commit(); +// PersistenceManager persistenceManager = ((TopiaJDOPersistenceHelper) getContext().getPersistenceHelper()).getPersistenceManager(); +// Transaction transaction = persistenceManager.currentTransaction(); +// transaction.begin(); +// try { + toDO(_to); +// transaction.commit(); +// } catch (TopiaException eee) { +// transaction.rollback(); +// throw eee; +// } return _to; } @@ -105,20 +143,20 @@ * @param topiaEntity - the TopiaEntity to delete. */ public void delete(TopiaEntity topiaEntity) throws TopiaException { - if (topiaEntity._getTopiaId() == null) { + Object id = topiaEntity.get_topiaId_(); + if (id == null) { throw new TopiaException("Can't delete "+topiaEntity+" has it seems not to exist (no identity found) !"); } - TopiaEntityTO _to = (TopiaEntityTO) Util.toTO(topiaEntity); PersistenceManager persistenceManager = ((TopiaJDOPersistenceHelper) getContext().getPersistenceHelper()).getPersistenceManager(); Transaction transaction = persistenceManager.currentTransaction(); transaction.begin(); try { - persistenceManager.deletePersistent(toDO(_to)); - } catch (TopiaException e) { + persistenceManager.deletePersistent(findDOByTopiaId(id)); + transaction.commit(); + } catch (TopiaException eee) { transaction.rollback(); - throw (e); + throw (eee); } - transaction.commit(); } }