View Javadoc
1   /*
2    * Copyright 2012 Oracle 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.navigation.support;
17  
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  import org.apache.commons.lang.StringEscapeUtils;
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import COM.FutureTense.Interfaces.ICS;
29  import COM.FutureTense.Interfaces.Utilities;
30  
31  import com.fatwire.assetapi.data.AssetId;
32  import com.fatwire.cs.core.db.PreparedStmt;
33  import com.fatwire.cs.core.db.StatementParam;
34  import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAsset;
35  import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAssetAccess;
36  import com.fatwire.gst.foundation.facade.runtag.render.LogDep;
37  import com.fatwire.gst.foundation.facade.sql.IListIterable;
38  import com.fatwire.gst.foundation.facade.sql.Row;
39  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
40  import com.fatwire.gst.foundation.facade.uri.TemplateUriBuilder;
41  import com.fatwire.gst.foundation.navigation.NavigationNode;
42  import com.fatwire.gst.foundation.navigation.NavigationService;
43  import com.fatwire.gst.foundation.wra.WraUriBuilder;
44  
45  /**
46   * To retrieve the Navigation Bar data.
47   * 
48   * 
49   * @author Dolf Dijkstra
50   * @since August 31,2012
51   */
52  public class SimpleNavigationHelper extends AbstractNavigationService implements NavigationService {
53  
54      protected static final Log LOG = LogFactory.getLog(SimpleNavigationHelper.class);
55  
56      private static final String CHILD_SQL = "SELECT otype,oid,nrank,nid from SitePlanTree where nparentid=? and ncode='Placed' order by nrank";
57      private static final PreparedStmt CHILD_STMT = new PreparedStmt(CHILD_SQL, Arrays.asList("SitePlanTree"));
58  
59      static {
60  
61          CHILD_STMT.setElement(0, "SitePlanTree", "nparentid");
62  
63      }
64  
65      /**
66       * Constructor.
67       * 
68       * @param ics object
69       */
70      public SimpleNavigationHelper(final ICS ics) {
71          super(ics);
72  
73      }
74  
75      /**
76       * 
77       * 
78       * @param ics
79       * @param assetTemplate
80       */
81      public SimpleNavigationHelper(final ICS ics, final TemplateAssetAccess assetTemplate) {
82          super(ics, assetTemplate);
83      }
84  
85      /**
86       * @param ics
87       * @param assetTemplate
88       * @param linkLabelAttribute
89       * @param pathAttribute
90       */
91      public SimpleNavigationHelper(final ICS ics, final TemplateAssetAccess assetTemplate,
92              final String linkLabelAttribute, final String pathAttribute) {
93          super(ics, assetTemplate, linkLabelAttribute, pathAttribute);
94  
95      }
96  
97      @Override
98      protected NavigationNode getNode(final Row row, final int level, final int depth, final String linkAttribute) {
99          final long nid = row.getLong("nid");
100         final long pageId = row.getLong("oid");
101 
102         final AssetId pid = assetTemplate.createAssetId(row.getString("otype"), pageId);
103         LogDep.logDep(ics, pid);
104         final TemplateAsset asset = assetTemplate
105                 .read(pid, "name", "subtype", "template", pathAttribute, linkAttribute);
106 
107         final NavigationNode node = new NavigationNode();
108 
109         final String p = ics.GetVar("p");
110         if (StringUtils.isNotBlank(p)) {
111             if (pid.getId() == Long.parseLong(p)) {
112                 node.setActive(true);
113             }
114         }
115 
116         node.setPage(pid);
117         node.setLevel(level);
118         node.setPagesubtype(asset.getSubtype());
119         node.setPagename(asset.asString("name"));
120         node.setId(asset.getAssetId());
121         final String url = getUrl(asset);
122 
123         if (url != null) {
124             node.setUrl(url); // escape by default.
125         }
126 
127         final String linktext = asset.asString(linkAttribute);
128 
129         if (linktext != null) {
130             node.setLinktext(linktext);
131         } else {
132             node.setLinktext(asset.asString("name"));
133         }
134         if (depth < 0 || depth > level) {
135             // get the children in the Site Plan, note recursing here
136             final Collection<NavigationNode> children = getNodeChildren(nid, level + 1, depth, linkAttribute);
137             for (final NavigationNode kid : children) {
138                 if (kid != null && kid.getPage() != null) {
139                     node.addChild(kid);
140                 }
141             }
142         }
143         return node;
144     }
145 
146     @Override
147     protected Collection<NavigationNode> getNodeChildren(final long nodeId, final int level, final int depth,
148             final String linkAttribute) {
149         final StatementParam param = CHILD_STMT.newParam();
150         param.setLong(0, nodeId);
151 
152         final IListIterable root = SqlHelper.select(ics, CHILD_STMT, param);
153         final List<NavigationNode> collection = new LinkedList<NavigationNode>();
154         for (final Row row : root) {
155             final NavigationNode node = getNode(row, level, depth, linkAttribute);
156             if (node != null) {
157                 collection.add(node);
158             }
159 
160         }
161         return collection;
162     }
163 
164     /**
165      * Builds up a URI for this asset, using the pathAttribute and the template
166      * field of the asset
167      * 
168      * @param asset
169      * @return the uri, xml escaped
170      */
171     protected String getUrl(final TemplateAsset asset) {
172         final String template = asset.asString("template");
173         final String path = asset.asString(pathAttribute);
174         if (StringUtils.isBlank(template)) {
175             LOG.debug("Asset " + asset.getAssetId() + " does not have a valid template set.");
176             return null;
177         }
178         if (StringUtils.isBlank(path)) {
179             LOG.debug("Asset " + asset.getAssetId()
180                     + " does not have a valid path set. Defaulting to a non Vanity Url.");
181             return new TemplateUriBuilder(asset.getAssetId(), template).toURI(ics);
182         }
183 
184         String wrapper = ics.GetProperty("com.fatwire.gst.foundation.url.wrapathassembler.dispatcher",
185                 "ServletRequest.properties", true);
186         if (!Utilities.goodString(wrapper)) {
187             wrapper = "GST/Dispatcher";
188         }
189         final String uri = new WraUriBuilder(asset.getAssetId()).wrapper(wrapper).template(template).toURI(ics);
190         return StringEscapeUtils.escapeXml(uri);
191     }
192 
193 }