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