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.model.util;
20  
21  import org.apache.log4j.Logger;
22  import org.itracker.core.resources.ITrackerResources;
23  import org.itracker.model.Configuration;
24  import org.itracker.model.NameValuePair;
25  
26  import java.util.NoSuchElementException;
27  import java.util.StringTokenizer;
28  
29  public class SystemConfigurationUtilities {
30  
31      private static final Logger log = Logger.getLogger(SystemConfigurationUtilities.class);
32  
33  
34      @Deprecated
35      public static final int TYPE_INITIALIZED = Configuration.Type.initialized.getCode();
36      @Deprecated
37      public static final int TYPE_LOCALE = Configuration.Type.locale.getCode();
38      @Deprecated
39      public static final int TYPE_STATUS = Configuration.Type.status.getCode();
40      @Deprecated
41      public static final int TYPE_SEVERITY = Configuration.Type.severity.getCode();
42      @Deprecated
43      public static final int TYPE_RESOLUTION = Configuration.Type.resolution.getCode();
44      @Deprecated
45      public static final int TYPE_CUSTOMFIELD = Configuration.Type.customfield.getCode();
46  
47      @Deprecated
48      public static final int ACTION_CREATE = 1;
49      @Deprecated
50      public static final int ACTION_UPDATE = 2;
51      @Deprecated
52      public static final int ACTION_REMOVE = 3;
53  
54      public static final int LOCALE_TYPE_INVALID = -1;
55      public static final int LOCALE_TYPE_BASE = 1;
56      public static final int LOCALE_TYPE_LANGUAGE = 2;
57      public static final int LOCALE_TYPE_LOCALE = 3;
58  
59      /**
60       * Returns the key for a particular configuration item. This is made up of a
61       * static part based on the type of configuration item, and the unique value
62       * of the configuration item.
63       *
64       * @param configuration the Configuration to return the key for
65       * @return the key for the item
66       */
67      public static String getLanguageKey(Configuration configuration) {
68          if (configuration != null) {
69              return configuration.getType().getLanguageKey(configuration);
70          }
71          return "";
72      }
73  
74      @Deprecated
75      public static String getTypeName(int type) {
76  
77          return Configuration.Type.valueOf(type).name();
78  
79      }
80  
81  
82      public static String getTypeLanguageKey(Configuration configuration) {
83          return configuration.getType().getTypeLanguageKey();
84      }
85  
86  
87      public static long getVersionAsLong(String version) {
88          long versionNumber = 0;
89          if (log.isDebugEnabled()) {
90              log.debug("getVersionAsLong: transforming " + version);
91          }
92          if (version != null) {
93              if ("0".equals(version)) {
94                  return 0;
95              }
96              // support -SNAPSHOT versions
97              version = version.split("-")[0];
98              StringTokenizer token = new StringTokenizer(version, ".");
99              try {
100                 if (token.countTokens() > 0 && token.countTokens() <= 3) {
101                     versionNumber += 1000000L * Integer.parseInt(token
102                             .nextToken());
103                     versionNumber += 1000L * Integer
104                             .parseInt(token.nextToken());
105                     versionNumber += Integer.parseInt(token.nextToken());
106                 } else {
107                     throw new IllegalArgumentException("The version " + version + " is not parseable, excpected '(int)number[.(int)major[.(int)minor]]");
108                 }
109             } catch (NumberFormatException nfe) {
110                 // OK
111             } catch (NoSuchElementException nsee) {
112                 // OK
113             }
114 
115         }
116 
117         if (log.isDebugEnabled()) {
118             log.debug("getVersionAsLong: returning " + versionNumber);
119         }
120         return versionNumber;
121     }
122 
123     public static int getLocaleType(String locale) {
124         if (locale == null || locale.equals("")) {
125             return LOCALE_TYPE_INVALID;
126         }
127 
128         if (ITrackerResources.BASE_LOCALE.equalsIgnoreCase(locale)) {
129             return LOCALE_TYPE_BASE;
130         } else if (locale.length() == 5 && locale.indexOf('_') == 2) {
131             return LOCALE_TYPE_LOCALE;
132         } else if (locale.length() == 2) {
133             return LOCALE_TYPE_LANGUAGE;
134         } else {
135             return LOCALE_TYPE_INVALID;
136         }
137     }
138 
139     public static String getLocalePart(String locale, int partType) {
140         if (locale == null || partType == LOCALE_TYPE_INVALID) {
141             return null;
142         }
143 
144         if (partType == LOCALE_TYPE_LOCALE && locale.length() == 5
145                 && locale.indexOf('_') == 2) {
146             return locale;
147         } else if (partType == LOCALE_TYPE_LANGUAGE && locale.length() == 5
148                 && locale.indexOf('_') == 2) {
149             return locale.substring(0, 2);
150         } else if (partType == LOCALE_TYPE_LANGUAGE && locale.length() == 2) {
151             return locale;
152         } else if (partType == LOCALE_TYPE_BASE) {
153             return ITrackerResources.BASE_LOCALE;
154         }
155 
156         return null;
157     }
158 
159     public static Configuration[] nvpArrayToConfigurationArray(Configuration.Type configType,
160                                                                NameValuePair[] names) {
161         if (names == null) {
162             return new Configuration[0];
163         }
164 
165         Configurationguration">Configuration[] configModels = new Configuration[names.length];
166         for (int i = 0; i < names.length; i++) {
167             configModels[i] = new Configuration(configType, names[i]);
168         }
169 
170         return configModels;
171     }
172 }