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