001: /*
002: * Copyright 2006-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016: package org.kuali.module.cg.rules;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.List;
021: import java.util.Map;
022:
023: import org.apache.log4j.Logger;
024: import org.kuali.core.document.MaintenanceDocument;
025: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
026: import org.kuali.core.service.BusinessObjectService;
027: import org.kuali.kfs.KFSKeyConstants;
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.module.cg.bo.Agency;
030: import org.kuali.module.cg.bo.AgencyType;
031:
032: /**
033: * Rules for processing Agency instances.
034: */
035: public class AgencyRule extends MaintenanceDocumentRuleBase {
036: protected static Logger LOG = org.apache.log4j.Logger
037: .getLogger(AgencyRule.class);
038:
039: Agency newAgency;
040: Agency oldAgency;
041:
042: BusinessObjectService businessObjectService;
043:
044: /**
045: * Default constructor.
046: */
047: public AgencyRule() {
048: super ();
049: businessObjectService = SpringContext
050: .getBean(BusinessObjectService.class);
051: }
052:
053: @Override
054: protected boolean processCustomApproveDocumentBusinessRules(
055: MaintenanceDocument document) {
056: LOG
057: .info("Entering AgencyRule.processCustomApproveDocumentBusinessRules");
058: boolean success = super
059: .processCustomApproveDocumentBusinessRules(document);
060:
061: success &= checkAgencyReportsTo(document);
062:
063: LOG
064: .info("Leaving AgencyRule.processCustomApproveDocumentBusinessRules");
065: return success;
066: }
067:
068: @Override
069: protected boolean processCustomRouteDocumentBusinessRules(
070: MaintenanceDocument document) {
071: LOG
072: .info("Entering AgencyRule.processCustomRouteDocumentBusinessRules");
073: boolean success = super
074: .processCustomRouteDocumentBusinessRules(document);
075:
076: success &= checkAgencyReportsTo(document);
077:
078: LOG
079: .info("Leaving AgencyRule.processCustomRouteDocumentBusinessRules");
080: return success;
081: }
082:
083: @Override
084: protected boolean processCustomSaveDocumentBusinessRules(
085: MaintenanceDocument document) {
086: LOG
087: .info("Entering AgencyRule.processCustomSaveDocumentBusinessRules");
088: boolean success = super
089: .processCustomSaveDocumentBusinessRules(document);
090:
091: success &= checkAgencyReportsTo(document);
092: success &= validateAgencyType(document);
093:
094: LOG
095: .info("Leaving AgencyRule.processCustomSaveDocumentBusinessRules");
096: return success;
097: }
098:
099: private boolean validateAgencyType(MaintenanceDocument document) {
100: String agencyType = newAgency.getAgencyTypeCode();
101: Map params = new HashMap();
102: params.put("code", agencyType);
103: Object o = businessObjectService.findByPrimaryKey(
104: AgencyType.class, params);
105: if (null == o) {
106: putFieldError("agencyTypeCode",
107: KFSKeyConstants.ERROR_AGENCY_TYPE_NOT_FOUND,
108: agencyType);
109: return false;
110: }
111: return false;
112: }
113:
114: private boolean checkAgencyReportsTo(MaintenanceDocument document) {
115: boolean success = true;
116:
117: if (newAgency.getReportsToAgencyNumber() != null) {
118: if (newAgency.getReportsToAgency() == null) { // Agency must exist
119:
120: putFieldError("reportsToAgencyNumber",
121: KFSKeyConstants.ERROR_AGENCY_NOT_FOUND,
122: newAgency.getReportsToAgencyNumber());
123: success = false;
124:
125: } else if (!newAgency.getReportsToAgency()
126: .isHistoricalIndicator()) { // Agency must be active. See KULCG-263
127:
128: putFieldError("reportsToAgencyNumber",
129: KFSKeyConstants.ERROR_AGENCY_INACTIVE,
130: newAgency.getReportsToAgencyNumber());
131: success = false;
132:
133: } else if (newAgency.getAgencyNumber().equals(
134: newAgency.getReportsToAgencyNumber())) {
135:
136: putFieldError("reportsToAgencyNumber",
137: KFSKeyConstants.ERROR_AGENCY_REPORTS_TO_SELF,
138: newAgency.getAgencyNumber());
139: success = false;
140:
141: } else { // No circular references
142:
143: List agencies = new ArrayList();
144:
145: Agency agency = newAgency;
146:
147: while (agency.getReportsToAgency() != null && success) {
148: if (!agencies.contains(agency.getAgencyNumber())) {
149: agencies.add(agency.getAgencyNumber());
150: } else {
151:
152: putFieldError(
153: "reportsToAgencyNumber",
154: KFSKeyConstants.ERROR_AGENCY_CIRCULAR_REPORTING,
155: agency.getAgencyNumber());
156: success = false;
157: }
158:
159: agency = agency.getReportsToAgency();
160: }
161: }
162: }
163: return success;
164: }
165:
166: /**
167: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#setupConvenienceObjects()
168: */
169: @Override
170: public void setupConvenienceObjects() {
171: newAgency = (Agency) super .getNewBo();
172: oldAgency = (Agency) super.getOldBo();
173: }
174:
175: }
|