--- /dev/null
+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;
+ }
+}
--- /dev/null
+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;
+ }
+
+}