001 /** 002 * Copyright 2005-2012 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.rice.kns.datadictionary.validation.charlevel; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.apache.log4j.Logger; 020 import org.kuali.rice.krad.datadictionary.exporter.ExportMap; 021 import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern; 022 023 /** 024 * This is a description of what this class does - ctdang don't forget to fill this in. 025 * 026 * @author Kuali Rice Team (rice.collab@kuali.org) 027 * 028 */ 029 public class RegexValidationPattern extends CharacterLevelValidationPattern { 030 031 private static final long serialVersionUID = -5642894236634278352L; 032 private static final Logger LOG=Logger.getLogger(RegexValidationPattern.class); 033 /** 034 * Regular expression, e.g. "[a-zA-Z0-9]" 035 */ 036 private String pattern; 037 038 private String validationErrorMessageKey; 039 /** 040 * This exports a representation of this instance by an ExportMap. 041 * 042 * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.rice.krad.datadictionary.exporter.ExportMap) 043 */ 044 @Override 045 public void extendExportMap(ExportMap exportMap) { 046 if (LOG.isTraceEnabled()) { 047 String message=String.format("ENTRY %s", 048 (exportMap==null)?"null":exportMap.toString()); 049 LOG.trace(message); 050 } 051 052 // Set element value 053 exportMap.set("type", "regex"); 054 // Set attribute (of the above element) value 055 exportMap.set("pattern", getPattern()); 056 057 if (LOG.isTraceEnabled()) { 058 String message=String.format("EXIT %s", 059 (exportMap==null)?"null":exportMap.toString()); 060 LOG.trace(message); 061 } 062 063 } 064 065 /** 066 * This returns an instance of this class as string. 067 * 068 * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getPatternXml() 069 */ 070 public String getPatternXml() { 071 if (LOG.isTraceEnabled()) { 072 String message=String.format("ENTRY"); 073 LOG.trace(message); 074 } 075 076 StringBuffer xml = new StringBuffer("<regex "); 077 xml.append(pattern); 078 xml.append("/>"); 079 080 if (LOG.isTraceEnabled()) { 081 String message=String.format("EXIT %s", xml.toString()); 082 LOG.trace(message); 083 } 084 085 return xml.toString(); 086 } 087 088 /** 089 * This returns the specified regular expression defined in the data dictionary 090 * entry for validating the value of an attribute. 091 * 092 * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString() 093 */ 094 @Override 095 protected String getRegexString() { 096 if (LOG.isTraceEnabled()) { 097 String message=String.format("ENTRY %s", 098 (pattern==null)?"null":pattern.toString()); 099 LOG.trace(message); 100 } 101 102 if (StringUtils.isEmpty(pattern)) { 103 throw new IllegalStateException(this.getClass().getName()+".pattern is empty"); 104 } 105 106 if (LOG.isTraceEnabled()) { 107 String message=String.format("EXIT"); 108 LOG.trace(message); 109 } 110 111 return pattern; 112 } 113 114 /** 115 * @return the pattern 116 */ 117 public final String getPattern() { 118 return this.pattern; 119 } 120 121 /** 122 * @param pattern the pattern to set 123 */ 124 public final void setPattern(String pattern) { 125 this.pattern = pattern; 126 } 127 128 /** 129 * @return the validationErrorMessageKey 130 */ 131 @Override 132 public String getValidationErrorMessageKey() { 133 return this.validationErrorMessageKey; 134 } 135 136 /** 137 * @param validationErrorMessageKey a message key from the application's message resource bundle signifying the error message 138 * to display if some validation does not match this pattern 139 */ 140 public void setValidationErrorMessageKey(String validationErrorMessageKey) { 141 this.validationErrorMessageKey = validationErrorMessageKey; 142 } 143 144 /** 145 * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#completeValidation() 146 */ 147 @Override 148 public void completeValidation() { 149 super.completeValidation(); 150 if (StringUtils.isBlank(validationErrorMessageKey)) { 151 throw new ValidationPatternException("Regex Validation Patterns must have a validation error message key defined"); 152 } 153 } 154 }