Convert.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.itracker.model.*;

  20. import java.util.*;

  21. public class Convert {
  22.     /**
  23.      * Converts an array of CustomFieldValueModels to NameValuePairs
  24.      *
  25.      * @param options the array of CustomFieldValueModels to convert
  26.      * @return the new NameValuePair array
  27.      */
  28.     @Deprecated
  29.     public static List<NameValuePair> customFieldOptionsToNameValuePairs(
  30.             List<CustomFieldValue> options) {
  31.         return customFieldOptionsToNameValuePairs(options, null);

  32.     }

  33.     /**
  34.      * Converts an array of CustomFieldValueModels to NameValuePairs
  35.      *
  36.      * @param options the array of CustomFieldValueModels to convert
  37.      * @return the new NameValuePair array
  38.      */
  39.     private static List<NameValuePair> customFieldOptionsToNameValuePairs(
  40.             List<CustomFieldValue> options, Locale locale) {
  41.         List<NameValuePair> returnValues = new ArrayList<NameValuePair>();
  42.         String name;
  43.         if (options != null) {
  44.             returnValues = new ArrayList<NameValuePair>();
  45.             for (int i = 0; i < options.size(); i++) {
  46.                 name = CustomFieldUtilities.getCustomFieldOptionName(options.get(i), locale);
  47.                 returnValues
  48.                         .add(new NameValuePair(name, options.get(i).getValue()));
  49.             }
  50.         }

  51.         return returnValues;
  52.     }

  53.     /**
  54.      * Returns the options sorted-
  55.      * @param customField LIST custom-field
  56.      * @param locale a locale to translate values
  57.      * @return
  58.      */
  59.     public static List<NameValuePair> customFieldOptionsToNameValuePairs(
  60.             CustomField customField, Locale locale) {
  61.         List<CustomFieldValue> kvs = customField.getOptions();
  62.         if (!customField.isSortOptionsByName()) {
  63.             Collections.sort(kvs, CustomFieldValue.SORT_ORDER_COMPARATOR);
  64.         }
  65.         List<NameValuePair> opts = customFieldOptionsToNameValuePairs(customField.getOptions(), locale);
  66.         if (customField.isSortOptionsByName()) {
  67.             Collections.sort(opts, NameValuePair.KEY_COMPARATOR);
  68.         }
  69.         return opts;
  70.     }

  71.     /**
  72.      * Converts an array of UserModels to NameValuePairs
  73.      *
  74.      * @param users the array of UserModels to convert
  75.      * @return the new NameValuePair array
  76.      */
  77.     public static List<NameValuePair> usersToNameValuePairs(List<User> users) {
  78.         List<NameValuePair> returnValues = new ArrayList<NameValuePair>();

  79.         if (users != null) {
  80.             returnValues = new ArrayList<NameValuePair>();
  81.             for (int i = 0; i < users.size(); i++) {
  82.                 returnValues.add(new NameValuePair(users.get(i).getFirstName()
  83.                         + " " + users.get(i).getLastName(), users.get(i)
  84.                         .getId().toString()));
  85.             }
  86.         }

  87.         return returnValues;
  88.     }

  89.     /**
  90.      * Converts an array of ComponentModels to NameValuePairs
  91.      *
  92.      * @param options the array of ComponentModels to convert
  93.      * @return the new NameValuePair array
  94.      */
  95.     public static List<NameValuePair> componentsToNameValuePairs(
  96.             List<Component> components) {
  97.         NameValuePair[] returnValues = new NameValuePair[0];

  98.         if (components != null) {
  99.             returnValues = new NameValuePair[components.size()];
  100.             for (int i = 0; i < components.size(); i++) {
  101.                 returnValues[i] = new NameValuePair(
  102.                         components.get(i).getName(), components.get(i).getId()
  103.                         .toString());
  104.             }
  105.         }

  106.         return Arrays.asList(returnValues);
  107.     }

  108.     /**
  109.      * Converts an array of VersionModels to NameValuePairs
  110.      *
  111.      * @param options the array of VersionModels to convert
  112.      * @return the new NameValuePair array
  113.      */
  114.     public static List<NameValuePair> versionsToNameValuePairs(
  115.             List<Version> versions) {
  116.         NameValuePair[] returnValues = new NameValuePair[0];

  117.         if (versions != null) {
  118.             returnValues = new NameValuePair[versions.size()];
  119.             for (int i = 0; i < versions.size(); i++) {
  120.                 returnValues[i] = new NameValuePair(
  121.                         versions.get(i).getNumber(), versions.get(i).getId()
  122.                         .toString());
  123.             }
  124.         }

  125.         return Arrays.asList(returnValues);
  126.     }

  127.     public static String[] stringToArray(String input) {
  128.         if (input == null || "".equals(input)) {
  129.             return new String[0];
  130.         }

  131.         List<String> tokenList = new ArrayList<String>();
  132.         StringTokenizer tokenizer = new StringTokenizer(input, " ");
  133.         while (tokenizer.hasMoreElements()) {
  134.             boolean quotedToken = false;
  135.             String tokenString = tokenizer.nextToken();
  136.             if (tokenString.startsWith("\"")) {
  137.                 quotedToken = true;
  138.                 tokenString = tokenString.substring(1);
  139.                 if (tokenString.endsWith("\"") && !tokenString.endsWith("\\\"")) {
  140.                     quotedToken = false;
  141.                     tokenString = tokenString.substring(0, tokenString.length() - 1);
  142.                 }
  143.             }

  144.             if (quotedToken) {
  145.                 boolean getNext = true;

  146.                 StringBuffer token = new StringBuffer(tokenString);
  147.                 while (getNext) {
  148.                     try {
  149.                         token.append(tokenizer.nextToken("\""));
  150.                         if (!token.toString().endsWith("\\\"")) {
  151.                             getNext = false;
  152.                         }
  153.                     } catch (NoSuchElementException e) {
  154.                         break;
  155.                     }
  156.                 }
  157.                 tokenString = token.toString();
  158.             }

  159.             tokenList.add(tokenString);
  160.         }

  161.         String[] stringArray = (String[]) tokenList.toArray(new String[]{});

  162.         return stringArray;
  163.     }
  164. }