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