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.asset;
18  
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.List;
22  
23  import com.fatwire.assetapi.common.AssetAccessException;
24  import com.fatwire.assetapi.data.AssetData;
25  import com.fatwire.assetapi.data.AssetId;
26  import com.fatwire.assetapi.data.AttributeData;
27  import com.fatwire.assetapi.data.BlobObject;
28  import com.fatwire.assetapi.data.BlobObject.BlobAddress;
29  import com.fatwire.assetapi.def.AssetTypeDef;
30  import com.fatwire.assetapi.def.AttributeDef;
31  import com.fatwire.assetapi.def.AttributeTypeEnum;
32  import com.fatwire.gst.foundation.facade.assetapi.AttributeDataUtils;
33  import com.fatwire.mda.Dimension;
34  
35  /**
36   * 
37   * 
38   * This class provides easy access to AssetData, to be used in rendering code in
39   * read-only mode. Is is called <tt>TemplateAsset</tt> because the intent is that it is to be used in Templates. 
40   * <p/>
41   * It has casting accessors for values of the different
42   * attribute types.
43   * <p/>
44   * It must be noted that naming conflicts between flex attribute names and meta
45   * attribute names are resolved by giving the meta attribute preference.
46   * <p/>
47   * <pre>
48   * {@code
49   * TemplateAsset asset = ...;
50   * String name = asset.asString("name");
51   * }
52   * </pre>
53   * 
54   * @author Dolf Dijkstra
55   * 
56   */
57  public class TemplateAsset {
58  
59      private final AssetData delegate;
60      private final List<String> metaList = new ArrayList<String>();
61  
62      /**
63       * @param delegate
64       */
65      public TemplateAsset(final AssetData delegate) {
66          super();
67          this.delegate = delegate;
68          fillMetaAttributes();
69      }
70  
71      public AssetData getDelegate() {
72          return delegate;
73      }
74  
75      /**
76       * @return the assetid
77       * @see com.fatwire.assetapi.data.AssetData#getAssetId()
78       */
79      public AssetId getAssetId() {
80          return delegate.getAssetId();
81      }
82  
83      /**
84       * @return the asset subtype.
85       */
86      public String getSubtype() {
87          return delegate.getAssetTypeDef().getSubtype();
88      }
89  
90      /**
91       * @return the asset type definition
92       * @see com.fatwire.assetapi.data.AssetData#getAssetTypeDef()
93       */
94      public AssetTypeDef getAssetTypeDef() {
95          return delegate.getAssetTypeDef();
96      }
97  
98      /**
99       * @param name name of the association
100      * @return list of assetids
101      * @see com.fatwire.assetapi.data.AssetData#getAssociatedAssets(java.lang.String)
102      */
103     public List<AssetId> getAssociatedAssets(final String name) {
104         return delegate.getAssociatedAssets(name);
105     }
106 
107     /**
108      * @param name name of the association
109      * @return the single associated asset.
110      * @see com.fatwire.assetapi.data.AssetData#getAssociatedAssets(java.lang.String)
111      */
112     public AssetId getAssociatedAsset(final String name) {
113         List<AssetId> assocs = delegate.getAssociatedAssets(name);
114         if (assocs != null && !assocs.isEmpty()) {
115             return assocs.get(0);
116         }
117         return null;
118     }
119 
120     /**
121      * @param name
122      * @return the attribute value
123      * @see com.fatwire.assetapi.data.AssetData#getAttributeData(java.lang.String,
124      *      boolean)
125      */
126     public Object getAttribute(final String name) {
127         AttributeData o = delegate.getAttributeData(name);
128         return o == null ? null : o.getData();
129     }
130 
131     /**
132      * @param name the name of the attribute.
133      * @return the meta attribute value.
134      * @see com.fatwire.assetapi.data.AssetData#getAttributeData(java.lang.String,
135      *      boolean)
136      */
137     public Object getMetaAttribute(final String name) {
138         AttributeData o = delegate.getAttributeData(name, true);
139         return o == null ? null : o.getData();
140     }
141 
142     /**
143      * @param name
144      * @return true is the attribute is defined as single valued.
145      */
146     public boolean isSingleValued(final String name) {
147         AttributeDef ad = delegate.getAssetTypeDef().getAttributeDef(name, true);
148         if (ad == null) {
149             ad = delegate.getAssetTypeDef().getAttributeDef(name, false);
150         }
151         return isSingleValued(ad);
152     }
153 
154     private boolean isSingleValued(final AttributeDef ad) {
155         return AttributeDataUtils.isSingleValued(ad);
156 
157     }
158 
159     private boolean isMetaAttribute(final String name) {
160         return this.metaList.contains(name);
161     }
162 
163     private AttributeData getMetaFirst(final String name) {
164         return delegate.getAttributeData(name, isMetaAttribute(name));
165     }
166 
167     /**
168      * @param name
169      * @return attribute as a List
170      */
171     public List<?> asList(final String name) {
172         final AttributeData attr = getMetaFirst(name);
173 
174         return AttributeDataUtils.asList(attr);
175     }
176 
177     /*
178      * <pre> INT Integer FLOAT Double STRING String DATE Date MONEY Double LONG
179      * Long LARGE_TEXT String ASSET AssetId BLOB BlobObject </pre>
180      */
181 
182     /**
183      * @param name
184      * @return attribute value as a Integer, can be null; please be careful with
185      *         autoboxing.
186      */
187 
188     public Integer asInt(final String name) {
189         final AttributeData attr = getMetaFirst(name);
190         return AttributeDataUtils.asInt(attr);
191 
192     }
193 
194     /**
195      * @param name
196      * @return attribute value as a Date.
197      */
198     public Date asDate(final String name) {
199         final AttributeData attr = getMetaFirst(name);
200         return AttributeDataUtils.asDate(attr);
201     }
202 
203     /**
204      * @param name
205      * @return attribute value as a BlobObject.
206      */
207     public BlobObject asBlob(final String name) {
208         final AttributeData attr = getMetaFirst(name);
209         return AttributeDataUtils.asBlob(attr);
210     }
211 
212     /**
213      * @param name
214      * @return attribute value as a Float, can be null; please be careful with
215      *         autoboxing.
216      */
217     public Float asFloat(final String name) {
218         final AttributeData attr = getMetaFirst(name);
219         return AttributeDataUtils.asFloat(attr);
220     }
221 
222     /**
223      * @param name
224      * @return attribute value as a Double, can be null; please be careful with
225      *         autoboxing.
226      */
227     public Double asDouble(final String name) {
228         final AttributeData attr = getMetaFirst(name);
229         return AttributeDataUtils.asDouble(attr);
230     }
231 
232     /**
233      * @param name
234      * @return attribute value as a Long, can be null; please be careful with
235      *         autoboxing.
236      */
237     public Long asLong(final String name) {
238         final AttributeData attr = getMetaFirst(name);
239         return AttributeDataUtils.asLong(attr);
240     }
241 
242     /**
243      * @param name
244      * @return attribute value as a AssetId.
245      */
246     public AssetId asAssetId(final String name) {
247 
248         final AttributeData attr = getMetaFirst(name);
249         return AttributeDataUtils.asAssetId(attr);
250     }
251 
252     /**
253      * @param name
254      * @return attribute value as a String.
255      */
256     public String asString(final String name) {
257         final AttributeData attr = getMetaFirst(name);
258         return AttributeDataUtils.asString(attr);
259     }
260 
261     /**
262      * @param name
263      * @return attribute value as a BlobAddress.
264      */
265     public BlobAddress asBlobAddress(final String name) {
266         final BlobObject blob = asBlob(name);
267         return blob == null ? null : blob.getBlobAddress();
268     }
269 
270     /**
271      * Get all the attribute names.
272      * 
273      * @return the name of all the attributes of the asset
274      * @see com.fatwire.assetapi.data.AssetData#getAttributeNames()
275      */
276     public List<String> getAttributeNames() {
277 
278         return delegate.getAttributeNames();
279     }
280 
281     /**
282      * Get the type of the attribute.
283      * 
284      * @param name the name of the attribute
285      * @return the attribute type
286      */
287     public AttributeTypeEnum getType(String name) {
288         return delegate.getAssetTypeDef().getAttributeDef(name, isMetaAttribute(name)).getType();
289     }
290 
291     /**
292      * Checks if the asset has an attribute by the provided name.
293      * 
294      * @param name the name of trhe attributes.
295      * @return true if the asset has an attribute by this name.
296      */
297     public boolean isAttribute(String name) {
298         return getAttributeNames().contains(name);
299     }
300 
301     /**
302      * Gets all the names of the meta attributes.
303      * 
304      * @return the name of all the attributes of the asset
305      * @see com.fatwire.assetapi.data.AssetData#getAttributeNames()
306      */
307     public List<String> getMetaAttributeNames() {
308 
309         return metaList;
310     }
311 
312     private void fillMetaAttributes() {
313         for (final AttributeDef def : delegate.getAssetTypeDef().getAttributeDefs()) {
314             if (def.isMetaDataAttribute()) {
315                 metaList.add(def.getName());
316             }
317         }
318     }
319 
320     /**
321      * @param name
322      * @return list of assetids
323      * @throws AssetAccessException
324      * @see com.fatwire.assetapi.data.AssetData#getImmediateParents(java.lang.String)
325      */
326     public List<AssetId> getImmediateParents(final String name) throws AssetAccessException {
327         return delegate.getImmediateParents(name);
328     }
329 
330     /**
331      * @return the parents of the asset
332      * @throws AssetAccessException
333      * @see com.fatwire.assetapi.data.AssetData#getParents()
334      */
335     public List<AssetId> getParents() throws AssetAccessException {
336         return delegate.getParents();
337     }
338 
339     /**
340      * @param name
341      * @param meta the asset attributes
342      * @return asset attributes
343      * @see com.fatwire.assetapi.data.AssetData#getAttributeData(java.lang.String,
344      *      boolean)
345      */
346     public AttributeData getAttributeData(final String name, final boolean meta) {
347         return delegate.getAttributeData(name, meta);
348     }
349 
350     /**
351      * @return the Dimension holding the locale
352      */
353     public Dimension getLocale() {
354         AttributeData dim = getAttributeData("Dimension", true);
355         if (dim == null)
356             return null;
357         return AttributeDataUtils.asDimension(dim);
358     }
359 
360     /*
361      * (non-Javadoc)
362      * 
363      * @see java.lang.Object#hashCode()
364      */
365     @Override
366     public int hashCode() {
367         final int prime = 31;
368         int result = 1;
369         result = prime * result + ((delegate == null) ? 0 : delegate.hashCode());
370         return result;
371     }
372 
373     /*
374      * (non-Javadoc)
375      * 
376      * @see java.lang.Object#equals(java.lang.Object)
377      */
378     @Override
379     public boolean equals(Object obj) {
380         if (this == obj)
381             return true;
382         if (obj == null)
383             return false;
384         if (!(obj instanceof TemplateAsset))
385             return false;
386         TemplateAsset other = (TemplateAsset) obj;
387         if (delegate == null) {
388             if (other.delegate != null)
389                 return false;
390         } else if (!delegate.equals(other.delegate))
391             return false;
392         return true;
393     }
394 
395     /*
396      * (non-Javadoc)
397      * 
398      * @see java.lang.Object#toString()
399      */
400     @Override
401     public String toString() {
402         return "TemplateAsset [getAssetId()=" + getAssetId() + "]";
403     }
404 
405 }