View Javadoc
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 static com.fatwire.gst.foundation.facade.runtag.asset.FilterAssetsByDate.isValidOnDate;
19  
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  import COM.FutureTense.Interfaces.ICS;
27  import COM.FutureTense.Interfaces.Utilities;
28  
29  import com.fatwire.assetapi.data.AssetData;
30  import com.fatwire.assetapi.data.AssetId;
31  import com.fatwire.gst.foundation.facade.assetapi.AssetClosure;
32  import com.fatwire.gst.foundation.facade.assetapi.AssetDataUtils;
33  import com.fatwire.gst.foundation.facade.assetapi.AttributeDataUtils;
34  import com.fatwire.gst.foundation.facade.assetapi.asset.DateFilterClosure;
35  import com.fatwire.gst.foundation.facade.assetapi.asset.PreviewContext;
36  import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAsset;
37  import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAssetAccess;
38  import com.fatwire.gst.foundation.facade.runtag.render.LogDep;
39  import com.fatwire.gst.foundation.facade.runtag.siteplan.ListPages;
40  import com.fatwire.gst.foundation.navigation.NavigationNode;
41  import com.fatwire.gst.foundation.navigation.NavigationService;
42  import com.fatwire.gst.foundation.wra.Alias;
43  import com.fatwire.gst.foundation.wra.AliasCoreFieldDao;
44  import com.fatwire.gst.foundation.wra.VanityAsset;
45  import com.fatwire.gst.foundation.wra.WebReferenceableAsset;
46  import com.fatwire.gst.foundation.wra.WraCoreFieldDao;
47  import com.fatwire.gst.foundation.wra.WraUriBuilder;
48  import com.fatwire.mda.DimensionFilterInstance;
49  
50  /**
51   * Used to retrieve the Navigation Bar data. See the description of
52   * {@link #getSitePlan(String)} for more details.
53   * 
54   * 
55   * @author Dolf Dijkstra
56   * @since Jun 8, 2012
57   * @deprecated replaced with {@link NavigationService}
58   */
59  public class NavigationHelper2 extends AbstractNavigationHelper {
60      /**
61       * Constructor. Initializes assetEffectiveDate to null.
62       * 
63       * @param ics object
64       */
65      public NavigationHelper2(final ICS ics) {
66          super(ics);
67  
68      }
69  
70      public NavigationHelper2(ICS ics, TemplateAssetAccess assetTemplate, WraCoreFieldDao wraDao,
71              AliasCoreFieldDao aliasDao) {
72          super(ics, assetTemplate, wraDao, aliasDao);
73      }
74  
75      /**
76       * Called from public {@link #getSitePlan(int, AssetId)}. See that
77       * function's description for details
78       * 
79       * @param depth the depth of the tree to retrieve, -1 for unlimited depth.
80       * @param pageId id of the page asset
81       * @param level starting level number when traversing the site plan tree
82       * @return NavNode of the site plan tree
83       */
84      @Override
85      protected NavigationNode getSitePlan(final int depth, final AssetId pageId, final int level,
86              final DimensionFilterInstance dimensionFilter) {
87          // check the start/end date of the page asset
88          LogDep.logDep(ics, pageId);
89          if (!isValidOnDate(ics, pageId, assetEffectiveDate)) {
90              // the input object is not valid. Abort
91              if (LOG.isDebugEnabled()) {
92                  LOG.debug("Input asset " + pageId + " is not effective on " + assetEffectiveDate);
93              }
94              return null;
95          }
96  
97          // determine if it's a wra, a placeholder or an alias
98  
99          final AssetData pageData = AssetDataUtils.getAssetData(ics, pageId, "subtype", "name");
100         final String subtype = AttributeDataUtils.asString(pageData.getAttributeData("subtype"));
101         final String name = AttributeDataUtils.asString(pageData.getAttributeData("name"));
102         final boolean isNavigationPlaceholder = NAVBAR_NAME.equals(subtype);
103         final NavigationNode node = new NavigationNode();
104 
105         node.setPage(pageId);
106         node.setLevel(level);
107         node.setPagesubtype(subtype);
108         node.setPagename(name);
109 
110         if (isNavigationPlaceholder) {
111             // no link if it's just a placeholder
112         } else if (NAVBAR_LINK.equals(subtype)) {
113             // add the WRA asset to the node
114             final AssetClosure closure = new NodeWraAssetClosure(node);
115             final DateFilterClosure dateFilterClosure = new DateFilterClosure(PreviewContext.getPreviewDate(ics,
116                     assetEffectiveDate), closure);
117 
118             // retrieve the unnamed association(s) based on date filter
119             if (dimensionFilter == null) {
120                 assetTemplate.readAssociatedAssets(pageId, "-", dateFilterClosure);
121             } else {
122 
123                 final Collection<AssetId> associatedWraList = assetTemplate.readAssociatedAssetIds(pageId, "-");
124                 for (final AssetId child : dimensionFilter.filterAssets(associatedWraList)) {
125                     assetTemplate.readAsset(child, dateFilterClosure);
126                 }
127             }
128 
129         } else {
130             // not a GSTNavLink, probably 11g page
131             final AssetClosure closure = new NodeNoneWraAssetClosure(node, "linktext");
132             final DateFilterClosure dateFilterClosure = new DateFilterClosure(PreviewContext.getPreviewDate(ics,
133                     assetEffectiveDate), closure);
134 
135             // retrieve the unnamed association(s) based on date filter
136             if (dimensionFilter == null) {
137                 assetTemplate.readAsset(pageId, dateFilterClosure);
138             } else {
139                 for (final AssetId child : dimensionFilter.filterAssets(Collections.singleton(pageId))) {
140                     assetTemplate.readAsset(child, dateFilterClosure);
141                 }
142             }
143 
144         }
145 
146         if (depth < 0 || depth > level) {
147             // get the children in the Site Plan
148             final List<AssetId> childrenIDs = ListPages.getChildPages(ics, pageId.getId());
149             for (final AssetId aid : childrenIDs) {
150                 // note recursing here
151                 final NavigationNode kidInfo = getSitePlan(depth, aid, level + 1, dimensionFilter);
152                 if (kidInfo != null && kidInfo.getPage() != null) {
153                     node.addChild(kidInfo);
154                 }
155             }
156         }
157         return node;
158     }
159 
160     class NodeWraAssetClosure implements AssetClosure {
161 
162         private final NavigationNode node;
163 
164         public NodeWraAssetClosure(final NavigationNode node) {
165             this.node = node;
166         }
167 
168         @Override
169         public boolean work(final AssetData asset) {
170             final AssetId id = asset.getAssetId();
171             node.setId(id);
172             if (isGstAlias(id)) {
173                 decorateAsAlias(id, node);
174             } else {
175                 decorateAsWra(id, node);
176             }
177             return false; // needs to return only one node
178         }
179 
180         protected void decorateAsAlias(final AssetId id, final NavigationNode node) {
181             final Alias alias = aliasDao.getAlias(id);
182             final String url = getUrlForAlias(alias);
183             final String linktext = alias.getLinkText();
184             if (url != null) {
185                 node.setUrl(url);
186             }
187             if (linktext != null) {
188                 node.setLinktext(linktext);
189             }
190 
191         }
192 
193         protected void decorateAsWra(final AssetId id, final NavigationNode node) {
194 
195             final WebReferenceableAsset wra = wraDao.getWra(id);
196             final String url = getUrlForWra(wra);
197             final String linktext = wra.getLinkText();
198             if (url != null) {
199                 node.setUrl(url);
200             }
201             if (linktext != null) {
202                 node.setLinktext(linktext);
203             }
204         }
205 
206         /**
207          * Get the URL for the alias.
208          * 
209          * For external links, the targeturl attribute is rendered.
210          * 
211          * For Aliases that refer to another WRA, the alias is allowed to
212          * override any WRA fields. For instance, the path, and the template can
213          * be overridden by an alias for a WRA (though the template in the Alias
214          * had better be typeless, or a template of the same name must exist in
215          * the WRA's asset type or there will be a problem).
216          * 
217          * @param alias Alias bean, which of course is also a WRA.
218          * @return url
219          */
220         protected String getUrlForAlias(final Alias alias) {
221             if (alias.getTargetUrl() != null) {
222                 return alias.getTargetUrl();
223             } else {
224                 return getUrlForWra(alias);
225             }
226         }
227 
228         /**
229          * Get the URL to use for the web-referenceable asset.
230          * 
231          * @param wra WebReferenceableAsset bean
232          * @return url
233          */
234         protected String getUrlForWra(final VanityAsset wra) {
235             if (wra.getTemplate() == null || wra.getTemplate().length() == 0) {
236                 LOG.warn("Asset " + wra + " does not have a valid template set.");
237                 return null;
238             }
239             String wrapper = ics.GetProperty("com.fatwire.gst.foundation.url.wrapathassembler.dispatcher",
240                     "ServletRequest.properties", true);
241             if (!Utilities.goodString(wrapper)) {
242                 wrapper = "GST/Dispatcher";
243             }
244             return new WraUriBuilder(wra, wrapper).toURI(ics);
245 
246         }
247 
248     }
249 
250     class NodeNoneWraAssetClosure implements AssetClosure {
251 
252         private final NavigationNode node;
253         private final String linkTextAttribute;
254 
255         public NodeNoneWraAssetClosure(final NavigationNode node, String linkTextAttribute) {
256             this.node = node;
257             this.linkTextAttribute = linkTextAttribute;
258 
259         }
260 
261         @Override
262         public boolean work(final AssetData asset) {
263             final AssetId id = asset.getAssetId();
264             node.setId(id);
265             decorateAsNoneWra(id, node);
266             return false; // needs to return only one node
267         }
268 
269         protected void decorateAsNoneWra(final AssetId id, final NavigationNode node) {
270             TemplateAsset asset = assetTemplate.read(id);
271             final String url = getUrl(asset);
272 
273             final String linktext = asset.asString(linkTextAttribute);
274 
275             if (url != null) {
276                 node.setUrl(url);
277             }
278             if (linktext != null) {
279                 node.setLinktext(linktext);
280             }
281         }
282 
283         private String getUrl(TemplateAsset asset) {
284             String template = asset.asString("template");
285             if (StringUtils.isNotBlank(template)) {
286                 LOG.warn("Asset " + asset.getAssetId() + " does not have a valid template set.");
287                 return null;
288             }
289             String wrapper = ics.GetProperty("com.fatwire.gst.foundation.url.wrapathassembler.dispatcher",
290                     "ServletRequest.properties", true);
291             if (!Utilities.goodString(wrapper)) {
292                 wrapper = "GST/Dispatcher";
293             }
294             return new WraUriBuilder(asset.getAssetId()).wrapper(wrapper).template(template).toURI(ics);
295 
296         }
297 
298     }
299 
300 }