View Javadoc
1   /*
2    * Copyright 2006 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.coa.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.coa.businessobject.ReportingCode;
20  import org.kuali.ole.sys.OLEKeyConstants;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.rice.kns.document.MaintenanceDocument;
23  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
24  import org.kuali.rice.krad.bo.PersistableBusinessObject;
25  import org.kuali.rice.krad.service.BusinessObjectService;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  /**
29   * 
30   * This class implements the business rules specific to the {@link ReportingCodes} Maintenance Document.
31   */
32  public class ReportingCodesRule extends MaintenanceDocumentRuleBase {
33  
34      protected ReportingCode oldReportingCode;
35      protected ReportingCode newReportingCode;
36  
37      protected BusinessObjectService businessObjectService;
38  
39      /**
40       * 
41       * Constructs a ReportingCodesRule and pseudo-injects services
42       */
43      public ReportingCodesRule() {
44          super();
45          setBusinessObjectService((BusinessObjectService) SpringContext.getBean(BusinessObjectService.class));
46      }
47  
48      /**
49       * This performs rules checks on document route
50       * <ul>
51       * <li>{@link ProjectCodeRule#checkReportsToReportingCode()}</li>
52       * </ul>
53       * This rule fails on business rule failures
54       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
55       */
56      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
57          boolean success = true;
58          setupConvenienceObjects(document);
59          success &= checkReportsToReportingCode();
60          return success;
61      }
62  
63      /**
64       * This performs rules checks on document save
65       * <ul>
66       * <li>{@link ProjectCodeRule#checkReportsToReportingCode()}</li>
67       * </ul>
68       * This rule does not fail on business rule failures
69       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
70       */
71      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
72          boolean success = true;
73          setupConvenienceObjects(document);
74          checkReportsToReportingCode();
75          return success;
76      }
77  
78      /**
79       * 
80       * This method sets the convenience objects like newReportingCode and oldReportingCode, so you have short and easy handles to the new and
81       * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
82       * all sub-objects from the DB by their primary keys, if available.
83       * 
84       * @param document
85       */
86      protected void setupConvenienceObjects(MaintenanceDocument document) {
87  
88          // setup oldAccount convenience objects, make sure all possible sub-objects are populated
89          oldReportingCode = (ReportingCode) super.getOldBo();
90  
91          // setup newAccount convenience objects, make sure all possible sub-objects are populated
92          newReportingCode = (ReportingCode) super.getNewBo();
93      }
94  
95      /**
96       * 
97       * This checks to see if the user has entered in two different values for the reporting code and the 
98       * reports to reporting code. If they are different then it makes sure that the reports to reporting code actually exists
99       * in the system.
100      * @return true if the reports to reporting code is filled and exists or true if it isn't filled in (doesn't need to be), false otherwise
101      */
102     protected boolean checkReportsToReportingCode() {
103         boolean success = true;
104         boolean oneMissing = false;
105         boolean bothMissing = false;
106         boolean doExistenceTest = false;
107 
108         // if one of the codes is blank but the other isnt (ie, they are different), then
109         // do the existence test
110         if (StringUtils.isBlank(newReportingCode.getFinancialReportingCode()) && StringUtils.isBlank(newReportingCode.getFinancialReportsToReportingCode())) {
111             bothMissing = true;
112         }
113         else if (StringUtils.isBlank(newReportingCode.getFinancialReportingCode()) || StringUtils.isBlank(newReportingCode.getFinancialReportsToReportingCode())) {
114             oneMissing = true;
115         }
116         if (oneMissing && !bothMissing) {
117             doExistenceTest = true;
118         }
119 
120         // if both codes are there, but they are different, then do the existence test
121         if (StringUtils.isNotBlank(newReportingCode.getFinancialReportingCode())) {
122             if (!newReportingCode.getFinancialReportingCode().equalsIgnoreCase(newReportingCode.getFinancialReportsToReportingCode())) {
123                 doExistenceTest = true;
124             }
125         }
126 
127         // if these two aren't equal then we need to make sure that the object exists
128         if (doExistenceTest) {
129 
130             // attempt to retrieve the specified object from the db
131             PersistableBusinessObject referenceBo;
132             referenceBo = (PersistableBusinessObject)businessObjectService.getReferenceIfExists((PersistableBusinessObject) newReportingCode, "reportingCodes");
133             if (!ObjectUtils.isNotNull(referenceBo)) {
134                 putFieldError("financialReportsToReportingCode", OLEKeyConstants.ERROR_EXISTENCE, "Reports To Reporting Code");
135                 success &= false;
136             }
137         }
138         return success;
139     }
140 
141     protected void setBusinessObjectService(BusinessObjectService boService) {
142         businessObjectService = boService;
143     }
144 
145 }