001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.kfs.dao.ojb;
017:
018: import java.util.Collection;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.ojb.broker.query.Criteria;
023: import org.apache.ojb.broker.query.QueryFactory;
024: import org.kuali.core.bo.KualiCode;
025: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
026: import org.kuali.kfs.KFSConstants;
027: import org.kuali.kfs.dao.KualiCodeDao;
028: import org.kuali.module.cg.bo.AgencyType;
029: import org.kuali.module.chart.bo.FundGroup;
030: import org.kuali.module.chart.bo.KualiSystemCode;
031: import org.kuali.module.chart.bo.ObjSubTyp;
032: import org.kuali.module.chart.bo.ObjectCode;
033: import org.kuali.module.chart.bo.ObjectType;
034: import org.kuali.module.chart.bo.ProjectCode;
035: import org.kuali.module.chart.bo.RestrictedStatus;
036: import org.kuali.module.chart.bo.codes.BalanceTyp;
037: import org.kuali.module.chart.bo.codes.BudgetAggregationCode;
038: import org.kuali.module.chart.bo.codes.FederalFundedCode;
039: import org.kuali.module.chart.bo.codes.MandatoryTransferEliminationCode;
040: import org.kuali.module.financial.bo.PaymentReasonCode;
041: import org.kuali.module.gl.bo.OriginEntrySource;
042: import org.kuali.module.kra.budget.bo.NonpersonnelSubCategory;
043: import org.springframework.dao.DataAccessException;
044:
045: /**
046: * This class is the OJB implementation of the KualiCodeDao interface.
047: */
048:
049: public class KualiCodeDaoOjb extends PlatformAwareDaoBaseOjb implements
050: KualiCodeDao {
051:
052: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
053: .getLogger(KualiCodeDaoOjb.class);
054:
055: private static Map codeColumns = new HashMap();
056: static {
057: codeColumns.put(AgencyType.class, "code");
058: codeColumns.put(BalanceTyp.class, "code");
059: codeColumns.put(BudgetAggregationCode.class, "code");
060: codeColumns.put(PaymentReasonCode.class, "code");
061: codeColumns.put(FederalFundedCode.class, "code");
062: codeColumns.put(FundGroup.class, "code");
063: codeColumns.put(MandatoryTransferEliminationCode.class, "code");
064: codeColumns.put(NonpersonnelSubCategory.class, "code");
065: codeColumns.put(ObjectCode.class, "financialObjectCode");
066: codeColumns.put(ObjectType.class, "code");
067: codeColumns.put(ObjSubTyp.class, "code");
068: codeColumns.put(OriginEntrySource.class, "code");
069: codeColumns.put(ProjectCode.class, "code");
070: codeColumns.put(RestrictedStatus.class,
071: "accountRestrictedStatusCode");
072:
073: // can't add entry for CashDetailTypeCode since the table doesn't yet exist
074: }
075:
076: private static Map nameColumns = new HashMap();
077: static {
078: nameColumns.put(AgencyType.class, "name");
079: nameColumns.put(BalanceTyp.class, "name");
080: nameColumns.put(BudgetAggregationCode.class, "name");
081: nameColumns.put(PaymentReasonCode.class, "name");
082: nameColumns.put(FederalFundedCode.class, "name");
083: nameColumns.put(FundGroup.class, "name");
084: nameColumns.put(MandatoryTransferEliminationCode.class, "name");
085: nameColumns.put(NonpersonnelSubCategory.class, "name");
086: nameColumns.put(ObjectCode.class, "financialObjectCodeName");
087: nameColumns.put(ObjectType.class, "name");
088: nameColumns.put(ObjSubTyp.class, "name");
089: nameColumns.put(OriginEntrySource.class, "name");
090: nameColumns.put(ProjectCode.class, "name");
091: nameColumns.put(RestrictedStatus.class,
092: "accountRestrictedStatusName");
093:
094: // can't add entry for CashDetailTypeCode since the table doesn't yet exist
095: }
096:
097: /**
098: * @param className - the name of the object being used, either KualiCodeBase or a subclass
099: * @param code - code to search for
100: * @return KualiCodeBase Retrieves an KualiCodeBase object by a given code.
101: */
102: public KualiCode getByCode(Class queryClass, String code) {
103: Criteria criteria = getCriteriaForGivenClass(codeColumns,
104: queryClass, code);
105: if (KualiSystemCode.class.isAssignableFrom(queryClass)) {
106: criteria.addEqualTo("CLASS_NAME", queryClass.getName());
107: // queryClass = KualiSystemCode.class;
108: }
109:
110: return (KualiCode) getPersistenceBrokerTemplate()
111: .getObjectByQuery(
112: QueryFactory.newQuery(queryClass, criteria));
113: }
114:
115: private Criteria getCriteriaForGivenClass(Map map,
116: Class queryClass, String value) {
117: Criteria criteria = new Criteria();
118: String column = (String) map.get(queryClass);
119: if (column == null) {
120: System.err.println("Need to add logic for "
121: + queryClass.getName());
122: throw new RuntimeException("Need to add logic for "
123: + queryClass.getName());
124: }
125: criteria.addEqualTo(column, value);
126: return criteria;
127: }
128:
129: public KualiCode getSystemCode(Class clazz, String code) {
130: Criteria criteria = new Criteria();
131: criteria.addEqualTo("CODE", code);
132: criteria.addEqualTo("CLASS_NAME", clazz.getName());
133:
134: return (KualiCode) getPersistenceBrokerTemplate()
135: .getObjectByQuery(
136: QueryFactory.newQuery(clazz, criteria));
137: }
138:
139: /**
140: * @param className - the name of the object being used, either KualiCodeBase or a subclass
141: * @param name - name to search for
142: * @return KualiCodeBase Retrieves an KualiCodeBase object by a given exact name.
143: */
144: public KualiCode getByName(Class queryClass, String name) {
145: Criteria criteria = getCriteriaForGivenClass(nameColumns,
146: queryClass, name);
147: if (KualiSystemCode.class.isAssignableFrom(queryClass)) {
148: criteria.addEqualTo("className", queryClass.getName());
149: queryClass = KualiSystemCode.class;
150: }
151:
152: return (KualiCode) getPersistenceBrokerTemplate()
153: .getObjectByQuery(
154: QueryFactory.newQuery(queryClass, criteria));
155: }
156:
157: /**
158: * @param kualiCode Pass the method a populated KualiCodeBase object, and it will be saved.
159: */
160: public void save(KualiCode kualiCode) throws DataAccessException {
161: getPersistenceBrokerTemplate().store(kualiCode);
162: }
163:
164: /**
165: * Deletes the object-record passed in.
166: *
167: * @param kualiCode
168: * @throws DataAccessException
169: */
170: public void delete(KualiCode kualiCode) {
171: // try to delete the retrieved object
172: getPersistenceBrokerTemplate().delete(kualiCode);
173: }
174:
175: /**
176: * @see org.kuali.core.dao.KualiCodeDao#getAllActive(java.lang.Class)
177: */
178: public Collection getAllActive(Class queryClass) {
179: Criteria criteria = new Criteria();
180: criteria.addEqualTo("FIN_OBJ_ACTIVE_CD",
181: KFSConstants.ACTIVE_INDICATOR);
182: if (KualiSystemCode.class.isAssignableFrom(queryClass)) {
183: criteria.addEqualTo("className", queryClass.getName());
184: queryClass = KualiSystemCode.class;
185: }
186:
187: return getPersistenceBrokerTemplate().getCollectionByQuery(
188: QueryFactory.newQuery(queryClass, criteria));
189: }
190:
191: /**
192: * @see org.kuali.core.dao.KualiCodeDao#getAll(java.lang.Class)
193: */
194: public Collection getAll(Class queryClass) {
195: Criteria criteria = new Criteria();
196: if (KualiSystemCode.class.isAssignableFrom(queryClass)) {
197: criteria.addEqualTo("className", queryClass.getName());
198: // queryClass = KualiSystemCode.class;
199: }
200:
201: // This previously was passing a NULL into the criteria paramter, for no
202: // apparent reason I could see. The result was that it was ignoring class
203: // and returning all codes, which is definitely not what we want, and
204: // definitely not what the interface JavaDocs said. This was fixed.
205: return getPersistenceBrokerTemplate().getCollectionByQuery(
206: QueryFactory.newQuery(queryClass, criteria));
207: }
208:
209: }
|