View Javadoc

1   /**
2    * Copyright 2005-2012 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.rice.kns.datadictionary.validation.charlevel;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
21  import org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern;
22  
23  /**
24   * This is a description of what this class does - ctdang don't forget to fill this in. 
25   * 
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   *
28   */
29  public class RegexValidationPattern extends CharacterLevelValidationPattern {
30  
31      private static final long serialVersionUID = -5642894236634278352L;
32      private static final Logger LOG=Logger.getLogger(RegexValidationPattern.class);
33      /**
34       * Regular expression, e.g. "[a-zA-Z0-9]"
35       */
36      private String pattern;
37  
38      private String validationErrorMessageKey;
39      /**
40       * This exports a representation of this instance by an ExportMap.
41       * 
42       * @see org.kuali.rice.krad.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.rice.krad.datadictionary.exporter.ExportMap)
43       */
44      @Override
45  	public void extendExportMap(ExportMap exportMap) {
46          if (LOG.isTraceEnabled()) {
47              String message=String.format("ENTRY %s",
48                      (exportMap==null)?"null":exportMap.toString());
49              LOG.trace(message);
50          }
51          
52          // Set element value
53          exportMap.set("type", "regex");
54          // Set attribute (of the above element) value
55          exportMap.set("pattern", getPattern());
56  
57          if (LOG.isTraceEnabled()) {
58              String message=String.format("EXIT %s",
59                      (exportMap==null)?"null":exportMap.toString());
60              LOG.trace(message);
61          }
62          
63       }
64  
65      /**
66       * This returns an instance of this class as string.
67       * 
68       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getPatternXml()
69       */
70      public String getPatternXml() {
71          if (LOG.isTraceEnabled()) {
72              String message=String.format("ENTRY");
73              LOG.trace(message);
74          }
75          
76          StringBuffer xml = new StringBuffer("<regex ");
77          xml.append(pattern);
78          xml.append("/>");
79  
80          if (LOG.isTraceEnabled()) {
81              String message=String.format("EXIT %s", xml.toString());
82              LOG.trace(message);
83          }
84          
85          return xml.toString();
86      }
87  
88      /**
89       * This returns the specified regular expression defined in the data dictionary
90       * entry for validating the value of an attribute.
91       * 
92       * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getRegexString()
93       */
94      @Override
95  	protected String getRegexString() {
96          if (LOG.isTraceEnabled()) {
97              String message=String.format("ENTRY %s",
98                      (pattern==null)?"null":pattern.toString());
99              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 }