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      public static AssetData getAssetData(String c, String cid, String... attributes) {
55          return getAssetData(AssetIdUtils.createAssetId(c, cid), attributes);
56      }
57  
58      /**
59       * Read all attributes for the given asset id.
60       * 
61       * @param id must be valid or else an exception is thrown
62       * @return asset data, never null.
63       */
64      public static AssetData getAssetData(AssetId id) {
65          return getAssetData(ICSFactory.getOrCreateICS(), id);
66      }
67  
68      /**
69       * Read all attributes for the given asset id.
70       * 
71       * @param ics ICS context
72       * @param id must be valid or else an exception is thrown
73       * @return asset data, never null.
74       */
75      public static AssetData getAssetData(ICS ics, AssetId id) {
76          AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
77          try {
78              for (AssetData data : mgr.read(Collections.singletonList(id))) {
79                  return data; // first one wins
80              }
81          } catch (AssetAccessException e) {
82              throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
83          }
84          throw new CSRuntimeException("Asset not found: " + id, ftErrors.badparams);
85      }
86  
87      /**
88       * Return the AssetData for the specified asset
89       * 
90       * @param id
91       * @param attributes
92       * @return asset data
93       */
94      public static AssetData getAssetData(AssetId id, String... attributes) {
95          return getAssetData(ICSFactory.getOrCreateICS(), id, attributes);
96      }
97  
98      /**
99       * Return the AssetData for the specified asset
100      * 
101      * @param id
102      * @param attributes
103      * @return asset data
104      */
105     public static AssetData getAssetData(ICS ics, AssetId id, String... attributes) {
106         AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
107         try {
108             return mgr.readAttributes(id, Arrays.asList(attributes));
109         } catch (AssetAccessException e) {
110             throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
111         }
112     }
113 
114     /**
115      * This is a convenience method to read AssetData for the current c/cid
116      * asset on the ics scope.
117      * 
118      * @param ics
119      * @param attributes
120      * @return asset data
121      */
122     public static AssetData getCurrentAssetData(ICS ics, String... attributes) {
123         AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
124         try {
125             AssetId id = AssetIdUtils.currentId(ics);
126             return mgr.readAttributes(id, Arrays.asList(attributes));
127         } catch (AssetAccessException e) {
128             throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
129         }
130 
131     }
132 
133     /**
134      * This is a convenience method to read AssetData for the current c/cid
135      * asset on the ics scope.
136      * 
137      * @param ics
138      * @return asset data
139      */
140     public static AssetData getCurrentAssetData(ICS ics) {
141         AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
142         AssetId id = AssetIdUtils.currentId(ics);
143         try {
144             for (AssetData data : mgr.read(Collections.singletonList(id))) {
145                 return data; // first one wins
146             }
147         } catch (AssetAccessException e) {
148             throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
149         }
150         throw new CSRuntimeException("Asset not found: " + id, ftErrors.badparams);
151 
152     }
153 
154     private static Session getSession(ICS ics) {
155         return SessionFactory.getSession(ics);
156     }
157 
158 }