1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
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
113
114
115
116
117
118
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);
138 }
139 }
140 }