001 /**
002 * Copyright 2005-2014 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.krad.datadictionary.validation;
017
018 import org.kuali.rice.krad.datadictionary.exporter.ExportMap;
019 import org.kuali.rice.krad.util.KRADConstants;
020
021 import java.util.regex.Pattern;
022
023 /**
024 * Abstraction of the regular expressions used to validate attribute values.
025 */
026 @Deprecated
027 abstract public class CharacterLevelValidationPattern extends ValidationPattern {
028 protected Pattern regexPattern;
029
030 protected int maxLength = -1;
031 protected int exactLength = -1;
032
033 /**
034 * Sets maxLength parameter for the associated regex.
035 *
036 * @param maxLength
037 */
038 public void setMaxLength(int maxLength) {
039 if (this.exactLength != -1) {
040 throw new IllegalStateException(
041 "illegal attempt to set maxLength after mutually-exclusive exactLength has been set");
042 }
043
044 this.maxLength = maxLength;
045 }
046
047 /**
048 * @return current maxLength, or -1 if none has been set
049 */
050 public int getMaxLength() {
051 return maxLength;
052 }
053
054 /**
055 * Sets exactLength parameter for the associated regex.
056 *
057 * @param exactLength
058 */
059 public void setExactLength(int exactLength) {
060 if (this.maxLength != -1) {
061 throw new IllegalStateException(
062 "illegal attempt to set exactLength after mutually-exclusive maxLength has been set");
063 }
064
065 this.exactLength = exactLength;
066 }
067
068 /**
069 * @return current exactLength, or -1 if none has been set
070 */
071 public int getExactLength() {
072 return exactLength;
073 }
074
075 /**
076 * @return regular expression Pattern generated using the individual ValidationPattern subclass
077 */
078 final public Pattern getRegexPattern() {
079 if (regexPattern == null) {
080 String regexString = getRegexString();
081
082 StringBuffer completeRegex = new StringBuffer("^");
083 completeRegex.append(getRegexString());
084
085 if (maxLength != -1) {
086 completeRegex.append("{0," + maxLength + "}");
087 } else if (exactLength != -1) {
088 completeRegex.append("{" + exactLength + "}");
089 } else {
090 completeRegex.append("*");
091 }
092
093 completeRegex.append("$");
094
095 regexPattern = Pattern.compile(completeRegex.toString());
096 }
097 return regexPattern;
098 }
099
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 }