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