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.itracker.core.resources.ITrackerResources;
22  import org.itracker.model.Status;
23  
24  import java.util.*;
25  
26  
27  public class ProjectUtilities {
28      // Options use bitmasks and are stored as a single integer in the db
29      public static final int OPTION_SURPRESS_HISTORY_HTML = 1;
30      public static final int OPTION_ALLOW_ASSIGN_TO_CLOSE = 2;
31      public static final int OPTION_PREDEFINED_RESOLUTIONS = 4;
32      public static final int OPTION_ALLOW_SELF_REGISTERED_CREATE = 8;
33      public static final int OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL = 16;
34      public static final int OPTION_NO_ATTACHMENTS = 32;
35      public static final int OPTION_LITERAL_HISTORY_HTML = 64;
36  
37      /**
38       * Cache of status names by Locale, loaded lazily on first request
39       * from itracker.properties.
40       * <p/>
41       * The Map implementation is synchronized because it can be accessed
42       * by multiple threads that will alter it in case of cache miss.
43       */
44      private static final Map<Locale, Map<Status, String>> statusNames =
45              new Hashtable<Locale, Map<Status, String>>();
46  
47      /**
48       * Contains only static methods and isn't intended to be instantiated.
49       */
50      private ProjectUtilities() {
51      }
52  
53      /**
54       * Returns the localized name of the given status for the application
55       * default locale.
56       *
57       * @param status enum constant of which we want the localized name
58       * @return name in the current locale
59       */
60      public static String getStatusName(Status status) {
61          return getStatusName(status, ITrackerResources.getLocale());
62      }
63  
64      /**
65       * Returns the localized name of the given status for the given locale.
66       *
67       * @param status enum constant of which we want the localized name
68       * @param locale desired locale
69       * @return name in the given locale or "MISSING RESOURCE " + resource key
70       *         if no resource could be found
71       */
72      public static String getStatusName(Status status, Locale locale) {
73          return ITrackerResources.getString(
74                  ITrackerResources.KEY_BASE_PROJECT_STATUS + status.getCode(),
75                  locale);
76      }
77  
78      /**
79       * @return unmodifiable map of status names for the application default Locale
80       */
81      @Deprecated
82      public static Map<Status, String> getStatusNames() {
83          return getStatusNames(ITrackerResources.getLocale());
84      }
85  
86      /**
87       * This method loads the status names in the cache if they're not
88       * found in it.
89       * <p/>
90       * <p>The returned map is cached for future requests. </p>
91       *
92       * @return unmodifiable map of status names for the requested Locale
93       */
94      public static Map<Status, String> getStatusNames(Locale locale) {
95          Map<Status, String> statuses = statusNames.get(locale);
96  
97  
98          if (statuses == null) {
99              // No labels found for the requested Locale => load in cache.
100             statuses = new EnumMap<Status, String>(Status.class);
101 
102             for (Status status : Status.values()) {
103                 statuses.put(status, getStatusName(status, locale));
104 
105 
106             }
107             statusNames.put(locale, Collections.unmodifiableMap(statuses));
108 
109         }
110         return Collections.unmodifiableMap(statusNames.get(locale));
111 
112     }
113 
114     public static boolean hasOption(int option, int currentOptions) {
115         return ((option & currentOptions) == option);
116     }
117 
118     public static Integer[] getOptions(int currentOptions) {
119         List<Integer> options = new ArrayList<Integer>();
120         if (hasOption(OPTION_SURPRESS_HISTORY_HTML, currentOptions)) {
121             options.add(OPTION_SURPRESS_HISTORY_HTML);
122         }
123         if (hasOption(OPTION_ALLOW_ASSIGN_TO_CLOSE, currentOptions)) {
124             options.add(OPTION_ALLOW_ASSIGN_TO_CLOSE);
125         }
126         if (hasOption(OPTION_PREDEFINED_RESOLUTIONS, currentOptions)) {
127             options.add(OPTION_PREDEFINED_RESOLUTIONS);
128         }
129         if (hasOption(OPTION_ALLOW_SELF_REGISTERED_CREATE, currentOptions)) {
130             options.add(OPTION_ALLOW_SELF_REGISTERED_CREATE);
131         }
132         if (hasOption(OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL, currentOptions)) {
133             options.add(OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL);
134         }
135         if (hasOption(OPTION_NO_ATTACHMENTS, currentOptions)) {
136             options.add(OPTION_NO_ATTACHMENTS);
137         }
138         if (hasOption(OPTION_LITERAL_HISTORY_HTML, currentOptions)) {
139             options.add(OPTION_LITERAL_HISTORY_HTML);
140         }
141         Integer[] optionsArray = new Integer[options.size()];
142         options.toArray(optionsArray);
143         return optionsArray;
144     }
145 
146     public static String getScriptPriorityLabelKey(Integer fieldId) {
147         return ITrackerResources.getString(ITrackerResources.KEY_BASE_PRIORITY + fieldId + ITrackerResources.KEY_BASE_PRIORITY_LABEL);
148     }
149 
150     public static String getScriptPrioritySize() {
151         return ITrackerResources.getString(ITrackerResources.KEY_BASE_PRIORITY + ITrackerResources.KEY_BASE_PRIORITY_SIZE);
152     }
153 }