]> git.argeo.org Git - lgpl/argeo-commons.git/blob - dao/AbstractTabularDaoSupport.java
Prepare next development cycle
[lgpl/argeo-commons.git] / dao / AbstractTabularDaoSupport.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.server.dao;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.argeo.ArgeoException;
26
27 public abstract class AbstractTabularDaoSupport extends
28 AbstractMemoryDaoSupport {
29 private final static Log log = LogFactory
30 .getLog(AbstractTabularDaoSupport.class);
31
32 private Map<String, List<Object>> tabularView = new HashMap<String, List<Object>>();
33
34 @Override
35 protected Object findInternalRef(Reference reference) {
36 TabularInternalReference tabReference = (TabularInternalReference) reference;
37 return getFromTabularView(tabReference.getTargetTableName(),
38 tabReference.getTargetRow());
39 }
40
41 protected Object getFromTabularView(String tableName, Integer row) {
42 return tabularView.get(tableName).get(row - 2);
43 }
44
45 protected void registerInTabularView(String tableName, Object object) {
46 if (!tabularView.containsKey(tableName))
47 tabularView.put(tableName, new ArrayList<Object>());
48 tabularView.get(tableName).add(object);
49 }
50
51 protected Class<?> findClassToInstantiate(String tableName) {
52 // TODO: ability to map sheet names and class names
53 String className = tableName;
54 Class<?> clss = null;
55 try {
56 clss = getClassLoader().loadClass(className);
57 return clss;
58 } catch (ClassNotFoundException e) {
59 // silent
60 }
61
62 scannedPkgs: for (String pkg : getScannedPackages()) {
63 try {
64 clss = getClassLoader().loadClass(pkg.trim() + "." + className);
65 break scannedPkgs;
66 } catch (ClassNotFoundException e) {
67 // silent
68 if (log.isTraceEnabled())
69 log.trace(e.getMessage());
70 }
71 }
72
73 if (clss == null)
74 throw new ArgeoException("Cannot find a class for table "
75 + tableName);
76
77 return clss;
78 }
79
80 protected static class TabularInternalReference extends Reference {
81 private String targetTableName;
82 private Integer targetRow;
83
84 public TabularInternalReference(Object object, String property,
85 String targetSheet, Integer targetRow) {
86 super(object, property, null);
87 this.targetTableName = targetSheet;
88 this.targetRow = targetRow;
89 }
90
91 public String getTargetTableName() {
92 return targetTableName;
93 }
94
95 public Integer getTargetRow() {
96 return targetRow;
97 }
98
99 }
100 }