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