View Javadoc
1   /*
2    * Copyright 2008 FatWire 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  
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   * {@code <siteplan:nodepath name="node" list="list name"/>}
32   *
33   * @author Tony Field
34   * @since Sep 28, 2008
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       * Compute and return the nodepath for the specified input asset. The
51       * returned list is in the same order and format as that returned by the
52       * siteplan.nodepath tag.
53       *
54       * @param ics context
55       * @param p   page asset ID
56       * @return nodepath IList. Includes publication at root.
57       */
58      public static IList getNodePathForPage(ICS ics, String p) {
59          return getNodePathForPage(ics, Long.valueOf(p));
60      }
61  
62      /**
63       * Compute and return the nodepath for the specified input asset. The
64       * returned list is in the same order and format as that returned by the
65       * siteplan.nodepath tag.
66       *
67       * @param ics  context
68       * @param page page asset ID
69       * @return nodepath IList. Includes publication at root.
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       * Get the parent page in the site plan tree of the specified page. If the
81       * page cannot be located in the site plan tree, an exception is thrown. If
82       * the page has no parents in the site plan tree, an exception is thrown.
83       *
84       * @param ics ICS context
85       * @param p   ID of page asset to inspect
86       * @return ID of parent page, never null.
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             // otype/oid are what we care about
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); // just to be safe
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      * Compute and return the nodepath for the specified input asset. The
156      * returned list is in the same order and format as that returned by the
157      * siteplan.nodepath tag.
158      *
159      * @param ics context
160      * @param p   page asset ID
161      * @return nodepath IList. Includes publication at root.
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             // otype/oid are what we care about
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); // just to be safe
215             ics.RemoveVar(CURRENT_PAGE_NODE);
216             ics.SetObj(LOADED_SITE_PLAN_TREE, null);
217             ics.RegisterList(ANCESTOR_LIST, null);
218         }
219     }
220 }