1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation.facade.assetapi;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Date;
24 import java.util.LinkedList;
25 import java.util.List;
26
27 import com.fatwire.assetapi.data.AssetData;
28 import com.fatwire.assetapi.data.AssetId;
29 import com.fatwire.assetapi.data.AttributeData;
30 import com.fatwire.assetapi.data.BlobObject;
31 import com.fatwire.assetapi.def.AttributeDef;
32 import com.fatwire.assetapi.def.AttributeDefProperties;
33 import com.fatwire.mda.Dimension;
34
35
36
37
38
39
40
41
42
43
44 public final class AttributeDataUtils {
45 private AttributeDataUtils() {
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59 public static String getWithFallback(AssetData assetData, String... orderedAttributeNames) {
60 for (String attr : orderedAttributeNames) {
61 AttributeData attrData = assetData.getAttributeData(attr);
62 if (attrData != null && attrData.getData() != null) {
63 return attrData.getData().toString();
64 }
65 }
66 throw new IllegalArgumentException("Asset data [" + assetData
67 + "] does not contain any of the specified attributes: " + Arrays.asList(orderedAttributeNames));
68 }
69
70
71
72
73
74
75
76
77 public static String getMultivaluedAsCommaSepString(AttributeData attributeData) {
78 StringBuilder sb = new StringBuilder();
79 for (Object vals : attributeData.getDataAsList()) {
80 if (sb.length() > 0) {
81 sb.append(",");
82 }
83 sb.append(vals.toString());
84 }
85 return sb.toString();
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99 public static Collection<String> getAndSplitString(AttributeData attributeData, String delimRegex) {
100 if (attributeData == null)
101 return Collections.emptyList();
102 Object o = attributeData.getData();
103 if (o instanceof String) {
104 String[] s = ((String) o).split(delimRegex);
105 if (s.length == 0)
106 return Collections.emptyList();
107 Collection<String> c = new ArrayList<String>();
108 for (String s1 : s)
109 if (s1 != null && s1.length() > 0)
110 c.add(s1);
111 return c;
112 } else
113 return Collections.emptyList();
114 }
115
116
117
118
119
120 static public boolean isSingleValued(AttributeData attr) {
121 AttributeDef ad = attr.getAttributeDef();
122 return isSingleValued(ad);
123
124 }
125
126
127
128
129
130
131
132 static public boolean isSingleValued(AttributeDef ad) {
133 return ad.getProperties() == null ? true : ad.getProperties().getValueCount()
134 .equals(AttributeDefProperties.ValueCount.SINGLE);
135
136 }
137
138
139
140
141
142
143 static public List<?> asList(AttributeData attr) {
144
145 Object o = attr == null ? null : attr.getData();
146 if (o == null)
147 return null;
148
149 if (isSingleValued(attr)) {
150 return (List<?>) attr.getData();
151 }
152 return attr.getDataAsList();
153 }
154
155
156
157
158
159
160 static public Integer asInt(AttributeData attr) {
161
162 Object o = attr == null ? null : attr.getData();
163 if (o == null)
164 return null;
165 switch (attr.getType()) {
166 case INT:
167 return (Integer) o;
168 case DATE:
169 case BLOB:
170 case FLOAT:
171 case LONG:
172 case ASSET:
173 case MONEY:
174 case STRING:
175 case LARGE_TEXT:
176 case ASSETREFERENCE:
177 case URL:
178 case ARRAY:
179 case STRUCT:
180 case LIST:
181 case ONEOF:
182 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Integer");
183 default:
184 throw new IllegalArgumentException("Don't know about " + attr.getType());
185 }
186
187 }
188
189
190
191
192
193 static public Date asDate(AttributeData attr) {
194
195 Object o = attr == null ? null : attr.getData();
196 if (o == null)
197 return null;
198 switch (attr.getType()) {
199 case DATE:
200 return (Date) o;
201 case BLOB:
202 case FLOAT:
203 case INT:
204 case LONG:
205 case ASSET:
206 case MONEY:
207 case STRING:
208 case LARGE_TEXT:
209 case ASSETREFERENCE:
210 case URL:
211 case ARRAY:
212 case STRUCT:
213 case LIST:
214 case ONEOF:
215 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Date");
216 default:
217 throw new IllegalArgumentException("Don't know about " + attr.getType());
218 }
219
220 }
221
222
223
224
225
226
227 static public BlobObject asBlob(AttributeData attr) {
228
229 Object o = attr == null ? null : attr.getData();
230 if (o == null)
231 return null;
232 switch (attr.getType()) {
233 case URL:
234 case BLOB:
235 return (BlobObject) o;
236 case FLOAT:
237 case INT:
238 case LONG:
239 case ASSET:
240 case MONEY:
241 case DATE:
242 case STRING:
243 case LARGE_TEXT:
244 case ASSETREFERENCE:
245 case ARRAY:
246 case STRUCT:
247 case LIST:
248 case ONEOF:
249 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a BlobObject");
250 default:
251 throw new IllegalArgumentException("Don't know about " + attr.getType());
252 }
253
254 }
255
256
257
258
259
260
261
262 static public Float asFloat(AttributeData attr) {
263
264 Object o = attr == null ? null : attr.getData();
265 if (o == null)
266 return null;
267 switch (attr.getType()) {
268 case FLOAT:
269 case INT:
270 return (Float) o;
271 case LONG:
272 case ASSET:
273 case MONEY:
274 case DATE:
275 case STRING:
276 case LARGE_TEXT:
277 case ASSETREFERENCE:
278 case BLOB:
279 case URL:
280 case ARRAY:
281 case STRUCT:
282 case LIST:
283 case ONEOF:
284 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Float");
285 default:
286 throw new IllegalArgumentException("Don't know about " + attr.getType());
287 }
288
289 }
290
291
292
293
294
295
296
297 static public Double asDouble(AttributeData attr) {
298
299 Object o = attr == null ? null : attr.getData();
300 if (o == null)
301 return null;
302 switch (attr.getType()) {
303 case FLOAT:
304 case INT:
305 case LONG:
306 case MONEY:
307 return (Double) o;
308 case ASSET:
309 case DATE:
310 case STRING:
311 case LARGE_TEXT:
312 case ASSETREFERENCE:
313 case BLOB:
314 case URL:
315 case ARRAY:
316 case STRUCT:
317 case LIST:
318 case ONEOF:
319 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Double");
320 default:
321 throw new IllegalArgumentException("Don't know about " + attr.getType());
322 }
323
324 }
325
326
327
328
329
330
331 static public Long asLong(AttributeData attr) {
332
333 Object o = attr == null ? null : attr.getData();
334 if (o == null)
335 return null;
336 switch (attr.getType()) {
337 case INT:
338 case LONG:
339 return (Long) o;
340 case ASSET:
341 case MONEY:
342 case FLOAT:
343 case DATE:
344 case STRING:
345 case LARGE_TEXT:
346 case ASSETREFERENCE:
347 case BLOB:
348 case URL:
349 case ARRAY:
350 case STRUCT:
351 case LIST:
352 case ONEOF:
353 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Long");
354 default:
355 throw new IllegalArgumentException("Don't know about " + attr.getType());
356 }
357
358 }
359
360
361
362
363
364
365 static public AssetId asAssetId(AttributeData attr) {
366
367 Object o = attr == null ? null : attr.getData();
368 if (o == null)
369 return null;
370 switch (attr.getType()) {
371 case ASSET:
372 return (AssetId) o;
373
374 case INT:
375 case FLOAT:
376 case LONG:
377 case MONEY:
378 case DATE:
379 case STRING:
380 case LARGE_TEXT:
381 case ASSETREFERENCE:
382 case BLOB:
383 case URL:
384 case ARRAY:
385 case STRUCT:
386 case LIST:
387 case ONEOF:
388 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a AssetId ");
389 default:
390 throw new IllegalArgumentException("Don't know about " + attr.getType());
391 }
392 }
393
394
395
396
397
398
399
400 static public String asString(AttributeData attr) {
401
402 Object o = attr == null ? null : attr.getData();
403 if (o == null)
404 return null;
405 switch (attr.getType()) {
406
407 case INT:
408 case FLOAT:
409 case LONG:
410 case MONEY:
411 return String.valueOf(o);
412 case DATE:
413 return com.fatwire.cs.core.db.Util.formatJdbcDate((Date) o);
414 case STRING:
415 case LARGE_TEXT:
416 return (String) o;
417
418 case ASSET:
419 case ASSETREFERENCE:
420
421 case BLOB:
422 case URL:
423
424 case ARRAY:
425 case STRUCT:
426 case LIST:
427 case ONEOF:
428 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a String");
429 default:
430 throw new IllegalArgumentException("Don't know about " + attr.getType());
431
432 }
433
434 }
435
436
437
438
439
440 @SuppressWarnings("unchecked")
441 static public List<Integer> asIntList(AttributeData attr) {
442
443 Object o = attr == null ? null : attr.getDataAsList();
444 if (o == null)
445 return null;
446 switch (attr.getType()) {
447 case INT:
448 return (List<Integer>) o;
449 case DATE:
450 case BLOB:
451 case FLOAT:
452 case LONG:
453 case ASSET:
454 case MONEY:
455 case STRING:
456 case LARGE_TEXT:
457 case ASSETREFERENCE:
458 case URL:
459 case ARRAY:
460 case STRUCT:
461 case LIST:
462 case ONEOF:
463 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Integer");
464 default:
465 throw new IllegalArgumentException("Don't know about " + attr.getType());
466 }
467
468 }
469
470
471
472
473
474 @SuppressWarnings("unchecked")
475 static public List<Date> asDateList(AttributeData attr) {
476
477 Object o = attr == null ? null : attr.getData();
478 if (o == null)
479 return null;
480 switch (attr.getType()) {
481 case DATE:
482 return (List<Date>) o;
483 case BLOB:
484 case FLOAT:
485 case INT:
486 case LONG:
487 case ASSET:
488 case MONEY:
489 case STRING:
490 case LARGE_TEXT:
491 case ASSETREFERENCE:
492 case URL:
493 case ARRAY:
494 case STRUCT:
495 case LIST:
496 case ONEOF:
497 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Date");
498 default:
499 throw new IllegalArgumentException("Don't know about " + attr.getType());
500 }
501
502 }
503
504
505
506
507
508
509 @SuppressWarnings("unchecked")
510 static public List<BlobObject> asBlobList(AttributeData attr) {
511
512 Object o = attr == null ? null : attr.getData();
513 if (o == null)
514 return null;
515 switch (attr.getType()) {
516 case URL:
517 case BLOB:
518 return (List<BlobObject>) o;
519 case FLOAT:
520 case INT:
521 case LONG:
522 case ASSET:
523 case MONEY:
524 case DATE:
525 case STRING:
526 case LARGE_TEXT:
527 case ASSETREFERENCE:
528 case ARRAY:
529 case STRUCT:
530 case LIST:
531 case ONEOF:
532 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a BlobObject");
533 default:
534 throw new IllegalArgumentException("Don't know about " + attr.getType());
535 }
536
537 }
538
539
540
541
542
543
544 @SuppressWarnings("unchecked")
545 static public List<Float> asFloatList(AttributeData attr) {
546
547 Object o = attr == null ? null : attr.getData();
548 if (o == null)
549 return null;
550 switch (attr.getType()) {
551 case FLOAT:
552 case INT:
553 return (List<Float>) o;
554 case LONG:
555 case ASSET:
556 case MONEY:
557 case DATE:
558 case STRING:
559 case LARGE_TEXT:
560 case ASSETREFERENCE:
561 case BLOB:
562 case URL:
563 case ARRAY:
564 case STRUCT:
565 case LIST:
566 case ONEOF:
567 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Float");
568 default:
569 throw new IllegalArgumentException("Don't know about " + attr.getType());
570 }
571
572 }
573
574
575
576
577
578
579 @SuppressWarnings("unchecked")
580 static public List<Double> asDoubleList(AttributeData attr) {
581
582 Object o = attr == null ? null : attr.getData();
583 if (o == null)
584 return null;
585 switch (attr.getType()) {
586 case FLOAT:
587 case INT:
588 case LONG:
589 case MONEY:
590 return (List<Double>) o;
591 case ASSET:
592 case DATE:
593 case STRING:
594 case LARGE_TEXT:
595 case ASSETREFERENCE:
596 case BLOB:
597 case URL:
598 case ARRAY:
599 case STRUCT:
600 case LIST:
601 case ONEOF:
602 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Double");
603 default:
604 throw new IllegalArgumentException("Don't know about " + attr.getType());
605 }
606
607 }
608
609
610
611
612
613 @SuppressWarnings("unchecked")
614 static public List<Long> asLongList(AttributeData attr) {
615
616 Object o = attr == null ? null : attr.getData();
617 if (o == null)
618 return null;
619 switch (attr.getType()) {
620 case INT:
621 case LONG:
622 return (List<Long>) o;
623 case ASSET:
624 case MONEY:
625 case FLOAT:
626 case DATE:
627 case STRING:
628 case LARGE_TEXT:
629 case ASSETREFERENCE:
630 case BLOB:
631 case URL:
632 case ARRAY:
633 case STRUCT:
634 case LIST:
635 case ONEOF:
636 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Long");
637 default:
638 throw new IllegalArgumentException("Don't know about " + attr.getType());
639 }
640
641 }
642
643
644
645
646
647 @SuppressWarnings("unchecked")
648 static public List<AssetId> asAssetIdList(AttributeData attr) {
649
650 Object o = attr == null ? null : attr.getData();
651 if (o == null)
652 return null;
653 switch (attr.getType()) {
654 case ASSET:
655 return (List<AssetId>) o;
656
657 case INT:
658 case FLOAT:
659 case LONG:
660 case MONEY:
661 case DATE:
662 case STRING:
663 case LARGE_TEXT:
664 case ASSETREFERENCE:
665 case BLOB:
666 case URL:
667 case ARRAY:
668 case STRUCT:
669 case LIST:
670 case ONEOF:
671 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a AssetId ");
672 default:
673 throw new IllegalArgumentException("Don't know about " + attr.getType());
674 }
675 }
676
677
678
679
680
681
682
683 @SuppressWarnings("unchecked")
684 static public List<String> asStringList(AttributeData attr) {
685
686 Object o = attr == null ? null : attr.getData();
687 if (o == null)
688 return null;
689 @SuppressWarnings("rawtypes")
690 List l = (List) o;
691 List<String> out = new LinkedList<String>();
692
693 switch (attr.getType()) {
694
695 case INT:
696 case FLOAT:
697 case LONG:
698 case MONEY:
699 for (Object u : l) {
700 out.add(String.valueOf(u));
701 }
702 return out;
703 case DATE:
704 for (Object u : l) {
705 out.add(com.fatwire.cs.core.db.Util.formatJdbcDate((Date) u));
706 }
707 return out;
708 case STRING:
709 case LARGE_TEXT:
710 return (List<String>) o;
711
712 case ASSET:
713 case ASSETREFERENCE:
714
715 case BLOB:
716 case URL:
717
718 case ARRAY:
719 case STRUCT:
720 case LIST:
721 case ONEOF:
722 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a String");
723 default:
724 throw new IllegalArgumentException("Don't know about " + attr.getType());
725
726 }
727
728 }
729
730
731
732
733
734
735
736 public static Dimension asDimension(AttributeData attr) {
737 List<?> o = attr == null ? null : attr.getDataAsList();
738 if (o == null)
739 return null;
740
741 for (final Object o1 : o) {
742 if (o1 instanceof Collection) {
743 for (Object o2 : (Collection<?>) o1) {
744 if (o2 instanceof Dimension) {
745 return (Dimension) o2;
746 }
747 }
748 }
749 }
750 return null;
751
752 }
753 }