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