Introduce argeo server hibernate
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 13 Oct 2009 14:33:12 +0000 (14:33 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 13 Oct 2009 14:33:12 +0000 (14:33 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@3020 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

server/runtime/org.argeo.server.hibernate/.classpath [new file with mode: 0644]
server/runtime/org.argeo.server.hibernate/.project [new file with mode: 0644]
server/runtime/org.argeo.server.hibernate/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
server/runtime/org.argeo.server.hibernate/.settings/org.maven.ide.eclipse.prefs [new file with mode: 0644]
server/runtime/org.argeo.server.hibernate/pom.xml
server/runtime/org.argeo.server.hibernate/src/main/java/org/argeo/server/hibernate/HibernateLightDaoSync.java [new file with mode: 0644]
server/runtime/org.argeo.server.hibernate/src/main/java/org/argeo/server/hibernate/LightDaoInterceptor.java [new file with mode: 0644]

diff --git a/server/runtime/org.argeo.server.hibernate/.classpath b/server/runtime/org.argeo.server.hibernate/.classpath
new file mode 100644 (file)
index 0000000..16f01e2
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="src" output="target/classes" path="src/main/java"/>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
+       <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+       <classpathentry kind="output" path="target/classes"/>
+</classpath>
diff --git a/server/runtime/org.argeo.server.hibernate/.project b/server/runtime/org.argeo.server.hibernate/.project
new file mode 100644 (file)
index 0000000..59718cb
--- /dev/null
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.argeo.server.hibernate</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.maven.ide.eclipse.maven2Builder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.maven.ide.eclipse.maven2Nature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/server/runtime/org.argeo.server.hibernate/.settings/org.eclipse.jdt.core.prefs b/server/runtime/org.argeo.server.hibernate/.settings/org.eclipse.jdt.core.prefs
new file mode 100644 (file)
index 0000000..405317e
--- /dev/null
@@ -0,0 +1,5 @@
+#Tue Oct 13 14:51:57 CEST 2009
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.source=1.5
diff --git a/server/runtime/org.argeo.server.hibernate/.settings/org.maven.ide.eclipse.prefs b/server/runtime/org.argeo.server.hibernate/.settings/org.maven.ide.eclipse.prefs
new file mode 100644 (file)
index 0000000..c523f7b
--- /dev/null
@@ -0,0 +1,9 @@
+#Tue Oct 13 14:51:50 CEST 2009
+activeProfiles=
+eclipse.preferences.version=1
+fullBuildGoals=process-test-resources
+includeModules=false
+resolveWorkspaceProjects=true
+resourceFilterGoals=process-resources resources\:testResources
+skipCompilerPlugin=true
+version=1
index f886e74fb58513079e23149b007decdb616563a7..da508fc8b720d36616de27d140901bc0859670f5 100644 (file)
                </plugins>
        </build>
        <dependencies>
+               <dependency>
+                       <groupId>org.argeo.commons.server</groupId>
+                       <artifactId>org.argeo.server.core</artifactId>
+                       <version>0.1.1-SNAPSHOT</version>
+               </dependency>
+
                <!-- Hibernate -->
                <dependency>
                        <groupId>org.argeo.commons.server</groupId>
diff --git a/server/runtime/org.argeo.server.hibernate/src/main/java/org/argeo/server/hibernate/HibernateLightDaoSync.java b/server/runtime/org.argeo.server.hibernate/src/main/java/org/argeo/server/hibernate/HibernateLightDaoSync.java
new file mode 100644 (file)
index 0000000..ebc7376
--- /dev/null
@@ -0,0 +1,66 @@
+package org.argeo.server.hibernate;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.server.dao.LightDaoSupport;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+
+public class HibernateLightDaoSync {
+       private final static Log log = LogFactory
+                       .getLog(HibernateLightDaoSync.class);
+
+       private String externalSuffix = LightDaoInterceptor.DEFAULT_EXTERNAL_SUFFIX;
+
+       private SessionFactory sessionFactory;
+
+       private LightDaoSupport lightDaoSupport;
+
+       private List<Class<?>> classes = new ArrayList<Class<?>>();
+
+       public void sync() {
+               List<Class<?>> lst;
+               if (classes.size() > 0)
+                       lst = classes;
+               else
+                       lst = lightDaoSupport.getSupportedClasses();
+
+               Session session = sessionFactory.getCurrentSession();
+               session.beginTransaction();
+               try {
+                       for (Class<?> clss : lst) {
+                               String entityName = clss.getSimpleName() + externalSuffix;
+                               int count = 0;
+                               for (Object obj : lightDaoSupport.list(clss, null)) {
+                                       session.save(entityName, obj);
+                                       count++;
+                               }
+                               if (log.isDebugEnabled())
+                                       log.debug("Synchronized " + count + "\tentities '"
+                                                       + entityName + "'");
+                       }
+                       session.getTransaction().commit();
+               } catch (Exception e) {
+                       session.getTransaction().rollback();
+               }
+       }
+
+       public void setClasses(List<Class<?>> classes) {
+               this.classes = classes;
+       }
+
+       public void setExternalSuffix(String externalSuffix) {
+               this.externalSuffix = externalSuffix;
+       }
+
+       public void setSessionFactory(SessionFactory sessionFactory) {
+               this.sessionFactory = sessionFactory;
+       }
+
+       public void setLightDaoSupport(LightDaoSupport lightDaoSupport) {
+               this.lightDaoSupport = lightDaoSupport;
+       }
+}
diff --git a/server/runtime/org.argeo.server.hibernate/src/main/java/org/argeo/server/hibernate/LightDaoInterceptor.java b/server/runtime/org.argeo.server.hibernate/src/main/java/org/argeo/server/hibernate/LightDaoInterceptor.java
new file mode 100644 (file)
index 0000000..9f3ba09
--- /dev/null
@@ -0,0 +1,116 @@
+package org.argeo.server.hibernate;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.server.dao.LightDaoSupport;
+import org.hibernate.EmptyInterceptor;
+import org.hibernate.type.Type;
+import org.springframework.beans.BeanWrapper;
+import org.springframework.beans.BeanWrapperImpl;
+
+public class LightDaoInterceptor extends EmptyInterceptor {
+       private final static Log log = LogFactory.getLog(LightDaoInterceptor.class);
+
+       private static final long serialVersionUID = 1L;
+
+       public final static String DEFAULT_EXTERNAL_SUFFIX = "_external";
+
+       private String externalSuffix = DEFAULT_EXTERNAL_SUFFIX;
+
+       private LightDaoSupport lightDaoSupport;
+
+       private List<Class<?>> classes = new ArrayList<Class<?>>();
+
+       private Map<Class<?>, String> businessIdFields = new HashMap<Class<?>, String>();
+
+       /** internal */
+       private final Map<Class<?>, Map<Serializable, Object>> bidMappings = new HashMap<Class<?>, Map<Serializable, Object>>();
+
+       @Override
+       public Object getEntity(String entityName, Serializable id) {
+               Class<?> clss = findSupportingClass(entityName);
+               if (clss != null) {
+                       if (businessIdFields.containsKey(clss))
+                               return lightDaoSupport.getByField(clss, businessIdFields
+                                               .get(clss), bidMappings.get(clss).get(id));
+                       else
+                               return lightDaoSupport.getByKey(clss, id);
+               } else {
+                       return super.getEntity(entityName, id);
+               }
+       }
+
+       @Override
+       public String getEntityName(Object object) {
+               if (supports(object)) {
+                       return toExternalName(object.getClass());
+               } else {
+                       return super.getEntityName(object);
+               }
+       }
+
+       @Override
+       public boolean onSave(Object entity, Serializable id, Object[] state,
+                       String[] propertyNames, Type[] types) {
+               if (supports(entity)) {
+                       Class<?> clss = entity.getClass();
+                       if (businessIdFields.containsKey(clss)) {
+                               if (!bidMappings.containsKey(clss))
+                                       bidMappings.put(clss, new HashMap<Serializable, Object>());
+                               BeanWrapper bw = new BeanWrapperImpl(entity);
+                               Object bid = bw.getPropertyValue(businessIdFields.get(clss));
+                               bidMappings.get(clss).put(id, bid);
+                               if (log.isDebugEnabled())
+                                       log.debug("Mapped tid " + id + " with bid " + bid + " for "
+                                                       + clss);
+                       }
+               }
+               return super.onSave(entity, id, state, propertyNames, types);
+       }
+
+       protected Boolean supports(Object object) {
+               if (classes.contains(object.getClass()))
+                       return lightDaoSupport.getSupportedClasses().contains(
+                                       object.getClass());
+               else
+                       return false;
+       }
+
+       /** @return null if not found */
+       protected Class<?> findSupportingClass(String entityName) {
+               for (Class<?> clss : lightDaoSupport.getSupportedClasses()) {
+                       if (toExternalName(clss).equals(entityName)) {
+                               if (classes.contains(clss))
+                                       return clss;
+                       }
+               }
+               return null;
+       }
+
+       protected final String toExternalName(Class<?> clss) {
+               return clss.getSimpleName() + externalSuffix;
+       }
+
+       public void setExternalSuffix(String externalSuffix) {
+               this.externalSuffix = externalSuffix;
+       }
+
+       public void setLightDaoSupport(LightDaoSupport lightDaoSupport) {
+               this.lightDaoSupport = lightDaoSupport;
+       }
+
+       public void setClasses(List<Class<?>> classes) {
+               this.classes = classes;
+       }
+
+       public void setBusinessIdFields(Map<Class<?>, String> businessIdFields) {
+               this.businessIdFields = businessIdFields;
+       }
+
+}