KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > util > SystemConfigurationUtilities


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 cowsultants.itracker.ejb.client.util;
20
21 import java.util.*;
22
23 import cowsultants.itracker.ejb.client.interfaces.SystemConfiguration;
24 import cowsultants.itracker.ejb.client.models.*;
25 import cowsultants.itracker.ejb.client.resources.*;
26
27 public class SystemConfigurationUtilities {
28     public static final String JavaDoc DEFAULT_DATASOURCE = "java:/ITrackerDS";
29     public static final String JavaDoc DEFAULT_COMPONENTBEAN_TABLE_NAME = "componentbean";
30     public static final String JavaDoc DEFAULT_COMPONENTBEAN_REL_TABLE_NAME = "issue_component_rel";
31     public static final String JavaDoc DEFAULT_ISSUEBEAN_TABLE_NAME = "issuebean";
32     public static final String JavaDoc DEFAULT_ISSUEHISTORYBEAN_TABLE_NAME = "issuehistorybean";
33     public static final String JavaDoc DEFAULT_PROJECTBEAN_TABLE_NAME = "projectbean";
34     public static final String JavaDoc DEFAULT_VERSIONBEAN_TABLE_NAME = "versionbean";
35     public static final String JavaDoc DEFAULT_VERSIONBEAN_REL_TABLE_NAME = "issue_version_rel";
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     /**
55       * Returns the key for a particular configuration item. This is made up of
56       * a static part based on the type of configuration item, and the unique value
57       * of the configuration item.
58       * @param model the ConfigurationModel to return the key for
59       * @return the key for the item
60       */

61     public static String JavaDoc getLanguageKey(ConfigurationModel model) {
62         if(model != null) {
63             int type = model.getType();
64             if(type == SystemConfigurationUtilities.TYPE_STATUS) {
65                 return ITrackerResources.KEY_BASE_STATUS + model.getValue();
66             } else if(type == SystemConfigurationUtilities.TYPE_SEVERITY) {
67                 return ITrackerResources.KEY_BASE_SEVERITY + model.getValue();
68             } else if(type == SystemConfigurationUtilities.TYPE_RESOLUTION) {
69                 return ITrackerResources.KEY_BASE_RESOLUTION + model.getValue();
70             }
71         }
72         return "";
73     }
74
75     /**
76       * This method will attempt to load all of the locales defined in the ITracker.properties file,
77       * and add them to the database if they don't already exist.
78       * @param sc a SystemConfiguration object to use when processing the locales
79       * @param forceReload if true, it will reload the languages from the property files even if they are listed
80       * as being up to date
81       */

82     public static void initializeAllLanguages(SystemConfiguration sc, boolean forceReload) {
83         HashSet definedLocales = new HashSet();
84
85         sc.initializeLocale(ITrackerResources.BASE_LOCALE, forceReload);
86
87         LanguageModel definedLocalesString = sc.getLanguageItemByKey(ITrackerResources.DEFINED_LOCALES_KEY, null);
88
89         if(definedLocalesString != null && definedLocalesString.getResourceValue() != null) {
90             String JavaDoc locales = definedLocalesString.getResourceValue();
91             StringTokenizer token = new StringTokenizer(locales, ",");
92             while(token.hasMoreTokens()) {
93                 String JavaDoc locale = token.nextToken();
94                 if(locale.length() == 5 && locale.indexOf('_') == 2) {
95                     definedLocales.add(locale.substring(0, 2));
96                 }
97                 definedLocales.add(locale);
98             }
99         }
100
101         for(Iterator iter = definedLocales.iterator(); iter.hasNext(); ) {
102             String JavaDoc locale = (String JavaDoc) iter.next();
103             Logger.logDebug("Starting language initialization for " + locale);
104             sc.initializeLocale(locale, forceReload);
105         }
106     }
107
108     public static long getVersionAsLong(String JavaDoc version) {
109         long versionNumber = 0;
110
111         if(version != null) {
112             try {
113                 StringTokenizer token = new StringTokenizer(version, ".");
114                 if(token.countTokens() == 3) {
115                       versionNumber += 1000000 * Integer.parseInt(token.nextToken());
116                       versionNumber += 1000 * Integer.parseInt(token.nextToken());
117                       versionNumber += Integer.parseInt(token.nextToken());
118                 }
119             } catch(Exception JavaDoc e) {
120             }
121         }
122
123         return versionNumber;
124     }
125
126     public static int getLocaleType(String JavaDoc locale) {
127         if(locale == null || locale.equals("")) {
128             return LOCALE_TYPE_INVALID;
129         }
130
131         if(ITrackerResources.BASE_LOCALE.equalsIgnoreCase(locale)) {
132             return LOCALE_TYPE_BASE;
133         } else if(locale.length() == 5 && locale.indexOf('_') == 2) {
134             return LOCALE_TYPE_LOCALE;
135         } else if(locale.length() == 2) {
136             return LOCALE_TYPE_LANGUAGE;
137         } else {
138             return LOCALE_TYPE_INVALID;
139         }
140     }
141
142     public static String JavaDoc getLocalePart(String JavaDoc locale, int partType) {
143         if(locale == null || partType == LOCALE_TYPE_INVALID) {
144             return null;
145         }
146
147         if(partType == LOCALE_TYPE_LOCALE && locale.length() == 5 && locale.indexOf('_') == 2) {
148             return locale;
149         } else if(partType == LOCALE_TYPE_LANGUAGE && locale.length() == 5 && locale.indexOf('_') == 2) {
150             return locale.substring(0, 2);
151         } else if(partType == LOCALE_TYPE_LANGUAGE && locale.length() == 2) {
152             return locale;
153         } else if(partType == LOCALE_TYPE_BASE) {
154             return ITrackerResources.BASE_LOCALE;
155         }
156
157         return null;
158     }
159
160     public static ConfigurationModel[] nvpArrayToConfigurationArray(int configType, NameValuePairModel[] names) {
161         if(names == null) {
162             return new ConfigurationModel[0];
163         }
164
165         ConfigurationModel[] configModels = new ConfigurationModel[names.length];
166         for(int i = 0; i < names.length; i++) {
167             configModels[i] = new ConfigurationModel(configType, names[i]);
168         }
169
170         return configModels;
171     }
172 }
173
Popular Tags