1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package tools.gsf.facade.assetapi;
18
19 import COM.FutureTense.Interfaces.ICS;
20 import COM.FutureTense.Interfaces.IList;
21 import com.fatwire.assetapi.common.AssetAccessException;
22 import com.fatwire.assetapi.common.SiteAccessException;
23 import com.fatwire.assetapi.data.AssetData;
24 import com.fatwire.assetapi.data.AssetDataManager;
25 import com.fatwire.assetapi.data.AssetId;
26 import com.fatwire.assetapi.query.ConditionFactory;
27 import com.fatwire.assetapi.query.OpTypeEnum;
28 import com.fatwire.assetapi.query.Query;
29 import com.fatwire.assetapi.query.SimpleQuery;
30 import com.fatwire.assetapi.site.Site;
31 import com.fatwire.assetapi.site.SiteInfo;
32 import com.fatwire.assetapi.site.SiteManager;
33 import com.fatwire.system.Session;
34 import com.fatwire.system.SessionFactory;
35 import com.openmarket.xcelerate.asset.AssetIdImpl;
36 import tools.gsf.facade.runtag.asset.AssetList;
37
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.Iterator;
42 import java.util.LinkedList;
43 import java.util.List;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class AssetAccessTemplate {
59
60 private final Session session;
61 private AssetDataManager assetDataManager;
62
63
64
65
66 public AssetAccessTemplate(final Session session) {
67 super();
68 if (session == null) {
69 throw new IllegalArgumentException("session cannot be null.");
70 }
71 this.session = session;
72 }
73
74
75
76
77
78
79 public AssetAccessTemplate(final ICS ics) {
80 if (ics == null) {
81 throw new IllegalArgumentException("ics cannot be null.");
82 }
83 session = SessionFactory.getSession(ics);
84 }
85
86
87
88
89
90
91
92
93 public AssetId createAssetId(final String c, final String cid) {
94 return new AssetIdImpl(c, Long.parseLong(cid));
95 }
96
97
98
99
100
101
102
103
104 public AssetId createAssetId(final String c, final long cid) {
105 return new AssetIdImpl(c, cid);
106 }
107
108
109
110
111
112
113
114
115
116
117 public <T> T readAsset(final AssetId id, final AssetMapper<T> mapper) {
118 final AssetDataManager m = getAssetDataManager();
119
120 T t = null;
121 try {
122 final Iterable<AssetData> assets = m.read(Arrays.asList(id));
123 for (final AssetData assetData : assets) {
124 t = mapper.map(assetData);
125 }
126 } catch (final AssetAccessException e) {
127 throw new RuntimeAssetAccessException(e);
128 }
129 return t;
130 }
131
132
133
134
135
136
137
138
139
140
141
142 public <T> T readAsset(final String c, final String cid, final AssetMapper<T> mapper) {
143 return readAsset(this.createAssetId(c, cid), mapper);
144 }
145
146
147
148
149
150
151
152
153
154
155
156 public <T> T readAsset(final String c, final long cid, final AssetMapper<T> mapper) {
157 return readAsset(new AssetIdImpl(c, cid), mapper);
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 public <T> T readAsset(final AssetId id, final AssetMapper<T> mapper, final String... attributes) {
172 final AssetDataManager m = getAssetDataManager();
173
174 T t = null;
175 try {
176 final AssetData asset = m.readAttributes(id, Arrays.asList(attributes));
177 t = mapper.map(asset);
178 } catch (final AssetAccessException e) {
179 throw new RuntimeAssetAccessException(e);
180 }
181 return t;
182 }
183
184
185
186
187
188
189
190
191
192 public void readAsset(final AssetId id, final AssetClosure closure, final String... attributes) {
193 final AssetDataManager m = getAssetDataManager();
194
195 try {
196 final AssetData asset = m.readAttributes(id, Arrays.asList(attributes));
197 if (asset != null) {
198 if (!closure.work(asset)) {
199 return;
200 }
201 }
202 } catch (final AssetAccessException e) {
203 throw new RuntimeAssetAccessException(e);
204 }
205 }
206
207
208
209
210
211
212
213
214 public void readAsset(final AssetId id, final AssetClosure closure) {
215 final AssetDataManager m = getAssetDataManager();
216
217 try {
218 final Iterable<AssetData> assets = m.read(Arrays.asList(id));
219 for (final AssetData assetData : assets) {
220 if (!closure.work(assetData)) {
221 return;
222 }
223
224 }
225 } catch (final AssetAccessException e) {
226 throw new RuntimeAssetAccessException(e);
227 }
228
229 }
230
231
232
233
234
235
236
237
238 public void readAsset(final List<AssetId> ids, final AssetClosure closure) {
239 final AssetDataManager m = getAssetDataManager();
240
241 try {
242 final Iterable<AssetData> assets = m.read(ids);
243 for (final AssetData assetData : assets) {
244 if (!closure.work(assetData)) {
245 return;
246 }
247
248 }
249 } catch (final AssetAccessException e) {
250 throw new RuntimeAssetAccessException(e);
251 }
252
253 }
254
255
256
257
258
259
260
261
262
263 public void readAsset(final Iterable<AssetId> ids, final AssetClosure closure, final String... attributes) {
264 final AssetDataManager m = getAssetDataManager();
265
266 try {
267 for (final AssetId id : ids) {
268 final AssetData asset = m.readAttributes(id, Arrays.asList(attributes));
269 if (asset != null) {
270 if (!closure.work(asset)) {
271 return;
272 }
273 }
274
275 }
276 } catch (final AssetAccessException e) {
277 throw new RuntimeAssetAccessException(e);
278 }
279
280 }
281
282
283
284
285
286
287
288
289
290
291
292 public AssetData readAsset(final AssetId id, final String... attributes) {
293 final AssetDataManager m = getAssetDataManager();
294
295 Iterable<AssetData> assets;
296 try {
297 assets = m.read(Arrays.asList(id));
298 } catch (final AssetAccessException e) {
299 throw new RuntimeAssetAccessException(e);
300 }
301 if (assets == null) {
302 return null;
303 }
304 if (assets instanceof List<?>) {
305 final List<AssetData> x = (List<AssetData>) assets;
306 if (x.isEmpty()) {
307 return null;
308 } else {
309 return x.get(0);
310 }
311 }
312 final Iterator<AssetData> i = assets.iterator();
313 if (i.hasNext()) {
314 return i.next();
315 }
316 return null;
317 }
318
319
320
321
322 protected AssetDataManager getAssetDataManager() {
323 if (assetDataManager == null) {
324 assetDataManager = (AssetDataManager) session.getManager(AssetDataManager.class.getName());
325 }
326 return assetDataManager;
327 }
328
329
330
331
332
333 public AssetData readAsset(final AssetId id) {
334
335 final AssetDataManager m = getAssetDataManager();
336
337 Iterable<AssetData> assets;
338 try {
339 assets = m.read(Collections.singletonList(id));
340 } catch (final AssetAccessException e) {
341 throw new RuntimeAssetAccessException(e);
342 }
343 if (assets == null) {
344 return null;
345 }
346 if (assets instanceof List<?>) {
347 final List<AssetData> x = (List<AssetData>) assets;
348 if (x.isEmpty()) {
349 return null;
350 } else {
351 return x.get(0);
352 }
353 }
354 final Iterator<AssetData> i = assets.iterator();
355 if (i.hasNext()) {
356 return i.next();
357 }
358 return null;
359 }
360
361
362
363
364
365 public Iterable<AssetData> readAssets(final Query query) {
366 final AssetDataManager m = getAssetDataManager();
367
368 Iterable<AssetData> assets;
369 try {
370 assets = m.read(query);
371 } catch (final AssetAccessException e) {
372 throw new RuntimeAssetAccessException(e);
373 }
374
375 return assets;
376 }
377
378
379
380
381
382
383
384
385 public void readAssets(final Query query, final AssetClosure closure) {
386 final AssetDataManager m = getAssetDataManager();
387
388 try {
389 for (final AssetData asset : m.read(query)) {
390 if (!closure.work(asset)) {
391 return;
392 }
393
394 }
395 } catch (final AssetAccessException e) {
396 throw new RuntimeAssetAccessException(e);
397 }
398 }
399
400
401
402
403
404
405
406
407
408
409 public <T> Iterable<T> readAssets(final Query query, final AssetMapper<T> mapper) {
410
411 final List<T> r = new LinkedList<T>();
412
413 for (final AssetData data : readAssets(query)) {
414 r.add(mapper.map(data));
415 }
416
417 return r;
418 }
419
420
421
422
423
424
425
426
427
428
429
430 public AssetId findByName(final ICS ics, final String assetType, final String name, final long siteid) {
431
432 final AssetList tag = new AssetList();
433 tag.setType(assetType);
434 tag.setField("name", name);
435 tag.setExcludeVoided(true);
436 tag.setPubid(Long.toString(siteid));
437 tag.setList("name__");
438 tag.execute(ics);
439
440 final IList list = ics.GetList("name__");
441 ics.RegisterList("name__", null);
442 if (list != null && list.hasData()) {
443 list.moveTo(1);
444 try {
445 return new AssetIdImpl(assetType, Long.parseLong(list.getValue("id")));
446 } catch (final NoSuchFieldException e) {
447 throw new RuntimeException(e);
448 }
449 } else {
450 return null;
451 }
452
453 }
454
455
456
457
458
459
460
461
462
463
464 public AssetId findByName(final ICS ics, final String assetType, final String name) {
465
466 final AssetList x = new AssetList();
467 x.setType(assetType);
468 x.setField("name", name);
469 x.setExcludeVoided(true);
470 x.setList("name__");
471 x.execute(ics);
472
473 final IList list = ics.GetList("name__");
474 ics.RegisterList("name__", null);
475 if (list != null && list.hasData()) {
476 list.moveTo(1);
477 try {
478 return new AssetIdImpl(assetType, Long.parseLong(list.getValue("id")));
479 } catch (final NoSuchFieldException e) {
480 throw new RuntimeException(e);
481 }
482 } else {
483 return null;
484 }
485
486 }
487
488
489
490
491
492
493
494
495 public SimpleQuery createNameQuery(final String assetType, final String assetName) {
496 final SimpleQuery q = new SimpleQuery(assetType, null, ConditionFactory.createCondition("name",
497 OpTypeEnum.EQUALS, assetName), Arrays.asList("id"));
498 q.getProperties().setIsBasicSearch(true);
499 return q;
500 }
501
502
503
504
505
506
507
508 public Site readSite(final String name) {
509 final SiteManager sm = (SiteManager) session.getManager(SiteManager.class.getName());
510 try {
511
512 final List<Site> list = sm.read(Arrays.asList(name));
513 if (list == null || list.isEmpty()) {
514 return null;
515 }
516 return list.get(0);
517 } catch (final SiteAccessException e) {
518 throw new SiteAccessRuntimeException(e);
519 }
520 }
521
522
523
524
525
526 public SiteInfo readSiteInfo(final String name) {
527 final SiteManager sm = (SiteManager) session.getManager(SiteManager.class.getName());
528 try {
529
530 for (SiteInfo si : sm.list()) {
531 if (si.getName().equals(name)) {
532 return si;
533 }
534 }
535 } catch (final SiteAccessException e) {
536 throw new SiteAccessRuntimeException(e);
537 }
538 throw new SiteAccessRuntimeException("Site " + name + " does not exist in Content Server.");
539 }
540
541
542
543
544
545
546
547
548 public Collection<AssetId> readAssociatedAssetIds(final AssetId id, final String associationType) {
549 final List<AssetId> list = readAsset(id).getAssociatedAssets(associationType);
550 if (list == null) {
551 return Collections.emptyList();
552 }
553 return list;
554
555 }
556
557
558
559
560
561
562
563
564
565
566
567
568
569 public void readAssociatedAssets(final AssetId id, final String associationType, final AssetClosure closure,
570 final String... attributes) {
571 final List<AssetId> list = this.readAsset(id).getAssociatedAssets(associationType);
572 if (list == null || list.isEmpty()) {
573 return;
574 }
575
576 for (final AssetId child : list) {
577 readAsset(child, closure, attributes);
578 }
579
580 }
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606 public <T> Iterable<T> query(final String assetType, final String subType, final String query,
607 AssetMapper<T> mapper, final String... attributes) {
608 final Query q = new QueryBuilder(assetType, subType).condition(query).attributes(attributes).toQuery();
609 return this.readAssets(q, mapper);
610 }
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633 public <T> Iterable<T> query(final String assetType, final String subType, final String query, AssetMapper<T> mapper) {
634 final Query q = new QueryBuilder(assetType, subType).condition(query).setReadAll(true).toQuery();
635 return this.readAssets(q, mapper);
636 }
637
638 }