1 /*
2 * Copyright 2010 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 package com.fatwire.gst.foundation.wra.navigation;
17
18 import java.util.Date;
19
20 import org.apache.commons.lang.StringUtils;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import COM.FutureTense.Interfaces.ICS;
25
26 import com.fatwire.assetapi.data.AssetId;
27 import com.fatwire.assetapi.site.SiteInfo;
28 import com.fatwire.gst.foundation.controller.AssetIdWithSite;
29 import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAssetAccess;
30 import com.fatwire.gst.foundation.navigation.NavigationNode;
31 import com.fatwire.gst.foundation.navigation.NavigationService;
32 import com.fatwire.gst.foundation.wra.Alias;
33 import com.fatwire.gst.foundation.wra.AliasCoreFieldDao;
34 import com.fatwire.gst.foundation.wra.AssetApiAliasCoreFieldDao;
35 import com.fatwire.gst.foundation.wra.AssetApiWraCoreFieldDao;
36 import com.fatwire.gst.foundation.wra.WraCoreFieldDao;
37 import com.fatwire.mda.DimensionFilterInstance;
38 import com.openmarket.xcelerate.asset.AssetIdImpl;
39
40
41 /**
42 *
43 * @author Dolf Dijkstra
44 * @deprecated replaced with {@link NavigationService}
45 */
46 public abstract class AbstractNavigationHelper {
47
48 /**
49 * ICS context
50 */
51 protected final ICS ics;
52
53 protected abstract NavigationNode getSitePlan(final int depth, final AssetId pageId, final int level,
54 final DimensionFilterInstance dimensionFilter);
55
56 protected final TemplateAssetAccess assetTemplate;
57 /**
58 * Local instance of the WraCoreFieldDao, pre-instantiated and ready to go
59 */
60 protected final WraCoreFieldDao wraDao;
61 /**
62 * Local instance of the AliasCoreFieldDao.
63 */
64 protected final AliasCoreFieldDao aliasDao;
65 /**
66 * Log file
67 */
68 protected static final Log LOG = LogFactory.getLog(AbstractNavigationHelper.class);
69 /**
70 * Effective date for the purposes of startdate/enddate comparisons for an
71 * asset.
72 */
73 protected final Date assetEffectiveDate;
74 /**
75 * Name of the page subtype indicating that this page is NOT rendered on the
76 * site but is instead merely used to group navigation components on the
77 * site.
78 */
79 public static final String NAVBAR_NAME = "GSTNavName";
80 /**
81 * Name of the page subtype indicating that this page is a Link, meaning
82 * that the content is in the unnamed association
83 */
84 public static final String NAVBAR_LINK = "GSTNavLink";
85 /**
86 * Constant containing the asset type of the GST Alias asset.
87 */
88 public final String GST_ALIAS_TYPE = Alias.ALIAS_ASSET_TYPE_NAME;
89
90 public AbstractNavigationHelper(final ICS ics) {
91 this.ics = ics;
92 this.wraDao = new AssetApiWraCoreFieldDao(ics);
93
94 this.aliasDao = new AssetApiAliasCoreFieldDao(ics, wraDao);
95 this.assetEffectiveDate = null;
96 assetTemplate = new TemplateAssetAccess(ics);
97 }
98
99 /**
100 * Constructor with all the dependencies listed. Initializes
101 * assetEffectiveDate to null.
102 *
103 * @param ics
104 * @param assetTemplate
105 * @param wraDao
106 * @param aliasDao
107 */
108 public AbstractNavigationHelper(final ICS ics, TemplateAssetAccess assetTemplate, WraCoreFieldDao wraDao,
109 AliasCoreFieldDao aliasDao) {
110 this.ics = ics;
111 this.wraDao = wraDao;
112 this.aliasDao = aliasDao;
113 this.assetTemplate = assetTemplate;
114 this.assetEffectiveDate = null;
115 }
116
117 /**
118 * @param name the name of the Page asset
119 * @return NavNode for the Page with the name
120 */
121 public NavigationNode getSitePlanByPage(final String name) {
122 return getSitePlanByPage(1, name);
123 }
124
125 /**
126 * Retrieves the NavNode for the given Page with the provided name.
127 *
128 * @param depth the maximum depth to retrieve, -1 for no limit.
129 * @param name the name of the Page asset
130 * @return NavNode for the Page with the name
131 */
132 public NavigationNode getSitePlanByPage(final int depth, final String name) {
133 final String sitename = ics.GetVar("site");
134 if (StringUtils.isBlank(sitename)) {
135 throw new IllegalStateException(
136 "site is not a ics variable. This function needs this variable to be avaible and contain the name of the site.");
137 }
138
139 return getSitePlanByPage(depth, name, sitename);
140 }
141
142 /**
143 * Retrieves the NavNode for the given Page with the provided name.
144 *
145 * @param depth the maximum depth to retrieve, -1 for no limit.
146 * @param name the name of the Page asset
147 * @param dimensionFilter in order to translate the output.
148 * @return NavNode for the Page with the name
149 */
150 public NavigationNode getSitePlanByPage(final int depth, final String name, final DimensionFilterInstance dimensionFilter) {
151 final String sitename = ics.GetVar("site");
152 if (StringUtils.isBlank(sitename)) {
153 throw new IllegalStateException(
154 "site is not a ics variable. This function needs this variable to be aviable and contain the name of the site.");
155 }
156
157 return getSitePlanByPage(depth, name, sitename, dimensionFilter);
158 }
159
160 /**
161 * Retrieves the NavNode for the given Page with the provided name.
162 *
163 * @param depth the maximum depth to retrieve, -1 for no limit.
164 * @param name the name of the Page asset
165 * @param sitename the name of the site you want the navigation for.
166 * @return NavNode for the Page with the name
167 */
168 public NavigationNode getSitePlanByPage(final int depth, final String name, final String sitename) {
169 return getSitePlanByPage(depth, name, sitename, null);
170 }
171
172 /**
173 * Retrieves the NavNode for the given Page with the provided name.
174 *
175 * @param depth the maximum depth to retrieve, -1 for no limit.
176 * @param name the name of the Page asset
177 * @param sitename the name of the site you want the navigation for.
178 * @param dimensionFilter in order to translate the output.
179 * @return NavNode for the Page with the name
180 */
181 public NavigationNode getSitePlanByPage(final int depth, final String name, final String sitename,
182 final DimensionFilterInstance dimensionFilter) {
183
184 final SiteInfo site = assetTemplate.readSiteInfo(sitename);
185 if (site == null) {
186 throw new RuntimeException("Site with name '" + sitename + "' not found.");
187 }
188 final AssetId pageid = assetTemplate.findByName(ics, "Page", name, site.getId());
189 if (pageid == null) {
190 return null;
191 }
192 return getSitePlan(depth, pageid, dimensionFilter);
193 }
194
195 /**
196 * Retrieves the NavNode for the given Page with the provided id.
197 *
198 * The NavNode contains all the attributes necessary to create a nav bar.
199 * <p/>
200 * Links are not populated for Navigation Placeholders, but it is often very
201 * convenient to pass a navigation placeholder into this function in order
202 * to return all children under a specific placeholder.
203 * <p/>
204 * StartDate and EndDate are checked and invalid pages aren't added. If a
205 * Page asset is not valid, its children are not even examined.
206 *
207 *
208 * @param pageid the assetid of the Page asset.
209 * @return the NavNode for this page
210 */
211 public NavigationNode getSitePlan(final String pageid) {
212 return getSitePlan(new AssetIdImpl("Page", Long.parseLong(pageid)));
213 }
214
215 /**
216 * Get the NavNode for the current page with unlimited depth.
217 *
218 * @param pageid
219 * @return the NavNode associated with this pageid.
220 */
221 public NavigationNode getSitePlan(final AssetId pageid) {
222 return getSitePlan(-1, pageid, 0, null);
223 }
224
225 /**
226 * Retrieves the NavNode for the given Page with the provided id.
227 *
228 * @param depth the maximum depth to retrieve, -1 for no limit.
229 * @param pageid the AssetId for the page
230 * @return the NavNode for this page
231 */
232 public NavigationNode getSitePlan(final int depth, final AssetId pageid) {
233 return getSitePlan(depth, pageid, 0, null);
234 }
235
236 /**
237 * Retrieves the NavNode for the given Page with the provided id.
238 *
239 * @param depth the maximum depth to retrieve, -1 for no limit.
240 * @param pageid the AssetId for the page
241 * @param dimensionFilter in order to translate the output.
242 * @return the NavNode for this page
243 */
244 public NavigationNode getSitePlan(final int depth, final AssetId pageid, final DimensionFilterInstance dimensionFilter) {
245 LOG.debug("Dimension filter " + dimensionFilter + " provided for site plan lookup");
246 return getSitePlan(depth, pageid, 0, dimensionFilter);
247 }
248
249 /**
250 * Return true if the asset type is a GSTAlias asset type. May be overridden
251 * if customers are attempting to retrofit this class for alias-like
252 * functionality that is not implemented by the GSTAlias asset type.
253 *
254 * @param id asset for which a link is required
255 * @return true if the asset is an alias, false if it is a web-referenceable
256 * asset
257 */
258 protected boolean isGstAlias(final AssetId id) {
259 return GST_ALIAS_TYPE.equals(id.getType());
260 }
261
262 /**
263 * Locate the page that contains the specified Web-Referenceable Asset.
264 * <p/>
265 * A WRA is supposed to just be placed on one page (in the unnamed
266 * association block), and this method locates it. If it is not found, 0L is
267 * returned.
268 * <p/>
269 * If multiple matches are found, a warning is logged and the first one is
270 * returned.
271 *
272 * @param site_name name of the site to search within
273 * @param wraAssetId the asset id of the web-referenceable asset
274 * @return page asset ID or 0L.
275 */
276 public long findP(final String site_name, final AssetId wraAssetId) {
277 return wraDao.findP(new AssetIdWithSite(wraAssetId.getType(), wraAssetId.getId(), site_name));
278 }
279
280 }