View Javadoc
1   /*
2    * Copyright 2008 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.fp.document.validation.impl;
17  
18  import java.util.List;
19  
20  import org.kuali.ole.fp.businessobject.CapitalAccountingLines;
21  import org.kuali.ole.fp.businessobject.CapitalAssetInformation;
22  import org.kuali.ole.fp.document.CapitalAccountingLinesDocumentBase;
23  import org.kuali.ole.fp.document.CapitalAssetEditable;
24  import org.kuali.ole.sys.OLEConstants;
25  import org.kuali.ole.sys.OLEKeyConstants;
26  import org.kuali.ole.sys.document.AccountingDocument;
27  import org.kuali.ole.sys.document.validation.GenericValidation;
28  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29  import org.kuali.rice.core.api.util.type.KualiDecimal;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.ObjectUtils;
32  
33  /**
34   * validate the capital accounting lines associated with the accounting document for validation
35   */
36  public class CapitalAccountingLinesValidations extends GenericValidation {
37      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CapitalAccountingLinesValidations.class);
38  
39      private AccountingDocument accountingDocumentForValidation;
40  
41      /**
42       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
43       */
44      public boolean validate(AttributedDocumentEvent event) {
45          return this.validateCapitalAccountingLines(accountingDocumentForValidation);
46      }
47  
48      // determine whether the given document's all capital accounting lines are processed.
49      protected boolean validateCapitalAccountingLines(AccountingDocument accountingDocumentForValidation) {
50          LOG.debug("validateCapitalAccountingLines - start");
51  
52          boolean isValid = true;
53          
54          if (accountingDocumentForValidation instanceof CapitalAssetEditable == false) {
55              return true;
56          }
57  
58          CapitalAccountingLinesDocumentBase capitalAccountingLinesDocumentBase = (CapitalAccountingLinesDocumentBase) accountingDocumentForValidation;
59          List <CapitalAccountingLines> capitalAccountingLines = capitalAccountingLinesDocumentBase.getCapitalAccountingLines();
60          
61          CapitalAssetEditable capitalAssetEditable = (CapitalAssetEditable) accountingDocumentForValidation;
62  
63          List<CapitalAssetInformation> capitalAssets = capitalAssetEditable.getCapitalAssetInformation();
64  
65          isValid &= allCapitalAccountingLinesProcessed(capitalAccountingLines, capitalAssets);
66          isValid &= capitalAssetExistsForCapitalAccountingLinesProcessed(capitalAccountingLines, capitalAssets);
67          isValid &= totalAmountMatchForCapitalAccountingLinesAndCapitalAssets(capitalAccountingLines, capitalAssets);
68          
69          return isValid;
70      }
71  
72      protected boolean allCapitalAccountingLinesProcessed(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> capitalAssets) {
73          LOG.debug("allCapitalAccountingLinesProcessed - start");
74          
75          boolean processed = true;
76          
77          for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
78              if (!capitalAccountingLine.isSelectLine()) {
79                  processed = false;
80                  GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_ACCOUNTING_LINES_FOR_CAPITALIZATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_FOR_CAPITALIZATION_NOT_PROCESSED);
81                  
82                  break;
83              }
84          }
85          
86          return processed;
87      }
88  
89      protected boolean capitalAssetExistsForCapitalAccountingLinesProcessed(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> capitalAssets) {
90          LOG.debug("capitalAssetExistsForCapitalAccountingLinesProcessed - start");
91          
92          boolean exists = true;
93          
94          for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
95              if (!capitalAssetExist(capitalAccountingLine, capitalAssets)) {
96                  exists = false;
97                  GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_ACCOUNTING_LINES_FOR_CAPITALIZATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_FOR_CAPITALIZATION_HAS_NO_CAPITAL_ASSET, capitalAccountingLine.getSequenceNumber().toString(), capitalAccountingLine.getLineType(), capitalAccountingLine.getChartOfAccountsCode(), capitalAccountingLine.getAccountNumber(), capitalAccountingLine.getFinancialObjectCode());
98                  
99                  break;
100             }
101         }
102         
103         return exists;
104     }
105     
106     /**
107      * 
108      * @param capitalAccountingLine
109      * @param capitalAssetInformation
110      * @return true if accounting line has a capital asset information
111      */
112     protected boolean capitalAssetExist(CapitalAccountingLines capitalAccountingLine, List<CapitalAssetInformation> capitalAssetInformation) {
113         boolean exists = false;
114         
115         if (ObjectUtils.isNull(capitalAssetInformation) && capitalAssetInformation.size() <= 0) {
116             return exists;
117         }
118         
119         for (CapitalAssetInformation capitalAsset : capitalAssetInformation) {
120           //  if (capitalAsset.getSequenceNumber().compareTo(capitalAccountingLine.getSequenceNumber()) == 0 &&
121          //           capitalAsset.getFinancialDocumentLineTypeCode().equals(OLEConstants.SOURCE.equals(capitalAccountingLine.getLineType()) ? OLEConstants.SOURCE_ACCT_LINE_TYPE_CODE : OLEConstants.TARGET_ACCT_LINE_TYPE_CODE) && 
122          //           capitalAsset.getChartOfAccountsCode().equals(capitalAccountingLine.getChartOfAccountsCode()) && 
123          //           capitalAsset.getAccountNumber().equals(capitalAccountingLine.getAccountNumber()) && 
124          //           capitalAsset.getFinancialObjectCode().equals(capitalAccountingLine.getFinancialObjectCode())) {
125                 return true;
126           //  }
127         }
128         
129         return exists;
130     }
131     
132     protected boolean totalAmountMatchForCapitalAccountingLinesAndCapitalAssets(List<CapitalAccountingLines> capitalAccountingLines, List<CapitalAssetInformation> capitalAssets) {
133         boolean totalAmountMatched = true;
134         
135         KualiDecimal capitalAccountingLinesTotals = KualiDecimal.ZERO;
136         KualiDecimal capitalAAssetTotals = KualiDecimal.ZERO;
137         
138         for (CapitalAccountingLines capitalAccountingLine : capitalAccountingLines) {
139             capitalAccountingLinesTotals = capitalAccountingLinesTotals.add(capitalAccountingLine.getAmount());
140         }
141     
142         for (CapitalAssetInformation capitalAsset : capitalAssets) {
143             capitalAAssetTotals = capitalAAssetTotals.add(capitalAsset.getCapitalAssetLineAmount());
144         }
145         
146         if (capitalAccountingLinesTotals.isGreaterThan(capitalAAssetTotals)) {
147             //not all the accounting lines amounts have been distributed to capital assets
148             GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_ACCOUNTING_LINES_FOR_CAPITALIZATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINES_NOT_ALL_TOTALS_DISTRIBUTED_TO_CAPITAL_ASSETS);
149             return false;
150         }
151         
152         if (capitalAccountingLinesTotals.isLessEqual(capitalAAssetTotals)) {
153             //not all the accounting lines amounts have been distributed to capital assets
154             GlobalVariables.getMessageMap().putError(OLEConstants.EDIT_ACCOUNTING_LINES_FOR_CAPITALIZATION_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_FOR_CAPITALIZATION_HAS_NO_CAPITAL_ASSET);
155             return false;
156         }
157         
158         return totalAmountMatched;
159     }
160     
161     
162     /**
163      * Sets the accountingDocumentForValidation attribute value.
164      * 
165      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
166      */
167     public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
168         this.accountingDocumentForValidation = accountingDocumentForValidation;
169     }
170 }