1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
28
29
30
31
32 @Deprecated
33 public static List<NameValuePair> customFieldOptionsToNameValuePairs(
34 List<CustomFieldValue> options) {
35 return customFieldOptionsToNameValuePairs(options, null);
36
37 }
38
39
40
41
42
43
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
63
64
65
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
82
83
84
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
103
104
105
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
125
126
127
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 }