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.chart.rules;
017:
018: import java.util.List;
019:
020: import org.kuali.core.document.MaintenanceDocument;
021: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
022: import org.kuali.core.util.GlobalVariables;
023: import org.kuali.core.util.ObjectUtils;
024: import org.kuali.kfs.KFSKeyConstants;
025: import org.kuali.module.chart.bo.OrganizationReversion;
026: import org.kuali.module.chart.bo.OrganizationReversionDetail;
027:
028: /**
029: * This class implements the business rules specific to the {@link OrganizationReversion} Maintenance Document.
030: */
031: public class OrganizationReversionRule extends
032: MaintenanceDocumentRuleBase {
033:
034: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
035: .getLogger(OrganizationReversionRule.class);
036:
037: OrganizationReversion oldOrgReversion;
038: OrganizationReversion newOrgReversion;
039:
040: /**
041: * No-Args Constructor for an OrganizationReversionRule.
042: */
043: public OrganizationReversionRule() {
044:
045: }
046:
047: /**
048: * This performs rules checks on document route
049: * <ul>
050: * <li>{@link OrganizationReversionRule#validateDetailBusinessObjects(OrganizationReversion)}</li>
051: * </ul>
052: * This rule fails on business rule failures
053: * @see org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.core.document.MaintenanceDocument)
054: */
055: @Override
056: protected boolean processCustomRouteDocumentBusinessRules(
057: MaintenanceDocument document) {
058:
059: boolean success = true;
060:
061: // make sure its a valid organization reversion MaintenanceDocument
062: if (!isCorrectMaintenanceClass(document,
063: OrganizationReversion.class)) {
064: throw new IllegalArgumentException(
065: "Maintenance Document passed in was of the incorrect type. Expected "
066: + "'"
067: + OrganizationReversion.class.toString()
068: + "', received "
069: + "'"
070: + document.getOldMaintainableObject()
071: .getBoClass().toString() + "'.");
072: }
073:
074: // get the real business object
075: newOrgReversion = (OrganizationReversion) document
076: .getNewMaintainableObject().getBusinessObject();
077:
078: // add check to validate document recursively to get to the collection attributes
079: success &= validateDetailBusinessObjects(newOrgReversion);
080:
081: return success;
082: }
083:
084: /**
085: * Tests each option attached to the main business object and validates its properties.
086: *
087: * @param orgReversion
088: * @return false if any of the detail objects fail with their validation
089: */
090: private boolean validateDetailBusinessObjects(
091: OrganizationReversion orgReversion) {
092: GlobalVariables.getErrorMap().addToErrorPath(
093: "document.newMaintainableObject");
094: List<OrganizationReversionDetail> details = orgReversion
095: .getOrganizationReversionDetail();
096: int index = 0;
097: int originalErrorCount = GlobalVariables.getErrorMap()
098: .getErrorCount();
099: for (OrganizationReversionDetail dtl : details) {
100: String errorPath = "organizationReversionDetail[" + index
101: + "]";
102: GlobalVariables.getErrorMap().addToErrorPath(errorPath);
103: validateOrganizationReversionDetail(dtl);
104: GlobalVariables.getErrorMap()
105: .removeFromErrorPath(errorPath);
106: index++;
107: }
108: GlobalVariables.getErrorMap().removeFromErrorPath(
109: "document.newMaintainableObject");
110: return GlobalVariables.getErrorMap().getErrorCount() == originalErrorCount;
111: }
112:
113: /**
114: *
115: * This checks to make sure that the organization reversion object on the detail object actually exists
116: * @param detail
117: * @return false if the organization reversion object doesn't exist
118: */
119: protected boolean validateOrganizationReversionDetail(
120: OrganizationReversionDetail detail) {
121: boolean result = true; // let's assume this detail will pass the rule
122: // 1. makes sure the financial object code exists
123: detail.refreshReferenceObject("organizationReversionObject");
124: LOG.debug("organization reversion finanical object = "
125: + detail.getOrganizationReversionObject());
126: if (ObjectUtils.isNull(detail.getOrganizationReversionObject())) {
127: result = false;
128: GlobalVariables
129: .getErrorMap()
130: .putError(
131: "organizationReversionObjectCode",
132: KFSKeyConstants.ERROR_EXISTENCE,
133: new String[] { "Financial Object Code: "
134: + detail
135: .getOrganizationReversionObjectCode() });
136: }
137: return result;
138: }
139:
140: }
|