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.module.labor.dao.ojb;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
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.QueryByCriteria;
026: import org.apache.ojb.broker.query.QueryFactory;
027: import org.apache.ojb.broker.query.ReportQueryByCriteria;
028: import org.kuali.kfs.KFSPropertyConstants;
029: import org.kuali.module.gl.bo.OriginEntryGroup;
030: import org.kuali.module.gl.dao.OriginEntryDao;
031: import org.kuali.module.gl.dao.ojb.OriginEntryDaoOjb;
032: import org.kuali.module.labor.LaborConstants;
033: import org.kuali.module.labor.bo.LaborOriginEntry;
034: import org.kuali.module.labor.dao.LaborOriginEntryDao;
035:
036: /**
037: * OJB Implementation of LaborOriginEntryDao.
038: */
039: public class LaborOriginEntryDaoOjb extends OriginEntryDaoOjb implements
040: LaborOriginEntryDao {
041: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
042: .getLogger(LaborOriginEntryDaoOjb.class);
043:
044: /**
045: * @see org.kuali.module.labor.dao.LaborOriginEntryDao#getEntriesByGroups(java.util.Collection)
046: */
047: public Iterator<LaborOriginEntry> getEntriesByGroups(
048: Collection<OriginEntryGroup> groups) {
049: LOG.debug("getEntriesByGroups() started");
050:
051: // extract the group ids of the given groups
052: List<Integer> groupIds = new ArrayList<Integer>();
053: for (OriginEntryGroup group : groups) {
054: groupIds.add(group.getId());
055: }
056:
057: Criteria criteria = new Criteria();
058: criteria.addIn(KFSPropertyConstants.ENTRY_GROUP_ID, groupIds);
059:
060: QueryByCriteria query = QueryFactory.newQuery(this
061: .getEntryClass(), criteria);
062: return getPersistenceBrokerTemplate().getIteratorByQuery(query);
063: }
064:
065: /**
066: * @see org.kuali.module.labor.dao.LaborOriginEntryDao#getCountOfEntriesInGroups(java.util.Collection)
067: */
068: public int getCountOfEntriesInGroups(
069: Collection<OriginEntryGroup> groups) {
070: LOG.debug("getCountOfEntriesInGroups() started");
071:
072: if (groups.size() == 0)
073: return 0;
074:
075: // extract the group ids of the given groups
076: List<Integer> groupIds = new ArrayList<Integer>();
077: for (OriginEntryGroup group : groups) {
078: groupIds.add(group.getId());
079: }
080:
081: Criteria criteria = new Criteria();
082: criteria.addIn(KFSPropertyConstants.ENTRY_GROUP_ID, groupIds);
083:
084: QueryByCriteria query = QueryFactory.newQuery(this
085: .getEntryClass(), criteria);
086: return getPersistenceBrokerTemplate().getCount(query);
087: }
088:
089: /**
090: * @see org.kuali.module.labor.dao.LaborOriginEntryDao#getConsolidatedEntriesByGroup(org.kuali.module.gl.bo.OriginEntryGroup)
091: */
092: public Iterator<Object[]> getConsolidatedEntriesByGroup(
093: OriginEntryGroup group) {
094: LOG.debug("getConsolidatedEntriesByGroup() started");
095:
096: Criteria criteria = new Criteria();
097: criteria.addEqualTo(KFSPropertyConstants.ENTRY_GROUP_ID, group
098: .getId());
099:
100: ReportQueryByCriteria query = QueryFactory.newReportQuery(this
101: .getEntryClass(), criteria);
102:
103: // set the selection attributes
104: List<String> attributeList = buildConsolidationAttributeList();
105: String[] attributes = attributeList
106: .toArray(new String[attributeList.size()]);
107: query.setAttributes(attributes);
108:
109: // add the group criteria into the selection statement
110: List<String> groupByList = buildGroupByList();
111: String[] groupBy = groupByList.toArray(new String[groupByList
112: .size()]);
113: query.addGroupBy(groupBy);
114:
115: // add the sorting criteria into the selection statement
116: for (String attribute : groupByList) {
117: query.addOrderByAscending(attribute);
118: }
119: return getPersistenceBrokerTemplate()
120: .getReportQueryIteratorByQuery(query);
121: }
122:
123: /**
124: * build the returning attribute list for the calling query
125: *
126: * @return the returning attribute list
127: */
128: private List<String> buildConsolidationAttributeList() {
129: List<String> attributeList = this .buildGroupByList();
130: attributeList.add("sum("
131: + KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT
132: + ")");
133: return attributeList;
134: }
135:
136: /**
137: * build the grouping attribute list for the calling query
138: *
139: * @return the grouping attribute list
140: */
141: private List<String> buildGroupByList() {
142: List<String> groupByList = new ArrayList<String>(LaborConstants
143: .consolidationAttributesOfOriginEntry());
144: groupByList
145: .remove(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT);
146: return groupByList;
147: }
148:
149: /**
150: * @see org.kuali.module.labor.dao.LaborOriginEntryDao#testingLaborGetAllEntries()
151: */
152: public Collection<LaborOriginEntry> testingLaborGetAllEntries() {
153: LOG.debug("testingGetAllEntries() started");
154:
155: Criteria criteria = new Criteria();
156: QueryByCriteria qbc = QueryFactory.newQuery(getEntryClass(),
157: criteria);
158: qbc.addOrderByAscending("entryGroupId");
159: return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
160: }
161:
162: /**
163: * @see org.kuali.module.labor.dao.LaborOriginEntryDao#getLaborEntriesByGroup(org.kuali.module.gl.bo.OriginEntryGroup, int)
164: */
165: public Iterator<LaborOriginEntry> getLaborEntriesByGroup(
166: OriginEntryGroup oeg, int sort) {
167: LOG.debug("getEntriesByGroup() started");
168:
169: Criteria criteria = new Criteria();
170: criteria.addEqualTo(KFSPropertyConstants.ENTRY_GROUP_ID, oeg
171: .getId());
172:
173: QueryByCriteria qbc = QueryFactory.newQuery(getEntryClass(),
174: criteria);
175:
176: if (sort == OriginEntryDao.SORT_DOCUMENT) {
177: qbc
178: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE);
179: qbc
180: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE);
181: qbc
182: .addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
183: qbc
184: .addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
185: qbc
186: .addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
187: qbc
188: .addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
189: qbc
190: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
191: qbc
192: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_DOCUMENT_REVERSAL_DATE);
193: qbc
194: .addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
195: qbc
196: .addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
197: // The above order by fields are required by the scrubber process. Adding these
198: // fields makes the data in the exact same order as the COBOL scrubber.
199: qbc
200: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
201: qbc
202: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
203: qbc
204: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
205: qbc
206: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
207: qbc
208: .addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
209: qbc
210: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE);
211: qbc
212: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE);
213: qbc
214: .addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
215: qbc
216: .addOrderByAscending(KFSPropertyConstants.TRANSACTION_ENTRY_SEQUENCE_NUMBER);
217: qbc
218: .addOrderByAscending(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_DESC);
219: qbc
220: .addOrderByAscending(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT);
221: qbc
222: .addOrderByAscending(KFSPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE);
223: } else if (sort == OriginEntryDao.SORT_REPORT) {
224: qbc
225: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE);
226: qbc
227: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE);
228: qbc
229: .addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
230: qbc
231: .addOrderByAscending(KFSPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE);
232: qbc
233: .addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
234: qbc
235: .addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
236: qbc
237: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
238: } else if (sort == OriginEntryDao.SORT_LISTING_REPORT) {
239: qbc
240: .addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
241: qbc
242: .addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
243: qbc
244: .addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
245: qbc
246: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
247: qbc
248: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
249: qbc
250: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
251: qbc
252: .addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
253: qbc
254: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE);
255: qbc
256: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE);
257: qbc
258: .addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
259: qbc
260: .addOrderByAscending(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_DESC);
261: } else {
262: qbc
263: .addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
264: qbc
265: .addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
266: qbc
267: .addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
268: qbc
269: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
270: qbc
271: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE);
272: qbc
273: .addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE);
274: qbc
275: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_DOCUMENT_TYPE_CODE);
276: qbc
277: .addOrderByAscending(KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE);
278: qbc
279: .addOrderByAscending(KFSPropertyConstants.DOCUMENT_NUMBER);
280: qbc
281: .addOrderByAscending(KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_DESC);
282: }
283:
284: return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
285: }
286:
287: /**
288: * @see org.kuali.module.labor.dao.ojb.LaborOriginEntryDaoOjb#getMatchingEntriesByCollection(java.util.Map)
289: */
290: @Override
291: public Collection getMatchingEntriesByCollection(Map searchCriteria) {
292: LOG.debug("getMatchingEntries() started");
293:
294: Criteria criteria = new Criteria();
295: for (Iterator iter = searchCriteria.keySet().iterator(); iter
296: .hasNext();) {
297: String element = (String) iter.next();
298: criteria.addEqualTo(element, searchCriteria.get(element));
299: }
300:
301: QueryByCriteria qbc = QueryFactory.newQuery(this
302: .getEntryClass(), criteria);
303: qbc.addOrderByAscending("entryGroupId");
304: return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
305: }
306:
307: /**
308: * @param entry the entry to save.
309: */
310: @Deprecated
311: public void saveOriginEntry(LaborOriginEntry entry) {
312: LOG.debug("saveOriginEntry() started");
313:
314: if ((entry != null)
315: && (entry.getTransactionLedgerEntryDescription() != null)
316: && (entry.getTransactionLedgerEntryDescription()
317: .length() > 40)) {
318: entry.setTransactionLedgerEntryDescription(entry
319: .getTransactionLedgerEntryDescription().substring(
320: 0, 40));
321: }
322: getPersistenceBrokerTemplate().store(entry);
323: }
324:
325: /**
326: * @see org.kuali.module.labor.dao.ojb.LaborOriginEntryDaoOjb#getSummaryByGroupId(java.util.Collection)
327: */
328: @Override
329: public Iterator getSummaryByGroupId(Collection groupIdList) {
330: LOG.debug("getSummaryByGroupId() started");
331:
332: if (groupIdList == null || groupIdList.size() <= 0) {
333: return null;
334: }
335:
336: Collection ids = new ArrayList();
337: for (Iterator iter = groupIdList.iterator(); iter.hasNext();) {
338: OriginEntryGroup element = (OriginEntryGroup) iter.next();
339: ids.add(element.getId());
340: }
341:
342: Criteria criteria = new Criteria();
343: criteria.addIn(KFSPropertyConstants.ENTRY_GROUP_ID, ids);
344:
345: ReportQueryByCriteria query = QueryFactory.newReportQuery(
346: getEntryClass(), criteria);
347:
348: String attributeList[] = {
349: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
350: KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE,
351: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
352: KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE,
353: KFSPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE,
354: "sum("
355: + KFSPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT
356: + ")", "count(*)" };
357:
358: String groupList[] = {
359: KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR,
360: KFSPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE,
361: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
362: KFSPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE,
363: KFSPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE };
364:
365: query.setAttributes(attributeList);
366: query.addGroupBy(groupList);
367:
368: // add the sorting criteria
369: for (int i = 0; i < groupList.length; i++) {
370: query.addOrderByAscending(groupList[i]);
371: }
372:
373: return getPersistenceBrokerTemplate()
374: .getReportQueryIteratorByQuery(query);
375: }
376:
377: /**
378: * @see org.kuali.module.labor.dao.LaborOriginEntryDao#getEntryCollectionByGroup(org.kuali.module.gl.bo.OriginEntryGroup)
379: */
380: public Collection<LaborOriginEntry> getEntryCollectionByGroup(
381: OriginEntryGroup group) {
382: LOG.debug("getEntriesByGroups() started");
383:
384: Criteria criteria = new Criteria();
385: criteria.addEqualTo(KFSPropertyConstants.ENTRY_GROUP_ID, group
386: .getId());
387:
388: QueryByCriteria query = QueryFactory.newQuery(this
389: .getEntryClass(), criteria);
390: return getPersistenceBrokerTemplate().getCollectionByQuery(
391: query);
392: }
393: }
|