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.FutureTense.Interfaces.ICS;
20 import com.fatwire.assetapi.data.AssetId;
21 import com.fatwire.assetapi.query.Query;
22 import tools.gsf.facade.assetapi.AssetAccessTemplate;
23 import tools.gsf.facade.assetapi.AssetIdUtils;
24 import tools.gsf.facade.assetapi.AssetMapper;
25 import tools.gsf.facade.runtag.asset.AssetRelationTreeUtils;
26
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.LinkedList;
30 import java.util.List;
31
32 /**
33 * @param <T> class to be returned based on asset mapper
34 * @author Dolf Dijkstra
35 */
36 public class MappedAssetAccessTemplate<T> extends AssetAccessTemplate {
37 protected final AssetMapper<T> mapper;
38 protected final ICS ics;
39
40 /**
41 * @param ics Content Server context object
42 * @param mapper asset mapper
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 * @param id asset id
53 * @return the mapped object
54 */
55 public T read(final AssetId id) {
56 return this.readAsset(id, mapper);
57 }
58
59 /**
60 * Reads the attributes of an asset.
61 *
62 * @param id asset id
63 * @param attributes array of attribute names
64 * @return the mapped object
65 */
66 public T read(final AssetId id, final String... attributes) {
67 return this.readAsset(id, mapper, attributes);
68 }
69
70 /**
71 * Reads the associated assets of an asset and returns them as a
72 * ScatteredAsset. This takes care of the asset read operation of the
73 * associated assets.
74 *
75 * @param id asset id
76 * @param associationType associated type
77 * @return the assets from the associations.
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 * Reads the list of associated assets based on the current c and cid cs
94 * variables.
95 *
96 * @param associationType associated type
97 * @return the assets from the associations.
98 */
99 public Collection<AssetId> readAssociatedAssetIds(final String associationType) {
100 return readAssociatedAssetIds(currentId(), associationType);
101 }
102
103 /**
104 * Read the assets that are parents of the current asset given the specified
105 * association name or names. Note this can result in an unknown dependency
106 *
107 * @param id the child asset
108 * @param associationName the association name or names to be used to filter parent query.
109 * @return collection of parents; never null.
110 */
111 public Collection<AssetId> readParentAssetIds(final AssetId id, final String... associationName) {
112 return AssetRelationTreeUtils.getParents(ics, id, associationName);
113 }
114
115 /**
116 * Read the assets that are parents of the current asset given the specified
117 * association name or names. Note this can result in an unknown dependency
118 *
119 * @param associationName the association name or names to be used to filter parent query.
120 * @return collection of parents; never null.
121 */
122 public Collection<AssetId> readParentAssetIds(final String... associationName) {
123 return readParentAssetIds(currentId(), associationName);
124 }
125
126 /**
127 * Reads the associated assets of an asset and returns them as a
128 * ScatteredAsset. This takes care of the asset read operation of the
129 * associated assets. The returned ScatteredAssets are only loaded with the
130 * mentioned attributes.
131 *
132 * @param id the parent asset
133 * @param associationType the name of the association or '-' for an unnamed
134 * association
135 * @param attributes the list of attributes to load
136 * @return the assets from the associations.
137 */
138 public Collection<T> readAssociatedAssets(final AssetId id, final String associationType,
139 final String... attributes) {
140 final List<AssetId> list = this.readAsset(id).getAssociatedAssets(associationType);
141 if (list == null || list.isEmpty()) {
142 return Collections.emptyList();
143 }
144 final List<T> l = new LinkedList<T>();
145 for (final AssetId child : list) {
146 l.add(read(child, attributes));
147 }
148 return l;
149
150 }
151
152 /**
153 * Reads the asset attributes of the asset identified by the current c and
154 * cid variables on the ics scope.
155 *
156 * @return the mapped object
157 */
158 public T readCurrent() {
159 final AssetId id = currentId();
160 return this.read(id);
161 }
162
163 public AssetId currentId() {
164 return AssetIdUtils.currentId(ics);
165 }
166
167 /**
168 * Reads the mentioned asset attributes of the asset identified by the
169 * current c and cid variables on the ics scope.
170 *
171 * @param attributes array of attribute names
172 * @return the mapped object
173 */
174 public T readCurrent(final String... attributes) {
175 final AssetId id = currentId();
176 return this.readAsset(id, mapper, attributes);
177 }
178
179 /**
180 * Queries the asset repository with the provided query.
181 *
182 * @param query query object
183 * @return Iterable of mapped objects
184 */
185 public Iterable<T> query(final Query query) {
186 return readAssets(query, mapper);
187 }
188
189 /**
190 * Queries for a list of scattered assets.
191 * <p>
192 * Sample queries are:
193 * <ul>
194 * <li>name='foo'</li>
195 * <li>name = 'foo'</li>
196 * <li>name = foo</li>
197 * <li>name= 'foo bar'</li>
198 * <li>size=[1,2]</li>
199 * <li>size{10,250}</li>
200 * <li>name!='foo'</li>
201 * </ul>
202 *
203 * @param assetType string value for asset type
204 * @param subType string value for sub-type
205 * @param query query object
206 * @return a Iterable of mapped objects
207 */
208 public Iterable<T> query(final String assetType, final String subType, final String query) {
209 return query(assetType, subType, query, mapper);
210 }
211
212 /**
213 * Queries for a list of scattered assets. Only the mentioned attributes are
214 * returned.
215 *
216 * @param assetType string value of asset type
217 * @param subType string value of subtype
218 * @param query string value of query
219 * @param attributes string array of attributes
220 * @return a Iterable of mapped objects
221 */
222 public Iterable<T> query(final String assetType, final String subType, final String query,
223 final String... attributes) {
224 return query(assetType, subType, query, mapper, attributes);
225 }
226
227 }