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 java.util.List;
19  
20  import org.kuali.ole.coa.businessobject.OrganizationReversion;
21  import org.kuali.ole.coa.businessobject.OrganizationReversionDetail;
22  import org.kuali.ole.sys.OLEKeyConstants;
23  import org.kuali.rice.kns.document.MaintenanceDocument;
24  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  import org.kuali.rice.krad.util.ObjectUtils;
27  
28  /**
29   * This class implements the business rules specific to the {@link OrganizationReversion} Maintenance Document.
30   */
31  public class OrganizationReversionRule extends MaintenanceDocumentRuleBase {
32  
33      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OrganizationReversionRule.class);
34  
35      protected OrganizationReversion oldOrgReversion;
36      protected OrganizationReversion newOrgReversion;
37  
38      /**
39       * No-Args Constructor for an OrganizationReversionRule.
40       */
41      public OrganizationReversionRule() {
42  
43      }
44  
45      /**
46       * This performs rules checks on document route
47       * <ul>
48       * <li>{@link OrganizationReversionRule#validateDetailBusinessObjects(OrganizationReversion)}</li>
49       * </ul>
50       * This rule fails on business rule failures
51       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
52       */
53      @Override
54      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
55  
56          boolean success = true;
57  
58          // make sure its a valid organization reversion MaintenanceDocument
59          if (!isCorrectMaintenanceClass(document, OrganizationReversion.class)) {
60              throw new IllegalArgumentException("Maintenance Document passed in was of the incorrect type.  Expected " + "'" + OrganizationReversion.class.toString() + "', received " + "'" + document.getOldMaintainableObject().getBoClass().toString() + "'.");
61          }
62  
63          // get the real business object
64          newOrgReversion = (OrganizationReversion) document.getNewMaintainableObject().getBusinessObject();
65  
66          // add check to validate document recursively to get to the collection attributes
67          success &= validateDetailBusinessObjects(newOrgReversion);
68  
69          return success;
70      }
71  
72      /**
73       * Tests each option attached to the main business object and validates its properties.
74       * 
75       * @param orgReversion
76       * @return false if any of the detail objects fail with their validation
77       */
78      protected boolean validateDetailBusinessObjects(OrganizationReversion orgReversion) {
79          GlobalVariables.getMessageMap().addToErrorPath("document.newMaintainableObject");
80          List<OrganizationReversionDetail> details = orgReversion.getOrganizationReversionDetail();
81          int index = 0;
82          int originalErrorCount = GlobalVariables.getMessageMap().getErrorCount();
83          for (OrganizationReversionDetail dtl : details) {
84              String errorPath = "organizationReversionDetail[" + index + "]";
85              GlobalVariables.getMessageMap().addToErrorPath(errorPath);
86              validateOrganizationReversionDetail(dtl);
87              validateOrganizationReversionCode(orgReversion, dtl);
88              GlobalVariables.getMessageMap().removeFromErrorPath(errorPath);
89              index++;
90          }
91          GlobalVariables.getMessageMap().removeFromErrorPath("document.newMaintainableObject");
92          return GlobalVariables.getMessageMap().getErrorCount() == originalErrorCount;
93      }
94      
95      /**
96       * 
97       * This checks to make sure that the organization reversion object on the detail object actually exists
98       * @param detail
99       * @return false if the organization reversion object doesn't exist
100      */
101     protected boolean validateOrganizationReversionDetail(OrganizationReversionDetail detail) {
102         
103         // let's assume this detail will pass the rule
104         boolean result = true;
105         
106         // 1. makes sure the financial object code exists
107         detail.refreshReferenceObject("organizationReversionObject");
108         if (ObjectUtils.isNull(detail.getOrganizationReversionObject())) {
109             LOG.debug("organization reversion finanical object = null");
110             result = false;
111             GlobalVariables.getMessageMap().putError("organizationReversionObjectCode", OLEKeyConstants.ERROR_EXISTENCE, new String[] { "Financial Object Code: " + detail.getOrganizationReversionObjectCode() });
112         } else {
113             if (LOG.isDebugEnabled()) {
114                 LOG.debug("organization reversion finanical object = " + detail.getOrganizationReversionObject().getName());            
115             }
116         }
117         return result;
118     }
119 
120     /**
121      * 
122      * Verifies that a reversion code exists when the 
123      * "Carry Forward by Object Code" indicator is selected.  If this indicator
124      * isn't selected, then the reversion codes isn't required.
125      * 
126      * @param reversion OrganizationReversion object
127      * @param detail OrganizationReversionDetail object
128      * 
129      * @return true for successful validation
130      */
131     protected boolean validateOrganizationReversionCode(OrganizationReversion reversion, OrganizationReversionDetail detail) {
132         
133         //
134         // Assume it will pass!
135         //
136         boolean result = true;
137         
138         //
139         // Only need to verify that organization reversion code exists if the
140         // "Carry Forward by Object Code Indicator" is not selected.
141         //
142         if (reversion.isCarryForwardByObjectCodeIndicator()) {
143             if (ObjectUtils.isNull(detail.getOrganizationReversionCode())) {
144                 result = false;
145                 GlobalVariables.getMessageMap().putError("organizationReversionCode", OLEKeyConstants.ERROR_DOCUMENT_GLOBAL_ORG_REVERSION_NO_REVERSION_CODE);
146             }
147         }
148         return result;
149     }
150 }