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.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.kuali.ole.coa.businessobject.SubFundGroup;
23  import org.kuali.ole.fp.businessobject.BudgetAdjustmentAccountingLine;
24  import org.kuali.ole.fp.document.BudgetAdjustmentDocument;
25  import org.kuali.ole.sys.OLEConstants;
26  import org.kuali.ole.sys.OLEKeyConstants;
27  import org.kuali.ole.sys.document.service.AccountingLineRuleHelperService;
28  import org.kuali.ole.sys.document.validation.GenericValidation;
29  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
30  import org.kuali.rice.krad.util.GlobalVariables;
31  import org.kuali.rice.krad.util.MessageMap;
32  
33  /**
34   * Validation for Budget Adjustment document that checks that the fund groups are correctly adjusted.
35   */
36  public class BudgetAdjustmentFundGroupAdjustmentRestrictionValidation extends GenericValidation {
37      private BudgetAdjustmentDocument accountingDocumentForValidation;
38      AccountingLineRuleHelperService accountingLineRuleHelperService;
39      
40      /**
41       * Retrieves the fund group and sub fund group for each accounting line. Then verifies that the codes associated with the
42       * 'Budget Adjustment Restriction Code' field are met.
43       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
44       */
45      public boolean validate(AttributedDocumentEvent event) {
46          MessageMap errors = GlobalVariables.getMessageMap();
47  
48          boolean isAdjustmentAllowed = true;
49  
50          List accountingLines = new ArrayList();
51          accountingLines.addAll(getAccountingDocumentForValidation().getSourceAccountingLines());
52          accountingLines.addAll(getAccountingDocumentForValidation().getTargetAccountingLines());
53  
54          // fund group is global restriction
55          boolean restrictedToSubFund = false;
56          boolean restrictedToChart = false;
57          boolean restrictedToOrg = false;
58          boolean restrictedToAccount = false;
59  
60          // fields to help with error messages
61          String accountRestrictingSubFund = "";
62          String accountRestrictingChart = "";
63          String accountRestrictingOrg = "";
64          String accountRestrictingAccount = "";
65  
66          // first find the restriction level required by the fund or sub funds used on document
67          String restrictionLevel = "";
68          for (Iterator iter = accountingLines.iterator(); iter.hasNext();) {
69              BudgetAdjustmentAccountingLine line = (BudgetAdjustmentAccountingLine) iter.next();
70              SubFundGroup subFund = line.getAccount().getSubFundGroup();
71              if (!OLEConstants.BudgetAdjustmentDocumentConstants.ADJUSTMENT_RESTRICTION_LEVEL_NONE.equals(subFund.getFundGroupBudgetAdjustmentRestrictionLevelCode())) {
72                  restrictionLevel = subFund.getFundGroupBudgetAdjustmentRestrictionLevelCode();
73                  restrictedToSubFund = true;
74                  accountRestrictingSubFund = line.getAccountNumber();
75              }
76              else {
77                  restrictionLevel = subFund.getFundGroup().getFundGroupBudgetAdjustmentRestrictionLevelCode();
78              }
79  
80              if (OLEConstants.BudgetAdjustmentDocumentConstants.ADJUSTMENT_RESTRICTION_LEVEL_CHART.equals(restrictionLevel)) {
81                  restrictedToChart = true;
82                  accountRestrictingChart = line.getAccountNumber();
83              }
84              else if (OLEConstants.BudgetAdjustmentDocumentConstants.ADJUSTMENT_RESTRICTION_LEVEL_ORGANIZATION.equals(restrictionLevel)) {
85                  restrictedToOrg = true;
86                  accountRestrictingOrg = line.getAccountNumber();
87              }
88              else if (OLEConstants.BudgetAdjustmentDocumentConstants.ADJUSTMENT_RESTRICTION_LEVEL_ACCOUNT.equals(restrictionLevel)) {
89                  restrictedToAccount = true;
90                  accountRestrictingAccount = line.getAccountNumber();
91              }
92  
93              // if we have a sub fund restriction, this overrides anything coming later
94              if (restrictedToSubFund) {
95                  break;
96              }
97          }
98  
99          String fundLabel = accountingLineRuleHelperService.getFundGroupCodeLabel();
100         String subFundLabel = accountingLineRuleHelperService.getSubFundGroupCodeLabel();
101         String chartLabel = accountingLineRuleHelperService.getChartLabel();
102         String orgLabel = accountingLineRuleHelperService.getOrganizationCodeLabel();
103         String acctLabel = accountingLineRuleHelperService.getAccountLabel();
104 
105         /*
106          * now iterate through the accounting lines again and check each record against the previous to verify the restrictions are
107          * met
108          */
109         BudgetAdjustmentAccountingLine previousLine = null;
110         for (Iterator iter = accountingLines.iterator(); iter.hasNext();) {
111             BudgetAdjustmentAccountingLine line = (BudgetAdjustmentAccountingLine) iter.next();
112 
113             if (previousLine != null) {
114                 String currentFundGroup = line.getAccount().getSubFundGroup().getFundGroupCode();
115                 String previousFundGroup = previousLine.getAccount().getSubFundGroup().getFundGroupCode();
116 
117                 if (!currentFundGroup.equals(previousFundGroup)) {
118                     errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_MIXED_FUND_GROUPS);
119                     isAdjustmentAllowed = false;
120                     break;
121                 }
122 
123                 if (restrictedToSubFund) {
124                     if (!line.getAccount().getSubFundGroupCode().equals(previousLine.getAccount().getSubFundGroupCode())) {
125                         errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_RESTRICTION_LEVELS, new String[] { accountRestrictingSubFund, subFundLabel });
126                         isAdjustmentAllowed = false;
127                         break;
128                     }
129                 }
130 
131                 if (restrictedToChart) {
132                     if (!line.getChartOfAccountsCode().equals(previousLine.getChartOfAccountsCode())) {
133                         if (restrictedToSubFund) {
134                             errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_RESTRICTION_LEVELS, new String[] { accountRestrictingChart, subFundLabel + " and " + chartLabel });
135                         }
136                         else {
137                             errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_RESTRICTION_LEVELS, new String[] { accountRestrictingChart, fundLabel + " and " + chartLabel });
138                         }
139                         isAdjustmentAllowed = false;
140                         break;
141                     }
142                 }
143 
144                 if (restrictedToOrg) {
145                     if (!line.getAccount().getOrganizationCode().equals(previousLine.getAccount().getOrganizationCode())) {
146                         if (restrictedToSubFund) {
147                             errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_RESTRICTION_LEVELS, new String[] { accountRestrictingOrg, subFundLabel + " and " + orgLabel });
148                         }
149                         else {
150                             errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_RESTRICTION_LEVELS, new String[] { accountRestrictingOrg, fundLabel + " and " + orgLabel });
151                         }
152                         isAdjustmentAllowed = false;
153                         break;
154                     }
155                 }
156 
157                 if (restrictedToAccount) {
158                     if (!line.getAccountNumber().equals(previousLine.getAccountNumber())) {
159                         errors.putErrorWithoutFullErrorPath(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.ERROR_DOCUMENT_BA_RESTRICTION_LEVELS, new String[] { accountRestrictingAccount, acctLabel });
160                         isAdjustmentAllowed = false;
161                         break;
162                     }
163                 }
164             }
165 
166             previousLine = line;
167         }
168 
169         return isAdjustmentAllowed;
170     }
171 
172     /**
173      * Gets the accountingDocumentForValidation attribute. 
174      * @return Returns the accountingDocumentForValidation.
175      */
176     public BudgetAdjustmentDocument getAccountingDocumentForValidation() {
177         return accountingDocumentForValidation;
178     }
179 
180     /**
181      * Sets the accountingDocumentForValidation attribute value.
182      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
183      */
184     public void setAccountingDocumentForValidation(BudgetAdjustmentDocument accountingDocumentForValidation) {
185         this.accountingDocumentForValidation = accountingDocumentForValidation;
186     }
187 
188     /**
189      * Gets the accountingLineRuleHelperService attribute. 
190      * @return Returns the accountingLineRuleHelperService.
191      */
192     public AccountingLineRuleHelperService getAccountingLineRuleHelperService() {
193         return accountingLineRuleHelperService;
194     }
195 
196     /**
197      * Sets the accountingLineRuleHelperService attribute value.
198      * @param accountingLineRuleHelperService The accountingLineRuleHelperService to set.
199      */
200     public void setAccountingLineRuleHelperService(AccountingLineRuleHelperService accountingLineRuleHelperService) {
201         this.accountingLineRuleHelperService = accountingLineRuleHelperService;
202     }
203 }