1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation.navigation.support;
18
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.Collections;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import COM.FutureTense.Interfaces.ICS;
28
29 import com.fatwire.assetapi.query.Query;
30 import com.fatwire.cs.core.db.PreparedStmt;
31 import com.fatwire.cs.core.db.StatementParam;
32 import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAsset;
33 import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAssetAccess;
34 import com.fatwire.gst.foundation.facade.sql.Row;
35 import com.fatwire.gst.foundation.facade.sql.SqlHelper;
36 import com.fatwire.gst.foundation.navigation.NavigationNode;
37 import com.fatwire.gst.foundation.navigation.NavigationService;
38
39 public abstract class AbstractNavigationService implements NavigationService {
40 private static final Log LOG = LogFactory.getLog(AbstractNavigationService.class);
41
42 private static final String NODE_SQL = "SELECT nid,oid,otype FROM SitePlanTree WHERE otype='Publication' AND exists (SELECT 1 FROM Publication WHERE name=? AND id=SitePlanTree.oid)";
43
44 private static final PreparedStmt NODE_STMT = new PreparedStmt(NODE_SQL, Arrays.asList("SitePlanTree",
45 "Publication"));
46
47 private static final String NAME_SQL = "SELECT nid,oid,otype FROM SitePlanTree WHERE EXISTS( SELECT 1 FROM Page p ,AssetPublication ap , Publication pub WHERE p.name=? AND pub.name=? AND ap.assetid=p.id AND pub.id = ap.pubid AND SitePlanTree.oid = p.id) AND ncode='Placed' ORDER BY nrank";
48
49 private static final PreparedStmt NAME_STMT = new PreparedStmt(NAME_SQL, Arrays.asList("SitePlanTree", "Page",
50 "AssetPublication", "Publication"));
51
52 private static final String ID_SQL = "SELECT nid,oid,otype FROM SitePlanTree WHERE SitePlanTree.oid = ? AND ncode='Placed' ORDER BY nrank";
53
54 private static final PreparedStmt ID_STMT = new PreparedStmt(ID_SQL, Arrays.asList("SitePlanTree"));
55
56 static {
57 NODE_STMT.setElement(0, "Publication", "name");
58
59 NAME_STMT.setElement(0, "Page", "name");
60 NAME_STMT.setElement(1, "Publication", "name");
61
62 ID_STMT.setElement(0, "SitePlanTree", "oid");
63
64 }
65
66 protected final ICS ics;
67
68 protected final TemplateAssetAccess assetTemplate;
69 protected String linkLabelAttribute = "linktext";
70 protected String pathAttribute = "path";
71
72 protected AbstractNavigationService(ICS ics) {
73 this(ics, new TemplateAssetAccess(ics));
74 }
75
76 protected AbstractNavigationService(ICS ics, TemplateAssetAccess assetTemplate) {
77 super();
78 this.ics = ics;
79 this.assetTemplate = assetTemplate;
80 }
81
82
83
84
85
86
87
88
89
90 protected AbstractNavigationService(ICS ics, TemplateAssetAccess assetTemplate, String linkLabelAttribute,
91 String pathAttribute) {
92 this.ics = ics;
93 this.assetTemplate = assetTemplate;
94 if (StringUtils.isBlank(linkLabelAttribute))
95 throw new IllegalArgumentException("linkLabelAttribute cannot be blank");
96 if (StringUtils.isBlank(pathAttribute))
97 throw new IllegalArgumentException("pathAttribute cannot be blank");
98
99 this.pathAttribute = pathAttribute;
100 this.linkLabelAttribute = linkLabelAttribute;
101 }
102
103
104
105
106
107 public Collection<NavigationNode> getRootNodesForSite(String site) {
108
109 return getRootNodesForSite(site, -1);
110
111 }
112
113 @Override
114 public Collection<NavigationNode> getRootNodesForSite(int depth) {
115
116 return getRootNodesForSite(ics.GetVar("site"));
117 }
118
119 @Override
120 public Collection<NavigationNode> getRootNodesForSite(String site, int depth) {
121
122 return getRootNodesForSite(site, depth, linkLabelAttribute);
123 }
124
125 @Override
126 public NavigationNode getNodeByName(String site, String pagename, int depth) {
127 return getNodeByName(site, pagename, depth, this.linkLabelAttribute);
128 }
129
130 @Override
131 public NavigationNode getNodeByName(String pagename, int depth, String linkAttribute) {
132 return getNodeByName(ics.GetVar("site"), pagename, depth, this.linkLabelAttribute);
133 }
134
135 @Override
136 public NavigationNode getNodeByName(String pagename, int depth) {
137
138 return getNodeByName(ics.GetVar("site"), pagename, depth);
139 }
140
141
142
143
144
145
146
147 @Override
148 public Collection<NavigationNode> getRootNodesForSite(String site, int depth, String linkAttribute) {
149 if (StringUtils.isBlank(site))
150 throw new IllegalArgumentException("site cannot be blank");
151 if (StringUtils.isBlank(linkAttribute))
152 throw new IllegalArgumentException("linkAttribute cannot be blank");
153 StatementParam param = NODE_STMT.newParam();
154 param.setString(0, site);
155 Row root = SqlHelper.selectSingle(ics, NODE_STMT, param);
156 if (root != null) {
157 Long nid = root.getLong("nid");
158 return getNodeChildren(nid, 0, depth, linkAttribute);
159 } else {
160 LOG.debug("No root SitePlanTree nodes found for site " + site);
161 }
162 return Collections.emptyList();
163
164 }
165
166 @Override
167 public NavigationNode getNodeByName(String site, String pagename, int depth, String linkAttribute) {
168 if (StringUtils.isBlank(site))
169 throw new IllegalArgumentException("site cannot be blank");
170 if (StringUtils.isBlank(pagename))
171 throw new IllegalArgumentException("pagename cannot be blank");
172 if (StringUtils.isBlank(linkAttribute))
173 throw new IllegalArgumentException("linkAttribute cannot be blank");
174
175 StatementParam param = NAME_STMT.newParam();
176 param.setString(0, pagename);
177 param.setString(1, site);
178
179 Row root = SqlHelper.selectSingle(ics, NAME_STMT, param);
180 if (root != null) {
181 final NavigationNode node = getNode(root, 0, depth, linkAttribute);
182 return node;
183 } else {
184 LOG.debug("No SitePlanTree nodes found for Page " + pagename + " in site " + site);
185 }
186 return null;
187
188 }
189
190 @Override
191 public NavigationNode getNodeByQuery(Query query, int depth, String linkAttribute) {
192 Iterable<TemplateAsset> assets = assetTemplate.query(query);
193 TemplateAsset asset;
194 if (assets != null)
195 asset = assets.iterator().next();
196 else
197 return null;
198 StatementParam param = ID_STMT.newParam();
199 param.setLong(0, asset.getAssetId().getId());
200
201 Row root = SqlHelper.selectSingle(ics, ID_STMT, param);
202 if (root != null) {
203 final NavigationNode node = getNode(root, 0, depth, linkAttribute);
204 return node;
205 } else {
206 LOG.debug("No SitePlanTree nodes found for Query " + query.toString());
207 }
208 return null;
209 }
210
211
212
213
214
215
216
217
218
219
220
221 protected abstract Collection<NavigationNode> getNodeChildren(long nodeId, int level, int depth,
222 String linkAttribute);
223
224
225
226
227
228
229
230
231 protected abstract NavigationNode getNode(Row row, int level, int depth, String linkAttribute);
232
233
234
235
236 public String getLinkLabelAttribute() {
237 return linkLabelAttribute;
238 }
239
240
241
242
243 public void setLinkLabelAttribute(String linkLabelAttribute) {
244 this.linkLabelAttribute = linkLabelAttribute;
245 }
246
247
248
249
250 public String getPathAttribute() {
251 return pathAttribute;
252 }
253
254
255
256
257 public void setPathAttribute(String pathAttribute) {
258 this.pathAttribute = pathAttribute;
259 }
260
261 }