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 tools.gsf.facade.assetapi;
18  
19  import COM.FutureTense.Interfaces.ICS;
20  import COM.FutureTense.Util.ftErrors;
21  import com.fatwire.assetapi.common.AssetAccessException;
22  import com.fatwire.assetapi.data.AssetData;
23  import com.fatwire.assetapi.data.AssetDataManager;
24  import com.fatwire.assetapi.data.AssetId;
25  import com.fatwire.system.Session;
26  import com.fatwire.system.SessionFactory;
27  import tools.gsf.runtime.CSRuntimeException;
28  
29  import java.util.Arrays;
30  import java.util.Collections;
31  
32  /**
33   * Convenient shortcuts for working with AssetData objects
34   *
35   * @author Tony Field
36   * @author Dolf Dijkstra
37   * @since Nov 17, 2009
38   */
39  public final class AssetDataUtils {
40      private AssetDataUtils() {
41      }
42  
43      /**
44       * Read all attributes for the given asset id.
45       *
46       * @param ics ICS context
47       * @param id  must be valid or else an exception is thrown
48       * @return asset data, never null.
49       */
50      public static AssetData getAssetData(ICS ics, AssetId id) {
51          AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
52          try {
53              for (AssetData data : mgr.read(Collections.singletonList(id))) {
54                  return data; // first one wins
55              }
56          } catch (AssetAccessException e) {
57              throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
58          }
59          throw new CSRuntimeException("Asset not found: " + id, ftErrors.badparams);
60      }
61  
62      /**
63       * Return the AssetData for the specified asset
64       *
65       * @param ics        content server context
66       * @param id         asset id
67       * @param attributes list of attribute names
68       * @return asset data
69       */
70      public static AssetData getAssetData(ICS ics, AssetId id, String... attributes) {
71          AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
72          try {
73              return mgr.readAttributes(id, Arrays.asList(attributes));
74          } catch (AssetAccessException e) {
75              throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
76          }
77      }
78  
79      /**
80       * This is a convenience method to read AssetData for the current c/cid
81       * asset on the ics scope.
82       *
83       * @param ics        Content Server context object
84       * @param attributes list of attribute names
85       * @return asset data
86       */
87      public static AssetData getCurrentAssetData(ICS ics, String... attributes) {
88          AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
89          try {
90              AssetId id = AssetIdUtils.currentId(ics);
91              return mgr.readAttributes(id, Arrays.asList(attributes));
92          } catch (AssetAccessException e) {
93              throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
94          }
95  
96      }
97  
98      /**
99       * This is a convenience method to read AssetData for the current c/cid
100      * asset on the ics scope.
101      *
102      * @param ics Content Server context object
103      * @return asset data
104      */
105     public static AssetData getCurrentAssetData(ICS ics) {
106         AssetDataManager mgr = (AssetDataManager) getSession(ics).getManager(AssetDataManager.class.getName());
107         AssetId id = AssetIdUtils.currentId(ics);
108         try {
109             for (AssetData data : mgr.read(Collections.singletonList(id))) {
110                 return data; // first one wins
111             }
112         } catch (AssetAccessException e) {
113             throw new CSRuntimeException("Failed to read attribute data: " + e, ftErrors.exceptionerr, e);
114         }
115         throw new CSRuntimeException("Asset not found: " + id, ftErrors.badparams);
116 
117     }
118 
119     private static Session getSession(ICS ics) {
120         return SessionFactory.getSession(ics);
121     }
122 
123 }