1 /* 2 * Copyright 2008 The Kuali Foundation 3 * 4 * Licensed under the Educational Community License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.opensource.org/licenses/ecl2.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.kuali.ole.sys.document.validation; 17 18 import java.util.List; 19 import java.util.Map; 20 21 import org.apache.commons.lang.StringUtils; 22 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent; 23 24 /** 25 * An abstract class that creates an easy way to branch between validations. Basically, 26 * extenders set a branch map - a map where the key is the name of the branch and the value 27 * is the validation to perform to check on that branch. Extenders also implement the 28 * determineBranch method, which returns the name of the branch to validate against; 29 * if null is returned, then no validation will occur. 30 */ 31 public abstract class BranchingValidation extends ParameterizedValidation implements Validation { 32 protected Map<String, Validation> branchMap; 33 protected List<ValidationFieldConvertible> parameterProperties; 34 protected boolean shouldQuitOnFail = false; 35 36 /** 37 * Determines which branch, if any, within the branchMap should be used as the validation to take. 38 * @param event the event which triggered this validation 39 * @return the name of the branch to take, or a null or empty string to not take any branch and simply pass validation as true 40 */ 41 protected abstract String determineBranch(AttributedDocumentEvent event); 42 43 /** 44 * Note: these parameter properties only help determine what branching should take place; these properties will not affect in anyway the branch children 45 * @see org.kuali.ole.sys.document.validation.Validation#getParameterProperties() 46 */ 47 public List<ValidationFieldConvertible> getParameterProperties() { 48 return this.parameterProperties; 49 } 50 51 /** 52 * Sets the parameterProperties attribute value. 53 * @param parameterProperties The parameterProperties to set. 54 */ 55 public void setParameterProperties(List<ValidationFieldConvertible> parameterProperties) { 56 this.parameterProperties = parameterProperties; 57 } 58 59 /** 60 * @see org.kuali.ole.sys.document.validation.Validation#shouldQuitOnFail() 61 */ 62 public boolean shouldQuitOnFail() { 63 return shouldQuitOnFail; 64 } 65 66 /** 67 * Sets the shouldQuitOnFail attribute value. 68 * @param shouldQuitOnFail The shouldQuitOnFail to set. 69 */ 70 public void setShouldQuitOnFail(boolean shouldQuitOnFail) { 71 this.shouldQuitOnFail = shouldQuitOnFail; 72 } 73 74 /** 75 * 76 * @see org.kuali.ole.sys.document.validation.Validation#stageValidation(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent) 77 */ 78 public boolean stageValidation(AttributedDocumentEvent event) { 79 populateParametersFromEvent(event); 80 return validate(event); 81 } 82 83 /** 84 * Using the branch name returned by determineBranch(), validates the event against the corresponding 85 * branch in the branch map. If a null or empty string is returned from determineBrach(), this method 86 * simply returns true; if there is no validation in the branchMap for the given name, an IllegalStateException 87 * is thrown. 88 * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent) 89 */ 90 public boolean validate(AttributedDocumentEvent event) { 91 String branchName = determineBranch(event); 92 if (!StringUtils.isBlank(branchName)) { 93 Validation validation = branchMap.get(branchName); 94 if (validation == null) { 95 throw new IllegalStateException("Branching Validation "+this.getClass().getName()+" cannot find a branch named "+branchName); 96 } 97 return validation.stageValidation(event); 98 } else { 99 return true; 100 } 101 } 102 103 /** 104 * Gets the branchMap attribute. 105 * @return Returns the branchMap. 106 */ 107 public Map<String, Validation> getBranchMap() { 108 return branchMap; 109 } 110 111 /** 112 * Sets the branchMap attribute value. 113 * @param branchMap The branchMap to set. 114 */ 115 public void setBranchMap(Map<String, Validation> branchMap) { 116 this.branchMap = branchMap; 117 } 118 119 }