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.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.ojb.broker.query.Criteria;
25 import org.apache.ojb.broker.query.Query;
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.OJBUtility;
30 import org.kuali.ole.gl.businessobject.Encumbrance;
31 import org.kuali.ole.gl.businessobject.Transaction;
32 import org.kuali.ole.gl.dataaccess.EncumbranceDao;
33 import org.kuali.ole.sys.OLEConstants;
34 import org.kuali.ole.sys.OLEPropertyConstants;
35 import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
36
37
38
39
40 public class EncumbranceDaoOjb extends PlatformAwareDaoBaseOjb implements EncumbranceDao {
41 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EncumbranceDaoOjb.class);
42
43
44
45
46
47
48
49
50 public Encumbrance getEncumbranceByTransaction(Transaction t) {
51 LOG.debug("getEncumbranceByTransaction() started");
52
53 Criteria crit = new Criteria();
54 crit.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, t.getUniversityFiscalYear());
55 crit.addEqualTo(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, t.getChartOfAccountsCode());
56 crit.addEqualTo(OLEPropertyConstants.ACCOUNT_NUMBER, t.getAccountNumber());
57 crit.addEqualTo(OLEPropertyConstants.SUB_ACCOUNT_NUMBER, t.getSubAccountNumber());
58 crit.addEqualTo(OLEPropertyConstants.OBJECT_CODE, t.getFinancialObjectCode());
59 crit.addEqualTo(OLEPropertyConstants.SUB_OBJECT_CODE, t.getFinancialSubObjectCode());
60 crit.addEqualTo(OLEPropertyConstants.BALANCE_TYPE_CODE, t.getFinancialBalanceTypeCode());
61 crit.addEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, t.getFinancialDocumentTypeCode());
62 crit.addEqualTo(OLEPropertyConstants.ORIGIN_CODE, t.getFinancialSystemOriginationCode());
63 crit.addEqualTo(OLEPropertyConstants.DOCUMENT_NUMBER, t.getDocumentNumber());
64
65 QueryByCriteria qbc = QueryFactory.newQuery(Encumbrance.class, crit);
66 return (Encumbrance) getPersistenceBrokerTemplate().getObjectByQuery(qbc);
67 }
68
69
70
71
72
73
74
75
76 public Iterator getEncumbrancesToClose(Integer fiscalYear) {
77
78 Criteria criteria = new Criteria();
79 criteria.addEqualTo(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
80
81 QueryByCriteria query = new QueryByCriteria(Encumbrance.class, criteria);
82 query.addOrderByAscending(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
83 query.addOrderByAscending(OLEPropertyConstants.ACCOUNT_NUMBER);
84 query.addOrderByAscending(OLEPropertyConstants.SUB_ACCOUNT_NUMBER);
85 query.addOrderByAscending(OLEPropertyConstants.OBJECT_CODE);
86 query.addOrderByAscending(OLEPropertyConstants.SUB_OBJECT_CODE);
87 query.addOrderByAscending(OLEPropertyConstants.BALANCE_TYPE_CODE);
88
89 return getPersistenceBrokerTemplate().getIteratorByQuery(query);
90 }
91
92
93
94
95
96
97
98
99 public void purgeYearByChart(String chartOfAccountsCode, int year) {
100 LOG.debug("purgeYearByChart() started");
101
102 Criteria criteria = new Criteria();
103 criteria.addEqualTo(OLEPropertyConstants.CHART, chartOfAccountsCode);
104 criteria.addLessThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, new Integer(year));
105
106 getPersistenceBrokerTemplate().deleteByQuery(new QueryByCriteria(Encumbrance.class, criteria));
107
108
109
110
111
112
113 getPersistenceBrokerTemplate().clearCache();
114 }
115
116
117
118
119
120
121
122 public Iterator getAllEncumbrances() {
123 Criteria criteria = new Criteria();
124 QueryByCriteria query = QueryFactory.newQuery(Encumbrance.class, criteria);
125 return getPersistenceBrokerTemplate().getIteratorByQuery(query);
126 }
127
128
129
130
131
132
133
134
135
136
137 public Iterator getSummarizedEncumbrances(String documentTypeCode, boolean included) {
138 Criteria criteria = new Criteria();
139
140 if (included) {
141 criteria.addEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, documentTypeCode);
142 }
143 else {
144 criteria.addNotEqualTo(OLEPropertyConstants.ENCUMBRANCE_DOCUMENT_TYPE_CODE, documentTypeCode);
145 }
146
147 ReportQueryByCriteria query = QueryFactory.newReportQuery(Encumbrance.class, criteria);
148
149
150 List attributeList = buildAttributeList();
151 String[] attributes = (String[]) attributeList.toArray(new String[attributeList.size()]);
152 query.setAttributes(attributes);
153
154
155 List groupByList = buildGroupByList();
156 String[] groupBy = (String[]) groupByList.toArray(new String[groupByList.size()]);
157 query.addGroupBy(groupBy);
158
159 return getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
160 }
161
162
163
164
165
166
167
168
169 public Iterator findOpenEncumbrance(Map fieldValues, boolean includeZeroEncumbrances) {
170 LOG.debug("findOpenEncumbrance() started");
171
172 Query query = this.getOpenEncumbranceQuery(fieldValues, includeZeroEncumbrances);
173 OJBUtility.limitResultSize(query);
174 return getPersistenceBrokerTemplate().getIteratorByQuery(query);
175 }
176
177
178
179
180
181
182
183
184 public Integer getOpenEncumbranceRecordCount(Map fieldValues, boolean includeZeroEncumbrances) {
185 LOG.debug("getOpenEncumbranceRecordCount() started");
186
187 Query query = this.getOpenEncumbranceQuery(fieldValues, includeZeroEncumbrances);
188 return getPersistenceBrokerTemplate().getCount(query);
189 }
190
191
192
193
194
195
196
197
198 protected Query getOpenEncumbranceQuery(Map fieldValues, boolean includeZeroEncumbrances) {
199 Criteria criteria = OJBUtility.buildCriteriaFromMap(fieldValues, new Encumbrance());
200 criteria.addIn(OLEPropertyConstants.BALANCE_TYPE_CODE, Arrays.asList(OLEConstants.ENCUMBRANCE_BALANCE_TYPE));
201 if (!includeZeroEncumbrances) {
202 Criteria nonZeroEncumbranceCriteria = new Criteria();
203 nonZeroEncumbranceCriteria.addNotEqualToField(OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_AMOUNT, OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_CLOSED_AMOUNT);
204 criteria.addAndCriteria(nonZeroEncumbranceCriteria);
205 }
206 return QueryFactory.newQuery(Encumbrance.class, criteria);
207 }
208
209
210
211
212
213
214 protected List buildAttributeList() {
215 List attributeList = this.buildGroupByList();
216
217 attributeList.add("sum(" + OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_AMOUNT + ")");
218 attributeList.add("sum(" + OLEPropertyConstants.ACCOUNT_LINE_ENCUMBRANCE_CLOSED_AMOUNT + ")");
219
220 return attributeList;
221 }
222
223
224
225
226
227
228 protected List buildGroupByList() {
229 List attributeList = new ArrayList();
230
231 attributeList.add(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
232 attributeList.add(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE);
233 attributeList.add(OLEPropertyConstants.ACCOUNT_NUMBER);
234 attributeList.add(OLEPropertyConstants.SUB_ACCOUNT_NUMBER);
235 attributeList.add(OLEPropertyConstants.OBJECT_CODE);
236 attributeList.add(OLEPropertyConstants.SUB_OBJECT_CODE);
237 attributeList.add(OLEPropertyConstants.BALANCE_TYPE_CODE);
238
239 return attributeList;
240 }
241
242
243
244
245 public Integer findCountGreaterOrEqualThan(Integer year) {
246 Criteria criteria = new Criteria();
247 criteria.addGreaterOrEqualThan(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, year);
248
249 ReportQueryByCriteria query = QueryFactory.newReportQuery(Encumbrance.class, criteria);
250
251 return getPersistenceBrokerTemplate().getCount(query);
252 }
253 }