Merge branch 'develop' into ticket-5066
authorAndreas Müller <a.mueller@bgbm.org>
Wed, 7 Oct 2015 11:39:59 +0000 (13:39 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Wed, 7 Oct 2015 11:39:59 +0000 (13:39 +0200)
Conflicts:
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/common/Language.java
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmListenerIntegrator.java

1  2 
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/common/CdmEntityDaoBase.java
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/CdmListenerIntegrator.java
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/PostMergeEntityListener.java

index 7d68ca22fd13c9f728f17fdf1b9253de92b0df9f,321887900c3f41a2134226e594120bdb5a7b8e8b..44d11a4af7f1e26098771cc6ea88f94b6c48025a
@@@ -42,14 -42,19 +42,14 @@@ import org.hibernate.envers.query.Audit
  import org.hibernate.metadata.ClassMetadata;\r
  import org.hibernate.search.FullTextQuery;\r
  import org.hibernate.type.Type;\r
 -import org.joda.time.DateTime;\r
  import org.springframework.beans.factory.annotation.Autowired;\r
  import org.springframework.dao.DataAccessException;\r
  import org.springframework.dao.InvalidDataAccessApiUsageException;\r
 -import org.springframework.security.core.Authentication;\r
 -import org.springframework.security.core.context.SecurityContextHolder;\r
  import org.springframework.stereotype.Repository;\r
  import org.springframework.util.ReflectionUtils;\r
  \r
  import eu.etaxonomy.cdm.config.Configuration;\r
  import eu.etaxonomy.cdm.model.common.CdmBase;\r
 -import eu.etaxonomy.cdm.model.common.User;\r
 -import eu.etaxonomy.cdm.model.common.VersionableEntity;\r
  import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;\r
  import eu.etaxonomy.cdm.persistence.dao.initializer.IBeanInitializer;\r
  import eu.etaxonomy.cdm.persistence.hibernate.replace.ReferringObjectMetadata;\r
@@@ -240,11 -245,21 +240,24 @@@ public abstract class CdmEntityDaoBase<
          if (logger.isDebugEnabled()){logger.debug("dao clear end");}\r
      }\r
  \r
+     @Override\r
+     public T merge(T transientObject, boolean returnTransientEntity) throws DataAccessException {\r
+         T persistentObject = merge(transientObject);\r
+         if(returnTransientEntity) {\r
+             if(transientObject != null && persistentObject != null) {\r
+                 transientObject.setId(persistentObject.getId());\r
+             }\r
+             return transientObject;\r
+         } else {\r
+             return persistentObject;\r
+         }\r
+     }\r
\r
      @Override\r
      public T merge(T transientObject) throws DataAccessException {\r
 +        if(transientObject == null) {\r
 +            return null;\r
 +        }\r
          Session session = getSession();\r
          @SuppressWarnings("unchecked")\r
                T persistentObject = (T)session.merge(transientObject);\r
              if (logger.isDebugEnabled()){logger.debug("dao saveOrUpdate start...");}\r
              if (logger.isDebugEnabled()){logger.debug("transientObject(" + transientObject.getClass().getSimpleName() + ") ID:" + transientObject.getId() + ", UUID: " + transientObject.getUuid()) ;}\r
              Session session = getSession();\r
 -            if(transientObject.getId() != 0 && VersionableEntity.class.isAssignableFrom(transientObject.getClass())) {\r
 -                VersionableEntity versionableEntity = (VersionableEntity)transientObject;\r
 -                versionableEntity.setUpdated(new DateTime());\r
 -                Authentication authentication = SecurityContextHolder.getContext().getAuthentication();\r
 -                if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {\r
 -                  User user = (User)authentication.getPrincipal();\r
 -                  versionableEntity.setUpdatedBy(user);\r
 -                }\r
 -            }\r
              session.saveOrUpdate(transientObject);\r
              if (logger.isDebugEnabled()){logger.debug("dao saveOrUpdate end");}\r
              return transientObject.getUuid();\r
          // I think this is preferable to catching lazy initialization errors\r
          // as that solution only swallows and hides the exception, but doesn't\r
          // actually solve it.\r
-         persistentObject = (T) getSession().merge(persistentObject);\r
+        persistentObject = (T) getSession().merge(persistentObject);\r
          getSession().delete(persistentObject);\r
          return persistentObject.getUuid();\r
      }\r
