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.Collection;
20 import java.util.Collections;
21 import java.util.LinkedList;
22 import java.util.List;
23
24 import COM.FutureTense.Interfaces.ICS;
25
26 import com.fatwire.assetapi.data.AssetId;
27 import com.fatwire.assetapi.query.Query;
28 import com.fatwire.gst.foundation.facade.assetapi.AssetAccessTemplate;
29 import com.fatwire.gst.foundation.facade.assetapi.AssetIdUtils;
30 import com.fatwire.gst.foundation.facade.assetapi.AssetMapper;
31 import com.fatwire.gst.foundation.facade.runtag.asset.AssetRelationTreeUtils;
32
33
34
35
36
37 public class MappedAssetAccessTemplate<T> extends AssetAccessTemplate {
38 protected final AssetMapper<T> mapper;
39 protected final ICS ics;
40
41
42
43
44 public MappedAssetAccessTemplate(ICS ics, AssetMapper<T> mapper) {
45 super(ics);
46 this.ics = ics;
47 this.mapper = mapper;
48
49 }
50
51
52
53
54
55 public T read(final AssetId id) {
56 return this.readAsset(id, mapper);
57 }
58
59
60
61
62
63
64
65
66 public T read(final AssetId id, final String... attributes) {
67 return this.readAsset(id, mapper, attributes);
68 }
69
70
71
72
73
74
75
76
77
78
79 public Collection<T> readAssociatedAssets(final AssetId id, final String associationType) {
80 final Collection<AssetId> list = readAssociatedAssetIds(id, associationType);
81 if (list == null || list.isEmpty()) {
82 return Collections.emptyList();
83 }
84 final List<T> l = new LinkedList<T>();
85 for (final AssetId child : list) {
86 l.add(read(child));
87 }
88 return l;
89
90 }
91
92
93
94
95
96
97
98
99 public Collection<AssetId> readAssociatedAssetIds(final String associationType) {
100 return readAssociatedAssetIds(currentId(), associationType);
101 }
102
103
104
105
106
107
108
109
110 public Collection<AssetId> readParentAssetIds(final AssetId id, final String ... associationName) {
111 return AssetRelationTreeUtils.getParents(ics, id, associationName);
112 }
113
114
115
116
117
118
119
120 public Collection<AssetId> readParentAssetIds(final String ... associationName) {
121 return readParentAssetIds(currentId(), associationName);
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public Collection<T> readAssociatedAssets(final AssetId id, final String associationType,
137 final String... attributes) {
138 final List<AssetId> list = this.readAsset(id).getAssociatedAssets(associationType);
139 if (list == null || list.isEmpty()) {
140 return Collections.emptyList();
141 }
142 final List<T> l = new LinkedList<T>();
143 for (final AssetId child : list) {
144 l.add(read(child, attributes));
145 }
146 return l;
147
148 }
149
150
151
152
153
154
155
156 public T readCurrent() {
157 final AssetId id = currentId();
158 return this.read(id);
159 }
160
161 public AssetId currentId() {
162 return AssetIdUtils.currentId(ics);
163 }
164
165
166
167
168
169
170
171
172 public T readCurrent(final String... attributes) {
173 final AssetId id = currentId();
174 return this.readAsset(id, mapper, attributes);
175 }
176
177
178
179
180
181
182
183 public Iterable<T> query(final Query query) {
184 return readAssets(query, mapper);
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203 public Iterable<T> query(final String assetType, final String subType, final String query) {
204 return query(assetType, subType, query, mapper);
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218 public Iterable<T> query(final String assetType, final String subType, final String query,
219 final String... attributes) {
220 return query(assetType, subType, query, mapper, attributes);
221 }
222
223 }