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