1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
35  
36  public class BudgetAdjustmentFundGroupAdjustmentRestrictionValidation extends GenericValidation {
37      private BudgetAdjustmentDocument accountingDocumentForValidation;
38      AccountingLineRuleHelperService accountingLineRuleHelperService;
39      
40      
41  
42  
43  
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          
55          boolean restrictedToSubFund = false;
56          boolean restrictedToChart = false;
57          boolean restrictedToOrg = false;
58          boolean restrictedToAccount = false;
59  
60          
61          String accountRestrictingSubFund = "";
62          String accountRestrictingChart = "";
63          String accountRestrictingOrg = "";
64          String accountRestrictingAccount = "";
65  
66          
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              
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 
107 
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 
174 
175 
176     public BudgetAdjustmentDocument getAccountingDocumentForValidation() {
177         return accountingDocumentForValidation;
178     }
179 
180     
181 
182 
183 
184     public void setAccountingDocumentForValidation(BudgetAdjustmentDocument accountingDocumentForValidation) {
185         this.accountingDocumentForValidation = accountingDocumentForValidation;
186     }
187 
188     
189 
190 
191 
192     public AccountingLineRuleHelperService getAccountingLineRuleHelperService() {
193         return accountingLineRuleHelperService;
194     }
195 
196     
197 
198 
199 
200     public void setAccountingLineRuleHelperService(AccountingLineRuleHelperService accountingLineRuleHelperService) {
201         this.accountingLineRuleHelperService = accountingLineRuleHelperService;
202     }
203 }