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 java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import COM.FutureTense.Interfaces.ICS;
24  import COM.FutureTense.Interfaces.IList;
25  import COM.FutureTense.Util.IterableIListWrapper;
26  
27  import com.fatwire.assetapi.data.AssetId;
28  import com.fatwire.gst.foundation.CSRuntimeException;
29  import com.fatwire.gst.foundation.IListUtils;
30  import com.fatwire.gst.foundation.facade.runtag.AbstractTagRunner;
31  import com.fatwire.gst.foundation.facade.runtag.asset.AssetLoadById;
32  import com.fatwire.gst.foundation.facade.runtag.asset.GetSiteNode;
33  import com.openmarket.xcelerate.asset.AssetIdImpl;
34  
35  /**
36   * List pages in the siteplan.
37   * 
38   * <SITEPLAN.LISTPAGES NAME="thePubNode" PLACEDLIST="placedPages" LEVEL="1"/>
39   * 
40   * @author Tony Field
41   * @since Jul 14, 2009
42   */
43  public final class ListPages extends AbstractTagRunner {
44      public ListPages() {
45          super("SITEPLAN.LISTPAGES");
46      }
47  
48      public void setName(String name) {
49          set("NAME", name);
50      }
51  
52      public void setPlacedList(String placedPages) {
53          set("PLACEDLIST", placedPages);
54      }
55  
56      public void setLevel(int level) {
57          set("LEVEL", Integer.toString(level));
58      }
59  
60      /**
61       * Return the immediate children of the specified page in the site plan
62       * tree. If the page is not present in the site plan tree, an exception is
63       * thrown. If no child pages are found an empty list is returned.
64       * 
65       * @param ics ICS context
66       * @param p ID of the the page whose children will be looked up
67       * @return list of children of the input page, never null
68       */
69      public static List<AssetId> getChildPages(ICS ics, long p) {
70          final String LOADED_PAGE_NAME = "__thePage";
71          final String LOADED_SITE_PLAN_NODE = "__siteplan";
72          final String CURRENT_PAGE_NODE_ID = "__nodeId";
73          final String PLACED_LIST = "__placedList";
74  
75          try {
76              AssetLoadById assetLoad = new AssetLoadById();
77              assetLoad.setAssetId(p);
78              assetLoad.setAssetType("Page");
79              assetLoad.setName(LOADED_PAGE_NAME);
80              assetLoad.execute(ics);
81              if (ics.GetErrno() < 0) {
82                  throw new CSRuntimeException("Failed to load page identified by Page:" + p, ics.GetErrno());
83              }
84  
85              GetSiteNode getSiteNode = new GetSiteNode();
86              getSiteNode.setName(LOADED_PAGE_NAME);
87              getSiteNode.setOutput(CURRENT_PAGE_NODE_ID);
88              getSiteNode.execute(ics);
89              if (ics.GetErrno() < 0) {
90                  throw new CSRuntimeException("Could not get site node for page identified by Page:" + p, ics.GetErrno());
91              }
92  
93              SitePlanLoad sitePlanLoad = new SitePlanLoad();
94              sitePlanLoad.setName(LOADED_SITE_PLAN_NODE);
95              sitePlanLoad.setNodeid(ics.GetVar(CURRENT_PAGE_NODE_ID));
96              sitePlanLoad.execute(ics);
97              if (ics.GetErrno() < 0) {
98                  throw new CSRuntimeException("Could not load site plan tree for page identified by Page:" + p,
99                          ics.GetErrno());
100             }
101 
102             ListPages listPages = new ListPages();
103             listPages.setName(LOADED_SITE_PLAN_NODE);
104             listPages.setPlacedList(PLACED_LIST);
105             listPages.setLevel(1);
106             listPages.execute(ics);
107             if (ics.GetErrno() < 0) {
108                 throw new CSRuntimeException("Could not get list child pages for Page:" + p, ics.GetErrno());
109             }
110 
111             // otype/oid are what we care about
112             IList placedList = ics.GetList(PLACED_LIST);
113             if (ics.GetErrno() < 0 || placedList == null || !placedList.hasData()) {
114                 ics.ClearErrno();
115                 return Collections.emptyList();
116             }
117             List<AssetId> list = new ArrayList<AssetId>();
118             for (IList row : new IterableIListWrapper(placedList)) {
119                 AssetId id = new AssetIdImpl(IListUtils.getStringValue(row, "AssetType"), IListUtils.getLongValue(row,
120                         "Id"));
121                 list.add(id);
122             }
123             return list;
124         } finally {
125             ics.SetObj(LOADED_PAGE_NAME, null); // just to be safe
126             ics.RemoveVar(CURRENT_PAGE_NODE_ID);
127             ics.SetObj(LOADED_SITE_PLAN_NODE, null);
128             ics.RegisterList(PLACED_LIST, null);
129         }
130     }
131 }