View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.gl.service.impl;
17  
18  import java.util.Date;
19  import java.util.Iterator;
20  
21  import org.kuali.ole.coa.service.AccountingPeriodService;
22  import org.kuali.ole.gl.businessobject.LedgerEntryForReporting;
23  import org.kuali.ole.gl.businessobject.LedgerEntryHolder;
24  import org.kuali.ole.gl.businessobject.Reversal;
25  import org.kuali.ole.gl.businessobject.Transaction;
26  import org.kuali.ole.gl.dataaccess.ReversalDao;
27  import org.kuali.ole.gl.service.ReversalService;
28  import org.kuali.ole.sys.OLEConstants;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.service.UniversityDateService;
31  import org.kuali.rice.krad.service.BusinessObjectService;
32  import org.springframework.transaction.annotation.Transactional;
33  
34  /**
35   * This transactional class provides the default implementation of the services required in ReversalService
36   * 
37   * @see org.kuali.ole.gl.service.ReversalService
38   */
39  @Transactional
40  public class ReversalServiceImpl implements ReversalService {
41      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ReversalServiceImpl.class);
42  
43      private ReversalDao reversalDao;
44      private AccountingPeriodService accountingPeriodService;
45      private UniversityDateService universityDateService;
46  
47      /**
48       * Deletes a reversal record
49       * 
50       * @param re the reversal to delete, remove, or otherwise expiate from the database
51       * @see org.kuali.ole.gl.service.ReversalService#delete(org.kuali.ole.gl.businessobject.Reversal)
52       */
53      public void delete(Reversal re) {
54          LOG.debug("delete() started");
55  
56          reversalDao.delete(re);
57      }
58  
59      /**
60       * Returns all the reversal records set to reverse on or before the given date
61       * 
62       * @param before the date to find reversals on or before
63       * @see org.kuali.ole.gl.service.ReversalService#getByDate(java.util.Date)
64       */
65      public Iterator getByDate(Date before) {
66          LOG.debug("getByDate() started");
67  
68          return reversalDao.getByDate(before);
69      }
70  
71      public Reversal getByTransaction(Transaction t) {
72          LOG.debug("getByTransaction() started");
73  
74          return reversalDao.getByTransaction(t);
75      }
76  
77      /**
78       * Summarizes all of the reversal records set to reverse before or on the given date
79       * @param before the date reversals summarized should be on or before
80       * @return a LedgerEntryHolder with a summary of
81       * @see org.kuali.ole.gl.service.ReversalService#getSummaryByDate(java.util.Date)
82       */
83      public LedgerEntryHolder getSummaryByDate(Date before) {
84          LOG.debug("getSummaryByDate() started");
85  
86          LedgerEntryHolder ledgerEntryHolder = new LedgerEntryHolder();
87  
88          Iterator reversalsIterator = reversalDao.getByDate(before);
89          while (reversalsIterator.hasNext()) {
90              Reversal reversal = (Reversal) reversalsIterator.next();
91              LedgerEntryForReporting ledgerEntry = buildLedgerEntryFromReversal(reversal);
92              ledgerEntryHolder.insertLedgerEntry(ledgerEntry, true);
93          }
94          return ledgerEntryHolder;
95      }
96  
97      /**
98       * Creates a LedgerEntry from a reversal, which is proper for summarization (ie, its fiscal year and period code are based off
99       * the reversal date, not off the transaction date or the reversal's current fiscal year and accounting period)
100      * 
101      * @param reversal reversal to build LedgerEntry with
102      * @return a new LedgerEntry, populated by the reversal
103      */
104     protected LedgerEntryForReporting buildLedgerEntryFromReversal(Reversal reversal) {
105         LedgerEntryForReporting entry = new LedgerEntryForReporting(universityDateService.getFiscalYear(reversal.getFinancialDocumentReversalDate()), accountingPeriodService.getByDate(reversal.getFinancialDocumentReversalDate()).getUniversityFiscalPeriodCode(), reversal.getFinancialBalanceTypeCode(), reversal.getFinancialSystemOriginationCode());
106         if (OLEConstants.GL_CREDIT_CODE.equals(reversal.getTransactionDebitCreditCode())) {
107             entry.setCreditAmount(reversal.getTransactionLedgerEntryAmount());
108             entry.setCreditCount(1);
109         }
110         else if (OLEConstants.GL_DEBIT_CODE.equals(reversal.getTransactionDebitCreditCode())) {
111             entry.setDebitAmount(reversal.getTransactionLedgerEntryAmount());
112             entry.setDebitCount(1);
113         }
114         else {
115             entry.setNoDCAmount(reversal.getTransactionLedgerEntryAmount());
116             entry.setNoDCCount(1);
117         }
118         entry.setRecordCount(1);
119         return entry;
120     }
121 
122     /**
123      * Saves a reversal record
124      * 
125      * @param re the reversal to save
126      * @see org.kuali.ole.gl.service.ReversalService#save(org.kuali.ole.gl.businessobject.Reversal)
127      */
128     public void save(Reversal re) {
129         LOG.debug("save() started");
130 
131         SpringContext.getBean(BusinessObjectService.class).save(re);
132     }
133 
134     /**
135      * Sets the reversalDao attribute, allowing injection of an implementation of that data access object
136      * 
137      * @param reversalDao the reversalDao implementation to set
138      * @see org.kuali.ole.gl.dataaccess.ReversalDao
139      */
140     public void setReversalDao(ReversalDao reversalDao) {
141         this.reversalDao = reversalDao;
142     }
143 
144     /**
145      * Sets the accountingPeriodService attribute, allowing injection of an implementation of that service
146      * 
147      * @param accountingPeriodService the accountingPeriodService implementation to set
148      * @see org.kuali.ole.coa.service.AccountingPeriodService
149      */
150     public void setAccountingPeriodService(AccountingPeriodService accountingPeriodService) {
151         this.accountingPeriodService = accountingPeriodService;
152     }
153 
154     /**
155      * Sets the unversityDateService attribute, allowing injection of an implementation of that service
156      * 
157      * @param universityDateService the universityDateService implementation to set
158      * @see org.kuali.ole.sys.service.UniversityDateService
159      */
160     public void setUniversityDateService(UniversityDateService universityDateService) {
161         this.universityDateService = universityDateService;
162     }
163 }