1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.model;
20
21 import org.apache.commons.lang.builder.ToStringBuilder;
22 import org.apache.log4j.Logger;
23 import org.itracker.IssueException;
24 import org.itracker.model.util.CustomFieldUtilities;
25
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.ResourceBundle;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public class CustomField extends AbstractEntity implements Comparable<Entity> {
68
69 private static final Logger logger = Logger.getLogger(CustomField.class);
70
71
72
73
74 public static final SimpleDateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
75
76
77
78 private static final long serialVersionUID = 1L;
79
80
81
82
83 private Type type;
84
85
86
87
88
89
90 private String dateFormat;
91
92
93
94
95
96 private boolean required;
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112 private List<CustomFieldValue> options = new ArrayList<CustomFieldValue>();
113
114
115
116
117
118 private boolean sortOptionsByName;
119
120
121
122
123
124
125
126
127
128
129 public CustomField() {
130 }
131
132
133 public Type getFieldType() {
134 return type;
135 }
136
137 public void setFieldType(Type type) {
138 this.type = type;
139 }
140
141 public String getDateFormat() {
142 return dateFormat;
143 }
144
145 public void setDateFormat(String dateFormat) {
146 this.dateFormat = dateFormat;
147 }
148
149 public boolean isRequired() {
150 return required;
151 }
152
153 public void setRequired(boolean required) {
154 this.required = required;
155 }
156
157 public List<CustomFieldValue> getOptions() {
158 return options;
159 }
160
161 public void setOptions(List<CustomFieldValue> options) {
162 this.options = options;
163 }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public void addOption(String value, String label) {
180 this.options.add(new CustomFieldValue(this, value));
181 }
182
183
184 public boolean isSortOptionsByName() {
185 return sortOptionsByName;
186 }
187
188 public void setSortOptionsByName(boolean sortOptionsByName) {
189 this.sortOptionsByName = sortOptionsByName;
190 }
191
192 @Override
193 public String toString() {
194
195 return new ToStringBuilder(this)
196 .append("id", getId())
197 .append("type", getFieldType())
198 .append("sortOptionsByName", isSortOptionsByName()).toString();
199 }
200
201
202
203
204
205
206
207
208 public void checkAssignable(String value, Locale locale,
209 ResourceBundle bundle) throws IssueException {
210
211
212 if (this.isRequired() && (value == null || value.trim().length() == 0)) {
213 throw new IssueException("Value is required.", IssueException.TYPE_CF_REQ_FIELD);
214 }
215
216 switch (this.type) {
217
218 case INTEGER:
219 try {
220 Integer.parseInt(value);
221 } catch (NumberFormatException nfe) {
222 throw new IssueException("Invalid integer.",
223 IssueException.TYPE_CF_PARSE_NUM);
224 }
225 break;
226
227 case DATE:
228 if (!CustomFieldUtilities.DATE_FORMAT_UNKNOWN.equals(this.dateFormat)) {
229 SimpleDateFormat format =
230
231 new SimpleDateFormat(bundle
232 .getString("itracker.dateformat." + this.dateFormat),
233 locale);
234
235 try {
236 format.parse(value);
237 } catch (ParseException ex) {
238 throw new IssueException("Invalid date format.",
239 IssueException.TYPE_CF_PARSE_DATE);
240 }
241 }
242 break;
243
244 case LIST:
245 for (CustomFieldValue customFieldValue : getOptions()) {
246 if (customFieldValue.getValue().equalsIgnoreCase(value)) {
247 return;
248 }
249 }
250 if (logger.isDebugEnabled()) {
251 logger.debug("checkAssignable: could not assign value to custom field values: " + value + ", " + getOptions());
252 }
253 throw new IssueException("Invalid value.", IssueException.TYPE_CF_INVALID_LIST_OPTION);
254 default:
255
256 }
257 }
258
259
260
261
262 public static enum Type implements IntCodeEnum<Type> {
263
264 STRING(1), INTEGER(2), DATE(3), LIST(4);
265
266 private final Integer code;
267
268 private Type(Integer code) {
269 this.code = code;
270 }
271
272 public Integer getCode() {
273 return code;
274 }
275
276 public Type fromCode(Integer code) {
277 return Type.valueOf(code);
278 }
279
280 public static Type valueOf(Integer code) {
281 for (Type val: values()) {
282 if (val.code.compareTo(code) == 0) {
283 return val;
284 }
285 }
286 throw new IllegalArgumentException("Unknown code : " + code);
287 }
288
289
290 }
291
292
293
294
295
296
297 public static enum DateFormat {
298
299 DATE_TIME("full"), DATE("dateonly"), TIME("timeonly");
300
301 final String code;
302
303 DateFormat(String code) {
304 this.code = code;
305 }
306
307 }
308 }