01: /*
02: * Copyright 2005-2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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.kuali.module.financial.dao.ojb;
17:
18: import java.util.Map;
19:
20: import org.apache.ojb.broker.PersistenceBroker;
21: import org.apache.ojb.broker.PersistenceBrokerException;
22: import org.apache.ojb.broker.PersistenceBrokerFactory;
23: import org.apache.ojb.broker.accesslayer.RowReaderDefaultImpl;
24: import org.apache.ojb.broker.metadata.ClassDescriptor;
25:
26: /**
27: * (Inspired by example posted at http://nagoya.apache.org/eyebrowse/ReadMsg?listName=ojb-user@db.apache.org&msgId=749837) This
28: * class enables mapping multiple (presumably similar) classes to a single database table. Subclasses must implement the
29: * getDiscriminatorColumns method, returning a String array of columns to consider when determining which class to return, as well
30: * as implement the corresponding chooseClass method that acts on received values for those columns. Sample OBJ config:
31: * <class-descriptor class="org.kuali.bo.ClassA" table="some_common_table" row-reader="org.kuali.dao.ojb.ClassADiscriminator"> ...
32: * </class-descriptor> <class-descriptor class="org.kuali.bo.ClassB" table="some_common_table"
33: * row-reader="org.kuali.dao.ojb.ClassBDiscriminator"> ... </class-descriptor> (where ClassADiscriminator and ClassBDiscriminator
34: * extend PolymorphicMultiColumnDiscriminator)
35: */
36: public abstract class PolymorphicMultiColumnDiscriminator extends
37: RowReaderDefaultImpl {
38:
39: /** Column(s) that distinguish the parent class */
40: private String[] column = null;
41:
42: public PolymorphicMultiColumnDiscriminator(ClassDescriptor cld) {
43: super (cld);
44: column = getDiscriminatorColumns();
45: }
46:
47: /**
48: * This method should return the column(s) necessary to determine which class to cast to.
49: *
50: * @return one or more column names
51: */
52: public abstract String[] getDiscriminatorColumns();
53:
54: /**
55: * Based on the received key values, this method determines the appropriate class.
56: *
57: * @param values
58: * @return an appropriately chosen class
59: */
60: public abstract Class chooseClass(String[] values);
61:
62: protected ClassDescriptor selectClassDescriptor(Map row)
63: throws PersistenceBrokerException {
64: String[] key = new String[column.length];
65:
66: for (int i = 0; i < column.length; i++) {
67: key[i] = (String) row.get(column[i]);
68: }
69:
70: Class clazz = null;
71:
72: if (key != null) {
73: clazz = chooseClass(key);
74: }
75: if (clazz == null) {
76: return getClassDescriptor();
77: }
78:
79: PersistenceBroker broker = null;
80: try {
81: broker = PersistenceBrokerFactory
82: .defaultPersistenceBroker();
83: ClassDescriptor result = broker.getClassDescriptor(clazz);
84: broker.close();
85: if (result == null) {
86: return getClassDescriptor();
87: } else {
88: return result;
89: }
90: } catch (PersistenceBrokerException e) {
91: broker.close();
92: throw e;
93: }
94: }
95:
96: }
|