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.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  	 * Returns the key for a particular configuration item. This is made up of a
56  	 * static part based on the type of configuration item, and the unique value
57  	 * of the configuration item.
58  	 * 
59  	 * @param model
60  	 *            the Configuration to return the key for
61  	 * @return the key for the item
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  	 * This method will attempt to load all of the locales defined in the
82  	 * ITracker.properties file, and add them to the database if they don't
83  	 * already exist.
84  	 * 
85  	 * @param configurationService
86  	 *            a configurationService object to use when processing the
87  	 *            locales
88  	 * @param forceReload
89  	 *            if true, it will reload the languages from the property files
90  	 *            even if they are listed as being up to date
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 }