CustomFieldUtilities.java

  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. package org.itracker.model.util;

  19. import org.apache.commons.lang.builder.CompareToBuilder;
  20. import org.apache.log4j.Logger;
  21. import org.itracker.core.resources.ITrackerResources;
  22. import org.itracker.model.CustomField;
  23. import org.itracker.model.CustomFieldValue;

  24. import java.io.Serializable;
  25. import java.util.Comparator;
  26. import java.util.List;
  27. import java.util.Locale;


  28. public class CustomFieldUtilities {

  29.     public static final String DATE_FORMAT_UNKNOWN = "UNKNOWN";
  30.     public static final String DATE_FORMAT_FULL = "full";
  31.     public static final String DATE_FORMAT_DATEONLY = "dateonly";
  32.     public static final String DATE_FORMAT_TIMEONLY = "timeonly";
  33.     private static final Logger logger = Logger.getLogger(CustomFieldUtilities.class);


  34.     /**
  35.      * Returns the string representation of a field type
  36.      *
  37.      * @param code   type code to translate
  38.      * @param locale the locale to translate the type into
  39.      * @return a string representation of the field type translated to the default locale
  40.      */
  41.     public static String getTypeString(int code, Locale locale) {
  42.         return getTypeString(CustomField.Type.valueOf(code), locale);
  43.     }

  44.     /**
  45.      * Returns the string representation of a field type.
  46.      *
  47.      * @param type   the type to translate
  48.      * @param locale the locale to translate the type into
  49.      * @return a string representation of the field type translated to the default locale
  50.      */
  51.     public static String getTypeString(CustomField.Type type, Locale locale) {
  52.         if (type == CustomField.Type.STRING) {
  53.             return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "string", locale);
  54.         } else if (type == CustomField.Type.INTEGER) {
  55.             return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "integer", locale);
  56.         } else if (type == CustomField.Type.DATE) {
  57.             return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "date", locale);
  58.         } else if (type == CustomField.Type.LIST) {
  59.             return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "list", locale);
  60.         }

  61.         return ITrackerResources.getString(ITrackerResources.KEY_BASE_CUSTOMFIELD_TYPE + "unknown", locale);
  62.     }

  63.     /**
  64.      * Returns the label key for a particular custom field.  This is made up of
  65.      * a static part and the unique value of the custom field.
  66.      *
  67.      * @param fieldId the CustomField id to return the label key for
  68.      * @return the label key for the field
  69.      */
  70.     public static String getCustomFieldLabelKey(Integer fieldId) {
  71.         return ITrackerResources.KEY_BASE_CUSTOMFIELD + fieldId + ITrackerResources.KEY_BASE_CUSTOMFIELD_LABEL;
  72.     }

  73.     /**
  74.      * Returns the label key for a particular custom field option.  This is made up of
  75.      * a static part and the unique value of the custom field option.
  76.      *
  77.      * @param fieldId  the CustomField id to return the label key for
  78.      * @param optionId the CustomField option's id to return the label key for
  79.      * @return the label key for the field option
  80.      */
  81.     public static String getCustomFieldOptionLabelKey(Integer fieldId, Integer optionId) {
  82.         return ITrackerResources.KEY_BASE_CUSTOMFIELD + fieldId + ITrackerResources.KEY_BASE_CUSTOMFIELD_OPTION + optionId + ITrackerResources.KEY_BASE_CUSTOMFIELD_LABEL;
  83.     }

  84.     /**
  85.      * Returns the label for a custom field in the default locale.
  86.      *
  87.      * @param fieldId the id of the field to return the label for
  88.      * @return the label for the field translated to the default locale
  89.      */
  90.     public static String getCustomFieldName(Integer fieldId) {
  91.         return getCustomFieldName(fieldId, ITrackerResources.getLocale());
  92.     }

  93.     /**
  94.      * Returns the label for a custom field in the specified locale.
  95.      *
  96.      * @param fieldId the id of the field to return the label for
  97.      * @param locale  the locale to return the label for
  98.      * @return the label for the field translated to the specified locale
  99.      */
  100.     public static String getCustomFieldName(Integer fieldId, Locale locale) {
  101.         return ITrackerResources.getString(CustomFieldUtilities.getCustomFieldLabelKey(fieldId), locale);
  102.     }

  103.     /**
  104.      * Returns the label for a custom field option in the specified locale.
  105.      *
  106.      * @param fieldId  the id of the field to return the label for
  107.      * @param optionId the id of the field option to return the label for
  108.      * @param locale   the locale to return the label for
  109.      * @return the label for the field option translated to the default locale
  110.      */
  111.     public static String getCustomFieldOptionName(Integer fieldId, Integer optionId, Locale locale) {
  112.         if (fieldId != null && optionId != null) {
  113.             return ITrackerResources.getString(CustomFieldUtilities.getCustomFieldOptionLabelKey(fieldId, optionId), locale);
  114.         }
  115.         return "";
  116.     }

  117.     public static String getCustomFieldOptionName(CustomFieldValue option, Locale locale) {
  118.         if (null == option) {
  119.             return null;
  120.         }
  121.         return getCustomFieldOptionName(option.getCustomField().getId(), option.getId(), locale);
  122.     }

  123.     public static CustomFieldValue getCustomFieldOptionByValue(List<CustomFieldValue> fields, String value) {

  124.         if (null != fields && !fields.isEmpty()) {
  125.             for (CustomFieldValue fieldValue : fields) {
  126.                 if (fieldValue.getValue().equalsIgnoreCase(value)) {
  127.                     return fieldValue;
  128.                 }
  129.             }
  130.             return fields.get(0);
  131.         }
  132.         return null;
  133.     }


  134.     public static String getCustomFieldOptionName(CustomField field,
  135.                                                         String value, Locale locale) {
  136.         if (null == field) {
  137.             return null;
  138.         }

  139.         if (field.getFieldType() != CustomField.Type.LIST) {
  140.             return value;
  141.         }
  142.         try {
  143.             return CustomFieldUtilities.getCustomFieldOptionName(field.getId(),
  144.                     CustomFieldUtilities.getCustomFieldOptionByValue(
  145.                             field.getOptions(),
  146.                             value).getId(),
  147.                     locale);
  148.         } catch (Exception e) {
  149.             logger.warn("doEndTag: failed to get custom field option name for value " + value + ", " + field.getOptions());
  150.         }
  151.         return value;
  152.     }

  153.     public static final class CustomFieldByNameComparator implements Comparator<CustomField>, Serializable {
  154.         /**
  155.          *
  156.          */
  157.         private static final long serialVersionUID = 1L;

  158.         private final Locale locale;

  159.         public CustomFieldByNameComparator(Locale locale) {
  160.             this.locale = locale;
  161.         }

  162.         public int compare(CustomField o1, CustomField o2) {
  163.             return new CompareToBuilder().append(
  164.                     getCustomFieldName(o1.getId(), locale),
  165.                     getCustomFieldName(o2.getId(), locale))
  166.                     .append(o1.getId(), o2.getId())
  167.                     .toComparison();
  168.         }
  169.     }

  170.     /**
  171.      * Compares 2 CustomFieldValues by name.
  172.      * <p/>
  173.      * <p>
  174.      * If 2 instances have the same name, they are ordered by sortOrder.
  175.      * </p>
  176.      * <p/>
  177.      * <p>
  178.      * It doesn't take into account the custom field. <br>
  179.      * It should therefore only be used to compare options that belong to a
  180.      * single custom field.
  181.      * </p>
  182.      */
  183.     public static final class CustomFieldValueByNameComparator implements
  184.             Comparator<CustomFieldValue>, Serializable {
  185.         /**
  186.          *
  187.          */
  188.         private static final long serialVersionUID = 1L;

  189.         private final Locale locale;

  190.         public CustomFieldValueByNameComparator(Locale locale) {
  191.             this.locale = locale;
  192.         }

  193.         public int compare(CustomFieldValue a, CustomFieldValue b) {
  194.             return new CompareToBuilder()
  195.                     .append(
  196.                             CustomFieldUtilities.getCustomFieldOptionName(a, this.locale),
  197.                             CustomFieldUtilities.getCustomFieldOptionName(b, this.locale))
  198.                     .append(a.getSortOrder(), b.getSortOrder())
  199.                     .append(a.getId(), b.getId()).toComparison();
  200.         }

  201.     }
  202. }