ProjectUtilities.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.itracker.core.resources.ITrackerResources;
  20. import org.itracker.model.Status;

  21. import java.util.*;


  22. public class ProjectUtilities {
  23.     // Options use bitmasks and are stored as a single integer in the db
  24.     public static final int OPTION_SURPRESS_HISTORY_HTML = 1;
  25.     public static final int OPTION_ALLOW_ASSIGN_TO_CLOSE = 2;
  26.     public static final int OPTION_PREDEFINED_RESOLUTIONS = 4;
  27.     public static final int OPTION_ALLOW_SELF_REGISTERED_CREATE = 8;
  28.     public static final int OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL = 16;
  29.     public static final int OPTION_NO_ATTACHMENTS = 32;
  30.     public static final int OPTION_LITERAL_HISTORY_HTML = 64;

  31.     /**
  32.      * Cache of status names by Locale, loaded lazily on first request
  33.      * from itracker.properties.
  34.      * <p/>
  35.      * The Map implementation is synchronized because it can be accessed
  36.      * by multiple threads that will alter it in case of cache miss.
  37.      */
  38.     private static final Map<Locale, Map<Status, String>> statusNames =
  39.             new Hashtable<Locale, Map<Status, String>>();

  40.     /**
  41.      * Contains only static methods and isn't intended to be instantiated.
  42.      */
  43.     private ProjectUtilities() {
  44.     }

  45.     /**
  46.      * Returns the localized name of the given status for the application
  47.      * default locale.
  48.      *
  49.      * @param status enum constant of which we want the localized name
  50.      * @return name in the current locale
  51.      */
  52.     public static String getStatusName(Status status) {
  53.         return getStatusName(status, ITrackerResources.getLocale());
  54.     }

  55.     /**
  56.      * Returns the localized name of the given status for the given locale.
  57.      *
  58.      * @param status enum constant of which we want the localized name
  59.      * @param locale desired locale
  60.      * @return name in the given locale or "MISSING RESOURCE " + resource key
  61.      *         if no resource could be found
  62.      */
  63.     public static String getStatusName(Status status, Locale locale) {
  64.         return ITrackerResources.getString(
  65.                 ITrackerResources.KEY_BASE_PROJECT_STATUS + status.getCode(),
  66.                 locale);
  67.     }

  68.     /**
  69.      * @return unmodifiable map of status names for the application default Locale
  70.      */
  71.     @Deprecated
  72.     public static Map<Status, String> getStatusNames() {
  73.         return getStatusNames(ITrackerResources.getLocale());
  74.     }

  75.     /**
  76.      * This method loads the status names in the cache if they're not
  77.      * found in it.
  78.      * <p/>
  79.      * <p>The returned map is cached for future requests. </p>
  80.      *
  81.      * @return unmodifiable map of status names for the requested Locale
  82.      */
  83.     public static Map<Status, String> getStatusNames(Locale locale) {
  84.         Map<Status, String> statuses = statusNames.get(locale);


  85.         if (statuses == null) {
  86.             // No labels found for the requested Locale => load in cache.
  87.             statuses = new EnumMap<Status, String>(Status.class);

  88.             for (Status status : Status.values()) {
  89.                 statuses.put(status, getStatusName(status, locale));


  90.             }
  91.             statusNames.put(locale, Collections.unmodifiableMap(statuses));

  92.         }
  93.         return Collections.unmodifiableMap(statusNames.get(locale));

  94.     }

  95.     public static boolean hasOption(int option, int currentOptions) {
  96.         return ((option & currentOptions) == option);
  97.     }

  98.     public static Integer[] getOptions(int currentOptions) {
  99.         List<Integer> options = new ArrayList<Integer>();
  100.         if (hasOption(OPTION_SURPRESS_HISTORY_HTML, currentOptions)) {
  101.             options.add(OPTION_SURPRESS_HISTORY_HTML);
  102.         }
  103.         if (hasOption(OPTION_ALLOW_ASSIGN_TO_CLOSE, currentOptions)) {
  104.             options.add(OPTION_ALLOW_ASSIGN_TO_CLOSE);
  105.         }
  106.         if (hasOption(OPTION_PREDEFINED_RESOLUTIONS, currentOptions)) {
  107.             options.add(OPTION_PREDEFINED_RESOLUTIONS);
  108.         }
  109.         if (hasOption(OPTION_ALLOW_SELF_REGISTERED_CREATE, currentOptions)) {
  110.             options.add(OPTION_ALLOW_SELF_REGISTERED_CREATE);
  111.         }
  112.         if (hasOption(OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL, currentOptions)) {
  113.             options.add(OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL);
  114.         }
  115.         if (hasOption(OPTION_NO_ATTACHMENTS, currentOptions)) {
  116.             options.add(OPTION_NO_ATTACHMENTS);
  117.         }
  118.         if (hasOption(OPTION_LITERAL_HISTORY_HTML, currentOptions)) {
  119.             options.add(OPTION_LITERAL_HISTORY_HTML);
  120.         }
  121.         Integer[] optionsArray = new Integer[options.size()];
  122.         options.toArray(optionsArray);
  123.         return optionsArray;
  124.     }

  125.     public static String getScriptPriorityLabelKey(Integer fieldId) {
  126.         return ITrackerResources.getString(ITrackerResources.KEY_BASE_PRIORITY + fieldId + ITrackerResources.KEY_BASE_PRIORITY_LABEL);
  127.     }

  128.     public static String getScriptPrioritySize() {
  129.         return ITrackerResources.getString(ITrackerResources.KEY_BASE_PRIORITY + ITrackerResources.KEY_BASE_PRIORITY_SIZE);
  130.     }
  131. }