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.Iterator;
20
21 import org.apache.ojb.broker.query.Criteria;
22 import org.apache.ojb.broker.query.QueryByCriteria;
23 import org.apache.ojb.broker.query.QueryFactory;
24 import org.apache.ojb.broker.query.ReportQueryByCriteria;
25 import org.kuali.ole.gl.businessobject.Entry;
26 import org.kuali.ole.gl.businessobject.Transaction;
27 import org.kuali.ole.gl.dataaccess.EntryDao;
28 import org.kuali.ole.gl.dataaccess.LedgerEntryBalancingDao;
29 import org.kuali.ole.sys.OLEConstants;
30 import org.kuali.ole.sys.OLEPropertyConstants;
31 import org.kuali.ole.sys.util.TransactionalServiceUtils;
32 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
33 import org.kuali.rice.krad.util.ObjectUtils;
34
35
36
37
38 public class EntryDaoOjb extends PlatformAwareDaoBaseOjb implements EntryDao, LedgerEntryBalancingDao {
39 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EntryDaoOjb.class);
40
41 private final static String UNIVERISITY_FISCAL_YEAR = "universityFiscalYear";
42 private final static String CHART_OF_ACCOUNTS_CODE = "chartOfAccountsCode";
43 private final static String ACCOUNT_NUMBER = "accountNumber";
44 private final static String SUB_ACCOUNT_NUMBER = "subAccountNumber";
45 private final static String FINANCIAL_OBJECT_CODE = "financialObjectCode";
46 private final static String FINANCIAL_SUB_OBJECT_CODE = "financialSubObjectCode";
47 private final static String FINANCIAL_BALANCE_TYPE_CODE = "financialBalanceTypeCode";
48 private final static String FINANCIAL_OBJECT_TYPE_CODE = "financialObjectTypeCode";
49 private final static String UNIVERISTY_FISCAL_PERIOD_CODE = "universityFiscalPeriodCode";
50 private final static String FINANCIAL_DOCUMENT_TYPE_CODE = "financialDocumentTypeCode";
51 private final static String FINANCIAL_SYSTEM_ORIGINATION_CODE = "financialSystemOriginationCode";
52 private final static String MAX_CONSTANT = "max(documentNumber)";
53
54
55
56
57
58 public EntryDaoOjb() {
59 super();
60 }
61
62
63
64
65
66
67
68
69 public int getMaxSequenceNumber(Transaction t) {
70 LOG.debug("getSequenceNumber() ");
71
72 Criteria crit = new Criteria();
73 crit.addEqualTo(UNIVERISITY_FISCAL_YEAR, t.getUniversityFiscalYear());
74 crit.addEqualTo(CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
75 crit.addEqualTo(ACCOUNT_NUMBER, t.getAccountNumber());
76 crit.addEqualTo(SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
77 crit.addEqualTo(FINANCIAL_OBJECT_CODE, t.getFinancialObjectCode());
78 crit.addEqualTo(FINANCIAL_SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
79 crit.addEqualTo(FINANCIAL_BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
80 crit.addEqualTo(FINANCIAL_OBJECT_TYPE_CODE, t.getFinancialObjectTypeCode());
81 crit.addEqualTo(UNIVERISTY_FISCAL_PERIOD_CODE, t.getUniversityFiscalPeriodCode());
82 crit.addEqualTo(FINANCIAL_DOCUMENT_TYPE_CODE, t.getFinancialDocumentTypeCode());
83 crit.addEqualTo(FINANCIAL_SYSTEM_ORIGINATION_CODE, t.getFinancialSystemOriginationCode());
84 crit.addEqualTo(OLEPropertyConstants.DOCUMENT_NUMBER, t.getDocumentNumber());
85
86 ReportQueryByCriteria q = QueryFactory.newReportQuery(Entry.class, crit);
87 q.setAttributes(new String[] { "max(transactionLedgerEntrySequenceNumber)" });
88
89 Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(q);
90
91 BigDecimal max = null;
92 while (iter.hasNext()) {
93 Object[] data = (Object[]) iter.next();
94 max = (BigDecimal) data[0];
95 }
96 if (max == null) {
97 return 0;
98 }
99 else {
100 return max.intValue();
101 }
102 }
103
104
105
106
107
108
109
110 public void purgeYearByChart(String chartOfAccountsCode, int year) {
111 LOG.debug("purgeYearByChart() started");
112
113 Criteria criteria = new Criteria();
114 criteria.addEqualTo(CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
115 criteria.addLessThan(UNIVERISITY_FISCAL_YEAR, new Integer(year));
116
117 getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(Entry.class, criteria));
118
119
120
121
122 getPersistenceBrokerTemplate().clearCache();
123 }
124
125
126
127
128 public Object[] findEntryByGroup(Integer universityFiscalYear, String chartOfAccountsCode, String financialObjectCode, String financialBalanceTypeCode, String universityFiscalPeriodCode, String transactionDebitCreditCode) {
129 Criteria criteria = new Criteria();
130 criteria.addEqualTo(OLEConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME, universityFiscalYear);
131 criteria.addEqualTo(OLEConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, chartOfAccountsCode);
132 criteria.addEqualTo(OLEConstants.FINANCIAL_OBJECT_CODE_PROPERTY_NAME, financialObjectCode);
133 criteria.addEqualTo(OLEConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME, financialBalanceTypeCode);
134 criteria.addEqualTo(OLEConstants.UNIVERSITY_FISCAL_PERIOD_CODE_PROPERTY_NAME, universityFiscalPeriodCode);
135 criteria.addEqualTo(OLEConstants.TRANSACTION_DEBIT_CREDIT_CODE, transactionDebitCreditCode);
136
137 ReportQueryByCriteria reportQuery = QueryFactory.newReportQuery(Entry.class, criteria);
138 reportQuery.setAttributes(new String[] { "count(*)", "sum(" + OLEConstants.TRANSACTION_LEDGER_ENTRY_AMOUNT + ")"});
139 reportQuery.addGroupBy(new String[] { OLEConstants.UNIVERSITY_FISCAL_YEAR_PROPERTY_NAME, OLEConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME, OLEConstants.FINANCIAL_OBJECT_CODE_PROPERTY_NAME, OLEConstants.FINANCIAL_BALANCE_TYPE_CODE_PROPERTY_NAME, OLEConstants.UNIVERSITY_FISCAL_PERIOD_CODE_PROPERTY_NAME, OLEConstants.TRANSACTION_DEBIT_CREDIT_CODE});
140
141 Iterator<Object[]> iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(reportQuery);
142 Object[] returnResult = TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator);
143
144 if (ObjectUtils.isNull(returnResult)) {
145
146 } else if (returnResult[0] instanceof BigDecimal) {
147 returnResult[0] = ((BigDecimal) returnResult[0]).intValue();
148 } else {
149 returnResult[0] = ((Long) returnResult[0]).intValue();
150 }
151
152 return returnResult;
153 }
154
155
156
157
158 public Integer findCountGreaterOrEqualThan(Integer year) {
159 Criteria criteria = new Criteria();
160 criteria.addGreaterOrEqualThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
161
162 ReportQueryByCriteria query = QueryFactory.newReportQuery(Entry.class, criteria);
163
164 return getPersistenceBrokerTemplate().getCount(query);
165 }
166 }