001: /*
002: * Copyright 2005-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.gl.batch.poster.impl;
017:
018: import java.sql.Timestamp;
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.Date;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.apache.ojb.broker.metadata.MetadataManager;
027: import org.kuali.core.service.DateTimeService;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.module.gl.GLConstants;
030: import org.kuali.module.gl.batch.poster.EncumbranceCalculator;
031: import org.kuali.module.gl.batch.poster.PostTransaction;
032: import org.kuali.module.gl.batch.poster.VerifyTransaction;
033: import org.kuali.module.gl.bo.Encumbrance;
034: import org.kuali.module.gl.bo.Entry;
035: import org.kuali.module.gl.bo.Transaction;
036: import org.kuali.module.gl.dao.EncumbranceDao;
037: import org.springframework.transaction.annotation.Transactional;
038:
039: /**
040: * This implementation of PostTransaction posts a transaction that could be an encumbrance
041: */
042: @Transactional
043: public class PostEncumbrance implements PostTransaction,
044: VerifyTransaction, EncumbranceCalculator {
045: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
046: .getLogger(PostEncumbrance.class);
047:
048: private EncumbranceDao encumbranceDao;
049: private DateTimeService dateTimeService;
050:
051: public void setEncumbranceDao(EncumbranceDao ed) {
052: encumbranceDao = ed;
053: }
054:
055: /**
056: * Constructs a PostEncumbrance instance
057: */
058: public PostEncumbrance() {
059: super ();
060: }
061:
062: /**
063: * Make sure the transaction is correct for posting. If there is an error, this will stop the transaction from posting in all
064: * files.
065: *
066: * @param t the transaction to verify
067: * @return a List of error messages, as Strings
068: */
069: public List verifyTransaction(Transaction t) {
070: LOG.debug("verifyTransaction() started");
071:
072: List errors = new ArrayList();
073:
074: // The encumbrance update code can only be space, N, R or D. Nothing else
075: if ((StringUtils.isNotBlank(t
076: .getTransactionEncumbranceUpdateCode()))
077: && (!" "
078: .equals(t.getTransactionEncumbranceUpdateCode()))
079: && (!KFSConstants.ENCUMB_UPDT_NO_ENCUMBRANCE_CD
080: .equals(t.getTransactionEncumbranceUpdateCode()))
081: && (!KFSConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD
082: .equals(t.getTransactionEncumbranceUpdateCode()))
083: && (!KFSConstants.ENCUMB_UPDT_DOCUMENT_CD.equals(t
084: .getTransactionEncumbranceUpdateCode()))) {
085: errors.add("Invalid Encumbrance Update Code ("
086: + t.getTransactionEncumbranceUpdateCode() + ")");
087: }
088:
089: return errors;
090: }
091:
092: /**
093: * Called by the poster to post a transaction. The transaction might or might not be an encumbrance transaction.
094: *
095: * @param t the transaction which is being posted
096: * @param mode the mode the poster is currently running in
097: * @param postDate the date this transaction should post to
098: * @return the accomplished post type
099: */
100: public String post(Transaction t, int mode, Date postDate) {
101: LOG.debug("post() started");
102:
103: String returnCode = GLConstants.UPDATE_CODE;
104:
105: // If the encumbrance update code is space or N, or the object type code is FB
106: // we don't need to post an encumbrance
107: if ((StringUtils.isBlank(t
108: .getTransactionEncumbranceUpdateCode()))
109: || " ".equals(t.getTransactionEncumbranceUpdateCode())
110: || KFSConstants.ENCUMB_UPDT_NO_ENCUMBRANCE_CD.equals(t
111: .getTransactionEncumbranceUpdateCode())
112: || t.getOption().getFinObjectTypeFundBalanceCd()
113: .equals(t.getFinancialObjectTypeCode())) {
114: LOG.debug("post() not posting non-encumbrance transaction");
115: return "";
116: }
117:
118: // Get the current encumbrance record if there is one
119: Entry e = new Entry(t, null);
120: if (KFSConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t
121: .getTransactionEncumbranceUpdateCode())) {
122: e
123: .setDocumentNumber(t
124: .getReferenceFinancialDocumentNumber());
125: e.setFinancialSystemOriginationCode(t
126: .getReferenceFinancialSystemOriginationCode());
127: e.setFinancialDocumentTypeCode(t
128: .getReferenceFinancialDocumentTypeCode());
129: }
130:
131: Encumbrance enc = encumbranceDao.getEncumbranceByTransaction(e);
132: if (enc == null) {
133: // Build a new encumbrance record
134: enc = new Encumbrance(e);
135:
136: returnCode = GLConstants.INSERT_CODE;
137: } else {
138: // Use the one retrieved
139: if (enc.getTransactionEncumbranceDate() == null) {
140: enc.setTransactionEncumbranceDate(t
141: .getTransactionDate());
142: }
143:
144: returnCode = GLConstants.UPDATE_CODE;
145: }
146:
147: updateEncumbrance(t, enc);
148:
149: enc.setTimestamp(new Timestamp(postDate.getTime()));
150:
151: encumbranceDao.save(enc);
152:
153: return returnCode;
154: }
155:
156: /**
157: * Given a Collection of encumbrances, returns the encumbrance that would affected by the given transaction
158: *
159: * @param encumbranceList a Collection of encumbrances
160: * @param t the transaction to find the appropriate encumbrance for
161: * @return the encumbrance found from the list, or, if not found, a newly created encumbrance
162: * @see org.kuali.module.gl.batch.poster.EncumbranceCalculator#findEncumbrance(java.util.Collection, org.kuali.module.gl.bo.Transaction)
163: */
164: public Encumbrance findEncumbrance(Collection encumbranceList,
165: Transaction t) {
166:
167: // If it isn't an encumbrance transaction, skip it
168: if ((!KFSConstants.ENCUMB_UPDT_DOCUMENT_CD.equals(t
169: .getTransactionEncumbranceUpdateCode()))
170: && (!KFSConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD
171: .equals(t.getTransactionEncumbranceUpdateCode()))) {
172: return null;
173: }
174:
175: // Try to find one that already exists
176: for (Iterator iter = encumbranceList.iterator(); iter.hasNext();) {
177: Encumbrance e = (Encumbrance) iter.next();
178:
179: if (KFSConstants.ENCUMB_UPDT_DOCUMENT_CD.equals(t
180: .getTransactionEncumbranceUpdateCode())
181: && e.getUniversityFiscalYear().equals(
182: t.getUniversityFiscalYear())
183: && e.getChartOfAccountsCode().equals(
184: t.getChartOfAccountsCode())
185: && e.getAccountNumber()
186: .equals(t.getAccountNumber())
187: && e.getSubAccountNumber().equals(
188: t.getSubAccountNumber())
189: && e.getObjectCode().equals(
190: t.getFinancialObjectCode())
191: && e.getSubObjectCode().equals(
192: t.getFinancialSubObjectCode())
193: && e.getBalanceTypeCode().equals(
194: t.getFinancialBalanceTypeCode())
195: && e.getDocumentTypeCode().equals(
196: t.getFinancialDocumentTypeCode())
197: && e.getOriginCode().equals(
198: t.getFinancialSystemOriginationCode())
199: && e.getDocumentNumber().equals(
200: t.getDocumentNumber())) {
201: return e;
202: }
203:
204: if (KFSConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t
205: .getTransactionEncumbranceUpdateCode())
206: && e.getUniversityFiscalYear().equals(
207: t.getUniversityFiscalYear())
208: && e.getChartOfAccountsCode().equals(
209: t.getChartOfAccountsCode())
210: && e.getAccountNumber()
211: .equals(t.getAccountNumber())
212: && e.getSubAccountNumber().equals(
213: t.getSubAccountNumber())
214: && e.getObjectCode().equals(
215: t.getFinancialObjectCode())
216: && e.getSubObjectCode().equals(
217: t.getFinancialSubObjectCode())
218: && e.getBalanceTypeCode().equals(
219: t.getFinancialBalanceTypeCode())
220: && e.getDocumentTypeCode().equals(
221: t.getReferenceFinancialDocumentTypeCode())
222: && e
223: .getOriginCode()
224: .equals(
225: t
226: .getReferenceFinancialSystemOriginationCode())
227: && e.getDocumentNumber().equals(
228: t.getReferenceFinancialDocumentNumber())) {
229: return e;
230: }
231: }
232:
233: // If we couldn't find one that exists, create a new one
234:
235: // NOTE: the date doesn't matter so there is no need to call the date service
236: // Changed to use the datetime service because of KULRNE-4183
237: Entry e = new Entry(t, dateTimeService.getCurrentDate());
238: if (KFSConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t
239: .getTransactionEncumbranceUpdateCode())) {
240: e
241: .setDocumentNumber(t
242: .getReferenceFinancialDocumentNumber());
243: e.setFinancialSystemOriginationCode(t
244: .getReferenceFinancialSystemOriginationCode());
245: e.setFinancialDocumentTypeCode(t
246: .getReferenceFinancialDocumentTypeCode());
247: }
248:
249: Encumbrance enc = new Encumbrance(e);
250: encumbranceList.add(enc);
251: return enc;
252: }
253:
254: /**
255: * @param t
256: * @param enc
257: */
258: public void updateEncumbrance(Transaction t, Encumbrance enc) {
259: if (KFSConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(t
260: .getTransactionEncumbranceUpdateCode())) {
261: // If using referring doc number, add or subtract transaction amount from
262: // encumbrance closed amount
263: if (KFSConstants.GL_DEBIT_CODE.equals(t
264: .getTransactionDebitCreditCode())) {
265: enc.setAccountLineEncumbranceClosedAmount(enc
266: .getAccountLineEncumbranceClosedAmount()
267: .subtract(t.getTransactionLedgerEntryAmount()));
268: } else {
269: enc.setAccountLineEncumbranceClosedAmount(enc
270: .getAccountLineEncumbranceClosedAmount().add(
271: t.getTransactionLedgerEntryAmount()));
272: }
273: } else {
274: // If not using referring doc number, add or subtract transaction amount from
275: // encumbrance amount
276: if (KFSConstants.GL_DEBIT_CODE.equals(t
277: .getTransactionDebitCreditCode())
278: || KFSConstants.GL_BUDGET_CODE.equals(t
279: .getTransactionDebitCreditCode())) {
280: enc.setAccountLineEncumbranceAmount(enc
281: .getAccountLineEncumbranceAmount().add(
282: t.getTransactionLedgerEntryAmount()));
283: } else {
284: enc.setAccountLineEncumbranceAmount(enc
285: .getAccountLineEncumbranceAmount().subtract(
286: t.getTransactionLedgerEntryAmount()));
287: }
288: }
289: }
290:
291: /**
292: * @see org.kuali.module.gl.batch.poster.PostTransaction#getDestinationName()
293: */
294: public String getDestinationName() {
295: return MetadataManager.getInstance().getGlobalRepository()
296: .getDescriptorFor(Encumbrance.class).getFullTableName();
297: }
298:
299: public void setDateTimeService(DateTimeService dateTimeService) {
300: this.dateTimeService = dateTimeService;
301: }
302: }
|