1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.services.util;
20
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Locale;
24
25 import org.apache.log4j.Logger;
26 import org.itracker.core.resources.ITrackerResources;
27 import org.itracker.model.CustomField;
28 import org.itracker.model.CustomFieldValue;
29 import org.itracker.model.IssueField;
30
31
32 public class CustomFieldUtilities {
33
34 public static final String DATE_FORMAT_UNKNOWN = "UNKNOWN";
35 public static final String DATE_FORMAT_FULL = "full";
36 public static final String DATE_FORMAT_DATEONLY = "dateonly";
37 public static final String DATE_FORMAT_TIMEONLY = "timeonly";
38 private static final Logger logger = Logger.getLogger(CustomFieldUtilities.class);
39
40
41
42
43
44
45
46 public static String getTypeString(CustomField.Type type) {
47 return getTypeString(type, ITrackerResources.getLocale());
48 }
49
50
51
52
53
54
55
56
57 public static String getTypeString(int code, Locale locale) {
58 return getTypeString(CustomField.Type.valueOf(code), locale);
59 }
60
61
62
63
64
65
66
67 public static String getTypeString(CustomField.Type type, Locale locale) {
68 if(type == CustomField.Type.STRING) {
69 return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "string", locale);
70 } else if(type == CustomField.Type.INTEGER) {
71 return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "integer", locale);
72 } else if(type == CustomField.Type.DATE) {
73 return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "date", locale);
74 } else if(type == CustomField.Type.LIST) {
75 return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "list", locale);
76 }
77
78 return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "unknown", locale);
79 }
80
81
82
83
84
85
86
87 public static String getCustomFieldLabelKey(Integer fieldId) {
88 return ITrackerResources.KEY_BASE_CUSTOMFIELD + fieldId + ITrackerResources.KEY_BASE_CUSTOMFIELD_LABEL;
89 }
90
91
92
93
94
95
96
97
98 public static String getCustomFieldOptionLabelKey(Integer fieldId, Integer optionId) {
99 return ITrackerResources.KEY_BASE_CUSTOMFIELD + fieldId + ITrackerResources.KEY_BASE_CUSTOMFIELD_OPTION + optionId + ITrackerResources.KEY_BASE_CUSTOMFIELD_LABEL;
100 }
101
102
103
104
105
106
107 public static String getCustomFieldName(Integer fieldId) {
108 return getCustomFieldName(fieldId, ITrackerResources.getLocale());
109 }
110
111
112
113
114
115
116
117 public static String getCustomFieldName(Integer fieldId, Locale locale) {
118 return ITrackerResources.getString(CustomFieldUtilities.getCustomFieldLabelKey(fieldId), locale);
119 }
120
121
122
123
124
125
126
127 public static String getCustomFieldOptionName(Integer fieldId, Integer optionId) {
128 return getCustomFieldOptionName(fieldId, optionId, ITrackerResources.getLocale());
129 }
130
131
132
133
134
135
136
137
138 public static String getCustomFieldOptionName(Integer fieldId, Integer optionId, Locale locale) {
139 if(fieldId != null && optionId != null) {
140 return ITrackerResources.getString(CustomFieldUtilities.getCustomFieldOptionLabelKey(fieldId, optionId), locale);
141 }
142 return "";
143 }
144
145 public static final String getCustomFieldOptionName(CustomFieldValue option, Locale locale) {
146 if (null == option) {
147 return null;
148 }
149 return getCustomFieldOptionName(option.getCustomField().getId(), option.getId(), locale);
150 }
151 public static final CustomFieldValue getCustomFieldOptionByValue(List<CustomFieldValue> fields, String value) {
152
153 if (null != fields && ! fields.isEmpty()) {
154 Iterator<CustomFieldValue> it = fields.iterator();
155 while (it.hasNext()) {
156 CustomFieldValue fieldValue = it.next();
157 if (fieldValue.getValue().equalsIgnoreCase(value)) {
158 return fieldValue;
159 }
160 }
161 }
162 return fields.get(0);
163 }
164
165
166 public static final String getCustomFieldOptionName(CustomField field,
167 String value, Locale locale) {
168 if (null == field) {
169 return null;
170 }
171
172 if (field.getFieldType() != CustomField.Type.LIST) {
173 return value;
174 }
175 try {
176 return CustomFieldUtilities.getCustomFieldOptionName(field.getId(),
177 CustomFieldUtilities.getCustomFieldOptionByValue(
178 field.getOptions(),
179 value).getId(),
180 locale);
181 } catch (Exception e) {
182 logger .warn("doEndTag: failed to get custom field option name for value " + value + ", " + field.getOptions());
183 }
184 return value;
185 }
186
187
188 public static final String getCustomFieldOptionName(IssueField field, Locale locale) {
189 if (null == field) {
190 return null;
191 }
192
193 return getCustomFieldOptionName(field.getCustomField(), field.getStringValue(), locale);
194
195 }
196 }