Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / org / hibernate / collection / internal / AbstractPersistentCollection.java
index 273b2b4133c5041fb0033592431e62740428a3e4..f2debccfeaae80793d8ff61b1984d24efa5c8c38 100644 (file)
@@ -60,9 +60,10 @@ import org.hibernate.type.Type;
 import org.jboss.logging.Logger;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
-import eu.etaxonomy.cdm.api.cache.CachedCommonServiceImpl.CollectionField;
-import eu.etaxonomy.cdm.api.cache.ICachedCommonService;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.taxeditor.remoting.CdmEagerLoadingException;
+import eu.etaxonomy.taxeditor.remoting.cache.ProxyUtils;
+import eu.etaxonomy.taxeditor.service.ICachedCommonService;
 
 /**
  * Base class implementing {@link org.hibernate.collection.spi.PersistentCollection}
@@ -592,26 +593,25 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
         * @throws LazyInitializationException if we cannot initialize
         */
        protected final void initialize(final boolean writing) {
-               // In remoting we are sure that session is null
-               // both when using property paths and switching off conversations
-               if(session == null && remoting) {
-                       remoteInitialize();
-               }
-
-               if ( initialized ) {
-                       return;
-               }
-
-
-               withTemporarySessionIfNeeded(
-                               new LazyInitializationWork<Object>() {
-                                       @Override
-                                       public Object doWork() {
-                                               session.initializeCollection( AbstractPersistentCollection.this, writing );
-                                               return null;
-                                       }
-                               }
-               );
+           if ( initialized ) {
+               return;
+           }
+
+           // In remoting we are sure that session is null
+           // both when using property paths and switching off conversations
+           if(session == null && remoting) {
+               remoteInitialize();
+           } else {
+               withTemporarySessionIfNeeded(
+                       new LazyInitializationWork<Object>() {
+                           @Override
+                           public Object doWork() {
+                               session.initializeCollection( AbstractPersistentCollection.this, writing );
+                               return null;
+                           }
+                       }
+                       );
+           }
        }
 
        private void throwLazyInitializationExceptionIfNotConnected() {
@@ -1296,9 +1296,14 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                        try {
                                String role = getRole();
                                String fieldName = role.substring(role.lastIndexOf(".") + 1);
-                               log.info("--> Remote Lazy Initializing Collection " + getRole() + " , key : " + getKey() + " , field : " + fieldName);
+                               log.info("--> Remote Lazy Initializing Collection " + getRole() + " , owner : " + getOwner().getClass() + "/" + getKey() + " , field : " + fieldName);
                                Object owner = getOwner();
-
+                               CdmBase cdmBase;
+                               if(owner instanceof CdmBase) {
+                                   cdmBase = (CdmBase)owner;
+                               } else {
+                                   throw new HibernateException("Owner of persistent collection is not a cdm entity");
+                               }
                                if(configuration == null) {
                                        throw new HibernateException("CdmApplicationRemoteConfiguration not initialized (null)");
                                }
@@ -1307,16 +1312,23 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
                                        throw new HibernateException("commonService not initialized (null)");
                                }
 
-                               PersistentCollection col = cachedCommonService.initializeCollection(this);
+                               //Object obj = ProxyUtils.deproxy(cachedCommonService.initializeCollection(this));
+                               Object obj = ProxyUtils.deproxy(cachedCommonService.initializeCollection(cdmBase.getUuid(), fieldName));
+                               if(ProxyUtils.isProxy(obj)) {
+                                   throw new HibernateException("Persistent Collection initialized but is still a proxy");
+                               }
                                afterInitialize();
 
                                Class<?> clazz = getClass();
                                if (clazz != null) {
-                                       CollectionField cf = cachedCommonService.getCollectionField(col);
-                                       cachedCommonService.updatePersistentCollection(cf);
-                                       Field field = clazz.getDeclaredField(cf.getType().toString());
+                                       //CollectionField cf = cachedCommonService.getCollectionField(col);
+                                       //cachedCommonService.updatePersistentCollection(cf);
+                                   Object collectionType = ProxyUtils.getCollectionType(obj);
+                                       Field field = clazz.getDeclaredField(collectionType.toString());
                                        field.setAccessible(true);
-                                       field.set(this, cf.getCollection());
+                                       field.set(this, obj);
+                                       ProxyUtils.setRoleValueInOwner(owner, role, obj);
+
                                }
                        } catch (Exception ex) {
                                throw new CdmEagerLoadingException(ex);