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.kuali.rice.krad.service.KRADServiceLocatorWeb;
33  import org.springframework.transaction.annotation.Transactional;
34  
35  /**
36   * This transactional class provides the default implementation of the services required in ReversalService
37   * 
38   * @see org.kuali.ole.gl.service.ReversalService
39   */
40  @Transactional
41  public class ReversalServiceImpl implements ReversalService {
42      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ReversalServiceImpl.class);
43  
44      private ReversalDao reversalDao;
45      private AccountingPeriodService accountingPeriodService;
46      private UniversityDateService universityDateService;
47  
48      /**
49       * Deletes a reversal record
50       * 
51       * @param re the reversal to delete, remove, or otherwise expiate from the database
52       * @see org.kuali.ole.gl.service.ReversalService#delete(org.kuali.ole.gl.businessobject.Reversal)
53       */
54      public void delete(Reversal re) {
55          LOG.debug("delete() started");
56  
57          reversalDao.delete(re);
58      }
59  
60      /**
61       * Returns all the reversal records set to reverse on or before the given date
62       * 
63       * @param before the date to find reversals on or before
64       * @see org.kuali.ole.gl.service.ReversalService#getByDate(java.util.Date)
65       */
66      public Iterator getByDate(Date before) {
67          LOG.debug("getByDate() started");
68  
69          return reversalDao.getByDate(before);
70      }
71  
72      public Reversal getByTransaction(Transaction t) {
73          LOG.debug("getByTransaction() started");
74  
75          return reversalDao.getByTransaction(t);
76      }
77  
78      /**
79       * Summarizes all of the reversal records set to reverse before or on the given date
80       * @param before the date reversals summarized should be on or before
81       * @return a LedgerEntryHolder with a summary of
82       * @see org.kuali.ole.gl.service.ReversalService#getSummaryByDate(java.util.Date)
83       */
84      public LedgerEntryHolder getSummaryByDate(Date before) {
85          LOG.debug("getSummaryByDate() started");
86  
87          LedgerEntryHolder ledgerEntryHolder = new LedgerEntryHolder();
88  
89          Iterator reversalsIterator = reversalDao.getByDate(before);
90          while (reversalsIterator.hasNext()) {
91              Reversal reversal = (Reversal) reversalsIterator.next();
92              LedgerEntryForReporting ledgerEntry = buildLedgerEntryFromReversal(reversal);
93              ledgerEntryHolder.insertLedgerEntry(ledgerEntry, true);
94          }
95          return ledgerEntryHolder;
96      }
97  
98      /**
99       * Creates a LedgerEntry from a reversal, which is proper for summarization (ie, its fiscal year and period code are based off
100      * the reversal date, not off the transaction date or the reversal's current fiscal year and accounting period)
101      * 
102      * @param reversal reversal to build LedgerEntry with
103      * @return a new LedgerEntry, populated by the reversal
104      */
105     protected LedgerEntryForReporting buildLedgerEntryFromReversal(Reversal reversal) {
106         LedgerEntryForReporting entry = new LedgerEntryForReporting(universityDateService.getFiscalYear(reversal.getFinancialDocumentReversalDate()), accountingPeriodService.getByDate(reversal.getFinancialDocumentReversalDate()).getUniversityFiscalPeriodCode(), reversal.getFinancialBalanceTypeCode(), reversal.getFinancialSystemOriginationCode());
107         if (OLEConstants.GL_CREDIT_CODE.equals(reversal.getTransactionDebitCreditCode())) {
108             entry.setCreditAmount(reversal.getTransactionLedgerEntryAmount());
109             entry.setCreditCount(1);
110         }
111         else if (OLEConstants.GL_DEBIT_CODE.equals(reversal.getTransactionDebitCreditCode())) {
112             entry.setDebitAmount(reversal.getTransactionLedgerEntryAmount());
113             entry.setDebitCount(1);
114         }
115         else {
116             entry.setNoDCAmount(reversal.getTransactionLedgerEntryAmount());
117             entry.setNoDCCount(1);
118         }
119         entry.setRecordCount(1);
120         return entry;
121     }
122 
123     /**
124      * Saves a reversal record
125      * 
126      * @param re the reversal to save
127      * @see org.kuali.ole.gl.service.ReversalService#save(org.kuali.ole.gl.businessobject.Reversal)
128      */
129     public void save(Reversal re) {
130         LOG.debug("save() started");
131 
132         KRADServiceLocatorWeb.getLegacyDataAdapter().save(re);
133     }
134 
135     /**
136      * Sets the reversalDao attribute, allowing injection of an implementation of that data access object
137      * 
138      * @param reversalDao the reversalDao implementation to set
139      * @see org.kuali.ole.gl.dataaccess.ReversalDao
140      */
141     public void setReversalDao(ReversalDao reversalDao) {
142         this.reversalDao = reversalDao;
143     }
144 
145     /**
146      * Sets the accountingPeriodService attribute, allowing injection of an implementation of that service
147      * 
148      * @param accountingPeriodService the accountingPeriodService implementation to set
149      * @see org.kuali.ole.coa.service.AccountingPeriodService
150      */
151     public void setAccountingPeriodService(AccountingPeriodService accountingPeriodService) {
152         this.accountingPeriodService = accountingPeriodService;
153     }
154 
155     /**
156      * Sets the unversityDateService attribute, allowing injection of an implementation of that service
157      * 
158      * @param universityDateService the universityDateService implementation to set
159      * @see org.kuali.ole.sys.service.UniversityDateService
160      */
161     public void setUniversityDateService(UniversityDateService universityDateService) {
162         this.universityDateService = universityDateService;
163     }
164 }