1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
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
101
102
103
104
105
106
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);
126 }
127 }
128 }