View Javadoc

1   /*
2    * Copyright 2007 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.sys.document;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.gl.service.SufficientFundsService;
23  import org.kuali.ole.sys.OLEConstants;
24  import org.kuali.ole.sys.OLEKeyConstants;
25  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
26  import org.kuali.ole.sys.businessobject.SufficientFundsItem;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.service.GeneralLedgerPendingEntryService;
29  import org.kuali.rice.kew.api.exception.WorkflowException;
30  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
31  import org.kuali.rice.krad.exception.ValidationException;
32  import org.kuali.rice.krad.rules.rule.event.ApproveDocumentEvent;
33  import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
34  import org.kuali.rice.krad.rules.rule.event.RouteDocumentEvent;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  /**
38   * Base implementation for a general ledger posting document.
39   */
40  public class GeneralLedgerPostingDocumentBase extends LedgerPostingDocumentBase implements GeneralLedgerPostingDocument {
41      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GeneralLedgerPostingDocumentBase.class);
42  
43      protected List<GeneralLedgerPendingEntry> generalLedgerPendingEntries;
44  
45      /**
46       * Default constructor.
47       */
48      public GeneralLedgerPostingDocumentBase() {
49          super();
50          setGeneralLedgerPendingEntries(new ArrayList<GeneralLedgerPendingEntry>());
51      }
52  
53      /**
54       * @see org.kuali.ole.sys.document.GeneralLedgerPostingDocument#getGeneralLedgerPendingEntries()
55       */
56      public List<GeneralLedgerPendingEntry> getGeneralLedgerPendingEntries() {
57          return generalLedgerPendingEntries;
58      }
59  
60      /**
61       * @see org.kuali.ole.sys.document.GeneralLedgerPostingDocument#getGeneralLedgerPendingEntry(int)
62       */
63      public GeneralLedgerPendingEntry getGeneralLedgerPendingEntry(int index) {
64          while (generalLedgerPendingEntries.size() <= index) {
65              generalLedgerPendingEntries.add(new GeneralLedgerPendingEntry());
66          }
67          return generalLedgerPendingEntries.get(index);
68      }
69  
70      /**
71       * @see org.kuali.ole.sys.document.GeneralLedgerPostingDocument#setGeneralLedgerPendingEntries(java.util.List)
72       */
73      public void setGeneralLedgerPendingEntries(List<GeneralLedgerPendingEntry> generalLedgerPendingEntries) {
74          this.generalLedgerPendingEntries = generalLedgerPendingEntries;
75      }
76  
77      /**
78       * @see org.kuali.ole.sys.document.GeneralLedgerPostingDocument#checkSufficientFunds()
79       */
80      public List<SufficientFundsItem> checkSufficientFunds() {
81          LOG.debug("checkSufficientFunds() started");
82  
83          if (documentPerformsSufficientFundsCheck()) {
84              SufficientFundsService sufficientFundsService = SpringContext.getBean(SufficientFundsService.class);
85              return sufficientFundsService.checkSufficientFunds(this);
86          }
87          else {
88              return new ArrayList<SufficientFundsItem>();
89          }
90      }
91  
92      /**
93       * This method checks to see if SF checking should be done for this document. This was originally part of
94       * SufficientFundsService.checkSufficientFunds() but was externalized so documents that need to override any of the SF methods
95       * can still explicitly check this
96       * 
97       * @return
98       */
99      public boolean documentPerformsSufficientFundsCheck() {
100         // check for reversing entries generated by an error correction.
101         return StringUtils.isBlank(this.getFinancialSystemDocumentHeader().getFinancialDocumentInErrorNumber());
102     }
103 
104     /**
105      * @see org.kuali.ole.sys.document.GeneralLedgerPostingDocument#getPendingLedgerEntriesForSufficientFundsChecking()
106      */
107     public List<GeneralLedgerPendingEntry> getPendingLedgerEntriesForSufficientFundsChecking() {
108         return getGeneralLedgerPendingEntries();
109     }
110 
111     /**
112      * Override to call super and then iterate over all GLPEs and update the approved code appropriately.
113      * 
114      * @see Document#doRouteStatusChange()
115      */
116     @Override
117     public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
118         super.doRouteStatusChange(statusChangeEvent);
119         if (getDocumentHeader().getWorkflowDocument().isProcessed()) {
120             changeGeneralLedgerPendingEntriesApprovedStatusCode(); // update all glpes for doc and set their status to approved
121         }
122         else if (getDocumentHeader().getWorkflowDocument().isCanceled() || getDocumentHeader().getWorkflowDocument().isDisapproved()) {
123             removeGeneralLedgerPendingEntries();
124             if (this instanceof ElectronicPaymentClaiming) {
125                 ((ElectronicPaymentClaiming)this).declaimElectronicPaymentClaims();
126             }
127         }
128     }
129 
130     /**
131      * This method iterates over all of the GLPEs for a document and sets their approved status code to APPROVED "A".
132      */
133     protected void changeGeneralLedgerPendingEntriesApprovedStatusCode() {
134         for (GeneralLedgerPendingEntry glpe : getGeneralLedgerPendingEntries()) {
135             glpe.setFinancialDocumentApprovedCode(OLEConstants.DocumentStatusCodes.APPROVED);
136         }
137     }
138 
139     /**
140      * This method calls the service to remove all of the GLPE's associated with this document
141      */
142     protected void removeGeneralLedgerPendingEntries() {
143         GeneralLedgerPendingEntryService glpeService = SpringContext.getBean(GeneralLedgerPendingEntryService.class);
144         glpeService.delete(getDocumentHeader().getDocumentNumber());
145     }
146 
147     /**
148      * @see org.kuali.rice.krad.document.DocumentBase#toCopy()
149      */
150     @Override
151     public void toCopy() throws WorkflowException {
152         super.toCopy();
153         getGeneralLedgerPendingEntries().clear();
154     }
155 
156     /**
157      * @see org.kuali.rice.krad.document.TransactionalDocumentBase#toErrorCorrection()
158      */
159     @Override
160     public void toErrorCorrection() throws WorkflowException {
161         super.toErrorCorrection();
162         getGeneralLedgerPendingEntries().clear();
163     }
164 
165     @Override
166     public void prepareForSave(KualiDocumentEvent event) {
167         super.prepareForSave(event);
168         // TODO - add OLE wrappers of Rice Events to list
169         if (event instanceof RouteDocumentEvent || event instanceof ApproveDocumentEvent) {
170             // generate general ledger pending entries should be called prior to sufficient funds checking
171             List<SufficientFundsItem> sfItems = checkSufficientFunds();
172             if (!sfItems.isEmpty()) {
173                 for (SufficientFundsItem sfItem : sfItems) {
174                     GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.SufficientFunds.ERROR_INSUFFICIENT_FUNDS, new String[] { sfItem.getAccount().getChartOfAccountsCode(), sfItem.getAccount().getAccountNumber(), StringUtils.isNotBlank(sfItem.getSufficientFundsObjectCode()) ? sfItem.getSufficientFundsObjectCode() : OLEConstants.NOT_AVAILABLE_STRING, sfItem.getAccountSufficientFundsCode() });
175                 }
176                 throw new ValidationException("Insufficient Funds on this Document:");
177             }
178         }
179     }
180     
181     /**
182      * Adds a GeneralLedgerPendingEntry to this document's list of pending entries
183      * @param pendingEntry a pending entry to add
184      */
185     public void addPendingEntry(GeneralLedgerPendingEntry pendingEntry) {
186         pendingEntry.refreshReferenceObject("financialObject");
187         generalLedgerPendingEntries.add(pendingEntry);
188     }
189     
190     /**
191      * This resets this document's list of general ledger pending etnries, though it does not delete those entries (however, the GeneralLedgerPendingEntryService will in most cases when this method is called).
192      */
193     public void clearAnyGeneralLedgerPendingEntries() {
194         generalLedgerPendingEntries = new ArrayList<GeneralLedgerPendingEntry>();
195     }
196 }