CdmApplicationRemoteConfiguration : removed spring bean name hack, since we now have...
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / org / hibernate / proxy / AbstractLazyInitializer.java
index b9068070c858b024527627b692f156f1fc38ce78..d0b8274468a64abfc4c765a0b76f5446b0295298 100644 (file)
@@ -24,6 +24,7 @@
 package org.hibernate.proxy;
 
 import java.io.Serializable;
+import java.util.Set;
 
 import javax.naming.NamingException;
 
@@ -32,13 +33,20 @@ import org.hibernate.LazyInitializationException;
 import org.hibernate.Session;
 import org.hibernate.SessionException;
 import org.hibernate.TransientObjectException;
+import org.hibernate.collection.internal.AbstractPersistentCollection;
 import org.hibernate.engine.spi.EntityKey;
 import org.hibernate.engine.spi.SessionFactoryImplementor;
 import org.hibernate.engine.spi.SessionImplementor;
 import org.hibernate.internal.SessionFactoryRegistry;
 import org.hibernate.persister.entity.EntityPersister;
 import org.jboss.logging.Logger;
+import org.springframework.beans.factory.annotation.Autowire;
 import org.springframework.beans.factory.annotation.Configurable;
+import org.springframework.stereotype.Component;
+
+import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
+import eu.etaxonomy.cdm.api.service.ICommonService;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 
 /**
  * Convenience base class for lazy initialization handlers.  Centralizes the basic plumbing of doing lazy
@@ -47,7 +55,8 @@ import org.springframework.beans.factory.annotation.Configurable;
  *
  * @author Gavin King
  */
-@Configurable(dependencyCheck = true)
+@Component
+@Configurable(dependencyCheck = true,autowire = Autowire.BY_TYPE)
 public abstract class AbstractLazyInitializer implements LazyInitializer {
        private static final Logger log = Logger.getLogger( AbstractLazyInitializer.class );
 
@@ -158,6 +167,11 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
 
        @Override
        public final void initialize() throws HibernateException {
+               // 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 ) {
                        if ( specjLazyLoad ) {
                                specialSpecjInitialization();
@@ -407,4 +421,49 @@ public abstract class AbstractLazyInitializer implements LazyInitializer {
        public void setUnwrap(boolean unwrap) {
                this.unwrap = unwrap;
        }
+       
+       /** Below is section of code which makes remote service calls */
+       
+       private static ICdmApplicationConfiguration configuration;
+       private static boolean remoting = false;
+       
+       public static void setConfiguration(ICdmApplicationConfiguration conf) {
+               configuration = conf;
+               Boolean isRemoting = (Boolean)configuration.getBean("isRemoting");
+               if(isRemoting != null) {
+                       remoting = isRemoting.booleanValue();
+               } else {
+                       remoting = false;
+               }
+       }
+       
+       
+       private void remoteInitialize() {               
+               
+               if(!initialized) {                              
+                       int classid = ((Integer)getIdentifier()).intValue();
+                       log.debug("--> Remote Lazy Initializing" + getEntityName() + " with id " + classid);
+                       Class clazz;
+                       try {
+                               clazz = (Class<? extends CdmBase>) Class.forName(getEntityName());
+                       } catch (ClassNotFoundException e) {
+                               throw new HibernateException("Class for " + getEntityName() + " not found", e);
+                       }
+                       if(configuration == null) {
+                               throw new HibernateException("CdmApplicationRemoteConfiguration not initialized (null)");
+                       }
+                       ICommonService commonService = configuration.getCommonService();
+                       if(commonService == null) {
+                               throw new HibernateException("commonService not initialized (null)");
+                       }
+                       
+                       CdmBase cdmBase = CdmBase.deproxy(commonService.find(clazz,classid),clazz);
+                       setImplementation(cdmBase);
+                       
+               }
+       }
+       
+       public static boolean isInitialized(AbstractLazyInitializer obj) {
+               return obj.initialized;
+       }
 }