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.runtag.commercecontext;
18  
19  import java.util.Collection;
20  
21  import COM.FutureTense.Interfaces.ICS;
22  import COM.FutureTense.Interfaces.IList;
23  import COM.FutureTense.Util.IterableIListWrapper;
24  
25  import com.fatwire.assetapi.data.AssetId;
26  import com.fatwire.gst.foundation.IListUtils;
27  import com.fatwire.gst.foundation.facade.assetapi.AssetIdIList;
28  
29  /**
30   * Helper to execute the GetRecommendations tag in a assetapi/java friendly way.
31   * 
32   * @author Dolf.Dijkstra
33   * @since Apr 28, 2011
34   */
35  public final class Recommendations {
36      private Recommendations() {
37      }
38  
39      /**
40       * Easy-to-use utility method to return recommendations for the specified
41       * asset.
42       * 
43       * @param ics context
44       * @param id AssetId of recommendation to return
45       * @param max max count
46       * @return Collection<AssetId> containing recommendations
47       */
48      public static Collection<AssetId> getRecommendations(ICS ics, AssetId id, int max) {
49          GetRecommendations gr = new GetRecommendations();
50          gr.setCollectionId(id.getId());
51          gr.setMaxCount(max);
52          String list = IListUtils.generateRandomListName();
53          gr.setListVarName(list);
54          try {
55              gr.execute(ics);
56              IList result = ics.GetList(list);
57              return IListUtils.toAssetIdCollection(result);
58          } finally {
59              ics.RegisterList(list, null); // unregister
60          }
61  
62      }
63  
64      /**
65       * Easy-to-use utility method to return recommendations for the specified
66       * asset.
67       * 
68       * @param ics context
69       * @param collection the name of the recommendation
70       * @param max max count
71       * @return Collection<AssetId> containing recommendations
72       * @see IterableIListWrapper
73       */
74      public static Collection<AssetId> getRecommendations(ICS ics, String collection, int max) {
75          GetRecommendations gr = new GetRecommendations();
76          gr.setCollection(collection);
77          gr.setMaxCount(max);
78          String list = IListUtils.generateRandomListName();
79          gr.setListVarName(list);
80          try {
81              gr.execute(ics);
82              IList result = ics.GetList(list);
83              return IListUtils.toAssetIdCollection(result);
84          } finally {
85              ics.RegisterList(list, null); // unregister
86          }
87  
88      }
89  
90      /**
91       * Get the context-based recommendations from the input Collection.
92       * 
93       * @param ics
94       * @param collection
95       * @param input
96       * @return the collection with recommended assets
97       */
98      public static Collection<AssetId> getRecommendations(ICS ics, String collection, Collection<AssetId> input) {
99          GetRecommendations gr = new GetRecommendations();
100         String inputList = IListUtils.generateRandomListName();
101         ics.RegisterList(inputList, new AssetIdIList(inputList, input));
102         gr.setList(inputList);
103         gr.setCollection(collection);
104         String list = IListUtils.generateRandomListName();
105         gr.setListVarName(list);
106         try {
107             gr.execute(ics);
108             IList result = ics.GetList(list);
109             return IListUtils.toAssetIdCollection(result);
110         } finally {
111             ics.RegisterList(inputList, null); // unregister
112             ics.RegisterList(list, null); // unregister
113 
114         }
115     }
116 
117 }