View Javadoc
1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package tools.gsf.facade.assetapi;
18  
19  import com.fatwire.assetapi.data.AssetData;
20  import com.fatwire.assetapi.data.AssetId;
21  import com.fatwire.assetapi.data.AttributeData;
22  import com.fatwire.assetapi.data.BlobObject;
23  import com.fatwire.assetapi.def.AttributeDef;
24  import com.fatwire.assetapi.def.AttributeDefProperties;
25  import com.fatwire.mda.Dimension;
26  
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.Date;
32  import java.util.LinkedList;
33  import java.util.List;
34  
35  /**
36   * Utility class for processing attribute data. Some of the data conversion
37   * methods have not been thoroughly tested and may therefore require some bug
38   * fixes.
39   *
40   * @author Tony Field
41   * @author Dolf Dijkstra
42   * @since Nov 17, 2009
43   */
44  public final class AttributeDataUtils {
45      private AttributeDataUtils() {
46      }
47  
48      /**
49       * Get the specified attribute field from the AssetData object. If it is not
50       * found, get the next field listed. Continue until the end of the list.
51       * Safe to use with a single value. If no data is found for any attribute,
52       * an exception is thrown.
53       *
54       * @param assetData             populated AssetData object
55       * @param orderedAttributeNames vararg array of attribute names that are
56       *                              expected to be found and populated in the assetData object
57       * @return the value, never null
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       * Get the specified attribute data, converting each of the values into a
72       * string and separating them with a comma (no space).
73       *
74       * @param attributeData multi-valued attribute data
75       * @return attribute data converted to a string, comma-separated.
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       * Get an attribute that is a comma-separated string and split it into a
90       * collection. If the attribute has no values, an empty list is returned.
91       * <p>
92       * All attribute values or substring values of zero length are dropped.
93       *
94       * @param attributeData attribute data object
95       * @param delimRegex    regex for splitting
96       * @return Collection{@literal <String>} of attribute data, never null (though an empty
97       * list may be returned
98       */
99      public static Collection<String> getAndSplitString(AttributeData attributeData, String delimRegex) {
100         if (attributeData == null) {
101             return Collections.emptyList();
102         }
103         Object o = attributeData.getData();
104         if (o instanceof String) { // test for null and String
105             String[] s = ((String) o).split(delimRegex);
106             if (s.length == 0) {
107                 return Collections.emptyList();
108             }
109             Collection<String> c = new ArrayList<String>();
110             for (String s1 : s)
111                 if (s1 != null && s1.length() > 0) {
112                     c.add(s1);
113                 }
114             return c;
115         } else {
116             return Collections.emptyList();
117         }
118     }
119 
120     /**
121      * @param attr the attribute data
122      * @return true if valueCount == ValueCount.SINGLE
123      */
124     static public boolean isSingleValued(AttributeData attr) {
125         AttributeDef ad = attr.getAttributeDef();
126         return isSingleValued(ad);
127 
128     }
129 
130     /**
131      * Returns true if the attribute definition is defined as single valued.
132      *
133      * @param ad attribute definition
134      * @return true if valueCount == ValueCount.SINGLE
135      */
136     static public boolean isSingleValued(AttributeDef ad) {
137         return ad.getProperties() == null ? true : ad.getProperties().getValueCount()
138                 .equals(AttributeDefProperties.ValueCount.SINGLE);
139 
140     }
141 
142     /**
143      * @param attr the attribute data
144      * @return the attribute value as a List. If attribute isSingleValued than
145      * return getData() and cast to List.
146      */
147     static public List<?> asList(AttributeData attr) {
148 
149         Object o = attr == null ? null : attr.getData();
150         if (o == null) {
151             return null;
152         }
153 
154         if (isSingleValued(attr)) {
155             return (List<?>) attr.getData();
156         }
157         return attr.getDataAsList();
158     }
159 
160     /**
161      * @param attr the attribute data
162      * @return attribute value as a Integer if the attribute type is a INT.
163      */
164 
165     static public Integer asInt(AttributeData attr) {
166 
167         Object o = attr == null ? null : attr.getData();
168         if (o == null) {
169             return null;
170         }
171         switch (attr.getType()) {
172             case INT:
173                 return (Integer) o;
174             case DATE:
175             case BLOB:
176             case FLOAT:
177             case LONG:
178             case ASSET:
179             case MONEY:
180             case STRING:
181             case LARGE_TEXT:
182             case ASSETREFERENCE:
183             case URL:
184             case ARRAY:
185             case STRUCT:
186             case LIST:
187             case ONEOF:
188                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Integer");
189             default:
190                 throw new IllegalArgumentException("Don't know about " + attr.getType());
191         }
192 
193     }
194 
195     /**
196      * @param attr the attribute data
197      * @return attribute value as a Date if the attribute type is a DATE
198      */
199     static public Date asDate(AttributeData attr) {
200 
201         Object o = attr == null ? null : attr.getData();
202         if (o == null) {
203             return null;
204         }
205         switch (attr.getType()) {
206             case DATE:
207                 return (Date) o;
208             case BLOB:
209             case FLOAT:
210             case INT:
211             case LONG:
212             case ASSET:
213             case MONEY:
214             case STRING:
215             case LARGE_TEXT:
216             case ASSETREFERENCE:
217             case URL:
218             case ARRAY:
219             case STRUCT:
220             case LIST:
221             case ONEOF:
222                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Date");
223             default:
224                 throw new IllegalArgumentException("Don't know about " + attr.getType());
225         }
226 
227     }
228 
229     /**
230      * @param attr the attribute data
231      * @return attribute value as a BlobObject if the attribute type is a URL or
232      * BLOB
233      */
234     static public BlobObject asBlob(AttributeData attr) {
235 
236         Object o = attr == null ? null : attr.getData();
237         if (o == null) {
238             return null;
239         }
240         switch (attr.getType()) {
241             case URL:
242             case BLOB:
243                 return (BlobObject) o;
244             case FLOAT:
245             case INT:
246             case LONG:
247             case ASSET:
248             case MONEY:
249             case DATE:
250             case STRING:
251             case LARGE_TEXT:
252             case ASSETREFERENCE:
253             case ARRAY:
254             case STRUCT:
255             case LIST:
256             case ONEOF:
257                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a BlobObject");
258             default:
259                 throw new IllegalArgumentException("Don't know about " + attr.getType());
260         }
261 
262     }
263 
264     /**
265      * @param attr the attribute data.
266      * @return attribute value as a Float if the attribute type is a FLOAT or
267      * INT.
268      */
269 
270     static public Float asFloat(AttributeData attr) {
271 
272         Object o = attr == null ? null : attr.getData();
273         if (o == null) {
274             return null;
275         }
276         switch (attr.getType()) {
277             case FLOAT:
278             case INT:
279                 return (Float) o;
280             case LONG:
281             case ASSET:
282             case MONEY:
283             case DATE:
284             case STRING:
285             case LARGE_TEXT:
286             case ASSETREFERENCE:
287             case BLOB:
288             case URL:
289             case ARRAY:
290             case STRUCT:
291             case LIST:
292             case ONEOF:
293                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Float");
294             default:
295                 throw new IllegalArgumentException("Don't know about " + attr.getType());
296         }
297 
298     }
299 
300     /**
301      * @param attr the attribute data
302      * @return attribute value as a Double if the attribute type is a
303      * FLOAT,INT,LONG or MONEY.
304      */
305 
306     static public Double asDouble(AttributeData attr) {
307 
308         Object o = attr == null ? null : attr.getData();
309         if (o == null) {
310             return null;
311         }
312         switch (attr.getType()) {
313             case FLOAT:
314             case INT:
315             case LONG:
316             case MONEY:
317                 return (Double) o;
318             case ASSET:
319             case DATE:
320             case STRING:
321             case LARGE_TEXT:
322             case ASSETREFERENCE:
323             case BLOB:
324             case URL:
325             case ARRAY:
326             case STRUCT:
327             case LIST:
328             case ONEOF:
329                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Double");
330             default:
331                 throw new IllegalArgumentException("Don't know about " + attr.getType());
332         }
333 
334     }
335 
336     /**
337      * @param attr the attribute data
338      * @return attribute value as a Long if the attribute type is a INT or LONG.
339      */
340 
341     static public Long asLong(AttributeData attr) {
342 
343         Object o = attr == null ? null : attr.getData();
344         if (o == null) {
345             return null;
346         }
347         switch (attr.getType()) {
348             case INT:
349             case LONG:
350                 return (Long) o;
351             case ASSET:
352             case MONEY:
353             case FLOAT:
354             case DATE:
355             case STRING:
356             case LARGE_TEXT:
357             case ASSETREFERENCE:
358             case BLOB:
359             case URL:
360             case ARRAY:
361             case STRUCT:
362             case LIST:
363             case ONEOF:
364                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Long");
365             default:
366                 throw new IllegalArgumentException("Don't know about " + attr.getType());
367         }
368 
369     }
370 
371     /**
372      * @param attr the attribute data
373      * @return attribute value as a AssetId if the attribute type is a ASSET.
374      */
375 
376     static public AssetId asAssetId(AttributeData attr) {
377 
378         Object o = attr == null ? null : attr.getData();
379         if (o == null) {
380             return null;
381         }
382         switch (attr.getType()) {
383             case ASSET:
384                 return (AssetId) o;
385 
386             case INT:
387             case FLOAT:
388             case LONG:
389             case MONEY:
390             case DATE:
391             case STRING:
392             case LARGE_TEXT:
393             case ASSETREFERENCE:
394             case BLOB:
395             case URL:
396             case ARRAY:
397             case STRUCT:
398             case LIST:
399             case ONEOF:
400                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a AssetId ");
401             default:
402                 throw new IllegalArgumentException("Don't know about " + attr.getType());
403         }
404     }
405 
406     /**
407      * @param attr the attribute data
408      * @return attribute value as a String if the attribute type is a
409      * INT,FLOAT,LONG,MONEY,DATE,STRING or LARGE_TEXT.
410      */
411 
412     static public String asString(AttributeData attr) {
413 
414         Object o = attr == null ? null : attr.getData();
415         if (o == null) {
416             return null;
417         }
418         switch (attr.getType()) {
419 
420             case INT:
421             case FLOAT:
422             case LONG:
423             case MONEY:
424                 return String.valueOf(o);
425             case DATE:
426                 return com.fatwire.cs.core.db.Util.formatJdbcDate((Date) o);
427             case STRING:
428             case LARGE_TEXT:
429                 return (String) o;
430 
431             case ASSET:
432             case ASSETREFERENCE:
433 
434             case BLOB:
435             case URL:
436 
437             case ARRAY:
438             case STRUCT:
439             case LIST:
440             case ONEOF:
441                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a String");
442             default:
443                 throw new IllegalArgumentException("Don't know about " + attr.getType());
444 
445         }
446 
447     }
448 
449     /**
450      * @param attr the attribute data
451      * @return attribute value as a Integer if the attribute type is a INT.
452      */
453     @SuppressWarnings("unchecked")
454     static public List<Integer> asIntList(AttributeData attr) {
455 
456         Object o = attr == null ? null : attr.getDataAsList();
457         if (o == null) {
458             return null;
459         }
460         switch (attr.getType()) {
461             case INT:
462                 return (List<Integer>) o;
463             case DATE:
464             case BLOB:
465             case FLOAT:
466             case LONG:
467             case ASSET:
468             case MONEY:
469             case STRING:
470             case LARGE_TEXT:
471             case ASSETREFERENCE:
472             case URL:
473             case ARRAY:
474             case STRUCT:
475             case LIST:
476             case ONEOF:
477                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Integer");
478             default:
479                 throw new IllegalArgumentException("Don't know about " + attr.getType());
480         }
481 
482     }
483 
484     /**
485      * @param attr the attribute data
486      * @return attribute value as a Date if the attribute type is a DATE
487      */
488     @SuppressWarnings("unchecked")
489     static public List<Date> asDateList(AttributeData attr) {
490 
491         Object o = attr == null ? null : attr.getData();
492         if (o == null) {
493             return null;
494         }
495         switch (attr.getType()) {
496             case DATE:
497                 return (List<Date>) o;
498             case BLOB:
499             case FLOAT:
500             case INT:
501             case LONG:
502             case ASSET:
503             case MONEY:
504             case STRING:
505             case LARGE_TEXT:
506             case ASSETREFERENCE:
507             case URL:
508             case ARRAY:
509             case STRUCT:
510             case LIST:
511             case ONEOF:
512                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Date");
513             default:
514                 throw new IllegalArgumentException("Don't know about " + attr.getType());
515         }
516 
517     }
518 
519     /**
520      * @param attr the attribute data
521      * @return attribute value as a BlobObject if the attribute type is a URL or
522      * BLOB
523      */
524     @SuppressWarnings("unchecked")
525     static public List<BlobObject> asBlobList(AttributeData attr) {
526 
527         Object o = attr == null ? null : attr.getData();
528         if (o == null) {
529             return null;
530         }
531         switch (attr.getType()) {
532             case URL:
533             case BLOB:
534                 return (List<BlobObject>) o;
535             case FLOAT:
536             case INT:
537             case LONG:
538             case ASSET:
539             case MONEY:
540             case DATE:
541             case STRING:
542             case LARGE_TEXT:
543             case ASSETREFERENCE:
544             case ARRAY:
545             case STRUCT:
546             case LIST:
547             case ONEOF:
548                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a BlobObject");
549             default:
550                 throw new IllegalArgumentException("Don't know about " + attr.getType());
551         }
552 
553     }
554 
555     /**
556      * @param attr the attribute data.
557      * @return attribute value as a Float if the attribute type is a FLOAT or
558      * INT.
559      */
560     @SuppressWarnings("unchecked")
561     static public List<Float> asFloatList(AttributeData attr) {
562 
563         Object o = attr == null ? null : attr.getData();
564         if (o == null) {
565             return null;
566         }
567         switch (attr.getType()) {
568             case FLOAT:
569             case INT:
570                 return (List<Float>) o;
571             case LONG:
572             case ASSET:
573             case MONEY:
574             case DATE:
575             case STRING:
576             case LARGE_TEXT:
577             case ASSETREFERENCE:
578             case BLOB:
579             case URL:
580             case ARRAY:
581             case STRUCT:
582             case LIST:
583             case ONEOF:
584                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Float");
585             default:
586                 throw new IllegalArgumentException("Don't know about " + attr.getType());
587         }
588 
589     }
590 
591     /**
592      * @param attr the attribute data
593      * @return attribute value as a Double if the attribute type is a
594      * FLOAT,INT,LONG or MONEY.
595      */
596     @SuppressWarnings("unchecked")
597     static public List<Double> asDoubleList(AttributeData attr) {
598 
599         Object o = attr == null ? null : attr.getData();
600         if (o == null) {
601             return null;
602         }
603         switch (attr.getType()) {
604             case FLOAT:
605             case INT:
606             case LONG:
607             case MONEY:
608                 return (List<Double>) o;
609             case ASSET:
610             case DATE:
611             case STRING:
612             case LARGE_TEXT:
613             case ASSETREFERENCE:
614             case BLOB:
615             case URL:
616             case ARRAY:
617             case STRUCT:
618             case LIST:
619             case ONEOF:
620                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Double");
621             default:
622                 throw new IllegalArgumentException("Don't know about " + attr.getType());
623         }
624 
625     }
626 
627     /**
628      * @param attr the attribute data
629      * @return attribute value as a Long if the attribute type is a INT or LONG.
630      */
631     @SuppressWarnings("unchecked")
632     static public List<Long> asLongList(AttributeData attr) {
633 
634         Object o = attr == null ? null : attr.getData();
635         if (o == null) {
636             return null;
637         }
638         switch (attr.getType()) {
639             case INT:
640             case LONG:
641                 return (List<Long>) o;
642             case ASSET:
643             case MONEY:
644             case FLOAT:
645             case DATE:
646             case STRING:
647             case LARGE_TEXT:
648             case ASSETREFERENCE:
649             case BLOB:
650             case URL:
651             case ARRAY:
652             case STRUCT:
653             case LIST:
654             case ONEOF:
655                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a Long");
656             default:
657                 throw new IllegalArgumentException("Don't know about " + attr.getType());
658         }
659 
660     }
661 
662     /**
663      * @param attr the attribute data
664      * @return attribute value as a AssetId if the attribute type is a ASSET.
665      */
666     @SuppressWarnings("unchecked")
667     static public List<AssetId> asAssetIdList(AttributeData attr) {
668 
669         Object o = attr == null ? null : attr.getData();
670         if (o == null) {
671             return null;
672         }
673         switch (attr.getType()) {
674             case ASSET:
675                 return (List<AssetId>) o;
676 
677             case INT:
678             case FLOAT:
679             case LONG:
680             case MONEY:
681             case DATE:
682             case STRING:
683             case LARGE_TEXT:
684             case ASSETREFERENCE:
685             case BLOB:
686             case URL:
687             case ARRAY:
688             case STRUCT:
689             case LIST:
690             case ONEOF:
691                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a AssetId ");
692             default:
693                 throw new IllegalArgumentException("Don't know about " + attr.getType());
694         }
695     }
696 
697     /**
698      * @param attr the attribute data
699      * @return attribute value as a String if the attribute type is a
700      * INT,FLOAT,LONG,MONEY,DATE,STRING or LARGE_TEXT.
701      */
702 
703     @SuppressWarnings("unchecked")
704     static public List<String> asStringList(AttributeData attr) {
705 
706         Object o = attr == null ? null : attr.getData();
707         if (o == null) {
708             return null;
709         }
710         @SuppressWarnings("rawtypes")
711         List l = (List) o;
712         List<String> out = new LinkedList<String>();
713 
714         switch (attr.getType()) {
715 
716             case INT:
717             case FLOAT:
718             case LONG:
719             case MONEY:
720                 for (Object u : l) {
721                     out.add(String.valueOf(u));
722                 }
723                 return out;
724             case DATE:
725                 for (Object u : l) {
726                     out.add(com.fatwire.cs.core.db.Util.formatJdbcDate((Date) u));
727                 }
728                 return out;
729             case STRING:
730             case LARGE_TEXT:
731                 return (List<String>) o;
732 
733             case ASSET:
734             case ASSETREFERENCE:
735 
736             case BLOB:
737             case URL:
738 
739             case ARRAY:
740             case STRUCT:
741             case LIST:
742             case ONEOF:
743                 throw new IllegalArgumentException("Can't cast " + attr.getType() + " into a String");
744             default:
745                 throw new IllegalArgumentException("Don't know about " + attr.getType());
746 
747         }
748 
749     }
750 
751     /**
752      * Returns the Dimension for the attribute.
753      *
754      * @param attr the attribute data for the 'Dimension' attribute.
755      * @return the Dimension if found.
756      */
757     public static Dimension asDimension(AttributeData attr) {
758         List<?> o = attr == null ? null : attr.getDataAsList();
759         if (o == null) {
760             return null;
761         }
762 
763         for (final Object o1 : o) {
764             if (o1 instanceof Collection) {
765                 for (Object o2 : (Collection<?>) o1) {
766                     if (o2 instanceof Dimension) {
767                         return (Dimension) o2;
768                     }
769                 }
770             }
771         }
772         return null;
773 
774     }
775 }