001: /*
002: * Copyright 2005-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 org.apache.commons.lang.StringUtils;
019: import org.kuali.core.document.Document;
020: import org.kuali.core.document.MaintenanceDocument;
021: import org.kuali.core.maintenance.Maintainable;
022: import org.kuali.core.rule.RouteDocumentRule;
023: import org.kuali.core.rule.SaveDocumentRule;
024: import org.kuali.core.util.GlobalVariables;
025: import org.kuali.kfs.KFSKeyConstants;
026: import org.kuali.module.chart.bo.Account;
027:
028: /**
029: * This class provides some basic saving and routing rules for Chart documents
030: */
031: public class ChartRuleBase implements RouteDocumentRule,
032: SaveDocumentRule {
033: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
034: .getLogger(ChartRuleBase.class);
035:
036: /**
037: * This routes the document
038: *
039: * @see org.kuali.core.rule.RouteDocumentRule#processRouteDocument(org.kuali.core.document.Document)
040: */
041: public boolean processRouteDocument(Document document) {
042: MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
043:
044: if (isDocumentValidForRouting(maintenanceDocument)) {
045: return processCustomSaveDocumentBusinessRules((MaintenanceDocument) document);
046: } else {
047: return false;
048: }
049: }
050:
051: /**
052: * This saves the document
053: *
054: * @see org.kuali.core.rule.SaveDocumentRule#processSaveDocument(org.kuali.core.document.Document)
055: */
056: public boolean processSaveDocument(Document document) {
057: MaintenanceDocument maintenanceDocument = (MaintenanceDocument) document;
058:
059: if (isDocumentValidForSave(maintenanceDocument)) {
060: return processCustomSaveDocumentBusinessRules((MaintenanceDocument) document);
061: } else {
062: return false;
063: }
064: }
065:
066: /**
067: * This method should be overridden to provide custom rules for processing document saving
068: *
069: * @param document
070: * @return
071: */
072: protected boolean processCustomSaveDocumentBusinessRules(
073: MaintenanceDocument document) {
074: return true;
075: }
076:
077: /**
078: * This method should be overridden to provide custom rules for processing document routing
079: *
080: * @param document
081: * @return
082: */
083: protected boolean processCustomRouteDocumentBusinessRules(
084: MaintenanceDocument document) {
085: return true;
086: }
087:
088: // Document Validation Helper Methods
089: /**
090: * Performs common validation for Maintenance Document saves.
091: *
092: * @param maintenanceDocument
093: * @return boolean True if the document is valid for saving, false otherwise.
094: */
095: protected boolean isDocumentValidForSave(
096: MaintenanceDocument maintenanceDocument) {
097: if (null == maintenanceDocument) {
098: return false;
099: }
100:
101: boolean valid = true;
102:
103: // do common checks here
104:
105: return valid;
106: }
107:
108: /**
109: * This method performs common validation for Maintenance Document routes.
110: *
111: * @param maintenanceDocument
112: * @return boolean True if the document is valid for routing, false otherwise.
113: */
114: protected boolean isDocumentValidForRouting(
115: MaintenanceDocument maintenanceDocument) {
116: boolean success = true;
117:
118: success &= validateDocument((Document) maintenanceDocument);
119: success &= validateMaintenanceDocument(maintenanceDocument);
120:
121: return success;
122: }
123:
124: private boolean validateDocument(Document document) {
125: boolean success = true;
126:
127: String documentHeaderId = document.getDocumentNumber();
128: if (documentHeaderId == null) {
129: GlobalVariables.getErrorMap().putError("documentHeaderId",
130: KFSKeyConstants.ERROR_REQUIRED);
131: success = false;
132: }
133:
134: return success;
135: }
136:
137: private boolean validateMaintenanceDocument(
138: MaintenanceDocument maintenanceDocument) {
139: boolean success = true;
140:
141: GlobalVariables.getErrorMap().addToErrorPath(
142: "newMaintainableObject");
143: Maintainable newMaintainable = maintenanceDocument
144: .getNewMaintainableObject();
145: if (newMaintainable == null) {
146: GlobalVariables.getErrorMap().putError("",
147: KFSKeyConstants.ERROR_REQUIRED, "Account");
148: success = false;
149: } else {
150: Account newAccount = (Account) newMaintainable
151: .getBusinessObject();
152: if (StringUtils.isBlank(newAccount.getAccountName())) {
153: GlobalVariables.getErrorMap().putError("accountNumber",
154: KFSKeyConstants.ERROR_REQUIRED,
155: "Account Number");
156: success = false;
157: }
158: }
159: GlobalVariables.getErrorMap().removeFromErrorPath(
160: "newMaintainableObject");
161:
162: if (maintenanceDocument.isOldBusinessObjectInDocument()) {
163: GlobalVariables.getErrorMap().addToErrorPath(
164: "oldMaintainableObject");
165: Maintainable oldMaintainable = maintenanceDocument
166: .getOldMaintainableObject();
167: if (oldMaintainable == null) {
168: GlobalVariables.getErrorMap().putError("",
169: KFSKeyConstants.ERROR_REQUIRED, "Account");
170: success = false;
171: } else {
172: Account oldAccount = (Account) oldMaintainable
173: .getBusinessObject();
174: if (StringUtils.isBlank(oldAccount.getAccountName())) {
175: GlobalVariables.getErrorMap().putError(
176: "accountNumber",
177: KFSKeyConstants.ERROR_REQUIRED,
178: "Account Number");
179: success = false;
180: }
181: }
182: GlobalVariables.getErrorMap().removeFromErrorPath(
183: "oldMaintainableObject");
184: }
185:
186: return success;
187: }
188:
189: }
|