X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=server%2Fruntime%2Forg.argeo.server.jxl%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fserver%2Fjxl%2Fdao%2FJxlDaoSupport.java;h=7b4110d8e487e31428398d37ccc7791f4dca5092;hb=c5bb48d71ad5d71e389137e5fb95f96d9005a942;hp=ef3dcf04b3de3532958278c69ee4befaf0a601d3;hpb=1e15cc14a83ef43e453709d04cdb13e3b0269cc1;p=lgpl%2Fargeo-commons.git diff --git a/server/runtime/org.argeo.server.jxl/src/main/java/org/argeo/server/jxl/dao/JxlDaoSupport.java b/server/runtime/org.argeo.server.jxl/src/main/java/org/argeo/server/jxl/dao/JxlDaoSupport.java index ef3dcf04b..7b4110d8e 100644 --- a/server/runtime/org.argeo.server.jxl/src/main/java/org/argeo/server/jxl/dao/JxlDaoSupport.java +++ b/server/runtime/org.argeo.server.jxl/src/main/java/org/argeo/server/jxl/dao/JxlDaoSupport.java @@ -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; @@ -17,16 +18,19 @@ 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, InitializingBean { +public class JxlDaoSupport implements LightDaoSupport, ApplicationContextAware, + InitializingBean { private final static Log log = LogFactory.getLog(JxlDaoSupport.class); private ClassLoader classLoader = getClass().getClassLoader(); @@ -41,37 +45,42 @@ public class JxlDaoSupport implements ApplicationContextAware, InitializingBean private List workbooks = new ArrayList(); public void afterPropertiesSet() throws Exception { + init(); + } + + public void init() { + // used to resolve inner references + Map> tempRefs = new HashMap>(); + + List references = new ArrayList(); + for (Resource res : workbooks) { InputStream in = null; try { in = res.getInputStream(); - load(in); + load(in, references, tempRefs); + } catch (Exception e) { + throw new ArgeoServerException("Cannot load stream", e); } finally { IOUtils.closeQuietly(in); } } + // Inject references + for (Reference ref : references) { + injectReference(ref, tempRefs); + } + if (log.isDebugEnabled()) + log.debug(references.size() + " references linked"); } - public void load(InputStream in) { + public void load(InputStream in, List references, + Map> tempRefs) { try { - // used to resolve inner references - Map> tempRefs = new HashMap>(); - - List references = new ArrayList(); - Workbook workbook = Workbook.getWorkbook(in); - for (Sheet sheet : workbook.getSheets()) { loadSheet(sheet, references, tempRefs); } - - for (Reference ref : references) { - injectReference(ref, tempRefs); - } - if (log.isDebugEnabled()) - log.debug(references.size() + " references linked"); - } catch (Exception e) { throw new ArgeoServerException("Cannot load workbook", e); } @@ -85,7 +94,7 @@ public class JxlDaoSupport implements ApplicationContextAware, InitializingBean Cell[] firstRow = sheet.getRow(0); Class clss = findClassToInstantiate(sheet); - model.put(clss, new HashMap()); + model.put(clss, new TreeMap()); tempRefs.put(sheet.getName(), new ArrayList()); @@ -126,6 +135,10 @@ public class JxlDaoSupport implements ApplicationContextAware, InitializingBean if (cell instanceof FormulaCell) { String formula = ((FormulaCell) cell).getFormula(); int index = formula.indexOf('!'); + if (index < 0) + throw new ArgeoServerException("Cannot interpret formula " + + formula); + ; String targetSheet = formula.substring(0, index); // assume no double letters!! String targetRowStr = formula.substring(index + 2); @@ -223,10 +236,36 @@ public class JxlDaoSupport implements ApplicationContextAware, InitializingBean return (T) model.get(findClass(clss)).get(key); } + /** + * Slow. + * + * @return the first found + */ + public T getByField(Class clss, String field, Object value) { + List 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 List list(Class clss, Object filter) { - return new ArrayList((Collection) model.get(findClass(clss)) - .values()); + List res = new ArrayList(); + + Class classToUse = findClass(clss); + if (classToUse != null) + res.addAll((Collection) model.get(classToUse).values()); + + if (applicationContext != null) + res.addAll(new GenericBeanFactoryAccessor(applicationContext) + .getBeansOfType(clss).values()); + + return res; } @SuppressWarnings("unchecked") @@ -238,8 +277,7 @@ public class JxlDaoSupport implements ApplicationContextAware, InitializingBean 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)