Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
67   203   42   11.17
46   141   0.63   6
6     7  
1    
 
 
  SystemConfigurationUtilities       Line # 32 67 42 0% 0.0
 
No Tests
 
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.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.NameValuePair;
30    import org.itracker.services.ConfigurationService;
31   
 
32    public class SystemConfigurationUtilities {
33   
34    public static final String DEFAULT_DATASOURCE = "java:/ITrackerDS";
35    private static final Logger log = Logger.getLogger(SystemConfigurationUtilities.class);
36   
37    public static final int TYPE_INITIALIZED = -1;
38    public static final int TYPE_LOCALE = 1;
39    public static final int TYPE_STATUS = 2;
40    public static final int TYPE_SEVERITY = 3;
41    public static final int TYPE_RESOLUTION = 4;
42    public static final int TYPE_CUSTOMFIELD = 5;
43   
44    public static final int ACTION_CREATE = 1;
45    public static final int ACTION_UPDATE = 2;
46    public static final int ACTION_REMOVE = 3;
47   
48    public static final int LOCALE_TYPE_INVALID = -1;
49    public static final int LOCALE_TYPE_BASE = 1;
50    public static final int LOCALE_TYPE_LANGUAGE = 2;
51    public static final int LOCALE_TYPE_LOCALE = 3;
52   
53    /**
54    * Returns the key for a particular configuration item. This is made up of a
55    * static part based on the type of configuration item, and the unique value
56    * of the configuration item.
57    *
58    * @param model
59    * the Configuration to return the key for
60    * @return the key for the item
61    */
 
62  0 toggle public static String getLanguageKey(Configuration configuration) {
63  0 if (configuration != null) {
64  0 int type = configuration.getType();
65  0 if (type == SystemConfigurationUtilities.TYPE_STATUS) {
66  0 return ITrackerResources.KEY_BASE_STATUS
67    + configuration.getValue();
68  0 } else if (type == SystemConfigurationUtilities.TYPE_SEVERITY) {
69  0 return ITrackerResources.KEY_BASE_SEVERITY
70    + configuration.getValue();
71  0 } else if (type == SystemConfigurationUtilities.TYPE_RESOLUTION) {
72  0 return ITrackerResources.KEY_BASE_RESOLUTION
73    + configuration.getValue();
74    }
75    }
76  0 return "";
77    }
78   
79    /**
80    * This method will attempt to load all of the locales defined in the
81    * ITracker.properties file, and add them to the database if they don't
82    * already exist.
83    *
84    * @param configurationService
85    * a configurationService object to use when processing the
86    * locales
87    * @param forceReload
88    * if true, it will reload the languages from the property files
89    * even if they are listed as being up to date
90    */
 
91  0 toggle public static void initializeAllLanguages(
92    ConfigurationService configurationService, boolean forceReload) {
93  0 HashSet<String> definedLocales = new HashSet<String>();
94   
95  0 configurationService.initializeLocale(ITrackerResources.BASE_LOCALE,
96    forceReload);
97   
98  0 String definedLocalesString;
99  0 try {
100  0 definedLocalesString = configurationService
101    .getLanguageItemByKey(ITrackerResources.DEFINED_LOCALES_KEY,
102    null).getResourceValue();
103    } catch (RuntimeException e) {
104  0 definedLocalesString = ITrackerResources.getString(ITrackerResources.DEFINED_LOCALES_KEY);
105    }
106  0 if (definedLocalesString != null) {
107  0 StringTokenizer token = new StringTokenizer(definedLocalesString, ",");
108  0 while (token.hasMoreTokens()) {
109  0 String locale = token.nextToken();
110  0 if (locale.length() == 5 && locale.indexOf('_') == 2) {
111  0 definedLocales.add(locale.substring(0, 2));
112    }
113  0 definedLocales.add(locale);
114    }
115    }
116   
117  0 for (Iterator<String> iter = definedLocales.iterator(); iter.hasNext();) {
118  0 String locale = iter.next();
119  0 configurationService.initializeLocale(locale, forceReload);
120    }
121    }
122   
 
123  0 toggle public static long getVersionAsLong(String version) {
124  0 long versionNumber = 0;
125  0 if (log.isDebugEnabled()) {
126  0 log.debug("getVersionAsLong: transforming " + version);
127    }
128  0 if (version != null) {
129  0 StringTokenizer token = new StringTokenizer(version, ".");
130  0 try {
131  0 if (token.countTokens() > 0 && token.countTokens() <= 3) {
132  0 versionNumber += 1000000L * Integer.parseInt(token
133    .nextToken());
134  0 versionNumber += 1000L * Integer
135    .parseInt(token.nextToken());
136  0 versionNumber += Integer.parseInt(token.nextToken());
137    } else {
138  0 throw new IllegalArgumentException("The version " + version + " is not parseable, excpected '(int)number[.(int)major[.(int)minor]]");
139    }
140    } catch (NumberFormatException nfe) {
141  0 log.warn("getVersionAsLong: failed to parse version " + version, nfe);
142    } catch (NoSuchElementException nsee) {
143  0 log.warn("getVersionAsLong: failed to parse version, elements are not complete for " + version, nsee);
144    }
145   
146    }
147   
148  0 if (log.isDebugEnabled()) {
149  0 log.debug("getVersionAsLong: returning " + versionNumber);
150    }
151  0 return versionNumber;
152    }
153   
 
154  0 toggle public static int getLocaleType(String locale) {
155  0 if (locale == null || locale.equals("")) {
156  0 return LOCALE_TYPE_INVALID;
157    }
158   
159  0 if (ITrackerResources.BASE_LOCALE.equalsIgnoreCase(locale)) {
160  0 return LOCALE_TYPE_BASE;
161  0 } else if (locale.length() == 5 && locale.indexOf('_') == 2) {
162  0 return LOCALE_TYPE_LOCALE;
163  0 } else if (locale.length() == 2) {
164  0 return LOCALE_TYPE_LANGUAGE;
165    } else {
166  0 return LOCALE_TYPE_INVALID;
167    }
168    }
169   
 
170  0 toggle public static String getLocalePart(String locale, int partType) {
171  0 if (locale == null || partType == LOCALE_TYPE_INVALID) {
172  0 return null;
173    }
174   
175  0 if (partType == LOCALE_TYPE_LOCALE && locale.length() == 5
176    && locale.indexOf('_') == 2) {
177  0 return locale;
178  0 } else if (partType == LOCALE_TYPE_LANGUAGE && locale.length() == 5
179    && locale.indexOf('_') == 2) {
180  0 return locale.substring(0, 2);
181  0 } else if (partType == LOCALE_TYPE_LANGUAGE && locale.length() == 2) {
182  0 return locale;
183  0 } else if (partType == LOCALE_TYPE_BASE) {
184  0 return ITrackerResources.BASE_LOCALE;
185    }
186   
187  0 return null;
188    }
189   
 
190  0 toggle public static Configuration[] nvpArrayToConfigurationArray(int configType,
191    NameValuePair[] names) {
192  0 if (names == null) {
193  0 return new Configuration[0];
194    }
195   
196  0 Configuration[] configModels = new Configuration[names.length];
197  0 for (int i = 0; i < names.length; i++) {
198  0 configModels[i] = new Configuration(configType, names[i]);
199    }
200   
201  0 return configModels;
202    }
203    }