001/* 002 * Copyright 2006 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.ole.module.cg.document.validation.impl; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.log4j.Logger; 024import org.kuali.ole.module.cg.businessobject.Agency; 025import org.kuali.ole.module.cg.businessobject.AgencyType; 026import org.kuali.ole.sys.OLEKeyConstants; 027import org.kuali.ole.sys.context.SpringContext; 028import org.kuali.rice.kns.document.MaintenanceDocument; 029import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase; 030import org.kuali.rice.krad.service.BusinessObjectService; 031 032/** 033 * Rules for processing Agency instances. 034 */ 035public class AgencyRule extends MaintenanceDocumentRuleBase { 036 protected static Logger LOG = org.apache.log4j.Logger.getLogger(AgencyRule.class); 037 038 protected Agency newAgency; 039 protected Agency oldAgency; 040 041 BusinessObjectService businessObjectService; 042 043 /** 044 * Default constructor. 045 */ 046 public AgencyRule() { 047 super(); 048 businessObjectService = SpringContext.getBean(BusinessObjectService.class); 049 } 050 051 @Override 052 protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) { 053 LOG.debug("Entering AgencyRule.processCustomApproveDocumentBusinessRules"); 054 boolean success = super.processCustomApproveDocumentBusinessRules(document); 055 056 success &= checkAgencyReportsTo(document); 057 058 LOG.info("Leaving AgencyRule.processCustomApproveDocumentBusinessRules"); 059 return success; 060 } 061 062 @Override 063 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) { 064 LOG.debug("Entering AgencyRule.processCustomRouteDocumentBusinessRules"); 065 boolean success = super.processCustomRouteDocumentBusinessRules(document); 066 067 success &= checkAgencyReportsTo(document); 068 069 LOG.info("Leaving AgencyRule.processCustomRouteDocumentBusinessRules"); 070 return success; 071 } 072 073 @Override 074 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) { 075 LOG.debug("Entering AgencyRule.processCustomSaveDocumentBusinessRules"); 076 boolean success = super.processCustomSaveDocumentBusinessRules(document); 077 078 success &= checkAgencyReportsTo(document); 079 success &= validateAgencyType(document); 080 081 LOG.info("Leaving AgencyRule.processCustomSaveDocumentBusinessRules"); 082 return success; 083 } 084 085 protected boolean validateAgencyType(MaintenanceDocument document) { 086 String agencyType = newAgency.getAgencyTypeCode(); 087 Map params = new HashMap(); 088 params.put("code", agencyType); 089 Object o = businessObjectService.findByPrimaryKey(AgencyType.class, params); 090 if (null == o) { 091 putFieldError("agencyTypeCode", OLEKeyConstants.ERROR_AGENCY_TYPE_NOT_FOUND, agencyType); 092 return false; 093 } 094 return false; 095 } 096 097 protected boolean checkAgencyReportsTo(MaintenanceDocument document) { 098 boolean success = true; 099 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}