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