Introduce light DAO support
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jxl / src / main / java / org / argeo / server / jxl / dao / JxlDaoSupport.java
index 0960b6c42ccff392b17c3c325f95d51e55d94052..0488d519815896da54ccde15f204a2860e12bb02 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeMap;
 
 import jxl.Cell;
 import jxl.FormulaCell;
@@ -13,17 +14,23 @@ import jxl.JXLException;
 import jxl.Sheet;
 import jxl.Workbook;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.server.ArgeoServerException;
+import org.argeo.server.dao.LightDaoSupport;
 import org.springframework.beans.BeanWrapper;
 import org.springframework.beans.BeanWrapperImpl;
 import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.generic.GenericBeanFactoryAccessor;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationContextAware;
+import org.springframework.core.io.Resource;
 import org.springframework.util.StringUtils;
 
-public class JxlDaoSupport implements ApplicationContextAware {
+public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware,
+               InitializingBean {
        private final static Log log = LogFactory.getLog(JxlDaoSupport.class);
 
        private ClassLoader classLoader = getClass().getClassLoader();
@@ -35,6 +42,21 @@ public class JxlDaoSupport implements ApplicationContextAware {
 
        private List<String> scannedPackages = new ArrayList<String>();
 
+       private List<Resource> workbooks = new ArrayList<Resource>();
+
+       public void afterPropertiesSet() throws Exception {
+               for (Resource res : workbooks) {
+                       InputStream in = null;
+                       try {
+                               in = res.getInputStream();
+                               load(in);
+                       } finally {
+                               IOUtils.closeQuietly(in);
+                       }
+               }
+
+       }
+
        public void load(InputStream in) {
                try {
                        // used to resolve inner references
@@ -67,7 +89,7 @@ public class JxlDaoSupport implements ApplicationContextAware {
                Cell[] firstRow = sheet.getRow(0);
 
                Class<?> clss = findClassToInstantiate(sheet);
-               model.put(clss, new HashMap<Object, Object>());
+               model.put(clss, new TreeMap<Object, Object>());
 
                tempRefs.put(sheet.getName(), new ArrayList<Object>());
 
@@ -184,10 +206,12 @@ public class JxlDaoSupport implements ApplicationContextAware {
 
                scannedPkgs: for (String pkg : scannedPackages) {
                        try {
-                               clss = classLoader.loadClass(pkg + "." + className);
+                               clss = classLoader.loadClass(pkg.trim() + "." + className);
                                break scannedPkgs;
                        } catch (ClassNotFoundException e) {
                                // silent
+                               if (log.isTraceEnabled())
+                                       log.trace(e.getMessage());
                        }
                }
 
@@ -203,10 +227,36 @@ public class JxlDaoSupport implements ApplicationContextAware {
                return (T) model.get(findClass(clss)).get(key);
        }
 
+       /**
+        * Slow.
+        * 
+        * @return the first found
+        */
+       public <T> T getByField(Class<T> clss, String field, Object value) {
+               List<T> all = list(clss, null);
+               T res = null;
+               for (T obj : all) {
+                       if (new BeanWrapperImpl(obj).getPropertyValue(field).equals(value)) {
+                               res = obj;
+                               break;
+                       }
+               }
+               return res;
+       }
+
        @SuppressWarnings("unchecked")
        public <T> List<T> list(Class<T> clss, Object filter) {
-               return new ArrayList<T>((Collection<T>) model.get(findClass(clss))
-                               .values());
+               List<T> res = new ArrayList<T>();
+
+               Class classToUse = findClass(clss);
+               if (classToUse != null)
+                       res.addAll((Collection<T>) model.get(classToUse).values());
+
+               if (applicationContext != null)
+                       res.addAll(new GenericBeanFactoryAccessor(applicationContext)
+                                       .getBeansOfType(clss).values());
+
+               return res;
        }
 
        @SuppressWarnings("unchecked")
@@ -218,8 +268,7 @@ public class JxlDaoSupport implements ApplicationContextAware {
                        if (parent.isAssignableFrom(clss))
                                return clss;// return the first found
                }
-               throw new ArgeoServerException("No implementing class found for "
-                               + parent);
+               return null;
        }
 
        public void setApplicationContext(ApplicationContext applicationContext)
@@ -243,6 +292,18 @@ public class JxlDaoSupport implements ApplicationContextAware {
                return scannedPackages;
        }
 
+       public void setWorkbooks(List<Resource> workbooks) {
+               this.workbooks = workbooks;
+       }
+
+       public List<Resource> getWorkbooks() {
+               return workbooks;
+       }
+
+       public void setClassLoader(ClassLoader classLoader) {
+               this.classLoader = classLoader;
+       }
+
        public static class Reference {
                private Object object;
                private String property;