001: /*
002: * Copyright 2005-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.module.gl.dao.ojb;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.ojb.broker.query.Criteria;
025: import org.apache.ojb.broker.query.Query;
026: import org.apache.ojb.broker.query.QueryByCriteria;
027: import org.apache.ojb.broker.query.QueryFactory;
028: import org.apache.ojb.broker.query.ReportQueryByCriteria;
029: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
030: import org.kuali.kfs.KFSConstants;
031: import org.kuali.kfs.KFSPropertyConstants;
032: import org.kuali.module.gl.bo.Encumbrance;
033: import org.kuali.module.gl.bo.Transaction;
034: import org.kuali.module.gl.dao.EncumbranceDao;
035: import org.kuali.module.gl.util.OJBUtility;
036:
037: /**
038: * An OJB implementation of the EncumbranceDao
039: */
040: public class EncumbranceDaoOjb extends PlatformAwareDaoBaseOjb
041: implements EncumbranceDao {
042: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
043: .getLogger(EncumbranceDaoOjb.class);
044:
045: /**
046: * Returns an encumbrance that would be affected by the given transaction
047: *
048: * @param t the transaction to find the affected encumbrance for
049: * @return an Encumbrance that would be affected by the posting of the transaction, or null
050: * @see org.kuali.module.gl.dao.EncumbranceDao#getEncumbranceByTransaction(org.kuali.module.gl.bo.Transaction)
051: */
052: public Encumbrance getEncumbranceByTransaction(Transaction t) {
053: LOG.debug("getEncumbranceByTransaction() started");
054:
055: Criteria crit = new Criteria();
056: crit.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, t
057: .getUniversityFiscalYear());
058: crit.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, t
059: .getChartOfAccountsCode());
060: crit.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER, t
061: .getAccountNumber());
062: crit.addEqualTo(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, t
063: .getSubAccountNumber());
064: crit.addEqualTo(KFSPropertyConstants.OBJECT_CODE, t
065: .getFinancialObjectCode());
066: crit.addEqualTo(KFSPropertyConstants.SUB_OBJECT_CODE, t
067: .getFinancialSubObjectCode());
068: crit.addEqualTo(KFSPropertyConstants.BALANCE_TYPE_CODE, t
069: .getFinancialBalanceTypeCode());
070: crit.addEqualTo(KFSPropertyConstants.DOCUMENT_TYPE_CODE, t
071: .getFinancialDocumentTypeCode());
072: crit.addEqualTo(KFSPropertyConstants.ORIGIN_CODE, t
073: .getFinancialSystemOriginationCode());
074: crit.addEqualTo(KFSPropertyConstants.DOCUMENT_NUMBER, t
075: .getDocumentNumber());
076:
077: QueryByCriteria qbc = QueryFactory.newQuery(Encumbrance.class,
078: crit);
079: return (Encumbrance) getPersistenceBrokerTemplate()
080: .getObjectByQuery(qbc);
081: }
082:
083: /**
084: * Returns an Iterator of all encumbrances that need to be closed for the fiscal year
085: *
086: * @param fiscalYear a fiscal year to find encumbrances for
087: * @return an Iterator of encumbrances to close
088: * @see org.kuali.module.gl.dao.EncumbranceDao#getEncumbrancesToClose(java.lang.Integer)
089: */
090: public Iterator getEncumbrancesToClose(Integer fiscalYear) {
091:
092: Criteria criteria = new Criteria();
093: criteria
094: .addEqualTo(
095: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
096: fiscalYear);
097:
098: QueryByCriteria query = new QueryByCriteria(Encumbrance.class,
099: criteria);
100: query
101: .addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
102: query.addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
103: query
104: .addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
105: query.addOrderByAscending(KFSPropertyConstants.OBJECT_CODE);
106: query.addOrderByAscending(KFSPropertyConstants.SUB_OBJECT_CODE);
107: query
108: .addOrderByAscending(KFSPropertyConstants.BALANCE_TYPE_CODE);
109:
110: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
111: }
112:
113: /**
114: * Saves an encumbrance to the database
115: *
116: * @param e an encumbrance to save
117: * @see org.kuali.module.gl.dao.EncumbranceDao#save(org.kuali.module.gl.bo.Encumbrance)
118: */
119: public void save(Encumbrance e) {
120: LOG.debug("save() started");
121:
122: getPersistenceBrokerTemplate().store(e);
123: }
124:
125: /**
126: * Purges the database of all those encumbrances with the given chart and year
127: *
128: * @param chartOfAccountsCode the chart of accounts code purged encumbrances will have
129: * @param year the university fiscal year purged encumbrances will have
130: * @see org.kuali.module.gl.dao.EncumbranceDao#purgeYearByChart(java.lang.String, int)
131: */
132: public void purgeYearByChart(String chartOfAccountsCode, int year) {
133: LOG.debug("purgeYearByChart() started");
134:
135: Criteria criteria = new Criteria();
136: criteria.addEqualTo(KFSPropertyConstants.CHART,
137: chartOfAccountsCode);
138: criteria.addLessThan(
139: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
140: new Integer(year));
141:
142: getPersistenceBrokerTemplate().deleteByQuery(
143: new QueryByCriteria(Encumbrance.class, criteria));
144:
145: // This is required because if any deleted account balances are in the cache, deleteByQuery
146: // doesn't
147: // remove them from the cache so a future select will retrieve these deleted account
148: // balances from
149: // the cache and return them. Clearing the cache forces OJB to go to the database again.
150: getPersistenceBrokerTemplate().clearCache();
151: }
152:
153: /**
154: * fetch all encumbrance records from GL open encumbrance table
155: *
156: * @return an Iterator with all encumbrances currently in the database
157: * @see org.kuali.module.gl.dao.EncumbranceDao#getAllEncumbrances()
158: */
159: public Iterator getAllEncumbrances() {
160: Criteria criteria = new Criteria();
161: QueryByCriteria query = QueryFactory.newQuery(
162: Encumbrance.class, criteria);
163: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
164: }
165:
166: /**
167: * group all encumbrances with/without the given document type code by fiscal year, chart, account, sub-account, object code,
168: * sub object code, and balance type code, and summarize the encumbrance amount and the encumbrance close amount.
169: *
170: * @param documentTypeCode the given document type code
171: * @param included indicate if all encumbrances with the given document type are included in the results or not
172: * @return an Iterator of arrays of java.lang.Objects holding summarization data about qualifying encumbrances
173: * @see org.kuali.module.gl.dao.EncumbranceDao#getSummarizedEncumbrances(String, boolean)
174: */
175: public Iterator getSummarizedEncumbrances(String documentTypeCode,
176: boolean included) {
177: Criteria criteria = new Criteria();
178:
179: if (included) {
180: criteria.addEqualTo(
181: KFSPropertyConstants.DOCUMENT_TYPE_CODE,
182: documentTypeCode);
183: } else {
184: criteria.addNotEqualTo(
185: KFSPropertyConstants.DOCUMENT_TYPE_CODE,
186: documentTypeCode);
187: }
188:
189: ReportQueryByCriteria query = QueryFactory.newReportQuery(
190: Encumbrance.class, criteria);
191:
192: // set the selection attributes
193: List attributeList = buildAttributeList();
194: String[] attributes = (String[]) attributeList
195: .toArray(new String[attributeList.size()]);
196: query.setAttributes(attributes);
197:
198: // add the group criteria into the selection statement
199: List groupByList = buildGroupByList();
200: String[] groupBy = (String[]) groupByList
201: .toArray(new String[groupByList.size()]);
202: query.addGroupBy(groupBy);
203:
204: return getPersistenceBrokerTemplate()
205: .getReportQueryIteratorByQuery(query);
206: }
207:
208: /**
209: * Queries the database to find all open encumbrances that qualify by the given keys
210: *
211: * @param fieldValues the input fields and values
212: * @return a collection of open encumbrances
213: * @see org.kuali.module.gl.dao.EncumbranceDao#findOpenEncumbrance(java.util.Map)
214: */
215: public Iterator findOpenEncumbrance(Map fieldValues) {
216: LOG.debug("findOpenEncumbrance() started");
217:
218: Query query = this .getOpenEncumbranceQuery(fieldValues);
219: OJBUtility.limitResultSize(query);
220: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
221: }
222:
223: /**
224: * Counts the number of open encumbrances that have the keys given in the map
225: *
226: * @param fieldValues the input fields and values
227: * @return the number of the open encumbrances
228: * @see org.kuali.module.gl.dao.EncumbranceDao#getOpenEncumbranceRecordCount(java.util.Map)
229: */
230: public Integer getOpenEncumbranceRecordCount(Map fieldValues) {
231: LOG.debug("getOpenEncumbranceRecordCount() started");
232:
233: Query query = this .getOpenEncumbranceQuery(fieldValues);
234: return getPersistenceBrokerTemplate().getCount(query);
235: }
236:
237: /**
238: * build the query for encumbrance search
239: *
240: * @param fieldValues a Map of values to use as keys for the query
241: * @return an OJB query
242: */
243: private Query getOpenEncumbranceQuery(Map fieldValues) {
244: Criteria criteria = OJBUtility.buildCriteriaFromMap(
245: fieldValues, new Encumbrance());
246: criteria.addIn(KFSPropertyConstants.BALANCE_TYPE_CODE, Arrays
247: .asList(KFSConstants.ENCUMBRANCE_BALANCE_TYPE));
248: return QueryFactory.newQuery(Encumbrance.class, criteria);
249: }
250:
251: /**
252: * This method builds the atrribute list used by balance searching
253: *
254: * @return a List of encumbrance attributes that need to be summed
255: */
256: private List buildAttributeList() {
257: List attributeList = this .buildGroupByList();
258:
259: attributeList.add("sum("
260: + KFSPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_AMOUNT
261: + ")");
262: attributeList
263: .add("sum("
264: + KFSPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_CLOSED_AMOUNT
265: + ")");
266:
267: return attributeList;
268: }
269:
270: /**
271: * This method builds group by attribute list used by balance searching
272: *
273: * @return a List of encumbrance attributes to search on
274: */
275: private List buildGroupByList() {
276: List attributeList = new ArrayList();
277:
278: attributeList.add(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
279: attributeList.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
280: attributeList.add(KFSPropertyConstants.ACCOUNT_NUMBER);
281: attributeList.add(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
282: attributeList.add(KFSPropertyConstants.OBJECT_CODE);
283: attributeList.add(KFSPropertyConstants.SUB_OBJECT_CODE);
284: attributeList.add(KFSPropertyConstants.BALANCE_TYPE_CODE);
285:
286: return attributeList;
287: }
288: }
|