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.sql;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import COM.FutureTense.Interfaces.FTValList;
23  import COM.FutureTense.Interfaces.ICS;
24  import COM.FutureTense.Interfaces.IList;
25  
26  import com.fatwire.assetapi.data.AssetId;
27  import com.fatwire.gst.foundation.CSRuntimeException;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * Simple class used to help out with TreeManager commands
34   *
35   * @author Tony Field
36   * @since 2011-10-13
37   */
38  public final class TreeHelper {
39      private static final Log LOG = LogFactory.getLog(TreeHelper.class.getPackage().getName());
40  
41      private TreeHelper() {
42      }
43  
44      public static IListIterable findNode(ICS ics, String tree, AssetId assetId) {
45          return findNode(ics, tree, assetId.getType(), assetId.getId());
46      }
47  
48      public static IListIterable findNode(ICS ics, String tree, String otype, long oid) {
49          return findNode(ics, tree, otype, Long.toString(oid));
50      }
51  
52      public static IListIterable findNode(ICS ics, String tree, String otype, String oid) {
53  
54          if (ics == null) throw new IllegalArgumentException("ICS may not be null");
55          if (tree == null || tree.length() == 0) throw new IllegalArgumentException("Tree name not specified");
56          if (otype == null) throw new IllegalArgumentException("Object type may not be null");
57          if (oid == null) throw new IllegalArgumentException("Object id may not be null");
58  
59          FTValList vl = new FTValList();
60          vl.setValString("ftcmd", "findnode");
61          vl.setValString("treename", tree);
62          vl.setValString("where", "oid");
63          vl.setValString("oid", oid);
64  
65          IList nodeInfo = treeManager(ics, vl, Arrays.asList(-111));
66  
67          IListIterable result = new IListIterable(nodeInfo);
68  
69          if (LOG.isTraceEnabled()) {
70              LOG.trace("Found " + result.size() + " nodes for object " + otype + ":" + oid + " in tree " + tree);
71          }
72  
73          return result;
74      }
75  
76      public static IListIterable findParents(ICS ics, String tree, String nid) {
77          if (ics == null) throw new IllegalArgumentException("ICS may not be null");
78          if (tree == null || tree.length() == 0) throw new IllegalArgumentException("Tree name not specified");
79          if (nid == null) throw new IllegalArgumentException("Node id may not be null");
80  
81          FTValList vl = new FTValList();
82          vl.setValString("ftcmd", "getparent");
83          vl.setValString("treename", tree);
84          vl.setValString("node", nid);
85  
86          IList parentInfo = treeManager(ics, vl, Arrays.asList(-112));
87  
88          IListIterable result = new IListIterable(parentInfo);
89  
90          if (LOG.isTraceEnabled()) {
91              if (LOG.isTraceEnabled()) {
92                  LOG.trace("Found " + result.size() + " parents for node " + nid + " in tree " + tree);
93              }
94          }
95  
96          return result;
97      }
98  
99      /**
100      * Query treemanager using the commands specified, and handles all required error checking.
101      *
102      * @param ics              context
103      * @param vl               commands
104      * @param acceptableErrnos list of errnos that are okay - that won't result in an exception being thrown.
105      * @return IList exactly as returned from TreeManager.  May be empty or null.
106      * @throws CSRuntimeException if the command fails or returns an unsafe errno.
107      */
108     private static IList treeManager(ICS ics, FTValList vl, List<Integer> acceptableErrnos) {
109         String ftcmd = vl.getValString("ftcmd");
110         String treeName = vl.getValString("treename");
111 
112         if (ics.TreeManager(vl)) {
113             int errno = ics.GetErrno();
114             if (errno < 0) {
115                 if (acceptableErrnos.contains(errno)) {
116                     LOG.trace("TreeManager failed with expected errno: " + errno + " for command " + ftcmd + ": " + vl);
117                 } else {
118                     throw new CSRuntimeException("TreeManager failed unexpectedly for command " + ftcmd + ": " + vl, errno);
119                 }
120             }
121             IList result = ics.GetList(treeName);
122             ics.RegisterList(treeName, null);
123             return result;
124         } else {
125             throw new CSRuntimeException("TreeManager failed unexpectedly for command " + ftcmd + ": " + vl, -4); // unknown
126         }
127     }
128 }