View Javadoc
1   /**
2    * Copyright 2005-2015 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.krad.datadictionary.validation;
17  
18  import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
19  import org.kuali.rice.krad.util.KRADConstants;
20  
21  import java.util.regex.Pattern;
22  
23  /**
24   * Abstraction of the regular expressions used to validate attribute values.
25   */
26  @Deprecated
27  abstract public class CharacterLevelValidationPattern extends ValidationPattern {
28      protected Pattern regexPattern;
29  
30      protected int maxLength = -1;
31      protected int exactLength = -1;
32  
33      /**
34       * Sets maxLength parameter for the associated regex.
35       *
36       * @param maxLength
37       */
38      public void setMaxLength(int maxLength) {
39          if (this.exactLength != -1) {
40              throw new IllegalStateException(
41                      "illegal attempt to set maxLength after mutually-exclusive exactLength has been set");
42          }
43  
44          this.maxLength = maxLength;
45      }
46  
47      /**
48       * @return current maxLength, or -1 if none has been set
49       */
50      public int getMaxLength() {
51          return maxLength;
52      }
53  
54      /**
55       * Sets exactLength parameter for the associated regex.
56       *
57       * @param exactLength
58       */
59      public void setExactLength(int exactLength) {
60          if (this.maxLength != -1) {
61              throw new IllegalStateException(
62                      "illegal attempt to set exactLength after mutually-exclusive maxLength has been set");
63          }
64  
65          this.exactLength = exactLength;
66      }
67  
68      /**
69       * @return current exactLength, or -1 if none has been set
70       */
71      public int getExactLength() {
72          return exactLength;
73      }
74  
75      /**
76       * @return regular expression Pattern generated using the individual ValidationPattern subclass
77       */
78      final public Pattern getRegexPattern() {
79          if (regexPattern == null) {
80              String regexString = getRegexString();
81  
82              StringBuffer completeRegex = new StringBuffer("^");
83              completeRegex.append(getRegexString());
84  
85              if (maxLength != -1) {
86                  completeRegex.append("{0," + maxLength + "}");
87              } else if (exactLength != -1) {
88                  completeRegex.append("{" + exactLength + "}");
89              } else {
90                  completeRegex.append("*");
91              }
92  
93              completeRegex.append("$");
94  
95              regexPattern = Pattern.compile(completeRegex.toString());
96          }
97          return regexPattern;
98      }
99  
100     /**
101      * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
102      */
103     final public ExportMap buildExportMap(String exportKey) {
104         ExportMap exportMap = new ExportMap(exportKey);
105 
106         if (getMaxLength() != -1) {
107             exportMap.set("maxLength", Integer.toString(getMaxLength()));
108         } else if (getExactLength() != -1) {
109             exportMap.set("exactLength", Integer.toString(getExactLength()));
110         }
111 
112         extendExportMap(exportMap);
113 
114         return exportMap;
115     }
116 
117     /**
118      * Extends the given (parent class) exportMap as needed to represent subclass instances
119      *
120      * @param exportMap
121      */
122     abstract public void extendExportMap(ExportMap exportMap);
123 
124     @Override
125     public String[] getValidationErrorMessageParameters(String attributeLabel) {
126         if (getMaxLength() != -1) {
127             return new String[]{attributeLabel, String.valueOf(getMaxLength())};
128         }
129         if (getExactLength() != -1) {
130             return new String[]{attributeLabel, String.valueOf(getExactLength())};
131         }
132         return new String[]{attributeLabel};
133     }
134 
135     /**
136      * This overridden method ...
137      *
138      * @see org.kuali.rice.krad.datadictionary.validation.ValidationPattern#getValidationErrorMessageKey()
139      */
140     @Override
141     public String getValidationErrorMessageKey() {
142         StringBuilder buf = new StringBuilder();
143         buf.append("error.format.").append(getClass().getName()).append(getValidationErrorMessageKeyOptions());
144         if (getMaxLength() != -1) {
145             buf.append(".maxLength");
146         }
147         if (getExactLength() != -1) {
148             buf.append(".exactLength");
149         }
150         return buf.toString();
151     }
152 
153     protected String getValidationErrorMessageKeyOptions() {
154         return KRADConstants.EMPTY_STRING;
155     }
156 }