index a822f06f9a3e0e63cb4105dcc153d5b92f99558a,1cc5976f99ed07cefd4a45f789a2e2d4e3fb8ab9..361db77875796a38e73fb39d05aeb161d7af9247
@@@ -47,10 -47,10 +47,16 @@@ public class CdmListenerIntegrator impl
  \r
                // prepend to register before or append to register after\r
                // this example will register a persist event listener\r
 -              eventRegistry.prependListeners(EventType.SAVE, new CacheStrategyGenerator(), new SaveEntityListener());\r
 +              //eventRegistry.prependListeners(EventType.SAVE, new CacheStrategyGenerator(), new SaveEntityListener());\r
                eventRegistry.prependListeners(EventType.UPDATE, new CacheStrategyGenerator(), new UpdateEntityListener());\r
 -              eventRegistry.prependListeners(EventType.SAVE_UPDATE, new CacheStrategyGenerator(), new SaveOrUpdateEntityListener());\r
++
 +              eventRegistry.prependListeners(EventType.SAVE_UPDATE, new CacheStrategyGenerator(), new SaveOrUpdateorMergeEntityListener());\r
 +              eventRegistry.prependListeners(EventType.MERGE, new CacheStrategyGenerator(), new SaveOrUpdateorMergeEntityListener());\r
++
++//            eventRegistry.prependListeners(EventType.SAVE_UPDATE, new CacheStrategyGenerator(), new SaveOrUpdateEntityListener());\r
++              //TODO Cherian move to SaveOrUpdateorMergeEntityListener\r
+               eventRegistry.appendListeners(EventType.MERGE, new PostMergeEntityListener());\r
++
                eventRegistry.appendListeners(EventType.DELETE, new CdmDeleteListener());\r
                eventRegistry.appendListeners(EventType.POST_LOAD, new CdmPostDataChangeObservableListener());\r
  //with validation\r
@@@ -61,9 -61,6 +67,9 @@@
                eventRegistry.appendListeners(EventType.POST_INSERT, new CdmPostDataChangeObservableListener());\r
                eventRegistry.appendListeners(EventType.POST_UPDATE, new CdmPostDataChangeObservableListener());\r
                eventRegistry.appendListeners(EventType.POST_DELETE, new CdmPostDataChangeObservableListener());\r
 +\r
 +              eventRegistry.appendListeners(EventType.PRE_INSERT, new CdmPreDataChangeObservableListener());\r
 +              eventRegistry.appendListeners(EventType.PRE_UPDATE, new CdmPreDataChangeObservableListener());\r
        }\r
  \r
  \r
index 0000000000000000000000000000000000000000,3d7311cfdc8e02fdd647614a2b8a903c89f77196..347c1101b19f358f0ac4697914f2ff928d5172c4
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,59 +1,53 @@@
 -
 -    /* (non-Javadoc)
 -     * @see org.hibernate.event.spi.MergeEventListener#onMerge(org.hibernate.event.spi.MergeEvent)
 -     */
+ // $Id$
+ /**
+  * Copyright (C) 2015 EDIT
+  * European Distributed Institute of Taxonomy
+  * http://www.e-taxonomy.eu
+  *
+  * The contents of this file are subject to the Mozilla Public License Version 1.1
+  * See LICENSE.TXT at the top of this package for the full license terms.
+  */
+ package eu.etaxonomy.cdm.persistence.hibernate;
+ import java.util.Map;
+ import org.hibernate.Hibernate;
+ import org.hibernate.HibernateException;
+ import org.hibernate.event.spi.MergeEvent;
+ import org.hibernate.event.spi.MergeEventListener;
+ import eu.etaxonomy.cdm.model.common.CdmBase;
+ /**
+  * @author cmathew
+  * @date 23 Sep 2015
+  *
+  */
+ public class PostMergeEntityListener implements MergeEventListener {
++    private static final long serialVersionUID = 1565797119368313987L;
 -    /* (non-Javadoc)
 -     * @see org.hibernate.event.spi.MergeEventListener#onMerge(org.hibernate.event.spi.MergeEvent, java.util.Map)
 -     */
+     @Override
+     public void onMerge(MergeEvent event) throws HibernateException {
+     }
+     @Override
+     public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
+         // at this point the original entity to merge has already been copied to the result
+         // => the result is an exact copy of the original EXCEPT or the the id which is set by hibernate
+         // the following code sets the id in the original entity so that it can be used as a return value
+         // for the CdmEntityDaoBase.merge(T transientObject, boolean returnTransientEntity) call
+         if(event.getOriginal() != null && CdmBase.class.isAssignableFrom(event.getOriginal().getClass()) &&
+                 event.getResult() != null && CdmBase.class.isAssignableFrom(event.getResult().getClass())) {
+             CdmBase original = (CdmBase) event.getOriginal();
+             CdmBase result = (CdmBase) event.getResult();
+             if(original != null && Hibernate.isInitialized(original) && original.getId() == 0 &&
+                     result != null && Hibernate.isInitialized(result)) {
+                 original.setId(result.getId());
+             }
+         }
+     }
+ }