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 java.text.SimpleDateFormat;
22 import java.util.Date;
23 import java.util.Locale;
24 import java.util.ResourceBundle;
25
26 import org.apache.commons.lang.builder.ToStringBuilder;
27 import org.apache.log4j.Logger;
28 import org.itracker.core.resources.ITrackerResources;
29 import org.itracker.model.CustomField.Type;
30 import org.itracker.services.exceptions.IssueException;
31
32
33
34
35
36
37
38
39
40
41
42 public class IssueField extends AbstractEntity {
43
44
45
46
47 private static final long serialVersionUID = 1L;
48
49 private static transient final Logger log = Logger
50 .getLogger(IssueField.class);
51
52 private Issue issue;
53
54 private CustomField customField;
55
56 private String stringValue;
57
58 private Integer intValue;
59
60 private Date dateValue;
61
62
63
64
65
66
67
68
69
70
71 public IssueField() {
72 }
73
74 public IssueField(Issue issue, CustomField field) {
75 setIssue(issue);
76 setCustomField(field);
77 }
78
79 public Issue getIssue() {
80 return issue;
81 }
82
83 public void setIssue(Issue issue) {
84 if (issue == null) {
85 throw new IllegalArgumentException("null issue");
86 }
87 this.issue = issue;
88 }
89
90 public CustomField getCustomField() {
91 return customField;
92 }
93
94 public void setCustomField(CustomField customField) {
95 if (customField == null) {
96 throw new IllegalArgumentException("null customField");
97 }
98 this.customField = customField;
99 }
100
101 public String getStringValue() {
102 if (null != this.getCustomField() && Type.DATE == this.getCustomField().getFieldType()) {
103 this.stringValue = "";
104 if (null != this.dateValue) {
105 String stringValue = formatDate(ITrackerResources.getBundle(ITrackerResources.BASE_LOCALE));
106 this.stringValue = stringValue;
107 }
108 }
109 return stringValue;
110 }
111
112 public void setStringValue(String stringValue) {
113 this.stringValue = stringValue;
114 }
115
116 public Integer getIntValue() {
117 return intValue;
118 }
119
120 public void setIntValue(Integer intValue) {
121 this.intValue = intValue;
122 }
123
124 public Date getDateValue() {
125 if (null == dateValue)
126 return null;
127 return new Date(dateValue.getTime());
128 }
129
130 public void setDateValue(Date dateValue) {
131 if (null == dateValue) {
132 this.dateValue = null;
133 } else {
134 this.dateValue = new Date(dateValue.getTime());
135 this.stringValue = formatDate(ITrackerResources.getBundle());
136 }
137 }
138
139
140
141
142
143
144
145
146 public String getValue(Locale locale) {
147
148 if (getCustomField().getFieldType() == Type.DATE) {
149 return getValue(ITrackerResources.getBundle(locale));
150 } else if (getCustomField().getFieldType() == Type.INTEGER) {
151 return String.valueOf(getIntValue());
152 }
153 return getStringValue();
154
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public String getValue(ResourceBundle bundle, Locale locale) {
170 if (log.isDebugEnabled()) {
171 log.debug("getValue: called with bundle: " + bundle + ", locale: "
172 + locale);
173 }
174 return getValue(bundle);
175 }
176
177
178
179
180
181
182
183
184
185 public String getValue(ResourceBundle bundle) {
186
187
188 Locale locale = bundle.getLocale();
189
190 if (log.isDebugEnabled()) {
191 log.debug("getValue: called with bundle: " + bundle + ", locale: "
192 + locale);
193 }
194 switch (customField.getFieldType()) {
195
196 case INTEGER:
197 if (log.isDebugEnabled()) {
198 log
199 .debug("getValue: type was INTEGER, value: "
200 + this.intValue);
201 }
202 return String.valueOf(this.intValue);
203
204 case DATE:
205 if (log.isDebugEnabled()) {
206 log.debug("getValue: type was DATE, value: " + this.dateValue);
207 }
208 if (!customField.isRequired() && this.dateValue == null) {
209
210 if (log.isDebugEnabled()) {
211 log.debug("getValue: value was null and not required");
212 }
213 return null;
214 }
215 if (this.dateValue == null) {
216 this.dateValue = new Date();
217 }
218 return formatDate(bundle);
219 default:
220 return this.stringValue;
221 }
222
223 }
224
225 private String formatDate(ResourceBundle bundle) {
226 assert (dateValue!= null): "dateValue failed";
227 try {
228
229 SimpleDateFormat sdf =
230 new SimpleDateFormat(bundle
231 .getString("itracker.dateformat."
232 + customField.getDateFormat()), bundle.getLocale());
233
234 if (log.isDebugEnabled()) {
235 log.debug("getValue: dateFormat from itracker configuration "
236 + sdf.toPattern());
237 }
238
239
240 String formattedDate = sdf.format(this.dateValue);
241 if (log.isDebugEnabled()) {
242 log.debug("getValue: formated date " + this.dateValue
243 + " to " + formattedDate);
244 }
245 return formattedDate;
246 } catch (NullPointerException ne) {
247 log.debug("getValue: ", ne);
248 if (dateValue == null) {
249 log.warn("getValue: failed to format date, null for "
250 + customField);
251 }
252 return "";
253 }
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279 public void setValue(String value, Locale locale, ResourceBundle bundle)
280 throws IssueException {
281 this.stringValue = null;
282 this.intValue = 0;
283 this.dateValue = null;
284
285 if (value != null && value.trim().length() > 0) {
286 switch (customField.getFieldType()) {
287
288 case INTEGER:
289 setStringValue(value);
290 try {
291 setIntValue(Integer.parseInt(value));
292 } catch (NumberFormatException nfe) {
293 throw new IssueException("Invalid integer.",
294 IssueException.TYPE_CF_PARSE_NUM);
295 }
296 break;
297
298 case DATE:
299 setStringValue(value);
300 try {
301 if (null == locale) {
302 locale = bundle.getLocale();
303 }
304 SimpleDateFormat sdf =
305 new SimpleDateFormat(bundle
306 .getString("itracker.dateformat."
307 + customField.getDateFormat()), locale);
308
309 Date dateValue = sdf.parse(value);
310 if (dateValue != null) {
311 setDateValue(dateValue);
312 } else {
313 log.error("setValue: caught exception for date "
314 + value);
315 throw new IssueException("Invalid date.",
316 IssueException.TYPE_CF_PARSE_DATE);
317 }
318 } catch (Exception ex) {
319 log.error("setValue: caught exception for date " + value,
320 ex);
321 throw new IssueException("Invalid date format.",
322 IssueException.TYPE_CF_PARSE_DATE);
323 }
324 break;
325
326 default:
327 setStringValue(value);
328 }
329
330 } else {
331
332 setStringValue("");
333 setDateValue(null);
334 setIntValue(0);
335 }
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356 public void setValue(String value, ResourceBundle bundle)
357 throws IssueException {
358 setValue(value, bundle.getLocale(), bundle);
359 }
360
361 @Override
362 public String toString() {
363 return new ToStringBuilder(this).append("id", getId()).append("issue",
364 getIssue()).append("customField", getCustomField()).toString();
365 }
366
367 }