001 /** 002 * Copyright 2010 The Kuali Foundation Licensed under the 003 * Educational Community License, Version 2.0 (the "License"); you may 004 * not use this file except in compliance with the License. You may 005 * obtain a copy of the License at 006 * 007 * http://www.osedu.org/licenses/ECL-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, 010 * software distributed under the License is distributed on an "AS IS" 011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 012 * or implied. See the License for the specific language governing 013 * permissions and limitations under the License. 014 */ 015 016 package org.kuali.student.common.validation.dto; 017 018 import java.io.Serializable; 019 import java.util.List; 020 021 import javax.xml.bind.annotation.XmlAccessType; 022 import javax.xml.bind.annotation.XmlAccessorType; 023 import javax.xml.bind.annotation.XmlElement; 024 025 @XmlAccessorType(XmlAccessType.FIELD) 026 public class ValidationResultInfo implements Serializable { 027 028 private static final long serialVersionUID = 1L; 029 030 public enum ErrorLevel { 031 OK(0), WARN(1), ERROR(2); 032 033 int level; 034 035 private ErrorLevel(int level) { 036 this.level = level; 037 } 038 039 public int getLevel() { 040 return level; 041 } 042 043 public static ErrorLevel min(ErrorLevel e1, ErrorLevel e2) { 044 return e1.ordinal() < e2.ordinal() ? e1 : e2; 045 } 046 047 public static ErrorLevel max(ErrorLevel e1, ErrorLevel e2) { 048 return e1.ordinal() > e2.ordinal() ? e1 : e2; 049 } 050 } 051 052 private transient Object invalidData = null; 053 054 @XmlElement 055 protected String element; 056 057 @XmlElement 058 protected ErrorLevel level = ErrorLevel.OK; 059 060 @XmlElement 061 protected String message; 062 063 public ValidationResultInfo() { 064 super(); 065 this.level = ErrorLevel.OK; 066 } 067 068 public ValidationResultInfo(String element) { 069 super(); 070 this.level = ErrorLevel.OK; 071 this.element = element; 072 } 073 074 public ValidationResultInfo(String element, Object invalidData) { 075 this(element); 076 this.invalidData = invalidData; 077 } 078 079 /** 080 * @return the level 081 */ 082 public ErrorLevel getLevel() { 083 return level; 084 } 085 086 /** 087 * @param level 088 * the level to set 089 */ 090 public void setLevel(ErrorLevel level) { 091 this.level = level; 092 } 093 094 /** 095 * @return the message 096 */ 097 public String getMessage() { 098 return message; 099 } 100 101 /** 102 * @param message 103 * the message to set 104 */ 105 public void setMessage(String message) { 106 this.message = message; 107 } 108 109 public String getElement() { 110 return element; 111 } 112 113 public void setElement(String element) { 114 this.element = element; 115 } 116 117 /** 118 * Returns the ValidationResult's error level 119 * 120 * @return 121 */ 122 public ErrorLevel getErrorLevel() { 123 return level; 124 } 125 126 /** 127 * Convenience method. Adds a message with an error level of WARN 128 * 129 * @param message 130 * the message to add 131 */ 132 public void setWarning(String message) { 133 this.level = ErrorLevel.WARN; 134 this.message = message; 135 } 136 137 /** 138 * Convenience method. Adds a message with an error level of ERROR 139 * 140 * @param message 141 * the message to add 142 */ 143 public void setError(String message) { 144 this.level = ErrorLevel.ERROR; 145 this.message = message; 146 } 147 148 /** 149 * Convenience method. Returns true if getErrorLevel() == ErrorLevel.OK 150 * 151 * @return true if getErrorLevel() == ErrorLevel.OK 152 */ 153 public boolean isOk() { 154 return getErrorLevel() == ErrorLevel.OK; 155 } 156 157 /** 158 * Convenience method. Returns true if getErrorLevel() == ErrorLevel.WARN 159 * 160 * @return true if getErrorLevel() == ErrorLevel.WARN 161 */ 162 public boolean isWarn() { 163 return getErrorLevel() == ErrorLevel.WARN; 164 } 165 166 /** 167 * Convenience method. Returns true if getErrorLevel() == ErrorLevel.ERROR 168 * 169 * @return true if getErrorLevel() == ErrorLevel.ERROR 170 */ 171 public boolean isError() { 172 return getErrorLevel() == ErrorLevel.ERROR; 173 } 174 175 public String toString() { 176 return "[" + level + "] Path: [" + element + "] - " + message 177 + " data=[" + invalidData + "]"; 178 } 179 180 /** 181 * Checks if there are any validation errors exceeding the threshold and ignoring the list of fieldPaths passed in 182 * @param validationResults 183 * @param threshold 184 * @param ignoreFields 185 * @return 186 */ 187 public static boolean hasValidationErrors(List<ValidationResultInfo> validationResults, ErrorLevel threshold, List<String> ignoreFields){ 188 if (validationResults != null) { 189 for (ValidationResultInfo validationResult : validationResults) { 190 //Ignore any fields that are in the list 191 if(ignoreFields!=null && !ignoreFields.contains(validationResult.getElement())){ 192 //Return true if any of the validation results exceed your threshold 193 if (validationResult.getLevel() == ErrorLevel.ERROR) { 194 return true; 195 } 196 if (ErrorLevel.WARN.equals(threshold) && validationResult.getLevel() == ErrorLevel.WARN) { 197 return true; 198 } 199 } 200 } 201 } 202 return false; 203 } 204 205 }