Author: athimel Date: 2012-05-15 16:47:30 +0200 (Tue, 15 May 2012) New Revision: 2484 Url: http://nuiton.org/repositories/revision/topia/2484 Log: Implement 'replicate' operation (Hibernate only) Remove i18n exception messages Modified: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernateSpecificUtil.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java trunk/topia-persistence/src/main/resources/i18n/topia-persistence_en_GB.properties trunk/topia-persistence/src/main/resources/i18n/topia-persistence_es_ES.properties trunk/topia-persistence/src/main/resources/i18n/topia-persistence_fr_FR.properties Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-05-15 10:10:32 UTC (rev 2483) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImpl.java 2012-05-15 14:47:30 UTC (rev 2484) @@ -35,6 +35,7 @@ import org.nuiton.topia.TopiaContextFactory; import org.nuiton.topia.TopiaException; import org.nuiton.topia.TopiaNotFoundException; +import org.nuiton.topia.TopiaRuntimeException; import org.nuiton.topia.event.TopiaContextListener; import org.nuiton.topia.event.TopiaEntitiesVetoable; import org.nuiton.topia.event.TopiaEntityListener; @@ -75,7 +76,6 @@ import java.util.WeakHashMap; import java.util.zip.GZIPInputStream; -import static org.nuiton.i18n.I18n._; //import org.hibernate.EntityMode; //import org.hibernate.ReplicationMode; @@ -333,13 +333,13 @@ } @Override - public TopiaSpecificUtil getSpecificUtil() throws TopiaException { + public TopiaSpecificUtil getSpecificUtil() { TopiaSpecificUtil result = specificUtil; if (getParentContext() != null) { result = getParentContext().getSpecificUtil(); } if (result == null) { - throw new TopiaException("No TopiaSpecificUtil found."); + throw new TopiaRuntimeException("No TopiaSpecificUtil found."); } return result; } @@ -455,76 +455,6 @@ // } else { // hibernateConfiguration = new Configuration(); // -// // Ajout du listeners pour les events -// TopiaFiresSupport.TopiaHibernateEvent listener = -// new TopiaFiresSupport.TopiaHibernateEvent(this); -// -// PreInsertEventListener[] preInsertEventListeners = -// hibernateConfiguration.getEventListeners(). -// getPreInsertEventListeners(); -// preInsertEventListeners = ArrayUtil.concatElems( -// preInsertEventListeners, listener); -// -// PreLoadEventListener[] preLoadEventListeners = -// hibernateConfiguration.getEventListeners(). -// getPreLoadEventListeners(); -// preLoadEventListeners = ArrayUtil.concatElems( -// preLoadEventListeners, listener); -// -// PreUpdateEventListener[] preUpdateEventListeners = -// hibernateConfiguration.getEventListeners(). -// getPreUpdateEventListeners(); -// preUpdateEventListeners = ArrayUtil.concatElems( -// preUpdateEventListeners, listener); -// -// PreDeleteEventListener[] preDeleteEventListeners = -// hibernateConfiguration.getEventListeners(). -// getPreDeleteEventListeners(); -// preDeleteEventListeners = ArrayUtil.concatElems( -// preDeleteEventListeners, listener); -// -// PostInsertEventListener[] postInsertEventListeners = -// hibernateConfiguration.getEventListeners(). -// getPostInsertEventListeners(); -// postInsertEventListeners = ArrayUtil.concatElems( -// postInsertEventListeners, listener); -// -// PostLoadEventListener[] postLoadEventListeners = -// hibernateConfiguration.getEventListeners(). -// getPostLoadEventListeners(); -// postLoadEventListeners = ArrayUtil.concatElems( -// postLoadEventListeners, listener); -// -// PostUpdateEventListener[] postUpdateEventListeners = -// hibernateConfiguration.getEventListeners(). -// getPostUpdateEventListeners(); -// postUpdateEventListeners = ArrayUtil.concatElems( -// postUpdateEventListeners, listener); -// -// PostDeleteEventListener[] postDeleteEventListeners = -// hibernateConfiguration.getEventListeners(). -// getPostDeleteEventListeners(); -// postDeleteEventListeners = ArrayUtil.concatElems( -// postDeleteEventListeners, listener); -// -// hibernateConfiguration.getEventListeners(). -// setPreInsertEventListeners(preInsertEventListeners); -// hibernateConfiguration.getEventListeners(). -// setPreLoadEventListeners(preLoadEventListeners); -// hibernateConfiguration.getEventListeners(). -// setPreUpdateEventListeners(preUpdateEventListeners); -// hibernateConfiguration.getEventListeners(). -// setPreDeleteEventListeners(preDeleteEventListeners); -// -// hibernateConfiguration.getEventListeners(). -// setPostInsertEventListeners(postInsertEventListeners); -// hibernateConfiguration.getEventListeners(). -// setPostLoadEventListeners(postLoadEventListeners); -// hibernateConfiguration.getEventListeners(). -// setPostUpdateEventListeners(postUpdateEventListeners); -// hibernateConfiguration.getEventListeners(). -// setPostDeleteEventListeners(postDeleteEventListeners); -// // // ajout des repertoires contenant les mappings hibernate // String[] dirs = getConfig().getProperty( // TopiaContextFactory.CONFIG_PERSISTENCE_DIRECTORIES, "").split(","); @@ -552,13 +482,14 @@ public <E extends TopiaEntity> TopiaDAO<E> getDAO(Class<E> entityClass) throws TopiaException { if (entityClass == null) { - throw new IllegalArgumentException( - _("topia.persistence.error.null.param", - "entityClass", "getDAO")); + String message = String.format( + "The method '%s' requires a non null parameter '%s'.", + "getDAO", "entityClass"); + throw new IllegalArgumentException(message); } if (equals(getRootContext())) { throw new TopiaException( - _("topia.persistence.error.rootContext.access")); + "You are on root context, you MUST open a transaction to perform any database access."); } Set<EntityType<?>> entities = getEntityManagerFactory().getMetamodel().getEntities(); boolean found; @@ -575,8 +506,8 @@ // if (!found) { // log.info(_("topia.persistence.supported.classes.for.context", // getEntityManagerFactory().getMetamodel().getEntities())); -// throw new TopiaException( -// _("topia.persistence.error.unsupported.class", +// throw new TopiaException(String.format( +// "Entity type '%s' not managed by this context, you probably forgot to declare it.", // entityClass.getName())); // } @@ -616,7 +547,7 @@ @Override public TopiaContext beginTransaction() throws TopiaException { - checkClosed(_("topia.persistence.error.context.is.closed")); + checkClosed("beginTransaction"); TopiaContextImpl result = new TopiaContextImpl(this); EntityManagerFactory factory = getEntityManagerFactory(); @@ -651,9 +582,8 @@ } throw new TopiaException( - _("topia.persistence.error.open.transaction.failed", - eee.getMessage()), - eee); + "An error occured while getting a new transaction : " + + eee.getMessage(), eee); } // 20081217 : add child AFTER EntityManager opening @@ -666,12 +596,12 @@ @Override public void commitTransaction() throws TopiaException { - if (equals(getRootContext())) { - throw new TopiaException(_("topia.persistence.error.unsupported.operation.on.root.context", - "commit")); + boolean isRootContext = equals(getRootContext()); + if (isRootContext) { + String message = String.format("Operation '%s' is not permitted on root context", "commitTransaction"); + throw new TopiaException(message); } - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "commit")); + checkClosed("commitTransaction"); try { // for (TopiaDAO<? extends TopiaEntity> dao : daoCache.values()) { @@ -697,19 +627,20 @@ // hibernate = getEntityManagerFactory().openSession(); // hibernate.setFlushMode(FlushMode.NEVER); } catch (Exception eee) { - throw new TopiaException(_("topia.persistence.error.on.commit", - eee.getMessage()), eee); + + throw new TopiaException( + "An error occured during commit operation : " + + eee.getMessage(), eee); } } @Override public void rollbackTransaction() throws TopiaException { if (equals(getRootContext())) { - throw new TopiaException(_("topia.persistence.error.unsupported.operation.on.root.context", - "rollback")); + String message = String.format("Operation '%s' is not permitted on root context", "rollbackTransaction"); + throw new TopiaException(message); } - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "rollback")); + checkClosed("rollbackTransaction"); try { // for (TopiaDAO<? extends TopiaEntity> dao : daoCache.values()) { // // TODO-fdesbois-20100507 : need to be removed for 2.5 version @@ -735,15 +666,15 @@ } catch (PersistenceException eee) { throw new TopiaException( - _("topia.persistence.error.on.rollback", - eee.getMessage()), eee); + "An error occured during rollback operation : " + + eee.getMessage(), eee); } } @Override public void closeContext() throws TopiaException { // Throw exception if context is already closed - checkClosed(_("topia.persistence.error.context.already.closed")); + checkClosed("closeContext"); // FD-20100421 : Ano #546 : no need to copy childContexts, the // {@link #getChildContext()} provides a thread-safe copy to iterate @@ -793,8 +724,9 @@ return closed; } - protected void checkClosed(String message) throws TopiaException { + protected void checkClosed(String method) throws TopiaException { if (closed) { + String message = String.format("This context is closed, unable to perform operation '%s'", method); throw new TopiaException(message); } } @@ -919,8 +851,7 @@ @SuppressWarnings({"unchecked"}) @Override public TopiaEntity findByTopiaId(String id) throws TopiaException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "findById")); + checkClosed("findByTopiaId"); Class<TopiaEntity> entityClass = TopiaId.getClassName(id); TopiaDAO<TopiaEntity> dao = getDAO(entityClass); @@ -941,8 +872,7 @@ @Override public List<?> find(String hql, Object... args) throws TopiaException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "find")); + checkClosed("find"); try { Query query = getEntityManager().createQuery(hql); @@ -965,16 +895,15 @@ result = firesSupport.fireEntitiesLoad(this, result); return result; } catch (PersistenceException eee) { - throw new TopiaException(_("topia.persistence.error.on.query", - hql, eee.getMessage()), eee); + String message = String.format("An error occured (%s) during a query operation : %s", eee.getMessage(), hql); + throw new TopiaException(message, eee); } } @Override public List<?> find(String hql, int startIndex, int endIndex, Object... args) throws TopiaException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "find")); + checkClosed("find"); try { Query query = getEntityManager().createQuery(hql); @@ -997,8 +926,8 @@ result = firesSupport.fireEntitiesLoad(this, result); return result; } catch (PersistenceException eee) { - throw new TopiaException(_("topia.persistence.error.on.query", - hql, eee.getMessage()), eee); + String message = String.format("An error occured (%s) during a query operation : %s", eee.getMessage(), hql); + throw new TopiaException(message, eee); } } @@ -1012,8 +941,7 @@ */ @Override public int execute(String hql, Object... args) throws TopiaException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "find")); + checkClosed("execute"); try { Query query = getEntityManager().createQuery(hql); @@ -1023,15 +951,14 @@ int result = query.executeUpdate(); return result; } catch (PersistenceException eee) { - throw new TopiaException(_("topia.persistence.error.on.query", - hql, eee.getMessage()), eee); + String message = String.format("An error occured (%s) during a query operation : %s", eee.getMessage(), hql); + throw new TopiaException(message, eee); } } @Override public void add(TopiaEntity e) throws TopiaException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "add")); + checkClosed("add"); String id = e.getTopiaId(); Class<TopiaEntity> entityClass = TopiaId.getClassName(id); @@ -1073,8 +1000,7 @@ @Override public boolean isSchemaExist(Class<?> clazz) throws TopiaException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "replicateEntity")); + checkClosed("isSchemaExist"); boolean result = getSpecificUtil().isSchemaExist(clazz); @@ -1092,9 +1018,8 @@ getSpecificUtil().createSchema(showSchema); getFiresSupport().firePostCreateSchema(this); } catch (PersistenceException eee) { - throw new TopiaException( - _("topia.persistence.error.create.schema", - eee.getMessage()), eee); + throw new TopiaException("Schema could not be created for the following reason : " + + eee.getMessage(), eee); } } @@ -1103,9 +1028,8 @@ try { getSpecificUtil().showCreateSchema(); } catch (PersistenceException eee) { - throw new TopiaException( - _("topia.persistence.error.create.schema", - eee.getMessage()), eee); + throw new TopiaException("Schema creation could not be displayed for the following reason : " + + eee.getMessage(), eee); } } @@ -1120,9 +1044,8 @@ getSpecificUtil().updateSchema(showSchema); getFiresSupport().firePostUpdateSchema(this); } catch (PersistenceException eee) { - throw new TopiaException( - _("topia.persistence.error.update.schema", - eee.getMessage()), eee); + throw new TopiaException("Schema could not be updated for the following reason : " + + eee.getMessage(), eee); } } @@ -1147,16 +1070,19 @@ TopiaService service = (TopiaService) newInstance; if (key.equals("topia.service." + service.getServiceName())) { result.put(service.getServiceName(), service); - log.info(_("topia.persistence.service.loaded", + + log.info(String.format( + "Service '%s' loaded (implementation %s)", key, classService)); } else { - log.warn(_("topia.persistence.warn.service.not.loaded", + log.warn(String.format( + "The service with key '%s' has a different name '%s' ! (service not activated)", key, service.getServiceName())); } } catch (Throwable eee) { - String message = - _("topia.persistence.error.service.unknown", - key, classService); + String message = String.format( + "The service %s of type %s was not found.", + key, classService); if (log.isDebugEnabled()) { log.debug(message, eee); } else if (log.isErrorEnabled()) { @@ -1171,7 +1097,8 @@ protected void preInitServices(Map<String, TopiaService> services) { for (TopiaService service : services.values()) { if (!service.preInit(this)) { - log.warn(_("topia.persistence.warn.service.not.preInit", + log.warn(String.format( + "The service named '%s' could not be pre-initialized (service not activated)", service.getServiceName())); } } @@ -1180,7 +1107,8 @@ protected void postInitServices(Map<String, TopiaService> services) { for (TopiaService service : services.values()) { if (!service.postInit(this)) { - log.warn(_("topia.persistence.warn.service.not.postInit", + log.warn(String.format( + "The service named '%s' could not be post-initialized (service not activated)", service.getServiceName())); } } @@ -1236,15 +1164,12 @@ String name = getServiceName(interfaceService); result = (E) getService(name); } catch (Exception eee) { - throw new TopiaNotFoundException( - _("topia.persistence.error.service.not.retreaved", - interfaceService, getProperExceptionMessage(eee)), - eee); + String message = String.format("Could not retrieve service '%s' for following reason : %s", interfaceService, getProperExceptionMessage(eee)); + throw new TopiaNotFoundException(message, eee); } if (result == null) { throw new TopiaNotFoundException( - _("topia.persistence.error.service.not.found", - interfaceService)); + String.format("The service '%s' not found.", interfaceService)); } return result; } @@ -1257,7 +1182,8 @@ String name = getServiceName(interfaceService); result = serviceEnabled(name); } catch (Exception eee) { - String message = _("topia.persistence.warn.service.not.found", + String message = String.format( + "The service named '%s' could not be found for following reason : %s", interfaceService, getProperExceptionMessage(eee)); if (log.isDebugEnabled()) { log.debug(message, eee); @@ -1273,7 +1199,7 @@ @Override public void importXML(Reader xml) throws TopiaException { -// checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", +// checkClosed(String.format("This context is closed, it is not possible to perform the operation '%s'", // "importXML")); // // Document doc; @@ -1286,8 +1212,8 @@ // } // } catch (DocumentException de) { // throw new TopiaException( -// _("topia.persistence.error.on.loding.xml.doc", -// de.getMessage()), de); +// "Could not read XML document for following reason : %s" + +// de.getMessage(), de); // } // // if (doc != null) { @@ -1299,22 +1225,22 @@ // try { // sessionDom4j.replicate(entity, ReplicationMode.EXCEPTION); // } catch (PersistenceException he) { -// log.warn(_("topia.persistence.error.replicate.entity", +// log.warn(String.format( +// "Could not replicate entity '%s' pour following reason : %s", // entity, he.getMessage()), he); // } // } // // must commit data, otherwise : no effects... // sessionDom4j.flush(); // } else { -// throw new TopiaException( -// _("topia.persistence.error.empty.doc")); +// throw new TopiaException("Empty document"); // } } @Override public void exportXML(Writer xml, Object... entityAndcondition) throws TopiaException { -// checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", +// checkClosed(String.format("This context is closed, it is not possible to perform the operation '%s'", // "exportXML")); // // String[] queries = buildQueries(entityAndcondition); @@ -1372,28 +1298,24 @@ // result.close(); // // } catch (PersistenceException eee) { -// throw new TopiaException(_("topia.persistence.error.on.export", -// eee.getMessage()), eee); +// throw new TopiaException("An error occurred during export operation : %s" + +// eee.getMessage(), eee); // } catch (IOException eee) { -// throw new TopiaException(_("topia.persistence.error.on.export", -// eee.getMessage()), eee); +// throw new TopiaException("An error occurred during export operation : %s" + +// eee.getMessage(), eee); // } } @Override - public void replicate(TopiaContext dstCtxt, Object... entityAndCondition) + public void replicate(TopiaContext destinationContext, Object... entityAndCondition) throws TopiaException, IllegalArgumentException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "replicate")); + checkClosed("replicate"); - TopiaContextImpl dstContextImpl = (TopiaContextImpl) dstCtxt; - dstContextImpl.checkClosed( - _("topia.persistence.error.unsupported.operation.on.closed.context", - "replicate")); + TopiaContextImpl destinationContextImpl = (TopiaContextImpl) destinationContext; + destinationContextImpl.checkClosed("replicate"); - if (getRootContext().equals(dstContextImpl.getRootContext())) { - throw new IllegalArgumentException( - _("topia.persistence.error.replicate.on.same.context")); + if (getRootContext().equals(destinationContextImpl.getRootContext())) { + throw new IllegalArgumentException("Can not perform a replication operation on same database."); } String[] queries = buildQueries(entityAndCondition); @@ -1404,51 +1326,47 @@ } // acquire data to replicate List<?> entities = find(query); - replicate0(dstContextImpl, entities.toArray()); + replicateOnDestinationContext(destinationContextImpl, entities.toArray()); if (log.isDebugEnabled()) { log.debug("replication of entities " + query + " was sucessfully done."); } } } catch (PersistenceException eee) { - throw new TopiaException(_("topia.persistence.error.on.replicate", - eee.getMessage()), eee); + throw new TopiaException( + "An error occurs while a replication operation : " + eee.getMessage(), eee); } } @Override - public <T extends TopiaEntity> void replicateEntity(TopiaContext dstCtxt, + public <T extends TopiaEntity> void replicateEntity(TopiaContext destinationContext, T entity) throws TopiaException, IllegalArgumentException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "replicateEntity")); + checkClosed("replicateEntity"); - TopiaContextImpl dstContextImpl = (TopiaContextImpl) dstCtxt; - dstContextImpl.checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "replicateEntity")); - if (getRootContext().equals(dstContextImpl.getRootContext())) { - throw new IllegalArgumentException(_( - "topia.persistence.error.replicate.on.same.context")); + TopiaContextImpl destinationContextImpl = (TopiaContextImpl) destinationContext; + destinationContextImpl.checkClosed("replicateEntity"); + + if (getRootContext().equals(destinationContextImpl.getRootContext())) { + throw new IllegalArgumentException("Can not perform a replication operation on same database."); } - replicate0(dstContextImpl, entity); + replicateOnDestinationContext(destinationContextImpl, entity); } @Override - public <T extends TopiaEntity> void replicateEntities(TopiaContext dstCtxt, + public <T extends TopiaEntity> void replicateEntities(TopiaContext destinationContext, List<T> entities) throws TopiaException, IllegalArgumentException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "replicateEntities")); + checkClosed("replicateEntities"); - TopiaContextImpl dstContextImpl = (TopiaContextImpl) dstCtxt; - dstContextImpl.checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "replicateEntities")); + TopiaContextImpl destinationContextImpl = (TopiaContextImpl) destinationContext; + destinationContextImpl.checkClosed("replicateEntities"); - if (getRootContext().equals(dstContextImpl.getRootContext())) { - throw new IllegalArgumentException(_("topia.persistence.error.replicate.on.same.context")); + if (getRootContext().equals(destinationContextImpl.getRootContext())) { + throw new IllegalArgumentException("Can not perform a replication operation on same database."); } - replicate0(dstContextImpl, entities.toArray()); + replicateOnDestinationContext(destinationContextImpl, entities.toArray()); } /** @@ -1462,8 +1380,7 @@ */ @Override public void backup(File file, boolean compress) throws TopiaException { - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "backup")); + checkClosed("backup"); try { String options = ""; if (compress) { @@ -1475,9 +1392,8 @@ query.getResultList(); } catch (Exception eee) { - throw new TopiaException(_( - "topia.persistence.error.on.backup", - eee.getMessage()), eee); + throw new TopiaException( + "Unable to backup to file : " + eee.getMessage(), eee); } } @@ -1492,8 +1408,7 @@ public void restore(File file) throws TopiaException { // send event getFiresSupport().firePreRestoreSchema(this); - checkClosed(_("topia.persistence.error.unsupported.operation.on.closed.context", - "restore")); + checkClosed("restore"); String sql = null; String options = ""; @@ -1525,9 +1440,9 @@ // send event AFTER restore getFiresSupport().firePostRestoreSchema(this); } catch (Exception eee) { - throw new TopiaException(_( - "topia.persistence.error.on.restore", - sql, eee.getMessage()), eee); + throw new TopiaException( + "Unable to restore from file : " + eee.getMessage() + ". " + + "SQL is " + sql, eee); } } @@ -1552,7 +1467,7 @@ root.finalize(); } catch (Throwable eee) { throw new TopiaException( - _("topia.persistence.error.on.clear", eee.getMessage()), eee); + "Unable to perform clear operation : " + eee.getMessage(), eee); } } @@ -1626,23 +1541,29 @@ return queries; } - protected void replicate0(TopiaContextImpl dstContextImpl, - Object... entities) throws TopiaException { + + protected void replicateOnDestinationContext( + TopiaContextImplementor destinationContext, + Object... entities) throws TopiaException { try { -// for (Object entity : entities) { -// // dettach entity to source session, to make possible copy of -// // collection without a hibernate exception (list opened in -// // two session...) -// getEntityManager().detach(entity); -// dstContextImpl.getEntityManager().replicate(entity, -// ReplicationMode.EXCEPTION); -// } + for (Object entity : entities) { + // detach entity from the source session, to make possible copy + // of collection without a hibernate exception (list opened in + // two sessions...) + getEntityManager().detach(entity); + destinationContext.receiveReplicatedEntity(entity); + } } catch (PersistenceException eee) { - throw new TopiaException(_("topia.persistence.error.on.replicate", - eee.getMessage()), eee); + throw new TopiaException( + "An error occurs while a replication operation : " + eee.getMessage(), eee); } } + @Override + public void receiveReplicatedEntity(Object entity) { + getSpecificUtil().receiveReplicatedEntity(this, entity); + } + } //TopiaContextImpl Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java 2012-05-15 10:10:32 UTC (rev 2483) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaContextImplementor.java 2012-05-15 14:47:30 UTC (rev 2484) @@ -134,7 +134,7 @@ * @return the specificUtil instance registered * @see TopiaSpecificUtil */ - TopiaSpecificUtil getSpecificUtil() throws TopiaException; + TopiaSpecificUtil getSpecificUtil(); /** * Used to register the TopiaSpecificUtil. The registered instance will be @@ -145,5 +145,11 @@ */ void registerSpecificUtil(TopiaSpecificUtil specificUtil); + /** + * Method used to receive an entity to replicate + * + * @param entity the entity to replicate + */ + void receiveReplicatedEntity(Object entity); + } //TopiaContextImplementor - Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java 2012-05-15 10:10:32 UTC (rev 2483) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/framework/TopiaSpecificUtil.java 2012-05-15 14:47:30 UTC (rev 2484) @@ -2,6 +2,7 @@ import org.nuiton.topia.TopiaContext; +import org.nuiton.topia.persistence.TopiaEntity; /** * Provides all non-JPA standard methods (specific to an implementation) needed @@ -48,4 +49,14 @@ */ void updateSchema(boolean showSchema); + /** + * Method used by the given TopiaContext when it receives a replicated + * entity + * + * @param context this receiver context + * @param entity the entity to replicate + * @see TopiaContext#replicateEntity(TopiaContext, TopiaEntity) + */ + void receiveReplicatedEntity(TopiaContextImplementor context, Object entity); + } Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2012-05-15 10:10:32 UTC (rev 2483) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/util/TopiaEntityHelper.java 2012-05-15 14:47:30 UTC (rev 2484) @@ -1146,9 +1146,10 @@ String parameterName, Object value) { if (value == null) { - throw new NullPointerException( - _("topia.persistence.error.null.param", - methodName, parameterName)); + String message = String.format( + "The method '%s' requires a non null parameter '%s'.", + methodName, parameterName); + throw new NullPointerException(message); } } Modified: trunk/topia-persistence/src/main/resources/i18n/topia-persistence_en_GB.properties =================================================================== --- trunk/topia-persistence/src/main/resources/i18n/topia-persistence_en_GB.properties 2012-05-15 10:10:32 UTC (rev 2483) +++ trunk/topia-persistence/src/main/resources/i18n/topia-persistence_en_GB.properties 2012-05-15 14:47:30 UTC (rev 2484) @@ -1,29 +1 @@ -topia.persistence.error.context.already.closed=Context was alredy closed -topia.persistence.error.context.is.closed=Context is closed, no operation is possible. -topia.persistence.error.create.schema=Schema could not be created for following reason \: %1$s -topia.persistence.error.empty.doc=Empty document -topia.persistence.error.null.param=The method '%1$s' requires a non null parameter '%2$s'. -topia.persistence.error.on.clear= -topia.persistence.error.on.commit=An error occurs while commit operation \: %1$s -topia.persistence.error.on.export=An error occurs while export operation \: %1$s -topia.persistence.error.on.loding.xml.doc=Could not read xml document for following reason \: %1$s -topia.persistence.error.on.query=An error occurs while query operation \: %1$s -topia.persistence.error.on.replicate=An error occurs while a replication operation \: %s -topia.persistence.error.on.rollback=An error occurs while rollback operation \: %1$s -topia.persistence.error.open.transaction.failed=An error occurs while asking a new transaction \: %1$s -topia.persistence.error.replicate.entity=Could not replicate entity '%1$s' pour following reason \: %2$s -topia.persistence.error.replicate.on.same.context=Can not do a replication operation on same database. -topia.persistence.error.rootContext.access=You are on root context, you MUST open a transaction to perform any database access. -topia.persistence.error.service.not.found=The service %1$s was not found. -topia.persistence.error.service.not.retreaved=Could not retrive service %1$s for following reason \: %2$s -topia.persistence.error.service.unknown=The service %1$s of type %2$s was not found. -topia.persistence.error.unsupported.class=The following entity type %1$s is not managed by this context, you probably forgot to declare it. -topia.persistence.error.unsupported.operation.on.closed.context=This context is closed, it is not possible to release the operation '%1$s' -topia.persistence.error.unsupported.operation.on.root.context= -topia.persistence.error.update.schema=Schema could not be updated for following reason \: %1$s -topia.persistence.service.loaded=Service '%1$s' loaded (implementation %2$s) topia.persistence.supported.classes.for.context=List of supported persistence classes \: %1$s -topia.persistence.warn.service.not.found=The service named '%1$s' could not be found for following reason \: \: %2$s -topia.persistence.warn.service.not.loaded=The service with key '%1$s' has a different name '%2$s' \! (service not activated) -topia.persistence.warn.service.not.postInit=The service named '%1$s' could not be pre-initialized (service not activated) -topia.persistence.warn.service.not.preInit=The service named '%1$s' could not be post-initialized (service not activated) Modified: trunk/topia-persistence/src/main/resources/i18n/topia-persistence_es_ES.properties =================================================================== --- trunk/topia-persistence/src/main/resources/i18n/topia-persistence_es_ES.properties 2012-05-15 10:10:32 UTC (rev 2483) +++ trunk/topia-persistence/src/main/resources/i18n/topia-persistence_es_ES.properties 2012-05-15 14:47:30 UTC (rev 2484) @@ -1,29 +1 @@ -topia.persistence.error.context.already.closed=El contexto ya ha sido cerrado -topia.persistence.error.context.is.closed=Este contexto ya ha sido cerrado, no se puede comenzar una transacción -topia.persistence.error.create.schema=No se puede crear el esquema debido a la razón siguiente \: %2$s -topia.persistence.error.empty.doc=Documento vacío -topia.persistence.error.null.param=El método '%1$s' requiere un parámetro '%2$s' no nulo. -topia.persistence.error.on.clear= -topia.persistence.error.on.commit=Se produjo un error durante el guardado \: %1$s -topia.persistence.error.on.export=Se produjo un error durante la exportación \: %1$s -topia.persistence.error.on.loding.xml.doc=No se puede leer el documento debido a \: %1$s -topia.persistence.error.on.query=Se produjo un error duratne la busqueda (consulta %1$s) \: %2$s -topia.persistence.error.on.replicate=Se produjo un error durante la vuelta a atrás (replication) \: %1$s -topia.persistence.error.on.rollback=Se produjo un error durante la vuelta a atrás (rollback) \: %1$s -topia.persistence.error.open.transaction.failed= -topia.persistence.error.replicate.entity=Error de repliación de la entidad %1$s\ndebido a la siguiente razón \: %2$s -topia.persistence.error.replicate.on.same.context=No se puede duplicar en la misma base -topia.persistence.error.rootContext.access=Está usted en la raíz, debe abrir una transacción para acceder a los datos. -topia.persistence.error.service.not.found=El servicio %1$s no está disponible -topia.persistence.error.service.not.retreaved=El servicio %1$s debido a \: %2$s -topia.persistence.error.service.unknown=El servicio '%1$s' de clase '%2$s' es desconocido -topia.persistence.error.unsupported.class=La clase %1$n no está soportada por TopiaContext, puede que se halla olvidado de añadir su mapa -topia.persistence.error.unsupported.operation.on.closed.context=El contexto está cerrado, no se puede realizar la operación %1$s -topia.persistence.error.unsupported.operation.on.root.context= -topia.persistence.error.update.schema=El esquema no se puede actualizar debido a \: %2$s -topia.persistence.service.loaded=Servicio '%1$s' cargado por '%2$s' -topia.persistence.supported.classes.for.context=Classes supportées par ce TopiaContext \: %1$s -topia.persistence.warn.service.not.found=El nombre del servicio '%1$s' no ha sido encontrado debido a \: %2$s -topia.persistence.warn.service.not.loaded=Le service de clé '%1$s' tiene un nombre de servicio '%2$s' diferente \! (servicio desactivado) -topia.persistence.warn.service.not.postInit=El servicio '%1$s' no puede ser inicializado (servicio desactivado) -topia.persistence.warn.service.not.preInit=El servicio '%1$s' no puede ser inicializado (servicio desactivado) +topia.persistence.supported.classes.for.context=List of supported persistence classes \: %1$s Modified: trunk/topia-persistence/src/main/resources/i18n/topia-persistence_fr_FR.properties =================================================================== --- trunk/topia-persistence/src/main/resources/i18n/topia-persistence_fr_FR.properties 2012-05-15 10:10:32 UTC (rev 2483) +++ trunk/topia-persistence/src/main/resources/i18n/topia-persistence_fr_FR.properties 2012-05-15 14:47:30 UTC (rev 2484) @@ -1,29 +1 @@ -topia.persistence.error.context.already.closed=Ce contexte a deja ete ferme -topia.persistence.error.context.is.closed=Ce contexte a ete ferme, impossible de commencer une transaction -topia.persistence.error.create.schema=Le schéma n'a pas pu être crée pour la raison suivante \: %2$s -topia.persistence.error.empty.doc=Document vide -topia.persistence.error.null.param=La méthode '%1$s' requière un paramètre '%2$s' non null. -topia.persistence.error.on.clear= -topia.persistence.error.on.commit=Une erreur est apparue pendant le commit \: %1$s -topia.persistence.error.on.export=Une erreur est apparue pendant l'export \: %1$s -topia.persistence.error.on.loding.xml.doc=Lecture du document impossible pour la raison suivante \: %1$s -topia.persistence.error.on.query=Une erreur est apparue pendant le recherche (requête %1$s) \: %2$s -topia.persistence.error.on.replicate=Une erreur lors de la réplication est survenue \: %s -topia.persistence.error.on.rollback=Une erreur est apparue pendant le rollback \: %1$s -topia.persistence.error.open.transaction.failed=Une erreur est apparue pendant la demande de transaction \: %1$s -topia.persistence.error.replicate.entity=Echec de replication de l'entite %1$s\npour la raison suivante \: %2$s -topia.persistence.error.replicate.on.same.context=Impossible de dupliquer dans la même base -topia.persistence.error.rootContext.access=Vous êtes sur le root context, vous devez ouvrir une transaction pour pouvoir accéder aux données. -topia.persistence.error.service.not.found=Le service %1$s n'est pas disponible -topia.persistence.error.service.not.retreaved=La service %1$s n'a pas pu être récupéré pour la raison suivante \: %2$s -topia.persistence.error.service.unknown=Le service '%1$s' ayant pour classe '%2$s' n'est pas connu -topia.persistence.error.unsupported.class=La classe %1$s n'est pas supportée par ce TopiaContext, vous avez sans doute oublié d'ajouter son mapping -topia.persistence.error.unsupported.operation.on.closed.context=Ce contexte a été fermé, impossible de réaliser l'opération %1$s -topia.persistence.error.unsupported.operation.on.root.context= -topia.persistence.error.update.schema=Le schéma n'a pas pu être mis à jour pour la raison suivante \: %2$s -topia.persistence.service.loaded=Service '%1$s' chargé par '%2$s' topia.persistence.supported.classes.for.context=Classes supportées par ce TopiaContext \: %1$s -topia.persistence.warn.service.not.found=Le nom du service '%1$s' n'a pas été trouvé pour la raison suivante \: %2$s -topia.persistence.warn.service.not.loaded=Le service de clé '%1$s' a un nom de service '%2$s' différent \! (service désactivé) -topia.persistence.warn.service.not.postInit=Le service '%1$s' n'a pas pu être initialisé (service désactivé) -topia.persistence.warn.service.not.preInit=Le service '%1$s' n'a pas pu être finalisé (service désactivé) Modified: trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernateSpecificUtil.java =================================================================== --- trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernateSpecificUtil.java 2012-05-15 10:10:32 UTC (rev 2483) +++ trunk/topia-persistence-hibernate/src/main/java/org/nuiton/topia/persistence/TopiaHibernateSpecificUtil.java 2012-05-15 14:47:30 UTC (rev 2484) @@ -2,17 +2,24 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.hibernate.ReplicationMode; +import org.hibernate.Session; import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.Configuration; import org.hibernate.dialect.Dialect; +import org.hibernate.ejb.EntityManagerImpl; import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.Table; import org.hibernate.tool.hbm2ddl.DatabaseMetadata; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.hbm2ddl.SchemaUpdate; import org.hibernate.tool.hbm2ddl.TableMetadata; +import org.nuiton.topia.TopiaException; +import org.nuiton.topia.TopiaRuntimeException; +import org.nuiton.topia.framework.TopiaContextImplementor; import org.nuiton.topia.framework.TopiaSpecificUtil; +import javax.persistence.EntityManager; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -117,4 +124,17 @@ return result; } + + @Override + public void receiveReplicatedEntity(TopiaContextImplementor context, Object entity) { + try { + EntityManager entityManager = context.getEntityManager(); + if (entityManager instanceof EntityManagerImpl) { + Session session = ((EntityManagerImpl) entityManager).getSession(); + session.replicate(entity, ReplicationMode.EXCEPTION); + } + } catch (TopiaException te) { + throw new TopiaRuntimeException("Unable to receive replicated entity", te); + } + } }