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 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   * 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       * 
92       * All attribute values or substring values of zero length are dropped.
93       * 
94       * @param attributeData
95       * @param delimRegex regex for splitting
96       * @return Collection<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         Object o = attributeData.getData();
103         if (o instanceof String) { // test for null and 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      * @param attr the attribute data
118      * @return true if valueCount == ValueCount.SINGLE
119      */
120     static public boolean isSingleValued(AttributeData attr) {
121         AttributeDef ad = attr.getAttributeDef();
122         return isSingleValued(ad);
123 
124     }
125 
126     /**
127      * Returns true if the attribute definition is defined as single valued.
128      * 
129      * @param ad attribute definition
130      * @return true if valueCount == ValueCount.SINGLE
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      * @param attr the attribute data
140      * @return the attribute value as a List. If attribute isSingleValued than
141      *         return getData() and cast to List.
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      * @param attr the attribute data
157      * @return attribute value as a Integer if the attribute type is a INT.
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      * @param attr the attribute data
191      * @return attribute value as a Date if the attribute type is a DATE
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      * @param attr the attribute data
224      * @return attribute value as a BlobObject if the attribute type is a URL or
225      *         BLOB
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      * @param attr the attribute data.
258      * @return attribute value as a Float if the attribute type is a FLOAT or
259      *         INT.
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      * @param attr the attribute data
293      * @return attribute value as a Double if the attribute type is a
294      *         FLOAT,INT,LONG or MONEY.
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      * @param attr the attribute data
328      * @return attribute value as a Long if the attribute type is a INT or LONG.
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      * @param attr the attribute data
362      * @return attribute value as a AssetId if the attribute type is a ASSET.
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      * @param attr the attribute data
396      * @return attribute value as a String if the attribute type is a
397      *         INT,FLOAT,LONG,MONEY,DATE,STRING or LARGE_TEXT.
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      * @param attr the attribute data
438      * @return attribute value as a Integer if the attribute type is a INT.
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      * @param attr the attribute data
472      * @return attribute value as a Date if the attribute type is a DATE
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      * @param attr the attribute data
506      * @return attribute value as a BlobObject if the attribute type is a URL or
507      *         BLOB
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      * @param attr the attribute data.
541      * @return attribute value as a Float if the attribute type is a FLOAT or
542      *         INT.
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      * @param attr the attribute data
576      * @return attribute value as a Double if the attribute type is a
577      *         FLOAT,INT,LONG or MONEY.
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      * @param attr the attribute data
611      * @return attribute value as a Long if the attribute type is a INT or LONG.
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      * @param attr the attribute data
645      * @return attribute value as a AssetId if the attribute type is a ASSET.
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      * @param attr the attribute data
679      * @return attribute value as a String if the attribute type is a
680      *         INT,FLOAT,LONG,MONEY,DATE,STRING or LARGE_TEXT.
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      * Returns the Dimension for the attribute.
732      * 
733      * @param attr the attribute data for the 'Dimension' attribute.
734      * @return the Dimension if found.
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 }