001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.module.endow.dataaccess.impl;
017
018 import java.math.BigDecimal;
019 import java.sql.Date;
020 import java.util.ArrayList;
021 import java.util.Collection;
022 import java.util.HashMap;
023 import java.util.Map;
024
025 import org.apache.commons.lang.StringUtils;
026 import org.apache.ojb.broker.query.Criteria;
027 import org.apache.ojb.broker.query.QueryByCriteria;
028 import org.apache.ojb.broker.query.QueryFactory;
029 import org.kuali.kfs.module.endow.EndowConstants;
030 import org.kuali.kfs.module.endow.EndowPropertyConstants;
031 import org.kuali.kfs.module.endow.businessobject.EndowmentAccountingLineBase;
032 import org.kuali.kfs.module.endow.businessobject.FeeEndowmentTransactionCode;
033 import org.kuali.kfs.module.endow.businessobject.FeeMethod;
034 import org.kuali.kfs.module.endow.businessobject.FeeTransaction;
035 import org.kuali.kfs.module.endow.businessobject.TransactionArchive;
036 import org.kuali.kfs.module.endow.dataaccess.EndowmentAccountingLineBaseDao;
037 import org.kuali.kfs.sys.context.SpringContext;
038 import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
039 import org.kuali.rice.kns.service.DataDictionaryService;
040
041 public class EndowmentAccountingLineBaseDaoOjb extends PlatformAwareDaoBaseOjb implements EndowmentAccountingLineBaseDao {
042 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EndowmentAccountingLineBaseDaoOjb.class);
043
044 /**
045 * @@see {@link org.kuali.kfs.module.endow.dataaccess.EndowmentAccountingLineBaseDao#getAllEndowmentAccountingLines(String)
046 */
047 public Collection<EndowmentAccountingLineBase> getAllEndowmentAccountingLines(String documentNumber) {
048 Collection<EndowmentAccountingLineBase> endowmentAccountingLines = new ArrayList();
049
050 Criteria criteria = new Criteria();
051 criteria.addEqualTo(EndowPropertyConstants.DOCUMENT_NUMBER, documentNumber);
052
053 //sort the data on these columns....
054 QueryByCriteria qbc = QueryFactory.newQuery(EndowmentAccountingLineBase.class, criteria);
055
056 qbc.addOrderByAscending(EndowPropertyConstants.ColumnNames.GlInterfaceBatchProcessLine.TRANSACTION_ARCHIVE_FDOC_LN_TYP_CD);
057
058 endowmentAccountingLines = getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
059
060 return endowmentAccountingLines;
061 }
062
063 /**
064 * @@see {@link org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getAllTransactionArchives()
065 */
066 public Collection<TransactionArchive> getAllTransactionArchives() {
067 Collection<TransactionArchive> transactionArchives = new ArrayList();
068
069 Criteria criteria = new Criteria();
070 criteria.addNotNull(EndowPropertyConstants.DOCUMENT_NUMBER);
071
072 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
073 transactionArchives = getPersistenceBrokerTemplate().getCollectionByQuery(query);
074
075 return transactionArchives;
076 }
077
078 /**
079 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getByPrimaryKey(String, int, String)
080 */
081 public TransactionArchive getByPrimaryKey(String documentNumber, int lineNumber, String lineTypeCode) {
082 TransactionArchive transactionArchive = null;
083
084 Criteria criteria = new Criteria();
085 criteria.addEqualTo(EndowPropertyConstants.TRANSACTION_ARCHIVE_DOCUMENT_NUMBER, documentNumber);
086 criteria.addEqualTo(EndowPropertyConstants.TRANSACTION_ARCHIVE_LINE_NUMBER, lineNumber);
087
088 if (SpringContext.getBean(DataDictionaryService.class).getAttributeForceUppercase(TransactionArchive.class, EndowPropertyConstants.TRANSACTION_ARCHIVE_LINE_TYPE_CODE)) {
089 lineTypeCode = lineTypeCode.toUpperCase();
090 }
091 criteria.addEqualTo(EndowPropertyConstants.TRANSACTION_ARCHIVE_LINE_TYPE_CODE, lineTypeCode);
092
093 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
094 transactionArchive = (TransactionArchive) getPersistenceBrokerTemplate().getObjectByQuery(query);
095
096 return transactionArchive;
097 }
098
099 /**
100 * Prepares the criteria to select the records from END_TRAN_ARCHV_T table
101 */
102 protected Collection<TransactionArchive> getTransactionArchivesForTransactions(FeeMethod feeMethod) {
103 Collection<TransactionArchive> transactionArchives = new ArrayList();
104
105 Collection incomePrincipalValues = new ArrayList();
106 incomePrincipalValues.add(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME);
107 incomePrincipalValues.add(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_PRINCIPAL);
108
109 Criteria criteria = new Criteria();
110
111 if (feeMethod.getFeeBaseCode().equalsIgnoreCase(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME_AND_PRINCIPAL)) {
112 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_INCOME_PRINCIPAL_INDICATOR, incomePrincipalValues);
113 }
114 else {
115 if (feeMethod.getFeeBaseCode().equalsIgnoreCase(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME)) {
116 criteria.addEqualTo(EndowPropertyConstants.TRANSACTION_ARCHIVE_INCOME_PRINCIPAL_INDICATOR, EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME);
117
118 }
119 if (feeMethod.getFeeBaseCode().equalsIgnoreCase(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_PRINCIPAL)) {
120 criteria.addEqualTo(EndowPropertyConstants.TRANSACTION_ARCHIVE_INCOME_PRINCIPAL_INDICATOR, EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_PRINCIPAL);
121 }
122 }
123
124 if (feeMethod.getFeeByTransactionType() && feeMethod.getFeeByETranCode()) {
125 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_TYPE_CODE, getTypeCodes(feeMethod.getCode()));
126 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_ETRAN_CODE, getETranCodes(feeMethod.getCode()));
127 }
128 else {
129 if (feeMethod.getFeeByTransactionType()) {
130 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_TYPE_CODE, getTypeCodes(feeMethod.getCode()));
131 }
132
133 if (feeMethod.getFeeByETranCode()) {
134 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_ETRAN_CODE, getETranCodes(feeMethod.getCode()));
135 }
136 }
137
138 if (feeMethod.getFeeByTransactionType() || feeMethod.getFeeByETranCode()) {
139 criteria.addGreaterThan(EndowPropertyConstants.TRANSACTION_ARCHIVE_POSTED_DATE,feeMethod.getFeeLastProcessDate());
140 }
141
142 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
143
144 transactionArchives = getPersistenceBrokerTemplate().getCollectionByQuery(query);
145
146 return transactionArchives;
147 }
148
149 /**
150 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesCountForTransactions(FeeMethod)
151 */
152 public long getTransactionArchivesCountForTransactions(FeeMethod feeMethod) {
153 long totalTransactionArchives = 0;
154 totalTransactionArchives = getTransactionArchivesForTransactions(feeMethod).size();
155
156 return totalTransactionArchives;
157 }
158
159 /**
160 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesIncomeCashAmountForTransactions(FeeMethod)
161 */
162 public BigDecimal getTransactionArchivesIncomeCashAmountForTransactions(FeeMethod feeMethod) {
163 BigDecimal incomeCashAmount = BigDecimal.ZERO;
164
165 Collection<TransactionArchive> transactionArchives = new ArrayList();
166 transactionArchives = getTransactionArchivesForTransactions(feeMethod);
167 for (TransactionArchive transactionArchive : transactionArchives) {
168 incomeCashAmount = incomeCashAmount.add(transactionArchive.getIncomeCashAmount());
169 }
170
171 return incomeCashAmount;
172 }
173
174 /**
175 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesPrincipalCashAmountForTransactions(FeeMethod)
176 */
177 public BigDecimal getTransactionArchivesPrincipalCashAmountForTransactions(FeeMethod feeMethod) {
178 BigDecimal principalCashAmount = new BigDecimal("0");
179
180 Collection<TransactionArchive> transactionArchives = new ArrayList();
181 transactionArchives = getTransactionArchivesForTransactions(feeMethod);
182 for (TransactionArchive transactionArchive : transactionArchives) {
183 principalCashAmount = principalCashAmount.add(transactionArchive.getPrincipalCashAmount());
184 }
185
186 return principalCashAmount;
187 }
188
189 /**
190 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesIncomeAndPrincipalCashAmountForTransactions(FeeMethod)
191 */
192 public HashMap<String, BigDecimal> getTransactionArchivesIncomeAndPrincipalCashAmountForTransactions(FeeMethod feeMethod) {
193 BigDecimal incomeCashAmount = BigDecimal.ZERO;
194 BigDecimal principalCashAmount = BigDecimal.ZERO;
195
196 HashMap<String, BigDecimal> incomeAndPrincipalCashAmounts = new HashMap();
197
198 incomeAndPrincipalCashAmounts.put(EndowPropertyConstants.TRANSACTION_ARCHIVE_INCOME_CASH_AMOUNT, getTransactionArchivesIncomeCashAmountForTransactions(feeMethod));
199 incomeAndPrincipalCashAmounts.put(EndowPropertyConstants.TRANSACTION_ARCHIVE_PRINCIPAL_CASH_AMOUNT, getTransactionArchivesPrincipalCashAmountForTransactions(feeMethod));
200
201 return incomeAndPrincipalCashAmounts;
202 }
203
204 /**
205 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesCountByDocumentTypeName(String, long)
206 */
207 public long getTransactionArchivesCountByDocumentTypeName(String feeMethodCode, Date transactionPostedDate) {
208 long totalTransactionArchives = 0;
209
210 Criteria criteria = new Criteria();
211 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_TYPE_CODE, getTypeCodes(feeMethodCode));
212 criteria.addGreaterThan(EndowPropertyConstants.TRANSACTION_ARCHIVE_POSTED_DATE, transactionPostedDate);
213 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
214
215 totalTransactionArchives = getPersistenceBrokerTemplate().getCollectionByQuery(query).size();
216
217 return totalTransactionArchives;
218 }
219
220 /**
221 * Gets the document Type codes for a given feeMethodCode in END_FEE_TRAN_DOC_TYP_T table
222 * @feeMethodCode FEE_MTH
223 * @return typeCodes
224 */
225 protected Collection getTypeCodes(String feeMethodCode) {
226 Collection typeCodes = new ArrayList();
227 Collection<FeeTransaction> feeTransactions = new ArrayList();
228
229 if (StringUtils.isNotBlank(feeMethodCode)) {
230 Map<String, String> crit = new HashMap<String, String>();
231
232 if (SpringContext.getBean(DataDictionaryService.class).getAttributeForceUppercase(FeeTransaction.class, EndowPropertyConstants.FEE_METHOD_CODE)) {
233 feeMethodCode = feeMethodCode.toUpperCase();
234 }
235
236 Criteria criteria = new Criteria();
237 criteria.addEqualTo(EndowPropertyConstants.FEE_METHOD_CODE, feeMethodCode);
238 criteria.addEqualTo(EndowPropertyConstants.FEE_TRANSACTION_INCLUDE, EndowConstants.YES);
239 QueryByCriteria query = QueryFactory.newQuery(FeeTransaction.class, criteria);
240
241 feeTransactions = getPersistenceBrokerTemplate().getCollectionByQuery(query);
242 for (FeeTransaction feeTransaction : feeTransactions) {
243 typeCodes.add(feeTransaction.getDocumentTypeName());
244 }
245 }
246
247 return typeCodes;
248 }
249
250 /**
251 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesCountByETranCode(String, long)
252 */
253 public long getTransactionArchivesCountByETranCode(String feeMethodCode, Date transactionPostedDate) {
254 long totalTransactionArchives = 0;
255
256 Criteria criteria = new Criteria();
257 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_ETRAN_CODE, getETranCodes(feeMethodCode));
258 criteria.addGreaterThan(EndowPropertyConstants.TRANSACTION_ARCHIVE_POSTED_DATE, transactionPostedDate);
259 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
260
261 totalTransactionArchives = getPersistenceBrokerTemplate().getCollectionByQuery(query).size();
262
263 return totalTransactionArchives;
264 }
265
266 /**
267 * Gets the document Type codes for a given feeMethodCode in END_FEE_TRAN_DOC_TYP_T table
268 * @feeMethodCode FEE_MTH
269 * @return typeCodes
270 */
271 protected Collection getETranCodes(String feeMethodCode) {
272 Collection<FeeEndowmentTransactionCode> feeEndowmentTransactions = new ArrayList();
273 Collection etranCodes = new ArrayList();
274
275 if (StringUtils.isNotBlank(feeMethodCode)) {
276 Map<String, String> crit = new HashMap<String, String>();
277
278 if (SpringContext.getBean(DataDictionaryService.class).getAttributeForceUppercase(FeeEndowmentTransactionCode.class, EndowPropertyConstants.FEE_METHOD_CODE)) {
279 feeMethodCode = feeMethodCode.toUpperCase();
280 }
281
282 Criteria criteria = new Criteria();
283 criteria.addEqualTo(EndowPropertyConstants.FEE_METHOD_CODE, feeMethodCode);
284 criteria.addEqualTo(EndowPropertyConstants.FEE_ENDOWMENT_TRANSACTION_INCLUDE, EndowConstants.YES);
285 QueryByCriteria query = QueryFactory.newQuery(FeeEndowmentTransactionCode.class, criteria);
286
287 feeEndowmentTransactions = getPersistenceBrokerTemplate().getCollectionByQuery(query);
288 for (FeeEndowmentTransactionCode feeEndowmentTransaction : feeEndowmentTransactions) {
289 etranCodes.add(feeEndowmentTransaction.getEndowmentTransactionCode());
290 }
291 }
292
293 return etranCodes;
294 }
295
296 /**
297 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesCountByDocumentTypeNameAndETranCode(String, long)
298 */
299 public long getTransactionArchivesCountByDocumentTypeNameAndETranCode(String feeMethodCode, Date transactionPostedDate) {
300 long totalTransactionArchives = 0;
301
302 Criteria criteria = new Criteria();
303 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_TYPE_CODE, getTypeCodes(feeMethodCode));
304 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_ETRAN_CODE, getETranCodes(feeMethodCode));
305 criteria.addGreaterThan(EndowPropertyConstants.TRANSACTION_ARCHIVE_POSTED_DATE, transactionPostedDate);
306 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
307
308 totalTransactionArchives = getPersistenceBrokerTemplate().getCollectionByQuery(query).size();
309
310 return totalTransactionArchives;
311 }
312
313 /**
314 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesCountByIncomeOrPrincipal(String)
315 */
316 public long getTransactionArchivesCountByIncomeOrPrincipal(String incomeOrPrincipalIndicator) {
317 long totalTransactionArchives = 0;
318
319 Criteria criteria = new Criteria();
320 criteria.addEqualTo(EndowPropertyConstants.TRANSACTION_ARCHIVE_INCOME_PRINCIPAL_INDICATOR, incomeOrPrincipalIndicator);
321 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
322
323 totalTransactionArchives = getPersistenceBrokerTemplate().getCollectionByQuery(query).size();
324
325 return totalTransactionArchives;
326 }
327
328 /**
329 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesCountByBothIncomeAndPrincipal()
330 */
331 public long getTransactionArchivesCountByBothIncomeAndPrincipal(FeeMethod feeMethod) {
332 long totalTransactionArchives = 0;
333
334 Collection incomePrincipalValues = new ArrayList();
335 incomePrincipalValues.add(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_INCOME);
336 incomePrincipalValues.add(EndowConstants.FeeMethod.FEE_BASE_CODE_VALUE_FOR_PRINCIPAL);
337
338 Criteria criteria = new Criteria();
339 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_INCOME_PRINCIPAL_INDICATOR, incomePrincipalValues);
340
341 if (feeMethod.getFeeByTransactionType() && feeMethod.getFeeByETranCode()) {
342 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_TYPE_CODE, getTypeCodes(feeMethod.getCode()));
343 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_ETRAN_CODE, getETranCodes(feeMethod.getCode()));
344 }
345 else {
346 if (feeMethod.getFeeByTransactionType()) {
347 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_TYPE_CODE, getTypeCodes(feeMethod.getCode()));
348 }
349
350 if (feeMethod.getFeeByETranCode()) {
351 criteria.addIn(EndowPropertyConstants.TRANSACTION_ARCHIVE_ETRAN_CODE, getETranCodes(feeMethod.getCode()));
352 }
353 }
354
355 if (feeMethod.getFeeByTransactionType() || feeMethod.getFeeByETranCode()) {
356 criteria.addGreaterThan(EndowPropertyConstants.TRANSACTION_ARCHIVE_POSTED_DATE,feeMethod.getFeeLastProcessDate());
357 }
358
359 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
360
361 totalTransactionArchives = getPersistenceBrokerTemplate().getCollectionByQuery(query).size();
362
363 return totalTransactionArchives;
364 }
365
366 /**
367 * @see org.kuali.kfs.module.endow.dataaccess.TransactionArchiveDao#getTransactionArchivesTotalCashActivity(String)
368 */
369 public BigDecimal getTransactionArchivesTotalCashActivity(String kemid, String securityId) {
370 BigDecimal totalCashActivity = BigDecimal.ZERO;
371
372 Collection<TransactionArchive> transactionArchives = new ArrayList();
373
374 Criteria criteria = new Criteria();
375
376 if (SpringContext.getBean(DataDictionaryService.class).getAttributeForceUppercase(TransactionArchive.class, EndowPropertyConstants.TRANSACTION_ARCHIVE_KEM_ID)) {
377 kemid = kemid.toUpperCase();
378 }
379 criteria.addEqualTo(EndowPropertyConstants.TRANSACTION_ARCHIVE_KEM_ID, kemid);
380
381 QueryByCriteria query = QueryFactory.newQuery(TransactionArchive.class, criteria);
382 transactionArchives = getPersistenceBrokerTemplate().getCollectionByQuery(query);
383
384 for (TransactionArchive transactionArchive : transactionArchives) {
385 if (transactionArchive.getArchiveSecurity().getSecurityId().equals(securityId)) {
386 totalCashActivity = totalCashActivity.add(transactionArchive.getIncomeCashAmount());
387 totalCashActivity = totalCashActivity.add(transactionArchive.getPrincipalCashAmount());
388 }
389 }
390
391 return totalCashActivity;
392 }
393 }