1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation;
18
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import COM.FutureTense.Interfaces.IList;
26 import COM.FutureTense.Interfaces.Utilities;
27 import COM.FutureTense.Util.IterableIListWrapper;
28
29 import com.fatwire.assetapi.data.AssetId;
30 import com.fatwire.cs.core.db.Util;
31 import com.fatwire.gst.foundation.facade.assetapi.AssetIdUtils;
32
33 import org.apache.commons.lang.StringUtils;
34
35
36
37
38
39
40
41 public final class IListUtils {
42 private IListUtils() {
43 }
44
45
46
47
48
49
50
51
52
53
54 public static String getStringValue(IList list, String colname) {
55 try {
56 return list.getValue(colname);
57 } catch (NoSuchFieldException e) {
58 throw new IllegalArgumentException("No such field: " + colname, e);
59 }
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73 public static long getLongValue(IList list, String colname) {
74 try {
75 String s = list.getValue(colname);
76 return Long.valueOf(s);
77 } catch (NoSuchFieldException e) {
78 throw new IllegalArgumentException("No such field: " + colname, e);
79 }
80 }
81
82
83
84
85
86
87
88
89
90
91 public static Date getDateValue(IList list, String colname) {
92 String s = getStringValue(list, colname);
93 return !Utilities.goodString(s) ? null : Util.parseJdbcDate(s);
94 }
95
96 private static final ThreadLocal<Long> counter = new ThreadLocal<Long>() {
97
98
99
100
101
102
103 @Override
104 protected Long initialValue() {
105 return System.currentTimeMillis();
106 }
107
108
109
110
111
112
113 @Override
114 public Long get() {
115 long c = super.get();
116 c++;
117 set(c);
118 return c;
119
120 }
121 };
122
123
124
125
126
127
128
129 public static String generateRandomListName() {
130 return generateRandomListName("rnd-");
131 }
132
133
134
135
136
137
138
139
140
141 public static String generateRandomListName(String prefix) {
142 if (StringUtils.isBlank(prefix))
143 throw new IllegalArgumentException("prefix must not be blank.");
144 return prefix + counter.get();
145 }
146
147
148
149
150
151
152
153
154 public static Collection<AssetId> toAssetIdCollection(IList result) {
155 if (result == null || !result.hasData())
156 return Collections.emptyList();
157 final List<AssetId> list = new LinkedList<AssetId>();
158 for (IList row : new IterableIListWrapper(result)) {
159 AssetId id;
160 try {
161 id = AssetIdUtils.createAssetId(row.getValue("assettype"), row.getValue("assetid"));
162 list.add(id);
163 } catch (NoSuchFieldException e) {
164 throw new RuntimeException(e.getMessage());
165 }
166
167 }
168 return list;
169 }
170
171 }