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.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        * Returns the string representation of a field type.
43        * @param type the type to translate
44        * @return a string representation of the field type translated to the specified locale
45        */
46      public static String getTypeString(CustomField.Type type) {
47          return getTypeString(type, ITrackerResources.getLocale());
48      }
49  
50      
51      /**
52       * Returns the string representation of a field type
53       * @param code type code to translate
54       * @param locale the locale to translate the type into
55       * @return a string representation of the field type translated to the default locale
56       */
57      public static String getTypeString(int code, Locale locale) {
58      	return getTypeString(CustomField.Type.valueOf(code), locale);
59      }
60      
61      /**
62        * Returns the string representation of a field type.
63        * @param type the type to translate
64        * @param locale the locale to translate the type into
65        * @return a string representation of the field type translated to the default locale
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        * Returns the label key for a particular custom field.  This is made up of
83        * a static part and the unique value of the custom field.
84        * @param fieldId the CustomField id to return the label key for
85        * @return the label key for the field
86        */
87      public static String getCustomFieldLabelKey(Integer fieldId) {
88          return ITrackerResources.KEY_BASE_CUSTOMFIELD + fieldId + ITrackerResources.KEY_BASE_CUSTOMFIELD_LABEL;
89      }
90  
91      /**
92        * Returns the label key for a particular custom field option.  This is made up of
93        * a static part and the unique value of the custom field option.
94        * @param fieldId the CustomField id to return the label key for
95        * @param optionId the CustomField option's id to return the label key for
96        * @return the label key for the field option
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       * Returns the label for a custom field in the default locale.
104       * @param fieldId the id of the field to return the label for
105       * @return the label for the field translated to the default locale
106       */
107     public static String getCustomFieldName(Integer fieldId) {
108         return getCustomFieldName(fieldId, ITrackerResources.getLocale());
109     }
110 
111     /**
112       * Returns the label for a custom field in the specified locale.
113       * @param fieldId the id of the field to return the label for
114       * @param locale the locale to return the label for
115       * @return the label for the field translated to the specified locale
116       */
117     public static String getCustomFieldName(Integer fieldId, Locale locale) {
118         return ITrackerResources.getString(CustomFieldUtilities.getCustomFieldLabelKey(fieldId), locale);
119     }
120 
121     /**
122       * Returns the label for a custom field option in the default locale.
123       * @param fieldId the id of the field to return the label for
124       * @param optionId the id of the field option to return the label for
125       * @return the label for the field option translated to the default locale
126       */
127     public static String getCustomFieldOptionName(Integer fieldId, Integer optionId) {
128         return getCustomFieldOptionName(fieldId, optionId, ITrackerResources.getLocale());
129     }
130 
131     /**
132       * Returns the label for a custom field option in the specified locale.
133       * @param fieldId the id of the field to return the label for
134       * @param optionId the id of the field option to return the label for
135       * @param locale the locale to return the label for
136       * @return the label for the field option translated to the default locale
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 }