1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.fatwire.gst.foundation.facade.runtag.asset;
18
19 import COM.FutureTense.Interfaces.ICS;
20 import COM.FutureTense.Interfaces.IList;
21 import COM.FutureTense.Util.IterableIListWrapper;
22
23 import com.fatwire.assetapi.data.AssetId;
24 import com.fatwire.gst.foundation.IListUtils;
25 import com.fatwire.gst.foundation.facade.assetapi.AssetIdUtils;
26 import com.fatwire.gst.foundation.facade.runtag.AbstractTagRunner;
27 import com.fatwire.gst.foundation.facade.sql.IListIterable;
28 import com.fatwire.gst.foundation.facade.sql.Row;
29 import com.fatwire.gst.foundation.facade.sql.SqlHelper;
30
31
32
33
34
35
36
37
38
39
40 public class AssetList extends AbstractTagRunner {
41 public AssetList() {
42 super("ASSET.LIST");
43 }
44
45 public void setType(String type) {
46 this.set("TYPE", type);
47 }
48
49 public void setList(String list) {
50 this.set("LIST", list);
51 }
52
53 public void setOrder(String order) {
54 this.set("ORDER", order);
55 }
56
57 public void setPubid(String pubid) {
58 this.set("PUBID", pubid);
59 }
60
61 public void setExcludeVoided(boolean b) {
62 this.set("EXCLUDEVOIDED", b ? "TRUE" : "FALSE");
63 }
64
65 private int index = 1;
66
67 public void setField(String name, String value) {
68 this.set("FIELD" + index, name);
69 this.set("VALUE" + index, value);
70 index++;
71 }
72
73
74
75
76
77
78
79
80
81
82 public static boolean assetExists(ICS ics, String c, String cid) {
83 if (c == null || c.length() == 0)
84 return false;
85 if (cid == null || cid.length() == 0)
86 return false;
87 ics.RegisterList("spu-gtfa", null);
88 AssetList al = new AssetList();
89 al.setField("id", cid);
90 al.setType(c);
91 al.setExcludeVoided(false);
92 al.setList("spu-gtfa");
93 al.execute(ics);
94 IList spugtfa = ics.GetList("spu-gtfa");
95 ics.RegisterList("spu-gtfa", null);
96 return spugtfa != null && spugtfa.hasData();
97 }
98
99 public static boolean assetExistsByName(ICS ics, String c, String name) {
100 if (c == null || c.length() == 0) return false;
101
102 if (!SqlHelper.tableExists(ics, c)) return false;
103
104 if (name == null || name.length() == 0) return false;
105 ics.RegisterList("spu-gtfa", null);
106 AssetList al = new AssetList();
107 al.setField("name", name);
108 al.setType(c);
109 al.setExcludeVoided(false);
110 al.setList("spu-gtfa");
111 al.execute(ics);
112 IList spugtfa = ics.GetList("spu-gtfa");
113 ics.RegisterList("spu-gtfa", null);
114 return spugtfa != null && spugtfa.hasData();
115 }
116
117
118
119
120
121
122
123
124 public static AssetId lookupAssetId(ICS ics, String c, String name) {
125 if (c == null || c.length() == 0) throw new IllegalArgumentException("No such asset type: " + c);
126
127 if (!SqlHelper.tableExists(ics, c)) throw new IllegalArgumentException("No such asset type: " + c);
128
129 if (name == null || name.length() == 0)
130 throw new IllegalArgumentException("Cannot look up asset by name with an actual name. Type: " + c);
131 ics.RegisterList("spu-gtfa", null);
132 AssetList al = new AssetList();
133 al.setField("name", name);
134 al.setType(c);
135 al.setExcludeVoided(false);
136 al.setList("spu-gtfa");
137 al.execute(ics);
138 IList spugtfa = ics.GetList("spu-gtfa");
139 ics.RegisterList("spu-gtfa", null);
140 if (spugtfa != null && spugtfa.hasData()) {
141 for (Row listRow : new IListIterable(spugtfa)) {
142 return AssetIdUtils.createAssetId(c, listRow.getString("id"));
143 }
144 }
145 return null;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159 public static String getRequiredSingleField(ICS ics, String c, String cid, String field) {
160 ics.RegisterList("spu-gtfa", null);
161 AssetList al = new AssetList();
162 al.setField("id", cid);
163 al.setType(c);
164 al.setExcludeVoided(true);
165 al.setList("spu-gtfa");
166 al.execute(ics);
167 IList spugtfa = ics.GetList("spu-gtfa");
168 ics.RegisterList("spu-gtfa", null);
169 if (spugtfa != null && spugtfa.hasData()) {
170 if (spugtfa.numRows() > 1) {
171 throw new IllegalStateException("ASSET.LIST failed for " + c + ":" + cid + ": multiple matches found:"
172 + spugtfa.numRows());
173 }
174 spugtfa.moveTo(1);
175 String ret = IListUtils.getStringValue(spugtfa, field);
176 if (ret == null || ret.length() == 0) {
177 throw new IllegalStateException("No " + field + " found for asset " + c + ":" + cid);
178 }
179 return ret;
180 } else {
181 throw new IllegalStateException("ASSET.LIST failed for " + c + ":" + cid + ": no data found.");
182 }
183 }
184
185 public static IList listSingleAsset(ICS ics, String c, String cid) {
186 ics.RegisterList("spu-gtfa", null);
187 AssetList al = new AssetList();
188 al.setField("id", cid);
189 al.setType(c);
190 al.setExcludeVoided(true);
191 al.setList("spu-gtfa");
192 al.execute(ics);
193 IList spugtfa = ics.GetList("spu-gtfa");
194 ics.RegisterList("spu-gtfa", null);
195 if (spugtfa != null && spugtfa.hasData()) {
196 if (spugtfa.numRows() > 1) {
197 throw new IllegalStateException("ASSET.LIST failed for " + c + ":" + cid + ": multiple matches found:"
198 + spugtfa.numRows());
199 }
200 return new IterableIListWrapper(spugtfa).iterator().next();
201 } else {
202 throw new IllegalStateException("ASSET.LIST failed for " + c + ":" + cid + ": no data found.");
203 }
204 }
205
206
207
208
209
210
211
212
213 @Override
214 protected void handleError(ICS ics) {
215 if (ics.GetErrno() == -101)
216 return;
217 super.handleError(ics);
218 }
219
220 }