SystemConfigurationUtilities.java

  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. package org.itracker.model.util;

  19. import org.apache.log4j.Logger;
  20. import org.itracker.core.resources.ITrackerResources;
  21. import org.itracker.model.Configuration;
  22. import org.itracker.model.NameValuePair;

  23. import java.util.NoSuchElementException;
  24. import java.util.StringTokenizer;

  25. public class SystemConfigurationUtilities {

  26.     private static final Logger log = Logger.getLogger(SystemConfigurationUtilities.class);


  27.     @Deprecated
  28.     public static final int TYPE_INITIALIZED = Configuration.Type.initialized.getCode();
  29.     @Deprecated
  30.     public static final int TYPE_LOCALE = Configuration.Type.locale.getCode();
  31.     @Deprecated
  32.     public static final int TYPE_STATUS = Configuration.Type.status.getCode();
  33.     @Deprecated
  34.     public static final int TYPE_SEVERITY = Configuration.Type.severity.getCode();
  35.     @Deprecated
  36.     public static final int TYPE_RESOLUTION = Configuration.Type.resolution.getCode();
  37.     @Deprecated
  38.     public static final int TYPE_CUSTOMFIELD = Configuration.Type.customfield.getCode();

  39.     @Deprecated
  40.     public static final int ACTION_CREATE = 1;
  41.     @Deprecated
  42.     public static final int ACTION_UPDATE = 2;
  43.     @Deprecated
  44.     public static final int ACTION_REMOVE = 3;

  45.     public static final int LOCALE_TYPE_INVALID = -1;
  46.     public static final int LOCALE_TYPE_BASE = 1;
  47.     public static final int LOCALE_TYPE_LANGUAGE = 2;
  48.     public static final int LOCALE_TYPE_LOCALE = 3;

  49.     /**
  50.      * Returns the key for a particular configuration item. This is made up of a
  51.      * static part based on the type of configuration item, and the unique value
  52.      * of the configuration item.
  53.      *
  54.      * @param configuration the Configuration to return the key for
  55.      * @return the key for the item
  56.      */
  57.     public static String getLanguageKey(Configuration configuration) {
  58.         if (configuration != null) {
  59.             return configuration.getType().getLanguageKey(configuration);
  60.         }
  61.         return "";
  62.     }

  63.     @Deprecated
  64.     public static String getTypeName(int type) {

  65.         return Configuration.Type.valueOf(type).name();

  66.     }


  67.     public static String getTypeLanguageKey(Configuration configuration) {
  68.         return configuration.getType().getTypeLanguageKey();
  69.     }


  70.     public static long getVersionAsLong(String version) {
  71.         long versionNumber = 0;
  72.         if (log.isDebugEnabled()) {
  73.             log.debug("getVersionAsLong: transforming " + version);
  74.         }
  75.         if (version != null) {
  76.             if ("0".equals(version)) {
  77.                 return 0;
  78.             }
  79.             // support -SNAPSHOT versions
  80.             version = version.split("-")[0];
  81.             StringTokenizer token = new StringTokenizer(version, ".");
  82.             try {
  83.                 if (token.countTokens() > 0 && token.countTokens() <= 3) {
  84.                     versionNumber += 1000000L * Integer.parseInt(token
  85.                             .nextToken());
  86.                     versionNumber += 1000L * Integer
  87.                             .parseInt(token.nextToken());
  88.                     versionNumber += Integer.parseInt(token.nextToken());
  89.                 } else {
  90.                     throw new IllegalArgumentException("The version " + version + " is not parseable, excpected '(int)number[.(int)major[.(int)minor]]");
  91.                 }
  92.             } catch (NumberFormatException nfe) {
  93.                 // OK
  94.             } catch (NoSuchElementException nsee) {
  95.                 // OK
  96.             }

  97.         }

  98.         if (log.isDebugEnabled()) {
  99.             log.debug("getVersionAsLong: returning " + versionNumber);
  100.         }
  101.         return versionNumber;
  102.     }

  103.     public static int getLocaleType(String locale) {
  104.         if (locale == null || locale.equals("")) {
  105.             return LOCALE_TYPE_INVALID;
  106.         }

  107.         if (ITrackerResources.BASE_LOCALE.equalsIgnoreCase(locale)) {
  108.             return LOCALE_TYPE_BASE;
  109.         } else if (locale.length() == 5 && locale.indexOf('_') == 2) {
  110.             return LOCALE_TYPE_LOCALE;
  111.         } else if (locale.length() == 2) {
  112.             return LOCALE_TYPE_LANGUAGE;
  113.         } else {
  114.             return LOCALE_TYPE_INVALID;
  115.         }
  116.     }

  117.     public static String getLocalePart(String locale, int partType) {
  118.         if (locale == null || partType == LOCALE_TYPE_INVALID) {
  119.             return null;
  120.         }

  121.         if (partType == LOCALE_TYPE_LOCALE && locale.length() == 5
  122.                 && locale.indexOf('_') == 2) {
  123.             return locale;
  124.         } else if (partType == LOCALE_TYPE_LANGUAGE && locale.length() == 5
  125.                 && locale.indexOf('_') == 2) {
  126.             return locale.substring(0, 2);
  127.         } else if (partType == LOCALE_TYPE_LANGUAGE && locale.length() == 2) {
  128.             return locale;
  129.         } else if (partType == LOCALE_TYPE_BASE) {
  130.             return ITrackerResources.BASE_LOCALE;
  131.         }

  132.         return null;
  133.     }

  134.     public static Configuration[] nvpArrayToConfigurationArray(Configuration.Type configType,
  135.                                                                NameValuePair[] names) {
  136.         if (names == null) {
  137.             return new Configuration[0];
  138.         }

  139.         Configuration[] configModels = new Configuration[names.length];
  140.         for (int i = 0; i < names.length; i++) {
  141.             configModels[i] = new Configuration(configType, names[i]);
  142.         }

  143.         return configModels;
  144.     }
  145. }