1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
52
53
54
55
56
57
58
59 public class NavigationHelper2 extends AbstractNavigationHelper {
60
61
62
63
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
77
78
79
80
81
82
83
84 @Override
85 protected NavigationNode getSitePlan(final int depth, final AssetId pageId, final int level,
86 final DimensionFilterInstance dimensionFilter) {
87
88 LogDep.logDep(ics, pageId);
89 if (!isValidOnDate(ics, pageId, assetEffectiveDate)) {
90
91 if (LOG.isDebugEnabled()) {
92 LOG.debug("Input asset " + pageId + " is not effective on " + assetEffectiveDate);
93 }
94 return null;
95 }
96
97
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
112 } else if (NAVBAR_LINK.equals(subtype)) {
113
114 final AssetClosure closure = new NodeWraAssetClosure(node);
115 final DateFilterClosure dateFilterClosure = new DateFilterClosure(PreviewContext.getPreviewDate(ics,
116 assetEffectiveDate), closure);
117
118
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
131 final AssetClosure closure = new NodeNoneWraAssetClosure(node, "linktext");
132 final DateFilterClosure dateFilterClosure = new DateFilterClosure(PreviewContext.getPreviewDate(ics,
133 assetEffectiveDate), closure);
134
135
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
148 final List<AssetId> childrenIDs = ListPages.getChildPages(ics, pageId.getId());
149 for (final AssetId aid : childrenIDs) {
150
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;
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
208
209
210
211
212
213
214
215
216
217
218
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
230
231
232
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;
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 }