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.itracker.model.*;
22  
23  import java.util.*;
24  
25  public class Convert {
26      /**
27       * Converts an array of CustomFieldValueModels to NameValuePairs
28       *
29       * @param options the array of CustomFieldValueModels to convert
30       * @return the new NameValuePair array
31       */
32      @Deprecated
33      public static List<NameValuePair> customFieldOptionsToNameValuePairs(
34              List<CustomFieldValue> options) {
35          return customFieldOptionsToNameValuePairs(options, null);
36  
37      }
38  
39      /**
40       * Converts an array of CustomFieldValueModels to NameValuePairs
41       *
42       * @param options the array of CustomFieldValueModels to convert
43       * @return the new NameValuePair array
44       */
45      private static List<NameValuePair> customFieldOptionsToNameValuePairs(
46              List<CustomFieldValue> options, Locale locale) {
47          List<NameValuePair> returnValues = new ArrayList<NameValuePair>();
48          String name;
49          if (options != null) {
50              returnValues = new ArrayList<NameValuePair>();
51              for (int i = 0; i < options.size(); i++) {
52                  name = CustomFieldUtilities.getCustomFieldOptionName(options.get(i), locale);
53                  returnValues
54                          .add(new NameValuePair(name, options.get(i).getValue()));
55              }
56          }
57  
58          return returnValues;
59      }
60  
61      /**
62       * Returns the options sorted-
63       * @param customField LIST custom-field
64       * @param locale a locale to translate values
65       * @return
66       */
67      public static List<NameValuePair> customFieldOptionsToNameValuePairs(
68              CustomField customField, Locale locale) {
69          List<CustomFieldValue> kvs = customField.getOptions();
70          if (!customField.isSortOptionsByName()) {
71              Collections.sort(kvs, CustomFieldValue.SORT_ORDER_COMPARATOR);
72          }
73          List<NameValuePair> opts = customFieldOptionsToNameValuePairs(customField.getOptions(), locale);
74          if (customField.isSortOptionsByName()) {
75              Collections.sort(opts, NameValuePair.KEY_COMPARATOR);
76          }
77          return opts;
78      }
79  
80      /**
81       * Converts an array of UserModels to NameValuePairs
82       *
83       * @param users the array of UserModels to convert
84       * @return the new NameValuePair array
85       */
86      public static List<NameValuePair> usersToNameValuePairs(List<User> users) {
87          List<NameValuePair> returnValues = new ArrayList<NameValuePair>();
88  
89          if (users != null) {
90              returnValues = new ArrayList<NameValuePair>();
91              for (int i = 0; i < users.size(); i++) {
92                  returnValues.add(new NameValuePair(users.get(i).getFirstName()
93                          + " " + users.get(i).getLastName(), users.get(i)
94                          .getId().toString()));
95              }
96          }
97  
98          return returnValues;
99      }
100 
101     /**
102      * Converts an array of ComponentModels to NameValuePairs
103      *
104      * @param options the array of ComponentModels to convert
105      * @return the new NameValuePair array
106      */
107     public static List<NameValuePair> componentsToNameValuePairs(
108             List<Component> components) {
109         NameValuePairaluePair">NameValuePair[] returnValues = new NameValuePair[0];
110 
111         if (components != null) {
112             returnValues = new NameValuePair[components.size()];
113             for (int i = 0; i < components.size(); i++) {
114                 returnValues[i] = new NameValuePair(
115                         components.get(i).getName(), components.get(i).getId()
116                         .toString());
117             }
118         }
119 
120         return Arrays.asList(returnValues);
121     }
122 
123     /**
124      * Converts an array of VersionModels to NameValuePairs
125      *
126      * @param options the array of VersionModels to convert
127      * @return the new NameValuePair array
128      */
129     public static List<NameValuePair> versionsToNameValuePairs(
130             List<Version> versions) {
131         NameValuePairaluePair">NameValuePair[] returnValues = new NameValuePair[0];
132 
133         if (versions != null) {
134             returnValues = new NameValuePair[versions.size()];
135             for (int i = 0; i < versions.size(); i++) {
136                 returnValues[i] = new NameValuePair(
137                         versions.get(i).getNumber(), versions.get(i).getId()
138                         .toString());
139             }
140         }
141 
142         return Arrays.asList(returnValues);
143     }
144 
145     public static String[] stringToArray(String input) {
146         if (input == null || "".equals(input)) {
147             return new String[0];
148         }
149 
150         List<String> tokenList = new ArrayList<String>();
151         StringTokenizer tokenizer = new StringTokenizer(input, " ");
152         while (tokenizer.hasMoreElements()) {
153             boolean quotedToken = false;
154             String tokenString = tokenizer.nextToken();
155             if (tokenString.startsWith("\"")) {
156                 quotedToken = true;
157                 tokenString = tokenString.substring(1);
158                 if (tokenString.endsWith("\"") && !tokenString.endsWith("\\\"")) {
159                     quotedToken = false;
160                     tokenString = tokenString.substring(0, tokenString.length() - 1);
161                 }
162             }
163 
164             if (quotedToken) {
165                 boolean getNext = true;
166 
167                 StringBuffer token = new StringBuffer(tokenString);
168                 while (getNext) {
169                     try {
170                         token.append(tokenizer.nextToken("\""));
171                         if (!token.toString().endsWith("\\\"")) {
172                             getNext = false;
173                         }
174                     } catch (NoSuchElementException e) {
175                         break;
176                     }
177                 }
178                 tokenString = token.toString();
179             }
180 
181             tokenList.add(tokenString);
182         }
183 
184         String[] stringArray = (String[]) tokenList.toArray(new String[]{});
185 
186         return stringArray;
187     }
188 }