View Javadoc
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 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   * @author Dolf Dijkstra
35   * 
36   */
37  public class MappedAssetAccessTemplate<T> extends AssetAccessTemplate {
38      protected final AssetMapper<T> mapper;
39      protected final ICS ics;
40  
41      /**
42       * @param ics
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
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
63       * @param attributes
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
76       * @param associationType
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
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      * @param id the child asset
107      * @param associationName the association name or names to be used to filter parent query.
108      * @return collection of parents; never null.
109      */
110     public Collection<AssetId> readParentAssetIds(final AssetId id, final String ... associationName) {
111         return AssetRelationTreeUtils.getParents(ics, id, associationName);
112     }
113 
114     /**
115      * Read the assets that are parents of the current asset given the specified
116      * association name or names.  Note this can result in an unknown dependency
117      * @param associationName the association name or names to be used to filter parent query.
118      * @return collection of parents; never null.
119      */
120     public Collection<AssetId> readParentAssetIds(final String ... associationName) {
121         return readParentAssetIds(currentId(), associationName);
122     }
123 
124     /**
125      * Reads the associated assets of an asset and returns them as a
126      * ScatteredAsset. This takes care of the asset read operation of the
127      * associated assets. The returned ScatteredAssets are only loaded with the
128      * mentioned attributes.
129      * 
130      * @param id the parent asset
131      * @param associationType the name of the association or '-' for an unnamed
132      *            association
133      * @param attributes the list of attributes to load
134      * @return the assets from the associations.
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      * Reads the asset attributes of the asset identified by the current c and
152      * cid variables on the ics scope.
153      * 
154      * @return the mapped object
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      * Reads the mentioned asset attributes of the asset identified by the
167      * current c and cid variables on the ics scope.
168      * 
169      * @param attributes
170      * @return the mapped object
171      */
172     public T readCurrent(final String... attributes) {
173         final AssetId id = currentId();
174         return this.readAsset(id, mapper, attributes);
175     }
176 
177     /**
178      * Queries the asset repository with the provided query.
179      * 
180      * @param query
181      * @return Iterable of mapped objects
182      */
183     public Iterable<T> query(final Query query) {
184         return readAssets(query, mapper);
185     }
186 
187     /**
188      * Queries for a list of scattered assets.
189      * <p/>
190      * Sample queries are:
191      * <ul>
192      * <li>name='foo'</li>
193      * <li>name = 'foo'</li>
194      * <li>name = foo</li>
195      * <li>name= 'foo bar'</li>
196      * <li>size=[1,2]</li>
197      * <li>size{10,250}</li>
198      * <li>name!='foo'</li>
199      * 
200      * @param query
201      * @return a Iterable of mapped objects
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      * Queries for a list of scattered assets. Only the mentioned attributes are
210      * returned.
211      * 
212      * @param assetType
213      * @param subType
214      * @param query
215      * @param attributes
216      * @return a Iterable of mapped objects
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 }