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.module.cg.document.validation.impl;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.log4j.Logger;
24  import org.kuali.ole.module.cg.businessobject.Agency;
25  import org.kuali.ole.module.cg.businessobject.AgencyType;
26  import org.kuali.ole.sys.OLEKeyConstants;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
30  import org.kuali.rice.krad.service.BusinessObjectService;
31  
32  /**
33   * Rules for processing Agency instances.
34   */
35  public class AgencyRule extends MaintenanceDocumentRuleBase {
36      protected static Logger LOG = org.apache.log4j.Logger.getLogger(AgencyRule.class);
37  
38      protected Agency newAgency;
39      protected Agency oldAgency;
40  
41      BusinessObjectService businessObjectService;
42  
43      /**
44       * Default constructor.
45       */
46      public AgencyRule() {
47          super();
48          businessObjectService = SpringContext.getBean(BusinessObjectService.class);
49      }
50  
51      @Override
52      protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
53          LOG.debug("Entering AgencyRule.processCustomApproveDocumentBusinessRules");
54          boolean success = super.processCustomApproveDocumentBusinessRules(document);
55  
56          success &= checkAgencyReportsTo(document);
57  
58          LOG.info("Leaving AgencyRule.processCustomApproveDocumentBusinessRules");
59          return success;
60      }
61  
62      @Override
63      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
64          LOG.debug("Entering AgencyRule.processCustomRouteDocumentBusinessRules");
65          boolean success = super.processCustomRouteDocumentBusinessRules(document);
66  
67          success &= checkAgencyReportsTo(document);
68  
69          LOG.info("Leaving AgencyRule.processCustomRouteDocumentBusinessRules");
70          return success;
71      }
72  
73      @Override
74      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
75          LOG.debug("Entering AgencyRule.processCustomSaveDocumentBusinessRules");
76          boolean success = super.processCustomSaveDocumentBusinessRules(document);
77  
78          success &= checkAgencyReportsTo(document);
79          success &= validateAgencyType(document);
80  
81          LOG.info("Leaving AgencyRule.processCustomSaveDocumentBusinessRules");
82          return success;
83      }
84  
85      protected boolean validateAgencyType(MaintenanceDocument document) {
86          String agencyType = newAgency.getAgencyTypeCode();
87          Map params = new HashMap();
88          params.put("code", agencyType);
89          Object o = businessObjectService.findByPrimaryKey(AgencyType.class, params);
90          if (null == o) {
91              putFieldError("agencyTypeCode", OLEKeyConstants.ERROR_AGENCY_TYPE_NOT_FOUND, agencyType);
92              return false;
93          }
94          return false;
95      }
96  
97      protected boolean checkAgencyReportsTo(MaintenanceDocument document) {
98          boolean success = true;
99  
100         if (newAgency.getReportsToAgencyNumber() != null) {
101             if (newAgency.getReportsToAgency() == null) { // Agency must exist
102 
103                 putFieldError("reportsToAgencyNumber", OLEKeyConstants.ERROR_AGENCY_NOT_FOUND, newAgency.getReportsToAgencyNumber());
104                 success = false;
105 
106             }
107             else if (!newAgency.getReportsToAgency().isActive()) { // Agency must be active. See KULCG-263
108 
109                 putFieldError("reportsToAgencyNumber", OLEKeyConstants.ERROR_AGENCY_INACTIVE, newAgency.getReportsToAgencyNumber());
110                 success = false;
111 
112             }
113             else if (newAgency.getAgencyNumber().equals(newAgency.getReportsToAgencyNumber())) {
114 
115                 putFieldError("reportsToAgencyNumber", OLEKeyConstants.ERROR_AGENCY_REPORTS_TO_SELF, newAgency.getAgencyNumber());
116                 success = false;
117 
118             }
119             else { // No circular references
120 
121                 List agencies = new ArrayList();
122 
123                 Agency agency = newAgency;
124 
125                 while (agency.getReportsToAgency() != null && success) {
126                     if (!agencies.contains(agency.getAgencyNumber())) {
127                         agencies.add(agency.getAgencyNumber());
128                     }
129                     else {
130 
131                         putFieldError("reportsToAgencyNumber", OLEKeyConstants.ERROR_AGENCY_CIRCULAR_REPORTING, agency.getAgencyNumber());
132                         success = false;
133                     }
134 
135                     agency = agency.getReportsToAgency();
136                 }
137             }
138         }
139         return success;
140     }
141 
142     /**
143      * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
144      */
145     @Override
146     public void setupConvenienceObjects() {
147         newAgency = (Agency) super.getNewBo();
148         oldAgency = (Agency) super.getOldBo();
149     }
150 
151 }