View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
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       * Returns the string representation of a field type
44       *
45       * @param code   type code to translate
46       * @param locale the locale to translate the type into
47       * @return a string representation of the field type translated to the default locale
48       */
49      public static String getTypeString(int code, Locale locale) {
50          return getTypeString(CustomField.Type.valueOf(code), locale);
51      }
52  
53      /**
54       * Returns the string representation of a field type.
55       *
56       * @param type   the type to translate
57       * @param locale the locale to translate the type into
58       * @return a string representation of the field type translated to the default locale
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       * Returns the label key for a particular custom field.  This is made up of
76       * a static part and the unique value of the custom field.
77       *
78       * @param fieldId the CustomField id to return the label key for
79       * @return the label key for the field
80       */
81      public static String getCustomFieldLabelKey(Integer fieldId) {
82          return ITrackerResources.KEY_BASE_CUSTOMFIELD + fieldId + ITrackerResources.KEY_BASE_CUSTOMFIELD_LABEL;
83      }
84  
85      /**
86       * Returns the label key for a particular custom field option.  This is made up of
87       * a static part and the unique value of the custom field option.
88       *
89       * @param fieldId  the CustomField id to return the label key for
90       * @param optionId the CustomField option's id to return the label key for
91       * @return the label key for the field option
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       * Returns the label for a custom field in the default locale.
99       *
100      * @param fieldId the id of the field to return the label for
101      * @return the label for the field translated to the default locale
102      */
103     public static String getCustomFieldName(Integer fieldId) {
104         return getCustomFieldName(fieldId, ITrackerResources.getLocale());
105     }
106 
107     /**
108      * Returns the label for a custom field in the specified locale.
109      *
110      * @param fieldId the id of the field to return the label for
111      * @param locale  the locale to return the label for
112      * @return the label for the field translated to the specified locale
113      */
114     public static String getCustomFieldName(Integer fieldId, Locale locale) {
115         return ITrackerResources.getString(CustomFieldUtilities.getCustomFieldLabelKey(fieldId), locale);
116     }
117 
118     /**
119      * Returns the label for a custom field option in the specified locale.
120      *
121      * @param fieldId  the id of the field to return the label for
122      * @param optionId the id of the field option to return the label for
123      * @param locale   the locale to return the label for
124      * @return the label for the field option translated to the default locale
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      * Compares 2 CustomFieldValues by name.
198      * <p/>
199      * <p>
200      * If 2 instances have the same name, they are ordered by sortOrder.
201      * </p>
202      * <p/>
203      * <p>
204      * It doesn't take into account the custom field. <br>
205      * It should therefore only be used to compare options that belong to a
206      * single custom field.
207      * </p>
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 }