1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation.facade.runtag.siteplan;
18
19 import COM.FutureTense.Interfaces.ICS;
20 import COM.FutureTense.Interfaces.IList;
21 import COM.FutureTense.Util.ftErrors;
22
23 import com.fatwire.assetapi.data.AssetId;
24 import com.fatwire.gst.foundation.CSRuntimeException;
25 import com.fatwire.gst.foundation.IListUtils;
26 import com.fatwire.gst.foundation.facade.runtag.AbstractTagRunner;
27 import com.fatwire.gst.foundation.facade.runtag.asset.AssetLoadById;
28 import com.fatwire.gst.foundation.facade.runtag.asset.GetSiteNode;
29 import com.openmarket.xcelerate.asset.AssetIdImpl;
30
31
32
33
34
35
36
37 public class NodePath extends AbstractTagRunner {
38 public NodePath() {
39 super("SITEPLAN.NODEPATH");
40 }
41
42 public void setName(String name) {
43 set("NAME", name);
44 }
45
46 public void setList(String listName) {
47 set("LIST", listName);
48 }
49
50
51
52
53
54
55
56
57
58
59 public static IList getNodePathForPage(ICS ics, String p) {
60 return getNodePathForPage(ics, Long.valueOf(p));
61 }
62
63
64
65
66
67
68
69
70
71
72 public static IList getNodePathForPage(ICS ics, AssetId page) {
73 if ("Page".equals(page.getType())) {
74 return getNodePathForPage(ics, page.getId());
75 } else {
76 throw new CSRuntimeException("Input asset ID is not a page: " + page, ftErrors.badparams);
77 }
78 }
79
80
81
82
83
84
85
86
87
88
89 public static AssetId getParentPage(ICS ics, long p) {
90 if (p < 1L) {
91 throw new IllegalArgumentException("Invalid page id specified: " + p);
92 }
93 if (ics == null) {
94 throw new IllegalArgumentException("Null ICS not allowed");
95 }
96 final String LOADED_PAGE_NAME = "__thePage";
97 final String LOADED_SITE_PLAN_NODE = "__siteplan";
98 final String CURRENT_PAGE_NODE_ID = "__nodeId";
99 final String ANCESTOR_LIST = "__ancestorList";
100
101 try {
102 AssetLoadById assetLoad = new AssetLoadById();
103 assetLoad.setAssetId(p);
104 assetLoad.setAssetType("Page");
105 assetLoad.setName(LOADED_PAGE_NAME);
106 assetLoad.execute(ics);
107 if (ics.GetErrno() < 0) {
108 throw new CSRuntimeException("Failed to load page identified by Page:" + p, ics.GetErrno());
109 }
110
111 GetSiteNode getSiteNode = new GetSiteNode();
112 getSiteNode.setName(LOADED_PAGE_NAME);
113 getSiteNode.setOutput(CURRENT_PAGE_NODE_ID);
114 getSiteNode.execute(ics);
115 if (ics.GetErrno() < 0) {
116 throw new CSRuntimeException("Could not get site node for page identified by Page:" + p, ics.GetErrno());
117 }
118
119 SitePlanLoad sitePlanLoad = new SitePlanLoad();
120 sitePlanLoad.setName(LOADED_SITE_PLAN_NODE);
121 sitePlanLoad.setNodeid(ics.GetVar(CURRENT_PAGE_NODE_ID));
122 sitePlanLoad.execute(ics);
123 if (ics.GetErrno() < 0) {
124 throw new CSRuntimeException("Could not load site plan tree for page identified by Page:" + p,
125 ics.GetErrno());
126 }
127
128 NodePath nodePath = new NodePath();
129 nodePath.setName(LOADED_SITE_PLAN_NODE);
130 nodePath.setList(ANCESTOR_LIST);
131 nodePath.execute(ics);
132 if (ics.GetErrno() < 0) {
133 throw new CSRuntimeException("Could not get node path for page identified by Page:" + p, ics.GetErrno());
134 }
135
136
137 IList ancestorList = ics.GetList(ANCESTOR_LIST);
138 if (ancestorList == null || !ancestorList.hasData()) {
139 throw new CSRuntimeException("No ancestors found in site plan tree for page identified by Page:" + p,
140 ics.GetErrno());
141 }
142 ancestorList.moveTo(1);
143
144 return new AssetIdImpl(IListUtils.getStringValue(ancestorList, "otype"), IListUtils.getLongValue(
145 ancestorList, "oid"));
146
147 } finally {
148 ics.SetObj(LOADED_PAGE_NAME, null);
149 ics.RemoveVar(CURRENT_PAGE_NODE_ID);
150 ics.SetObj(LOADED_SITE_PLAN_NODE, null);
151 ics.RegisterList(ANCESTOR_LIST, null);
152 }
153 }
154
155
156
157
158
159
160
161
162
163
164 public static IList getNodePathForPage(ICS ics, long p) {
165 final String LOADED_PAGE_NAME = "__thePage";
166 final String LOADED_SITE_PLAN_TREE = "__siteplan";
167 final String CURRENT_PAGE_NODE = "__nodeId";
168 final String ANCESTOR_LIST = "__ancestorList";
169
170 try {
171 AssetLoadById assetLoad = new AssetLoadById();
172 assetLoad.setAssetId(p);
173 assetLoad.setAssetType("Page");
174 assetLoad.setName(LOADED_PAGE_NAME);
175 assetLoad.execute(ics);
176 if (ics.GetErrno() < 0) {
177 throw new CSRuntimeException("Failed to load page identified by Page:" + p, ics.GetErrno());
178 }
179
180 GetSiteNode getSiteNode = new GetSiteNode();
181 getSiteNode.setName(LOADED_PAGE_NAME);
182 getSiteNode.setOutput(CURRENT_PAGE_NODE);
183 getSiteNode.execute(ics);
184 if (ics.GetErrno() < 0) {
185 throw new CSRuntimeException("Could not get site node for page identified by Page:" + p, ics.GetErrno());
186 }
187
188 SitePlanLoad sitePlanLoad = new SitePlanLoad();
189 sitePlanLoad.setName(LOADED_SITE_PLAN_TREE);
190 sitePlanLoad.setNodeid(ics.GetVar(CURRENT_PAGE_NODE));
191 sitePlanLoad.execute(ics);
192 if (ics.GetErrno() < 0) {
193 throw new CSRuntimeException("Could not load site plan tree for page identified by Page:" + p,
194 ics.GetErrno());
195 }
196
197 NodePath nodePath = new NodePath();
198 nodePath.setName(LOADED_SITE_PLAN_TREE);
199 nodePath.setList(ANCESTOR_LIST);
200 nodePath.execute(ics);
201 if (ics.GetErrno() < 0) {
202 throw new CSRuntimeException("Could not get node path for page identified by Page:" + p, ics.GetErrno());
203 }
204
205
206 IList ancestorList = ics.GetList(ANCESTOR_LIST);
207 if (ancestorList == null || !ancestorList.hasData()) {
208 throw new CSRuntimeException("No ancestors found in site plan tree for page identified by Page:" + p,
209 ics.GetErrno());
210 }
211
212 return ancestorList;
213
214 } finally {
215 ics.SetObj(LOADED_PAGE_NAME, null);
216 ics.RemoveVar(CURRENT_PAGE_NODE);
217 ics.SetObj(LOADED_SITE_PLAN_TREE, null);
218 ics.RegisterList(ANCESTOR_LIST, null);
219 }
220 }
221 }