1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validation.constraint;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.CoreConstants;
20 import org.kuali.rice.core.api.config.property.ConfigContext;
21 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
22 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
23 import org.kuali.rice.krad.datadictionary.parse.BeanTags;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29
30
31
32
33
34
35
36
37
38
39 @BeanTags({@BeanTag(name = "datePatternConstraint", parent = "DatePatternConstraint"),
40 @BeanTag(name = "basicDatePatternConstraint", parent = "BasicDatePatternConstraint")})
41 public class DatePatternConstraint extends ValidDataPatternConstraint {
42
43 private List<String> allowedFormats;
44
45
46
47
48
49
50
51 @Override
52 protected String getRegexString() {
53 List<String> dateFormatParams = parseConfigValues(ConfigContext.getCurrentContextConfig().getProperty(
54 CoreConstants.STRING_TO_DATE_FORMATS));
55 if (allowedFormats != null && !allowedFormats.isEmpty()) {
56 if (dateFormatParams.containsAll(allowedFormats)) {
57 dateFormatParams = allowedFormats;
58 } else {
59
60 }
61 }
62
63 if (dateFormatParams.isEmpty()) {
64
65 }
66 String regex = "";
67 int i = 0;
68 for (String format : dateFormatParams) {
69 if (i == 0) {
70 regex = "(^" + convertDateFormatToRegex(format.trim()) + "$)";
71 } else {
72 regex = regex + "|(^" + convertDateFormatToRegex(format.trim()) + "$)";
73 }
74 i++;
75 }
76 return regex;
77 }
78
79
80
81
82
83
84
85 private String convertDateFormatToRegex(String format) {
86 format = format.replace("\\", "\\\\").replace(".", "\\.").replace("-", "\\-").replace("+", "\\+").replace("(",
87 "\\(").replace(")", "\\)").replace("[", "\\[").replace("]", "\\]").replace("|", "\\|").replace("yyyy",
88 "((19|2[0-9])[0-9]{2})").replace("yy", "([0-9]{2})").replaceAll("M{4,}",
89 "([@]+)")
90 .replace("MMM", "([@]{3})")
91 .replace("MM", "(0[1-9]|1[012])").replace("M", "(0?[1-9]|1[012])").replace("dd",
92 "(0[1-9]|[12][0-9]|3[01])").replace("d", "(0?[1-9]|[12][0-9]|3[01])").replace("hh",
93 "(1[0-2]|0[1-9])").replace("h", "(1[0-2]|0?[1-9])").replace("HH", "(2[0-3]|1[0-9]|0[0-9])")
94 .replace("H", "(2[0-3]|1[0-9]|0?[0-9])").replace("kk", "(2[0-4]|1[0-9]|0[1-9])").replace("k",
95 "(2[0-4]|1[0-9]|0?[1-9])").replace("KK", "(1[01]|0[0-9])").replace("K", "(1[01]|0?[0-9])")
96 .replace("mm", "([0-5][0-9])").replace("m", "([1-5][0-9]|0?[0-9])").replace("ss", "([0-5][0-9])")
97 .replace("s", "([1-5][0-9]|0?[0-9])").replace("SSS", "([0-9][0-9][0-9])").replace("SS",
98 "([0-9][0-9][0-9]?)").replace("S", "([0-9][0-9]?[0-9]?)").replaceAll("E{4,}",
99 "([@]+)")
100 .replaceAll("E{1,3}", "([@]{3})")
101 .replace("DDD", "(3[0-6][0-5]|[1-2][0-9][0-9]|0[0-9][1-9])").replace("DD",
102 "(3[0-6][0-5]|[1-2][0-9][0-9]|0?[0-9][1-9])").replace("D",
103 "(3[0-6][0-5]|[1-2][0-9][0-9]|0?[0-9]?[1-9])").replace("F", "([1-5])").replace("ww",
104 "(5[0-3]|[1-4][0-9]|0[1-9])").replace("w", "(5[0-3]|[1-4][0-9]|[1-9])").replace("W", "([1-5])")
105 .replaceAll("z{4,}", "([@]+)").replaceAll("z{1,3}", "([@]{1,4})").replaceAll("a{1,}", "([aApP][mM])")
106 .replaceAll("G{1,}", "([aA][dD]|[bB][cC])").replace(" ", "\\s").replace("@", "a-zA-Z");
107
108 return format;
109
110 }
111
112
113
114
115
116
117
118 private List<String> parseConfigValues(String configValue) {
119 if (configValue == null || "".equals(configValue)) {
120 return Collections.emptyList();
121 }
122 return Arrays.asList(configValue.split(";"));
123 }
124
125
126
127
128 @BeanTagAttribute(name = "allowedFormats", type = BeanTagAttribute.AttributeType.LISTVALUE)
129 public List<String> getAllowedFormats() {
130 return this.allowedFormats;
131 }
132
133
134
135
136
137
138
139
140 public void setAllowedFormats(List<String> allowedFormats) {
141 this.allowedFormats = allowedFormats;
142 }
143
144
145
146
147
148
149 @Override
150 public List<String> getValidationMessageParams() {
151 if (validationMessageParams == null) {
152 validationMessageParams = new ArrayList<String>();
153 if (allowedFormats != null && !allowedFormats.isEmpty()) {
154 validationMessageParams.add(StringUtils.join(allowedFormats, ", "));
155 } else {
156 List<String> dateFormatParams = parseConfigValues(ConfigContext.getCurrentContextConfig().getProperty(
157 CoreConstants.STRING_TO_DATE_FORMATS));
158 validationMessageParams.add(StringUtils.join(dateFormatParams, ", "));
159 }
160 }
161 return validationMessageParams;
162 }
163
164 }