1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class TemplateAsset {
58
59 private final AssetData delegate;
60 private final List<String> metaList = new ArrayList<String>();
61
62
63
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
77
78
79 public AssetId getAssetId() {
80 return delegate.getAssetId();
81 }
82
83
84
85
86 public String getSubtype() {
87 return delegate.getAssetTypeDef().getSubtype();
88 }
89
90
91
92
93
94 public AssetTypeDef getAssetTypeDef() {
95 return delegate.getAssetTypeDef();
96 }
97
98
99
100
101
102
103 public List<AssetId> getAssociatedAssets(final String name) {
104 return delegate.getAssociatedAssets(name);
105 }
106
107
108
109
110
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
122
123
124
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
133
134
135
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
144
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
169
170
171 public List<?> asList(final String name) {
172 final AttributeData attr = getMetaFirst(name);
173
174 return AttributeDataUtils.asList(attr);
175 }
176
177
178
179
180
181
182
183
184
185
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
196
197
198 public Date asDate(final String name) {
199 final AttributeData attr = getMetaFirst(name);
200 return AttributeDataUtils.asDate(attr);
201 }
202
203
204
205
206
207 public BlobObject asBlob(final String name) {
208 final AttributeData attr = getMetaFirst(name);
209 return AttributeDataUtils.asBlob(attr);
210 }
211
212
213
214
215
216
217 public Float asFloat(final String name) {
218 final AttributeData attr = getMetaFirst(name);
219 return AttributeDataUtils.asFloat(attr);
220 }
221
222
223
224
225
226
227 public Double asDouble(final String name) {
228 final AttributeData attr = getMetaFirst(name);
229 return AttributeDataUtils.asDouble(attr);
230 }
231
232
233
234
235
236
237 public Long asLong(final String name) {
238 final AttributeData attr = getMetaFirst(name);
239 return AttributeDataUtils.asLong(attr);
240 }
241
242
243
244
245
246 public AssetId asAssetId(final String name) {
247
248 final AttributeData attr = getMetaFirst(name);
249 return AttributeDataUtils.asAssetId(attr);
250 }
251
252
253
254
255
256 public String asString(final String name) {
257 final AttributeData attr = getMetaFirst(name);
258 return AttributeDataUtils.asString(attr);
259 }
260
261
262
263
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
272
273
274
275
276 public List<String> getAttributeNames() {
277
278 return delegate.getAttributeNames();
279 }
280
281
282
283
284
285
286
287 public AttributeTypeEnum getType(String name) {
288 return delegate.getAssetTypeDef().getAttributeDef(name, isMetaAttribute(name)).getType();
289 }
290
291
292
293
294
295
296
297 public boolean isAttribute(String name) {
298 return getAttributeNames().contains(name);
299 }
300
301
302
303
304
305
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
322
323
324
325
326 public List<AssetId> getImmediateParents(final String name) throws AssetAccessException {
327 return delegate.getImmediateParents(name);
328 }
329
330
331
332
333
334
335 public List<AssetId> getParents() throws AssetAccessException {
336 return delegate.getParents();
337 }
338
339
340
341
342
343
344
345
346 public AttributeData getAttributeData(final String name, final boolean meta) {
347 return delegate.getAttributeData(name, meta);
348 }
349
350
351
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
362
363
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
375
376
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
397
398
399
400 @Override
401 public String toString() {
402 return "TemplateAsset [getAssetId()=" + getAssetId() + "]";
403 }
404
405 }