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