1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.dataaccess.impl;
17
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.ojb.broker.query.Criteria;
26 import org.apache.ojb.broker.query.QueryByCriteria;
27 import org.apache.ojb.broker.query.QueryFactory;
28 import org.apache.ojb.broker.query.ReportQueryByCriteria;
29 import org.kuali.ole.gl.businessobject.OriginEntryFull;
30 import org.kuali.ole.gl.businessobject.OriginEntryGroup;
31 import org.kuali.ole.gl.businessobject.OriginEntryInformation;
32 import org.kuali.ole.gl.dataaccess.OriginEntryDao;
33 import org.kuali.ole.sys.OLEConstants;
34 import org.kuali.ole.sys.OLEPropertyConstants;
35 import org.kuali.ole.sys.util.TransactionalServiceUtils;
36 import org.kuali.rice.core.api.util.type.KualiDecimal;
37 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
38
39
40
41
42 public class OriginEntryDaoOjb extends PlatformAwareDaoBaseOjb implements OriginEntryDao {
43 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OriginEntryDaoOjb.class);
44
45 private static final String ENTRY_GROUP_ID = "entryGroupId";
46 private static final String ENTRY_ID = "entryId";
47 private static final String FINANCIAL_BALANCE_TYPE_CODE = "financialBalanceTypeCode";
48 private static final String CHART_OF_ACCOUNTS_CODE = "chartOfAccountsCode";
49 private static final String ACCOUNT_NUMBER = "accountNumber";
50 private static final String SUB_ACCOUNT_NUMBER = "subAccountNumber";
51 private static final String FINANCIAL_DOCUMENT_TYPE_CODE = "financialDocumentTypeCode";
52 private static final String FINANCIAL_SYSTEM_ORIGINATION_CODE = "financialSystemOriginationCode";
53 private static final String FINANCIAL_DOCUMENT_REVERSAL_DATE = "financialDocumentReversalDate";
54 private static final String UNIVERSITY_FISCAL_PERIOD_CODE = "universityFiscalPeriodCode";
55 private static final String UNIVERSITY_FISCAL_YEAR = "universityFiscalYear";
56 private static final String FINANCIAL_OBJECT_CODE = "financialObjectCode";
57 private static final String FINANCIAL_SUB_OBJECT_CODE = "financialSubObjectCode";
58 private static final String FINANCIAL_OBJECT_TYPE_CODE = "financialObjectTypeCode";
59 private static final String TRANSACTION_LEDGER_ENTRY_SEQUENCE_NUMBER = "transactionLedgerEntrySequenceNumber";
60 private static final String TRANSACTION_LEDGER_ENTRY_DESCRIPTION = "transactionLedgerEntryDescription";
61 private static final String TRANSACTION_LEDGER_ENTRY_AMOUNT = "transactionLedgerEntryAmount";
62 private static final String TRANSACTION_DEBIT_CREDIT_CODE = "transactionDebitCreditCode";
63
64 private Class entryClass;
65
66
67
68
69
70
71
72
73 public void setEntryClass(Class entryClass) {
74 this.entryClass = entryClass;
75 }
76
77
78
79
80
81
82 public Class getEntryClass() {
83 return entryClass;
84 }
85
86
87
88
89 public OriginEntryDaoOjb() {
90 super();
91 }
92
93
94
95
96
97
98
99
100 @Override
101 public KualiDecimal getGroupTotal(Integer groupId, boolean isCredit) {
102 LOG.debug("getGroupTotal() started");
103
104 Criteria crit = new Criteria();
105 crit.addEqualTo(OriginEntryDaoOjb.ENTRY_GROUP_ID, groupId);
106 if (isCredit) {
107 crit.addEqualTo(OriginEntryDaoOjb.TRANSACTION_DEBIT_CREDIT_CODE, OLEConstants.GL_CREDIT_CODE);
108 }
109 else {
110 crit.addNotEqualTo(OriginEntryDaoOjb.TRANSACTION_DEBIT_CREDIT_CODE, OLEConstants.GL_CREDIT_CODE);
111 }
112
113 ReportQueryByCriteria q = QueryFactory.newReportQuery(entryClass, crit);
114 q.setAttributes(new String[] { "SUM(" + OriginEntryDaoOjb.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")" });
115
116 Iterator i = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
117 if (i.hasNext()) {
118 Object[] data = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(i);
119 return (KualiDecimal) data[0];
120 }
121 else {
122 return null;
123 }
124 }
125
126
127
128
129
130
131
132 @Override
133 public Integer getGroupCount(Integer groupId) {
134 LOG.debug("getGroupCount() started");
135
136 Criteria crit = new Criteria();
137 crit.addEqualTo(OriginEntryDaoOjb.ENTRY_GROUP_ID, groupId);
138
139 ReportQueryByCriteria q = QueryFactory.newReportQuery(entryClass, crit);
140 q.setAttributes(new String[] { "count(*)" });
141
142 Iterator i = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
143 if (i.hasNext()) {
144 Object[] data = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(i);
145
146 if (data[0] instanceof BigDecimal) {
147 return ((BigDecimal) data[0]).intValue();
148 }
149 else {
150 return ((Long) data[0]).intValue();
151 }
152 }
153 else {
154 return null;
155 }
156 }
157
158
159
160
161
162
163
164 @Override
165 public Iterator getGroupCounts() {
166 LOG.debug("getGroupCounts() started");
167
168 Criteria crit = new Criteria();
169
170 ReportQueryByCriteria q = QueryFactory.newReportQuery(entryClass, crit);
171 q.setAttributes(new String[] { ENTRY_GROUP_ID, "count(*)" });
172 q.addGroupBy(ENTRY_GROUP_ID);
173
174 return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
175 }
176
177
178
179
180
181
182 @Override
183 public void deleteEntry(OriginEntryInformation oe) {
184 LOG.debug("deleteEntry() started");
185
186 getPersistenceBrokerTemplate().delete(oe);
187 }
188
189
190
191
192
193
194
195
196 @Override
197 public Iterator getDocumentsByGroup(OriginEntryGroup oeg) {
198 LOG.debug("getDocumentsByGroup() started");
199
200 Criteria criteria = new Criteria();
201 criteria.addEqualTo(ENTRY_GROUP_ID, oeg.getId());
202
203 ReportQueryByCriteria q = QueryFactory.newReportQuery(entryClass, criteria);
204 q.setAttributes(new String[] { OLEPropertyConstants.DOCUMENT_NUMBER, "financialDocumentTypeCode", "financialSystemOriginationCode" });
205
206 q.setDistinct(true);
207
208 return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
209 }
210
211
212
213
214
215
216
217
218 @Override
219 public Iterator<OriginEntryFull> getMatchingEntries(Map searchCriteria) {
220 LOG.debug("getMatchingEntries() started");
221
222 Criteria criteria = new Criteria();
223 for (Iterator iter = searchCriteria.keySet().iterator(); iter.hasNext();) {
224 String element = (String) iter.next();
225 criteria.addEqualTo(element, searchCriteria.get(element));
226 }
227
228 QueryByCriteria qbc = QueryFactory.newQuery(entryClass, criteria);
229 qbc.addOrderByAscending(ENTRY_GROUP_ID);
230 return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
231 }
232
233
234
235
236
237
238
239
240 @Override
241 public Iterator<OriginEntryFull> getBadBalanceEntries(Collection groups) {
242 LOG.debug("getBadBalanceEntries() started");
243
244 if (groups.size() <= 0) {
245 return null;
246 }
247
248 Collection ids = new ArrayList();
249 for (Iterator iter = groups.iterator(); iter.hasNext();) {
250 OriginEntryGroup element = (OriginEntryGroup) iter.next();
251 ids.add(element.getId());
252 }
253
254 Criteria crit1 = new Criteria();
255 crit1.addIn(ENTRY_GROUP_ID, ids);
256
257 Criteria crit2 = new Criteria();
258 crit2.addIsNull(FINANCIAL_BALANCE_TYPE_CODE);
259
260 Criteria crit3 = new Criteria();
261 crit3.addEqualTo(FINANCIAL_BALANCE_TYPE_CODE, " ");
262
263 crit2.addOrCriteria(crit3);
264
265 crit1.addAndCriteria(crit2);
266
267 QueryByCriteria qbc = QueryFactory.newQuery(entryClass, crit1);
268 qbc.addOrderByAscending(UNIVERSITY_FISCAL_YEAR);
269 qbc.addOrderByAscending(CHART_OF_ACCOUNTS_CODE);
270 qbc.addOrderByAscending(ACCOUNT_NUMBER);
271 qbc.addOrderByAscending(FINANCIAL_OBJECT_CODE);
272 qbc.addOrderByAscending(FINANCIAL_OBJECT_TYPE_CODE);
273 qbc.addOrderByAscending(FINANCIAL_BALANCE_TYPE_CODE);
274 qbc.addOrderByAscending(UNIVERSITY_FISCAL_PERIOD_CODE);
275 qbc.addOrderByAscending(FINANCIAL_DOCUMENT_TYPE_CODE);
276 qbc.addOrderByAscending(FINANCIAL_SYSTEM_ORIGINATION_CODE);
277 qbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
278
279 return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
280 }
281
282
283
284
285
286
287
288
289
290
291 @Override
292 public <T> Iterator<T> getEntriesByGroup(OriginEntryGroup oeg, int sort) {
293 LOG.debug("getEntriesByGroup() started");
294
295
296
297
298 getPersistenceBrokerTemplate().clearCache();
299
300 Criteria criteria = new Criteria();
301 criteria.addEqualTo(ENTRY_GROUP_ID, oeg.getId());
302
303 QueryByCriteria qbc = QueryFactory.newQuery(entryClass, criteria);
304
305 if (sort == OriginEntryDao.SORT_DOCUMENT) {
306 qbc.addOrderByAscending(FINANCIAL_DOCUMENT_TYPE_CODE);
307 qbc.addOrderByAscending(FINANCIAL_SYSTEM_ORIGINATION_CODE);
308 qbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
309 qbc.addOrderByAscending(CHART_OF_ACCOUNTS_CODE);
310 qbc.addOrderByAscending(ACCOUNT_NUMBER);
311 qbc.addOrderByAscending(SUB_ACCOUNT_NUMBER);
312 qbc.addOrderByAscending(FINANCIAL_BALANCE_TYPE_CODE);
313 qbc.addOrderByAscending(FINANCIAL_DOCUMENT_REVERSAL_DATE);
314 qbc.addOrderByAscending(UNIVERSITY_FISCAL_PERIOD_CODE);
315 qbc.addOrderByAscending(UNIVERSITY_FISCAL_YEAR);
316
317
318 qbc.addOrderByAscending(FINANCIAL_OBJECT_CODE);
319 qbc.addOrderByAscending(FINANCIAL_SUB_OBJECT_CODE);
320 qbc.addOrderByAscending(FINANCIAL_BALANCE_TYPE_CODE);
321 qbc.addOrderByAscending(FINANCIAL_OBJECT_TYPE_CODE);
322 qbc.addOrderByAscending(UNIVERSITY_FISCAL_PERIOD_CODE);
323 qbc.addOrderByAscending(FINANCIAL_DOCUMENT_TYPE_CODE);
324 qbc.addOrderByAscending(FINANCIAL_SYSTEM_ORIGINATION_CODE);
325 qbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
326 qbc.addOrderByAscending(TRANSACTION_LEDGER_ENTRY_SEQUENCE_NUMBER);
327 qbc.addOrderByAscending(TRANSACTION_LEDGER_ENTRY_DESCRIPTION);
328 qbc.addOrderByAscending(TRANSACTION_LEDGER_ENTRY_AMOUNT);
329 qbc.addOrderByAscending(TRANSACTION_DEBIT_CREDIT_CODE);
330 }
331 else if (sort == OriginEntryDao.SORT_REPORT) {
332 qbc.addOrderByAscending(FINANCIAL_DOCUMENT_TYPE_CODE);
333 qbc.addOrderByAscending(FINANCIAL_SYSTEM_ORIGINATION_CODE);
334 qbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
335 qbc.addOrderByAscending(TRANSACTION_DEBIT_CREDIT_CODE);
336 qbc.addOrderByAscending(CHART_OF_ACCOUNTS_CODE);
337 qbc.addOrderByAscending(ACCOUNT_NUMBER);
338 qbc.addOrderByAscending(FINANCIAL_OBJECT_CODE);
339 }
340 else if (sort == OriginEntryDao.SORT_LISTING_REPORT) {
341 qbc.addOrderByAscending(UNIVERSITY_FISCAL_YEAR);
342 qbc.addOrderByAscending(CHART_OF_ACCOUNTS_CODE);
343 qbc.addOrderByAscending(ACCOUNT_NUMBER);
344 qbc.addOrderByAscending(FINANCIAL_OBJECT_CODE);
345 qbc.addOrderByAscending(FINANCIAL_OBJECT_TYPE_CODE);
346 qbc.addOrderByAscending(FINANCIAL_BALANCE_TYPE_CODE);
347 qbc.addOrderByAscending(UNIVERSITY_FISCAL_PERIOD_CODE);
348 qbc.addOrderByAscending(FINANCIAL_DOCUMENT_TYPE_CODE);
349 qbc.addOrderByAscending(FINANCIAL_SYSTEM_ORIGINATION_CODE);
350 qbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
351 qbc.addOrderByAscending(TRANSACTION_LEDGER_ENTRY_DESCRIPTION);
352 }
353 else {
354 qbc.addOrderByAscending(CHART_OF_ACCOUNTS_CODE);
355 qbc.addOrderByAscending(ACCOUNT_NUMBER);
356 qbc.addOrderByAscending(SUB_ACCOUNT_NUMBER);
357 qbc.addOrderByAscending(FINANCIAL_OBJECT_CODE);
358 qbc.addOrderByAscending(FINANCIAL_OBJECT_TYPE_CODE);
359 qbc.addOrderByAscending(UNIVERSITY_FISCAL_PERIOD_CODE);
360 qbc.addOrderByAscending(FINANCIAL_DOCUMENT_TYPE_CODE);
361 qbc.addOrderByAscending(FINANCIAL_SYSTEM_ORIGINATION_CODE);
362 qbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
363 qbc.addOrderByAscending(TRANSACTION_LEDGER_ENTRY_DESCRIPTION);
364 }
365
366 return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
367 }
368
369
370
371
372
373
374
375 @Override
376 public Collection<OriginEntryFull> testingGetAllEntries() {
377 LOG.debug("testingGetAllEntries() started");
378
379 Criteria criteria = new Criteria();
380 QueryByCriteria qbc = QueryFactory.newQuery(entryClass, criteria);
381 qbc.addOrderByAscending(ENTRY_GROUP_ID);
382 qbc.addOrderByAscending(ENTRY_ID);
383 return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
384 }
385
386
387
388
389
390
391 @Override
392 public void deleteMatchingEntries(Map searchCriteria) {
393 LOG.debug("deleteMatchingEntries() started");
394
395 Criteria criteria = new Criteria();
396 for (Iterator iter = searchCriteria.keySet().iterator(); iter.hasNext();) {
397 String element = (String) iter.next();
398 criteria.addEqualTo(element, searchCriteria.get(element));
399 }
400
401 QueryByCriteria qbc = QueryFactory.newQuery(entryClass, criteria);
402 getPersistenceBrokerTemplate().deleteByQuery(qbc);
403
404
405
406
407 getPersistenceBrokerTemplate().clearCache();
408 }
409
410
411
412
413
414
415
416
417 @Override
418 public void deleteGroups(Collection<OriginEntryGroup> groups) {
419 LOG.debug("deleteGroups() started");
420
421 if (groups == null || groups.size() <= 0) {
422 return;
423 }
424
425 List ids = new ArrayList();
426 for (Iterator iter = groups.iterator(); iter.hasNext();) {
427 OriginEntryGroup element = (OriginEntryGroup) iter.next();
428 ids.add(element.getId());
429 }
430
431 Criteria criteria = new Criteria();
432 criteria.addIn(ENTRY_GROUP_ID, ids);
433
434 QueryByCriteria qbc = QueryFactory.newQuery(entryClass, criteria);
435 getPersistenceBrokerTemplate().deleteByQuery(qbc);
436
437
438
439
440 getPersistenceBrokerTemplate().clearCache();
441 }
442
443
444
445
446
447
448
449
450 @Override
451 public Collection<OriginEntryFull> getMatchingEntriesByCollection(Map searchCriteria) {
452 LOG.debug("getMatchingEntries() started");
453
454 Criteria criteria = new Criteria();
455 for (Iterator iter = searchCriteria.keySet().iterator(); iter.hasNext();) {
456 String element = (String) iter.next();
457 criteria.addEqualTo(element, searchCriteria.get(element));
458 }
459
460 QueryByCriteria qbc = QueryFactory.newQuery(entryClass, criteria);
461 qbc.addOrderByAscending(ENTRY_GROUP_ID);
462 return getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
463 }
464
465
466
467
468
469
470
471
472 @Override
473 public Iterator getSummaryByGroupId(Collection groupIdList) {
474 LOG.debug("getSummaryByGroupId() started");
475
476 if (groupIdList == null || groupIdList.size() <= 0) {
477 return null;
478 }
479
480 Collection ids = new ArrayList();
481 for (Iterator iter = groupIdList.iterator(); iter.hasNext();) {
482 OriginEntryGroup element = (OriginEntryGroup) iter.next();
483 ids.add(element.getId());
484 }
485
486 Criteria criteria = new Criteria();
487 criteria.addIn(OLEPropertyConstants.ENTRY_GROUP_ID, ids);
488
489 ReportQueryByCriteria query = QueryFactory.newReportQuery(entryClass, criteria);
490
491 String attributeList[] = { OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, OLEPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE, OLEPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE, "sum(" + OLEPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")", "count(*)" };
492
493 String groupList[] = { OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, OLEPropertyConstants.FINANCIAL_SYSTEM_ORIGINATION_CODE, OLEPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE };
494
495 query.setAttributes(attributeList);
496 query.addGroupBy(groupList);
497
498
499 for (int i = 0; i < groupList.length; i++) {
500 query.addOrderByAscending(groupList[i]);
501 }
502
503 return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
504 }
505
506
507
508
509
510
511
512
513 @Override
514 public OriginEntryFull getExactMatchingEntry(Integer entryId) {
515 LOG.debug("getMatchingEntries() started");
516 OriginEntryFull oe = new OriginEntryFull();
517
518 try {
519 oe = (OriginEntryFull) getPersistenceBrokerTemplate().getObjectById(entryClass, entryId);
520
521 }
522 catch (Exception e) {
523 }
524
525 return oe;
526 }
527
528
529
530
531
532
533
534
535 @Override
536 public Iterator getPosterOutputSummaryByGroupId(Collection groups) {
537 LOG.debug("getPosterInputSummaryByGroupId() started");
538
539 if (groups == null || groups.size() <= 0) {
540 return null;
541 }
542
543 Collection ids = new ArrayList();
544 for (Iterator iter = groups.iterator(); iter.hasNext();) {
545 OriginEntryGroup element = (OriginEntryGroup) iter.next();
546 ids.add(element.getId());
547 }
548
549 Criteria criteria = new Criteria();
550 criteria.addIn(OLEPropertyConstants.ENTRY_GROUP_ID, ids);
551 String fundGroupCode = OLEPropertyConstants.ACCOUNT + "." + OLEPropertyConstants.SUB_FUND_GROUP + "." + OLEPropertyConstants.FUND_GROUP_CODE;
552
553 ReportQueryByCriteria query = QueryFactory.newReportQuery(entryClass, criteria);
554
555 String attributeList[] = { OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, fundGroupCode, OLEPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, OLEPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE, "sum(" + OLEPropertyConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")" };
556
557 String groupList[] = { OLEPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, fundGroupCode, OLEPropertyConstants.FINANCIAL_OBJECT_TYPE_CODE, OLEPropertyConstants.TRANSACTION_DEBIT_CREDIT_CODE };
558
559 query.setAttributes(attributeList);
560 query.addGroupBy(groupList);
561
562
563 for (int i = 0; i < groupList.length; i++) {
564 query.addOrderByAscending(groupList[i]);
565 }
566
567 return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
568 }
569 }