1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.coa.document.validation.impl;
17  
18  import java.util.Iterator;
19  
20  import org.apache.commons.lang.StringUtils;
21  import org.kuali.ole.coa.businessobject.ObjectCode;
22  import org.kuali.ole.coa.businessobject.OrganizationReversionGlobal;
23  import org.kuali.ole.coa.businessobject.OrganizationReversionGlobalDetail;
24  import org.kuali.ole.coa.businessobject.OrganizationReversionGlobalOrganization;
25  import org.kuali.ole.coa.businessobject.options.OrganizationReversionCodeValuesFinder;
26  import org.kuali.ole.coa.document.OrganizationReversionGlobalMaintainableImpl;
27  import org.kuali.ole.coa.service.ObjectCodeService;
28  import org.kuali.ole.coa.service.OrganizationReversionService;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.OLEKeyConstants;
31  import org.kuali.ole.sys.context.SpringContext;
32  import org.kuali.rice.core.api.util.KeyValue;
33  import org.kuali.rice.kns.document.MaintenanceDocument;
34  import org.kuali.rice.krad.bo.PersistableBusinessObject;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.kuali.rice.krad.util.ObjectUtils;
37  
38  
39  
40  
41  
42  public class OrganizationReversionGlobalRule extends GlobalDocumentRuleBase {
43      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionGlobalRule.class);
44      protected OrganizationReversionGlobal globalOrganizationReversion;
45      protected OrganizationReversionService organizationReversionService;
46      protected ObjectCodeService objectCodeService;
47  
48      
49  
50  
51  
52  
53      public OrganizationReversionGlobalRule() {
54          super();
55          setOrganizationReversionService(SpringContext.getBean(OrganizationReversionService.class));
56          setObjectCodeService(SpringContext.getBean(ObjectCodeService.class));
57      }
58  
59      
60  
61  
62  
63  
64  
65  
66  
67      @Override
68      public void setupConvenienceObjects() {
69          this.globalOrganizationReversion = (OrganizationReversionGlobal) super.getNewBo();
70          for (OrganizationReversionGlobalDetail detail : this.globalOrganizationReversion.getOrganizationReversionGlobalDetails()) {
71              detail.refreshNonUpdateableReferences();
72          }
73          for (OrganizationReversionGlobalOrganization org : this.globalOrganizationReversion.getOrganizationReversionGlobalOrganizations()) {
74              org.refreshNonUpdateableReferences();
75          }
76      }
77  
78      
79  
80  
81  
82  
83  
84  
85  
86      @Override
87      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
88          checkSimpleRules(getGlobalOrganizationReversion());
89          return true; 
90      }
91  
92      
93  
94  
95  
96  
97  
98  
99  
100     @Override
101     protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
102         return checkSimpleRules(getGlobalOrganizationReversion());
103     }
104 
105     
106 
107 
108 
109 
110 
111 
112 
113     @Override
114     protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
115         return checkSimpleRules(getGlobalOrganizationReversion());
116     }
117 
118     
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135     @Override
136     public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject line) {
137         boolean success = true;
138         OrganizationReversionGlobal globalOrgRev = (OrganizationReversionGlobal) ((OrganizationReversionGlobalMaintainableImpl) document.getNewMaintainableObject()).getBusinessObject();
139         if (line instanceof OrganizationReversionGlobalDetail) {
140             OrganizationReversionGlobalDetail detail = (OrganizationReversionGlobalDetail) line;
141             success &= checkDetailObjectCodeValidity(globalOrgRev, detail);
142             success &= checkDetailObjectReversionCodeValidity(detail);
143         }
144         else if (line instanceof OrganizationReversionGlobalOrganization) {
145             OrganizationReversionGlobalOrganization org = (OrganizationReversionGlobalOrganization) line;
146             if (!checkEmptyValue(org.getChartOfAccountsCode())) {
147                 GlobalVariables.getMessageMap().putError("chartOfAccountsCode", OLEKeyConstants.ERROR_REQUIRED, "Chart of Accounts Code");
148             }
149             if (!checkEmptyValue(org.getOrganizationCode())) {
150                 GlobalVariables.getMessageMap().putError("organizationCode", OLEKeyConstants.ERROR_REQUIRED, "Organization Code");
151             }
152             if (success) {
153                 success &= checkAllObjectCodesForValidity(globalOrgRev, org);
154                 success &= checkOrganizationChartValidity(org);
155                 success &= checkOrganizationValidity(org);
156                 success &= checkOrganizationReversionForOrganizationExists(globalOrgRev, org);
157                 success &= checkOrganizationIsNotAmongOrgRevOrganizations(globalOrgRev, org);
158             }
159         }
160         return success;
161     }
162 
163     
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174     public boolean checkSimpleRules(OrganizationReversionGlobal globalOrgRev) {
175         boolean success = true;
176 
177         success &= checkBudgetReversionAccountPair(globalOrgRev);
178         success &= checkCashReversionAccountPair(globalOrgRev);
179 
180         success &= areAllDetailsValid(globalOrgRev);
181         success &= areAllOrganizationsValid(globalOrgRev);
182 
183         return success;
184     }
185 
186     
187 
188 
189 
190 
191 
192 
193     public boolean checkBudgetReversionAccountPair(OrganizationReversionGlobal globalOrgRev) {
194         boolean success = true;
195         if ((!StringUtils.isBlank(globalOrgRev.getBudgetReversionChartOfAccountsCode()) && StringUtils.isBlank(globalOrgRev.getBudgetReversionAccountNumber())) || (StringUtils.isBlank(globalOrgRev.getBudgetReversionChartOfAccountsCode()) && !StringUtils.isBlank(globalOrgRev.getBudgetReversionAccountNumber()))) {
196             success = false;
197             GlobalVariables.getMessageMap().putError(MAINTAINABLE_ERROR_PREFIX + "budgetReversionChartOfAccountsCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_BUDGET_REVERSION_INCOMPLETE, new String[] {});
198         }
199         return success;
200     }
201 
202     
203 
204 
205 
206 
207 
208 
209     public boolean checkCashReversionAccountPair(OrganizationReversionGlobal globalOrgRev) {
210         boolean success = true;
211         if ((!StringUtils.isBlank(globalOrgRev.getCashReversionFinancialChartOfAccountsCode()) && StringUtils.isBlank(globalOrgRev.getCashReversionAccountNumber())) || (StringUtils.isBlank(globalOrgRev.getCashReversionFinancialChartOfAccountsCode()) && !StringUtils.isBlank(globalOrgRev.getCashReversionAccountNumber()))) {
212             success = false;
213             GlobalVariables.getMessageMap().putError(MAINTAINABLE_ERROR_PREFIX + "cashReversionFinancialChartOfAccountsCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_CASH_REVERSION_INCOMPLETE, new String[] {});
214         }
215         return success;
216     }
217 
218     
219 
220 
221 
222 
223 
224 
225     public boolean areAllDetailsValid(OrganizationReversionGlobal globalOrgRev) {
226         boolean success = true;
227         for (int i = 0; i < globalOrgRev.getOrganizationReversionGlobalDetails().size(); i++) {
228             OrganizationReversionGlobalDetail detail = globalOrgRev.getOrganizationReversionGlobalDetails().get(i);
229             
230             String errorPath = MAINTAINABLE_ERROR_PREFIX + "organizationReversionGlobalDetails[" + i + "]";
231             GlobalVariables.getMessageMap().addToErrorPath(errorPath);
232 
233             if (!StringUtils.isBlank(detail.getOrganizationReversionObjectCode()) && !StringUtils.isBlank(detail.getOrganizationReversionCode())) {
234                 success &= this.checkDetailOrgReversionCategoryValidity(detail);
235                 success &= this.checkDetailObjectCodeValidity(globalOrgRev, detail);
236                 success &= this.checkDetailObjectReversionCodeValidity(detail);
237             }
238             GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
239         }
240         return success;
241     }
242 
243     
244 
245 
246 
247 
248 
249     public boolean checkDetailOrgReversionCategoryValidity(OrganizationReversionGlobalDetail detail) {
250         boolean success = true;
251         if (StringUtils.isBlank(detail.getOrganizationReversionCategoryCode())) {
252             success = false;
253             GlobalVariables.getMessageMap().putError("organizationReversionCategoryCode", OLEKeyConstants.ERROR_REQUIRED, new String[] {});
254         }
255         else {
256             detail.refreshReferenceObject("organizationReversionCategory");
257             if (detail.getOrganizationReversionCategory() == null || !detail.getOrganizationReversionCategory().isActive()) {
258                 success = false;
259                 GlobalVariables.getMessageMap().putError("organizationReversionCategoryCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_INVALID_ORG_REVERSION_CATEGORY, new String[] { detail.getOrganizationReversionCategoryCode() });
260             }
261         }
262         return success;
263     }
264 
265     
266 
267 
268 
269 
270 
271     public boolean checkDetailObjectCodeValidity(OrganizationReversionGlobal globalOrgRev, OrganizationReversionGlobalDetail detail) {
272         boolean success = true;
273         for (OrganizationReversionGlobalOrganization org : globalOrgRev.getOrganizationReversionGlobalOrganizations()) {
274             if (!validObjectCode(globalOrgRev.getUniversityFiscalYear(), org.getChartOfAccountsCode(), detail.getOrganizationReversionObjectCode())) {
275                 success = false;
276                 GlobalVariables.getMessageMap().putError("organizationReversionObjectCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_OBJECT_CODE_INVALID, new String[] { globalOrgRev.getUniversityFiscalYear().toString(), org.getChartOfAccountsCode(), detail.getOrganizationReversionObjectCode(), org.getChartOfAccountsCode(), org.getOrganizationCode() });
277             }
278         }
279         return success;
280     }
281 
282     
283 
284 
285 
286 
287 
288 
289 
290     public boolean checkAllObjectCodesForValidity(OrganizationReversionGlobal globalOrgRev, OrganizationReversionGlobalOrganization org) {
291         boolean success = true;
292         for (OrganizationReversionGlobalDetail detail : globalOrgRev.getOrganizationReversionGlobalDetails()) {
293             if (!validObjectCode(globalOrgRev.getUniversityFiscalYear(), org.getChartOfAccountsCode(), detail.getOrganizationReversionObjectCode())) {
294                 success = false;
295                 GlobalVariables.getMessageMap().putError("organizationCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_OBJECT_CODE_INVALID, new String[] { globalOrgRev.getUniversityFiscalYear().toString(), org.getChartOfAccountsCode(), detail.getOrganizationReversionObjectCode(), org.getChartOfAccountsCode(), org.getOrganizationCode() });
296             }
297         }
298         return success;
299     }
300 
301     
302 
303 
304 
305 
306 
307 
308 
309     public boolean validObjectCode(Integer universityFiscalYear, String chartOfAccountsCode, String objectCode) {
310         if (!StringUtils.isBlank(objectCode) && universityFiscalYear != null && !StringUtils.isBlank(chartOfAccountsCode)) {
311             ObjectCode objCode = objectCodeService.getByPrimaryId(universityFiscalYear, chartOfAccountsCode, objectCode);
312             return (ObjectUtils.isNotNull(objCode));
313         }
314         else {
315             return true; 
316         }
317     }
318 
319     
320 
321 
322 
323 
324 
325     public boolean checkDetailObjectReversionCodeValidity(OrganizationReversionGlobalDetail detail) {
326         boolean success = true;
327         if (!StringUtils.isBlank(detail.getOrganizationReversionCode())) {
328             boolean foundInList = false;
329             
330             
331             
332             for (Object kvPairObj : new OrganizationReversionCodeValuesFinder().getKeyValues()) {
333                 KeyValue kvPair = (KeyValue) kvPairObj;
334                 if (kvPair.getKey().toString().equals(detail.getOrganizationReversionCode())) {
335                     foundInList = true;
336                     break;
337                 }
338             }
339             if (!foundInList) {
340                 success = false; 
341                 GlobalVariables.getMessageMap().putError("organizationReversionCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_INVALID_ORG_REVERSION_CODE, new String[] { detail.getOrganizationReversionCode() });
342             }
343         }
344         return success;
345     }
346 
347     
348 
349 
350 
351 
352 
353 
354     public boolean areAllOrganizationsValid(OrganizationReversionGlobal globalOrgRev) {
355         boolean success = true;
356         if (globalOrgRev.getOrganizationReversionGlobalOrganizations().size() == 0) {
357             putFieldError(OLEConstants.MAINTENANCE_ADD_PREFIX + "organizationReversionGlobalOrganizations.organizationCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_NO_ORGANIZATIONS);
358         }
359         else {
360             for (int i = 0; i < globalOrgRev.getOrganizationReversionGlobalOrganizations().size(); i++) {
361                 OrganizationReversionGlobalOrganization org = globalOrgRev.getOrganizationReversionGlobalOrganizations().get(i);
362                 String errorPath = MAINTAINABLE_ERROR_PREFIX + "organizationReversionGlobalOrganizations[" + i + "]";
363                 GlobalVariables.getMessageMap().addToErrorPath(errorPath);
364                 success &= checkAllObjectCodesForValidity(globalOrgRev, org);
365                 success &= checkOrganizationValidity(org);
366                 success &= checkOrganizationChartValidity(org);
367                 success &= checkOrganizationReversionForOrganizationExists(globalOrgRev, org);
368                 GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
369             }
370         }
371         return success;
372     }
373 
374     
375 
376 
377 
378 
379 
380 
381 
382     public boolean checkOrganizationChartValidity(OrganizationReversionGlobalOrganization org) {
383         boolean success = true;
384         if (StringUtils.isBlank(org.getChartOfAccountsCode())) {
385             if (!StringUtils.isBlank(org.getOrganizationCode())) {
386                 success = false;
387                 GlobalVariables.getMessageMap().putError("chartOfAccountsCode", OLEKeyConstants.ERROR_REQUIRED, new String[] {});
388             }
389         }
390         else {
391             org.setChartOfAccountsCode(org.getChartOfAccountsCode().toUpperCase());
392             org.refreshReferenceObject("chartOfAccounts");
393             if (org.getChartOfAccounts() == null) {
394                 success = false;
395                 GlobalVariables.getMessageMap().putError("chartOfAccountsCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_INVALID_CHART, new String[] { org.getChartOfAccountsCode() });
396             }
397         }
398         return success;
399     }
400 
401     
402 
403 
404 
405 
406 
407     public boolean checkOrganizationValidity(OrganizationReversionGlobalOrganization org) {
408         boolean success = true;
409         if (StringUtils.isBlank(org.getOrganizationCode())) {
410             if (!StringUtils.isBlank(org.getChartOfAccountsCode())) {
411                 success = false;
412                 GlobalVariables.getMessageMap().putError("organizationCode", OLEKeyConstants.ERROR_REQUIRED, new String[] {});
413             }
414         }
415         else if (!StringUtils.isBlank(org.getChartOfAccountsCode())) {
416             org.refreshReferenceObject("organization");
417             if (org.getOrganization() == null) {
418                 success = false;
419                 GlobalVariables.getMessageMap().putError("organizationCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_INVALID_ORGANIZATION, new String[] { org.getChartOfAccountsCode(), org.getOrganizationCode() });
420             }
421         }
422         return success;
423     }
424 
425     
426 
427 
428 
429 
430 
431 
432 
433     public boolean checkOrganizationReversionForOrganizationExists(OrganizationReversionGlobal globalOrgRev, OrganizationReversionGlobalOrganization org) {
434         boolean success = true;
435         if (globalOrgRev.getUniversityFiscalYear() != null) {
436             if (organizationReversionService.getByPrimaryId(globalOrgRev.getUniversityFiscalYear(), org.getChartOfAccountsCode(), org.getOrganizationCode()) == null) {
437                 success = false;
438                 GlobalVariables.getMessageMap().putError("organizationCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_NO_ORG_REVERSION, new String[] { globalOrgRev.getUniversityFiscalYear().toString(), org.getChartOfAccountsCode(), org.getOrganizationCode() });
439             }
440         }
441         return success;
442     }
443 
444     
445 
446 
447 
448 
449 
450 
451 
452     public boolean checkOrganizationIsNotAmongOrgRevOrganizations(OrganizationReversionGlobal globalOrgRev, OrganizationReversionGlobalOrganization orgRevOrg) {
453         boolean success = true;
454         Iterator<OrganizationReversionGlobalOrganization> iter = globalOrgRev.getOrganizationReversionGlobalOrganizations().iterator();
455         while (iter.hasNext() && success) {
456             OrganizationReversionGlobalOrganization currOrg = iter.next();
457             if (areContainingSameOrganizations(currOrg, orgRevOrg)) {
458                 success = false;
459                 GlobalVariables.getMessageMap().putError("organizationCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_DUPLICATE_ORGS, new String[] { orgRevOrg.getChartOfAccountsCode(), orgRevOrg.getOrganizationCode() });
460             }
461         }
462         return success;
463     }
464 
465     
466 
467 
468 
469 
470 
471 
472     public static boolean areContainingSameOrganizations(OrganizationReversionGlobalOrganization orgRevOrgA, OrganizationReversionGlobalOrganization orgRevOrgB) {
473         boolean containingSame = false;
474         if (orgRevOrgA.getChartOfAccountsCode() != null && orgRevOrgB.getChartOfAccountsCode() != null && orgRevOrgA.getOrganizationCode() != null && orgRevOrgB.getOrganizationCode() != null) {
475             containingSame = (orgRevOrgA.getChartOfAccountsCode().equals(orgRevOrgB.getChartOfAccountsCode()) && orgRevOrgA.getOrganizationCode().equals(orgRevOrgB.getOrganizationCode()));
476         }
477         return containingSame;
478     }
479 
480     public void setOrganizationReversionService(OrganizationReversionService organizationReversionService) {
481         this.organizationReversionService = organizationReversionService;
482     }
483 
484     public void setObjectCodeService(ObjectCodeService objectCodeService) {
485         this.objectCodeService = objectCodeService;
486     }
487 
488     protected OrganizationReversionGlobal getGlobalOrganizationReversion() {
489         return this.globalOrganizationReversion;
490     }
491 }