1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.imixs.workflow.xml;
28
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Vector;
35
36 import org.imixs.workflow.ItemCollection;
37
38
39
40
41
42
43
44
45
46
47
48 public class XMLItemCollectionAdapter {
49
50
51
52
53
54
55
56
57
58 @SuppressWarnings("unchecked")
59 public static ItemCollection getItemCollection(XMLItemCollection entity) {
60 ItemCollection itemCol = new ItemCollection();
61 if (entity == null)
62 return itemCol;
63
64 try {
65 XMLItem items[] = entity.getItem();
66 if (items != null)
67 for (int i = 0; i < items.length; i++) {
68 XMLItem it = items[i];
69 if (it == null)
70 continue;
71 String key = it.getName();
72 Object[] it_value = it.getValue();
73 if (it_value == null) {
74
75 itemCol.replaceItemValue(key, new Vector());
76 } else {
77 Vector myVector = new Vector(Arrays.asList(it
78 .getValue()));
79 itemCol.replaceItemValue(key, myVector);
80 }
81 }
82 } catch (Exception e) {
83 System.out
84 .println("[XMLItemCollectionAdapter] getItemCollection error");
85 System.out.println(e.toString());
86 itemCol = null;
87 }
88
89 return itemCol;
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 @SuppressWarnings("unchecked")
107 public static XMLItemCollection putItemCollection(
108 ItemCollection aItemCollection, List<String> itemNames)
109 throws Exception {
110 String sName = null;
111 XMLItemCollection entity = new XMLItemCollection();
112 int i = 0;
113 XMLItem[] items = null;
114 try {
115 if (aItemCollection != null) {
116
117 if (itemNames != null && itemNames.size() > 0) {
118 items = new XMLItem[itemNames.size()];
119 for (String aField : itemNames) {
120
121
122 sName = aField;
123 XMLItem item = new XMLItem();
124
125 Vector vOrg = (Vector) aItemCollection
126 .getItemValue(aField);
127 if (!isBasicType(vOrg,sName)) {
128
129 item.setName(sName);
130 vOrg=new Vector();
131 vOrg.add(null);
132 item.setValue(vOrg.toArray());
133 } else {
134 item.setName(sName);
135
136 if (vOrg.size()==0)
137 vOrg.add(null);
138 item.setValue(vOrg.toArray());
139 }
140 items[i] = item;
141 i++;
142 }
143
144 } else {
145
146 Iterator it = aItemCollection.getAllItems().entrySet()
147 .iterator();
148 int max = aItemCollection.getAllItems().entrySet().size();
149 items = new XMLItem[max];
150
151
152 while (it.hasNext()) {
153 Map.Entry entry = (Map.Entry) it.next();
154 sName = (String) entry.getKey();
155
156
157 Vector vOrg = (Vector) entry.getValue();
158
159 if (!isBasicType(vOrg,sName))
160 continue;
161 XMLItem item = new XMLItem();
162 item.setName(sName);
163 item.setValue(vOrg.toArray());
164
165 items[i] = item;
166 i++;
167 }
168 }
169
170 entity.setItem(items);
171 }
172
173 } catch (Exception e) {
174 System.out
175 .println("[XMLItemCollectionAdapter] Error putItemCollection ("
176 + sName + ")");
177 throw e;
178 }
179
180 return entity;
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194 public static XMLItemCollection putItemCollection(
195 ItemCollection aItemCollection) throws Exception {
196 return putItemCollection(aItemCollection,null);
197 }
198
199
200
201
202
203
204
205
206
207 public static EntityCollection putCollection(Collection<ItemCollection> col)
208 throws Exception {
209
210 return putCollection(col, null);
211 }
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 public static EntityCollection putCollection(
229 Collection<ItemCollection> col, List<String> itemNames)
230 throws Exception {
231 EntityCollection entiCol = new EntityCollection();
232 Iterator<ItemCollection> it = col.iterator();
233 int max = col.size();
234 int i = 0;
235 XMLItemCollection[] entities = new XMLItemCollection[max];
236 while (it.hasNext()) {
237 ItemCollection icw = (ItemCollection) it.next();
238 XMLItemCollection entity = putItemCollection(icw,itemNames);
239 entities[i] = entity;
240 i++;
241 }
242 if (max > 0)
243 entiCol.setEntity(entities);
244 return entiCol;
245 }
246
247
248
249
250
251
252
253
254 private static boolean isBasicType(Vector v, String fieldname) {
255 for (Object o : v) {
256
257 Class c = o.getClass();
258
259 String name = c.getName();
260 if (!name.startsWith("java.lang.")
261 && !name.startsWith("java.math.")
262 && !"java.util.Date".equals(name)) {
263 System.out
264 .println("WARNING : XMLItemCollectionAdapter - unsported java type for property '" + fieldname + "' = "
265 + name);
266 return false;
267 }
268 }
269 return true;
270 }
271
272 }