]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/dao/AbstractTabularDaoSupport.java
Prepare next development cycle
[lgpl/argeo-commons.git] / server / dao / AbstractTabularDaoSupport.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.server.dao;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.ArgeoException;
27
28 public abstract class AbstractTabularDaoSupport extends
29 AbstractMemoryDaoSupport {
30 private final static Log log = LogFactory
31 .getLog(AbstractTabularDaoSupport.class);
32
33 private Map<String, List<Object>> tabularView = new HashMap<String, List<Object>>();
34
35 @Override
36 protected Object findInternalRef(Reference reference) {
37 TabularInternalReference tabReference = (TabularInternalReference) reference;
38 return getFromTabularView(tabReference.getTargetTableName(),
39 tabReference.getTargetRow());
40 }
41
42 protected Object getFromTabularView(String tableName, Integer row) {
43 return tabularView.get(tableName).get(row - 2);
44 }
45
46 protected void registerInTabularView(String tableName, Object object) {
47 if (!tabularView.containsKey(tableName))
48 tabularView.put(tableName, new ArrayList<Object>());
49 tabularView.get(tableName).add(object);
50 }
51
52 protected Class<?> findClassToInstantiate(String tableName) {
53 // TODO: ability to map sheet names and class names
54 String className = tableName;
55 Class<?> clss = null;
56 try {
57 clss = getClassLoader().loadClass(className);
58 return clss;
59 } catch (ClassNotFoundException e) {
60 // silent
61 }
62
63 scannedPkgs: for (String pkg : getScannedPackages()) {
64 try {
65 clss = getClassLoader().loadClass(pkg.trim() + "." + className);
66 break scannedPkgs;
67 } catch (ClassNotFoundException e) {
68 // silent
69 if (log.isTraceEnabled())
70 log.trace(e.getMessage());
71 }
72 }
73
74 if (clss == null)
75 throw new ArgeoException("Cannot find a class for table "
76 + tableName);
77
78 return clss;
79 }
80
81 protected static class TabularInternalReference extends Reference {
82 private String targetTableName;
83 private Integer targetRow;
84
85 public TabularInternalReference(Object object, String property,
86 String targetSheet, Integer targetRow) {
87 super(object, property, null);
88 this.targetTableName = targetSheet;
89 this.targetRow = targetRow;
90 }
91
92 public String getTargetTableName() {
93 return targetTableName;
94 }
95
96 public Integer getTargetRow() {
97 return targetRow;
98 }
99
100 }
101 }