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.Collection;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.ojb.broker.PersistenceBrokerException;
26  import org.kuali.ole.coa.businessobject.BudgetAggregationCode;
27  import org.kuali.ole.coa.businessobject.IndirectCostRecoveryExclusionAccount;
28  import org.kuali.ole.coa.businessobject.ObjectCode;
29  import org.kuali.ole.coa.businessobject.ObjectConsolidation;
30  import org.kuali.ole.coa.businessobject.ObjectLevel;
31  import org.kuali.ole.coa.businessobject.OffsetDefinition;
32  import org.kuali.ole.coa.service.ChartService;
33  import org.kuali.ole.coa.service.ObjectCodeService;
34  import org.kuali.ole.coa.service.ObjectConsService;
35  import org.kuali.ole.coa.service.ObjectLevelService;
36  import org.kuali.ole.sys.OLEConstants;
37  import org.kuali.ole.sys.OLEKeyConstants;
38  import org.kuali.ole.sys.context.SpringContext;
39  import org.kuali.ole.sys.service.UniversityDateService;
40  import org.kuali.rice.core.api.config.property.ConfigurationService;
41  import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
42  import org.kuali.rice.kns.document.MaintenanceDocument;
43  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
44  import org.kuali.rice.krad.service.BusinessObjectService;
45  import org.kuali.rice.krad.util.GlobalVariables;
46  
47  
48  
49  
50  public class ObjectCodeRule extends MaintenanceDocumentRuleBase {
51  
52      protected static ObjectLevelService objectLevelService;
53      protected static ObjectCodeService objectCodeService;
54      protected static ObjectConsService objectConsService;
55  
56      protected static ConfigurationService configService;
57      protected static ChartService chartService;
58      protected Map reportsTo;
59  
60      
61  
62  
63  
64  
65      public ObjectCodeRule() {
66  
67          if (objectConsService == null) {
68              configService = SpringContext.getBean(ConfigurationService.class);
69              objectLevelService = SpringContext.getBean(ObjectLevelService.class);
70              objectCodeService = SpringContext.getBean(ObjectCodeService.class);
71              chartService = SpringContext.getBean(ChartService.class);
72              objectConsService = SpringContext.getBean(ObjectConsService.class);
73          }
74          reportsTo = chartService.getReportsToHierarchy();
75      }
76  
77  
78      
79  
80  
81  
82  
83  
84  
85  
86      @Override
87      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
88  
89          
90          boolean success = true;
91  
92          Object maintainableObject = document.getNewMaintainableObject().getBusinessObject();
93  
94          success &= processObjectCodeRules((ObjectCode) maintainableObject);
95  
96          if (isObjectCodeInactivating(document)) {
97              success &= checkForBlockingOffsetDefinitions((ObjectCode)maintainableObject);
98              success &= checkForBlockingIndirectCostRecoveryExclusionAccounts((ObjectCode)maintainableObject);
99          }
100 
101         return success;
102 
103     }
104 
105     
106 
107 
108 
109 
110 
111 
112     @Override
113     protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
114         LOG.debug("processCustomRouteDocumentBusinessRules called");
115 
116         boolean success = true;
117 
118         Object maintainableObject = document.getNewMaintainableObject().getBusinessObject();
119         success &= processObjectCodeRules((ObjectCode) maintainableObject);
120 
121         if (isObjectCodeInactivating(document)) {
122             success &= checkForBlockingOffsetDefinitions((ObjectCode)maintainableObject);
123             success &= checkForBlockingIndirectCostRecoveryExclusionAccounts((ObjectCode)maintainableObject);
124         }
125 
126         return success;
127     }
128 
129     
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142     protected boolean processObjectCodeRules(ObjectCode objectCode) {
143 
144         boolean result = true;
145 
146         String objCode = objectCode.getFinancialObjectCode();
147 
148         if (!SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(ObjectCode.class, OLEConstants.ChartApcParms.OBJECT_CODE_ILLEGAL_VALUES, objCode).evaluationSucceeds()) {
149             this.putFieldError("financialObjectCode", OLEKeyConstants.ERROR_DOCUMENT_OBJCODE_ILLEGAL, objCode);
150             result = false;
151         }
152 
153         Integer year = objectCode.getUniversityFiscalYear();
154         String chartCode = objectCode.getChartOfAccountsCode();
155         String calculatedReportsToChartCode;
156         String reportsToObjectCode = objectCode.getReportsToFinancialObjectCode();
157         String nextYearObjectCode = objectCode.getNextYearFinancialObjectCode();
158 
159         
160         if (chartCode != null) {
161 
162             
163             
164             
165             
166             
167             calculatedReportsToChartCode = (String) reportsTo.get(chartCode);
168 
169             if (!verifyReportsToChartCode(year, chartCode, objectCode.getFinancialObjectCode(), calculatedReportsToChartCode, reportsToObjectCode)) {
170                 this.putFieldError("reportsToFinancialObjectCode", OLEKeyConstants.ERROR_DOCUMENT_REPORTS_TO_OBJCODE_ILLEGAL, new String[] { reportsToObjectCode, calculatedReportsToChartCode });
171                 result = false;
172             }
173         }
174 
175         String budgetAggregationCode = objectCode.getFinancialBudgetAggregationCd();
176 
177         
178 
179         
180 
181 
182 
183 
184 
185         
186 
187         
188         
189         objectCode.refreshNonUpdateableReferences();
190 
191         
192 
193         if (!this.consolidationTableDoesNotHave(chartCode, objCode)) {
194             this.putFieldError("financialObjectCode", OLEKeyConstants.ERROR_DOCUMENT_OBJCODE_CONSOLIDATION_ERROR, chartCode + "-" + objCode);
195             result = false;
196         }
197 
198         if (!this.objectLevelTableDoesNotHave(chartCode, objCode)) {
199             this.putFieldError("financialObjectCode", OLEKeyConstants.ERROR_DOCUMENT_OBJCODE_LEVEL_ERROR, chartCode + "-" + objCode);
200             result = false;
201         }
202         if (!StringUtils.isEmpty(nextYearObjectCode) && nextYearObjectCodeDoesNotExistThisYear(year, chartCode, nextYearObjectCode)) {
203             this.putFieldError("nextYearFinancialObjectCode", OLEKeyConstants.ERROR_DOCUMENT_OBJCODE_MUST_BEVALID, "Next Year Object Code");
204             result = false;
205         }
206         if (!this.isValidYear(year)) {
207             this.putFieldError("universityFiscalYear", OLEKeyConstants.ERROR_DOCUMENT_OBJCODE_MUST_BEVALID, "Fiscal Year");
208         }
209 
210         result = checkResearchAdminAttributes(objectCode);
211 
212         
213 
214 
215 
216 
217 
218 
219 
220 
221 
222 
223 
224 
225 
226         return result;
227 
228     }
229 
230     
231 
232 
233 
234 
235 
236 
237     public boolean objectLevelTableDoesNotHave(String chartCode, String objectCode) {
238         try {
239             ObjectLevel objLevel = objectLevelService.getByPrimaryId(chartCode, objectCode);
240             if (objLevel != null) {
241                 objLevel.getFinancialObjectLevelCode(); 
242                 return false;
243             }
244         }
245         catch (PersistenceBrokerException e) {
246             
247         }
248 
249         return true;
250     }
251 
252     
253 
254 
255 
256 
257 
258 
259     public boolean consolidationTableDoesNotHave(String chartCode, String objectCode) {
260         try {
261             ObjectConsolidation objectCons = objectConsService.getByPrimaryId(chartCode, objectCode);
262             if (objectCons != null) {
263                 objectCons.getFinConsolidationObjectCode(); 
264                 return false;
265             }
266         }
267         catch (PersistenceBrokerException e) {
268             
269         }
270         return true;
271     }
272 
273     
274 
275 
276 
277 
278 
279 
280 
281     public boolean nextYearObjectCodeDoesNotExistThisYear(Integer year, String chartCode, String objCode) {
282         try {
283             ObjectCode objectCode = objectCodeService.getByPrimaryId(year, chartCode, objCode);
284             if (objectCode != null) {
285                 return false;
286             }
287         }
288         catch (PersistenceBrokerException e) {
289             
290         }
291         return true;
292     }
293 
294     
295 
296 
297 
298 
299 
300     
301 
302 
303 
304     @Deprecated
305     public boolean isValidYear(Integer year) {
306         return true;
307     }
308 
309 
310     
311 
312 
313 
314 
315 
316 
317     protected boolean permitted(Set set, Object value) {
318         if (set != null) {
319             return set.contains(value);
320         }
321         return false;
322     }
323 
324     
325 
326 
327 
328 
329 
330 
331     protected boolean denied(List set, Object value) {
332         if (set != null) {
333             return !set.contains(value);
334         }
335         return true;
336     }
337 
338     
339 
340 
341 
342 
343 
344     protected boolean isLegalBudgetAggregationCode(String budgetAggregationCode) {
345 
346         
347         Map whereMap = new HashMap();
348         whereMap.put("code", budgetAggregationCode);
349 
350         Collection budgetAggregationCodes = getBoService().findMatching(BudgetAggregationCode.class, whereMap);
351 
352         
353         return budgetAggregationCodes.size() > 0;
354     }
355 
356     
357 
358 
359 
360 
361 
362 
363 
364     protected boolean verifyObjectCode(Integer year, String chart, String objectCode) {
365         return null != objectCodeService.getByPrimaryId(year, chart, objectCode);
366     }
367 
368     
369 
370 
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381 
382     protected boolean verifyReportsToChartCode(Integer year, String chart, String objectCode, String reportsToChartCode, String reportsToObjectCode) {
383         
384         
385         
386 
387         
388         
389         if (StringUtils.equals(reportsToChartCode, chart) && StringUtils.equals(reportsToObjectCode, objectCode)) {
390             return true;
391         }
392 
393         
394         return verifyObjectCode(year, reportsToChartCode, reportsToObjectCode);
395     }
396 
397     
398 
399 
400 
401 
402     protected boolean isObjectCodeInactivating(MaintenanceDocument maintenanceDocument) {
403         if (maintenanceDocument.isEdit() && maintenanceDocument.getOldMaintainableObject() != null && maintenanceDocument.getOldMaintainableObject().getBusinessObject() != null) {
404             final ObjectCode oldObjectCode = (ObjectCode)maintenanceDocument.getOldMaintainableObject().getBusinessObject();
405             final ObjectCode newObjectCode = (ObjectCode)maintenanceDocument.getNewMaintainableObject().getBusinessObject();
406 
407             return oldObjectCode.isActive() && !newObjectCode.isActive();
408         }
409         return false;
410     }
411 
412     
413 
414 
415 
416 
417     protected boolean checkForBlockingOffsetDefinitions(ObjectCode objectCode) {
418         final BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
419         boolean result = true;
420 
421         Map<String, Object> keys = new HashMap<String, Object>();
422         keys.put("universityFiscalYear", objectCode.getUniversityFiscalYear());
423         keys.put("chartOfAccountsCode", objectCode.getChartOfAccountsCode());
424         keys.put("financialObjectCode", objectCode.getFinancialObjectCode());
425 
426         final int matchingCount = businessObjectService.countMatching(OffsetDefinition.class, keys);
427         if (matchingCount > 0) {
428             GlobalVariables.getMessageMap().putErrorForSectionId("Edit Object Code",OLEKeyConstants.ERROR_DOCUMENT_OBJECTMAINT_INACTIVATION_BLOCKING,new String[] {(objectCode.getUniversityFiscalYear() != null ? objectCode.getUniversityFiscalYear().toString() : ""), objectCode.getChartOfAccountsCode(), objectCode.getFinancialObjectCode(), Integer.toString(matchingCount), OffsetDefinition.class.getName()});
429             result = false;
430         }
431         return result;
432     }
433 
434     
435 
436 
437 
438 
439     protected boolean checkForBlockingIndirectCostRecoveryExclusionAccounts(ObjectCode objectCode) {
440         boolean result = true;
441 
442         final UniversityDateService universityDateService = SpringContext.getBean(UniversityDateService.class);
443         if (objectCode.getUniversityFiscalYear() != null && objectCode.getUniversityFiscalYear().equals(universityDateService.getCurrentFiscalYear())) {
444             final BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
445 
446             Map<String, Object> keys = new HashMap<String, Object>();
447             keys.put("chartOfAccountsCode", objectCode.getChartOfAccountsCode());
448             keys.put("financialObjectCode", objectCode.getFinancialObjectCode());
449 
450             final int matchingCount = businessObjectService.countMatching(IndirectCostRecoveryExclusionAccount.class, keys);
451             if (matchingCount > 0) {
452                 GlobalVariables.getMessageMap().putErrorForSectionId("Edit Object Code",OLEKeyConstants.ERROR_DOCUMENT_OBJECTMAINT_INACTIVATION_BLOCKING,new String[] {(objectCode.getUniversityFiscalYear() != null ? objectCode.getUniversityFiscalYear().toString() : ""), objectCode.getChartOfAccountsCode(), objectCode.getFinancialObjectCode(), Integer.toString(matchingCount), IndirectCostRecoveryExclusionAccount.class.getName()});
453                 result = false;
454             }
455         }
456         return result;
457     }
458 
459     
460 
461 
462 
463 
464 
465     protected boolean checkResearchAdminAttributes(ObjectCode objectCode) {
466 
467 
468 
469 
470 
471 
472 
473 
474 
475 
476 
477 
478 
479 
480 
481         return true;
482     }
483 }