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