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;
18  
19  import java.util.Arrays;
20  import java.util.Collections;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  import COM.FutureTense.Util.ftErrors;
24  
25  import com.fatwire.assetapi.common.AssetAccessException;
26  import com.fatwire.assetapi.data.AssetData;
27  import com.fatwire.assetapi.data.AssetDataManager;
28  import com.fatwire.assetapi.data.AssetId;
29  import com.fatwire.gst.foundation.CSRuntimeException;
30  import com.fatwire.gst.foundation.facade.ics.ICSFactory;
31  import com.fatwire.system.Session;
32  import com.fatwire.system.SessionFactory;
33  
34  /**
35   * Convenient shortcuts for working with AssetData objects
36   * 
37   * @author Tony Field
38   * @author Dolf Dijkstra
39   * @since Nov 17, 2009
40   */
41  public final class AssetDataUtils {
42      private AssetDataUtils() {
43      }
44  
45      /**
46       * Return the AssetData for the specified asset
47       * 
48       * @param c
49       * @param cid
50       * @param attributes
51       * @return asset data
52       * @deprecated inconsistent method signature, replaced by {@link AssetDataUtils#getAssetData(AssetId, String...)}. 
53       */
54      @Deprecated
55      public static AssetData getAssetData(String c, String cid, String... attributes) {
56          return getAssetData(AssetIdUtils.createAssetId(c, cid), attributes);
57      }
58  
59      /**
60       * Read all attributes for the given asset id.
61       * 
62       * @param id must be valid or else an exception is thrown
63       * @return asset data, never null.
64       * @deprecated use #getAssetData(ICS, AssetId) instead
65       */
66      @Deprecated
67      public static AssetData getAssetData(AssetId id) {
68          return getAssetData(ICSFactory.getOrCreateICS(), id);
69      }
70  
71      /**
72       * Read all attributes for the given asset id.
73       * 
74       * @param ics ICS context
75       * @param id must be valid or else an exception is thrown
76       * @return asset data, never null.
77       */
78      public static AssetData getAssetData(ICS ics, AssetId id) {
79          AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
80          try {
81              for (AssetData data : mgr.read(Collections.singletonList(id))) {
82                  return data; // first one wins
83              }
84          } catch (AssetAccessException e) {
85              throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
86          }
87          throw new CSRuntimeException("Asset not found: " + id, ftErrors.badparams);
88      }
89  
90      /**
91       * Return the AssetData for the specified asset
92       * 
93       * @param id
94       * @param attributes
95       * @return asset data
96       * @deprecated use #getAssetData(ICS, AssetId, String...) instead
97       */
98      @Deprecated
99      public static AssetData getAssetData(AssetId id, String... attributes) {
100         return getAssetData(ICSFactory.getOrCreateICS(), id, attributes);
101     }
102 
103     /**
104      * Return the AssetData for the specified asset
105      * 
106      * @param id
107      * @param attributes
108      * @return asset data
109      */
110     public static AssetData getAssetData(ICS ics, AssetId id, String... attributes) {
111         AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
112         try {
113             return mgr.readAttributes(id, Arrays.asList(attributes));
114         } catch (AssetAccessException e) {
115             throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
116         }
117     }
118 
119     /**
120      * This is a convenience method to read AssetData for the current c/cid
121      * asset on the ics scope.
122      * 
123      * @param ics
124      * @param attributes
125      * @return asset data
126      */
127     public static AssetData getCurrentAssetData(ICS ics, String... attributes) {
128         AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
129         try {
130             AssetId id = AssetIdUtils.currentId(ics);
131             return mgr.readAttributes(id, Arrays.asList(attributes));
132         } catch (AssetAccessException e) {
133             throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
134         }
135 
136     }
137 
138     /**
139      * This is a convenience method to read AssetData for the current c/cid
140      * asset on the ics scope.
141      * 
142      * @param ics
143      * @return asset data
144      */
145     public static AssetData getCurrentAssetData(ICS ics) {
146         AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
147         AssetId id = AssetIdUtils.currentId(ics);
148         try {
149             for (AssetData data : mgr.read(Collections.singletonList(id))) {
150                 return data; // first one wins
151             }
152         } catch (AssetAccessException e) {
153             throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
154         }
155         throw new CSRuntimeException("Asset not found: " + id, ftErrors.badparams);
156 
157     }
158 
159     private static Session getSession(ICS ics) {
160         return SessionFactory.getSession(ics);
161     }
162 
163 }