- if (log.isTraceEnabled())
- log.debug("Instantiate sheet " + sheet.getName());
-
- Cell[] firstRow = sheet.getRow(0);
-
- // TODO: ability to map sheet names and class names
- String className = sheet.getName();
- Class<?> clss = classLoader.loadClass(className);
- model.put(clss, new HashMap<Object, Object>());
-
- tempRefs.put(sheet.getName(), new ArrayList<Object>());
-
- String keyProperty = firstRow[0].getContents();
- for (int row = 1; row < sheet.getRows(); row++) {
- if (log.isTraceEnabled())
- log.trace(" row " + row);
-
- Cell[] currentRow = sheet.getRow(row);
- BeanWrapper bw = new BeanWrapperImpl(clss);
- for (int col = 0; col < firstRow.length; col++) {
- String pName = firstRow[col].getContents();
-
- Cell cell = currentRow[col];
- if (cell instanceof FormulaCell) {
- String formula = ((FormulaCell) cell).getFormula();
- int index = formula.indexOf('!');
- String targetSheet = formula.substring(0, index);
- // assume no double letters!!
- String targetRowStr = formula.substring(index + 2);
- if (targetRowStr.charAt(0) == '$')
- targetRowStr = targetRowStr.substring(1);
- Integer targetRow = Integer.parseInt(targetRowStr);
- links.add(new Reference(bw.getWrappedInstance(),
- pName, targetSheet, targetRow));
-
- if (log.isTraceEnabled())
- log.debug(" formula: " + formula
- + " | content: " + cell.getContents()
- + " | targetSheet=" + targetSheet
- + ", targetRow=" + targetRow);
- } else {
- String contents = cell.getContents();
- if (pName.equals(keyProperty)
- && !StringUtils.hasText(contents)) {
- // auto allocate key column if empty
- contents = Integer.toString(row);
- }
-
- if (pName.charAt(0) == '#') {// externalRef
- links.add(new Reference(
- bw.getWrappedInstance(), pName
- .substring(1), contents));
- } else {
- bw.setPropertyValue(pName, contents);
- }
-
- if (log.isTraceEnabled())
- log.debug(" " + pName + "=" + contents);
- }
- }// properties set
-
- model.get(clss).put(bw.getPropertyValue(keyProperty),
- bw.getWrappedInstance());
- tempRefs.get(sheet.getName()).add(bw.getWrappedInstance());
+ loadSheet(sheet, references, tempRefs);
+ }
+ } catch (Exception e) {
+ throw new ArgeoServerException("Cannot load workbook", e);
+ }
+ }
+
+ protected void loadSheet(Sheet sheet, List<Reference> references,
+ Map<String, List<Object>> tempRefs) throws JXLException {
+ if (log.isTraceEnabled())
+ log.debug("Instantiate sheet " + sheet.getName());
+
+ Cell[] firstRow = sheet.getRow(0);
+
+ Class<?> clss = findClassToInstantiate(sheet);
+ model.put(clss, new TreeMap<Object, Object>());
+
+ tempRefs.put(sheet.getName(), new ArrayList<Object>());
+
+ String keyProperty = firstRow[0].getContents();
+ for (int row = 1; row < sheet.getRows(); row++) {
+ if (log.isTraceEnabled())
+ log.trace(" row " + row);
+
+ Cell[] currentRow = sheet.getRow(row);
+ BeanWrapper bw = new BeanWrapperImpl(clss);
+ cells: for (int col = 0; col < firstRow.length; col++) {
+ String pName = firstRow[col].getContents();
+
+ if (col < currentRow.length) {
+ Cell cell = currentRow[col];
+ if (overrideCell(cell, bw, pName, keyProperty, row,
+ references, tempRefs))
+ continue cells;
+ loadCell(cell, bw, pName, keyProperty, row, references);