1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.services.util;
20
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.NoSuchElementException;
24 import java.util.StringTokenizer;
25
26 import org.apache.log4j.Logger;
27 import org.itracker.core.resources.ITrackerResources;
28 import org.itracker.model.Configuration;
29 import org.itracker.model.Language;
30 import org.itracker.model.NameValuePair;
31 import org.itracker.services.ConfigurationService;
32
33 public class SystemConfigurationUtilities {
34
35 public static final String DEFAULT_DATASOURCE = "java:/ITrackerDS";
36 private static final Logger log = Logger.getLogger(SystemConfigurationUtilities.class);
37
38 public static final int TYPE_INITIALIZED = -1;
39 public static final int TYPE_LOCALE = 1;
40 public static final int TYPE_STATUS = 2;
41 public static final int TYPE_SEVERITY = 3;
42 public static final int TYPE_RESOLUTION = 4;
43 public static final int TYPE_CUSTOMFIELD = 5;
44
45 public static final int ACTION_CREATE = 1;
46 public static final int ACTION_UPDATE = 2;
47 public static final int ACTION_REMOVE = 3;
48
49 public static final int LOCALE_TYPE_INVALID = -1;
50 public static final int LOCALE_TYPE_BASE = 1;
51 public static final int LOCALE_TYPE_LANGUAGE = 2;
52 public static final int LOCALE_TYPE_LOCALE = 3;
53
54
55
56
57
58
59
60
61
62
63 public static String getLanguageKey(Configuration configuration) {
64 if (configuration != null) {
65 int type = configuration.getType();
66 if (type == SystemConfigurationUtilities.TYPE_STATUS) {
67 return ITrackerResources.KEY_BASE_STATUS
68 + configuration.getValue();
69 } else if (type == SystemConfigurationUtilities.TYPE_SEVERITY) {
70 return ITrackerResources.KEY_BASE_SEVERITY
71 + configuration.getValue();
72 } else if (type == SystemConfigurationUtilities.TYPE_RESOLUTION) {
73 return ITrackerResources.KEY_BASE_RESOLUTION
74 + configuration.getValue();
75 }
76 }
77 return "";
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public static void initializeAllLanguages(
93 ConfigurationService configurationService, boolean forceReload) {
94 HashSet<String> definedLocales = new HashSet<String>();
95
96 configurationService.initializeLocale(ITrackerResources.BASE_LOCALE,
97 forceReload);
98
99 String definedLocalesString;
100 try {
101 definedLocalesString = configurationService
102 .getLanguageItemByKey(ITrackerResources.DEFINED_LOCALES_KEY,
103 null).getResourceValue();
104 } catch (RuntimeException e) {
105 definedLocalesString = ITrackerResources.getString(ITrackerResources.DEFINED_LOCALES_KEY);
106 }
107 if (definedLocalesString != null) {
108 StringTokenizer token = new StringTokenizer(definedLocalesString, ",");
109 while (token.hasMoreTokens()) {
110 String locale = token.nextToken();
111 if (locale.length() == 5 && locale.indexOf('_') == 2) {
112 definedLocales.add(locale.substring(0, 2));
113 }
114 definedLocales.add(locale);
115 }
116 }
117
118 for (Iterator<String> iter = definedLocales.iterator(); iter.hasNext();) {
119 String locale = iter.next();
120 configurationService.initializeLocale(locale, forceReload);
121 }
122 }
123
124 public static long getVersionAsLong(String version) {
125 long versionNumber = 0;
126 if (log.isDebugEnabled()) {
127 log.debug("getVersionAsLong: transforming " + version);
128 }
129 if (version != null) {
130 StringTokenizer token = new StringTokenizer(version, ".");
131 try {
132 if (token.countTokens() > 0 && token.countTokens() <= 3) {
133 versionNumber += 1000000L * Integer.parseInt(token
134 .nextToken());
135 versionNumber += 1000L * Integer
136 .parseInt(token.nextToken());
137 versionNumber += Integer.parseInt(token.nextToken());
138 } else {
139 throw new IllegalArgumentException("The version " + version + " is not parseable, excpected '(int)number[.(int)major[.(int)minor]]");
140 }
141 } catch (NumberFormatException nfe) {
142 log.warn("getVersionAsLong: failed to parse version " + version, nfe);
143 } catch (NoSuchElementException nsee) {
144 log.warn("getVersionAsLong: failed to parse version, elements are not complete for " + version, nsee);
145 }
146
147 }
148
149 if (log.isDebugEnabled()) {
150 log.debug("getVersionAsLong: returning " + versionNumber);
151 }
152 return versionNumber;
153 }
154
155 public static int getLocaleType(String locale) {
156 if (locale == null || locale.equals("")) {
157 return LOCALE_TYPE_INVALID;
158 }
159
160 if (ITrackerResources.BASE_LOCALE.equalsIgnoreCase(locale)) {
161 return LOCALE_TYPE_BASE;
162 } else if (locale.length() == 5 && locale.indexOf('_') == 2) {
163 return LOCALE_TYPE_LOCALE;
164 } else if (locale.length() == 2) {
165 return LOCALE_TYPE_LANGUAGE;
166 } else {
167 return LOCALE_TYPE_INVALID;
168 }
169 }
170
171 public static String getLocalePart(String locale, int partType) {
172 if (locale == null || partType == LOCALE_TYPE_INVALID) {
173 return null;
174 }
175
176 if (partType == LOCALE_TYPE_LOCALE && locale.length() == 5
177 && locale.indexOf('_') == 2) {
178 return locale;
179 } else if (partType == LOCALE_TYPE_LANGUAGE && locale.length() == 5
180 && locale.indexOf('_') == 2) {
181 return locale.substring(0, 2);
182 } else if (partType == LOCALE_TYPE_LANGUAGE && locale.length() == 2) {
183 return locale;
184 } else if (partType == LOCALE_TYPE_BASE) {
185 return ITrackerResources.BASE_LOCALE;
186 }
187
188 return null;
189 }
190
191 public static Configuration[] nvpArrayToConfigurationArray(int configType,
192 NameValuePair[] names) {
193 if (names == null) {
194 return new Configuration[0];
195 }
196
197 Configuration[] configModels = new Configuration[names.length];
198 for (int i = 0; i < names.length; i++) {
199 configModels[i] = new Configuration(configType, names[i]);
200 }
201
202 return configModels;
203 }
204 }