1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package tools.gsf.facade.assetapi;
18
19 import COM.FutureTense.Interfaces.ICS;
20 import com.fatwire.assetapi.data.AssetId;
21 import com.fatwire.cs.core.db.PreparedStmt;
22 import com.fatwire.cs.core.db.StatementParam;
23 import tools.gsf.facade.sql.Row;
24 import tools.gsf.facade.sql.SqlHelper;
25
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.Map;
29
30
31
32
33
34
35
36
37
38
39
40 public final class DirectSqlAccessTools {
41 private final ICS ics;
42
43 public DirectSqlAccessTools(ICS ics) {
44 this.ics = ics;
45 }
46
47 public boolean isFlex(AssetId id) {
48 StatementParam param = FLEX_ATTR_TYPE.newParam();
49 param.setString(0, id.getType());
50 param.setString(1, id.getType());
51 return SqlHelper.selectSingle(ics, FLEX_ATTR_TYPE, param) != null;
52 }
53
54 private static final PreparedStmt FLEX_ATTR_TYPE = new PreparedStmt(
55 "SELECT assetattr FROM FlexAssetTypes WHERE assettype = ? UNION SELECT assetattr FROM FlexGroupTypes WHERE assettype = ?",
56 Arrays.asList("FlexAssetTypes", "FlexGroupTypes"));
57
58 static {
59 FLEX_ATTR_TYPE.setElement(0, "FlexAssetTypes", "assettype");
60 FLEX_ATTR_TYPE.setElement(1, "FlexGroupTypes", "assettype");
61 }
62
63 public String getFlexAttributeType(AssetId id) {
64 StatementParam param = FLEX_ATTR_TYPE.newParam();
65 param.setString(0, id.getType());
66 param.setString(1, id.getType());
67 Row row = SqlHelper.selectSingle(ics, FLEX_ATTR_TYPE, param);
68 if (row == null) {
69 throw new IllegalArgumentException("Asset " + id + " is not a flex asset!");
70 }
71 return row.getString("assetattr");
72 }
73
74
75 public String getFlexAttributeValue(AssetId id, String attrName) {
76
77 String attrType = getFlexAttributeType(id);
78 PreparedStmt flexFields = new PreparedStmt("SELECT attr.name AS name, cmungo.stringvalue AS stringvalue "
79 + "FROM " + attrType + " attr, " + id.getType() + "_Mungo cmungo " + "WHERE cmungo.cs_ownerid = ? "
80 + "AND cmungo.cs_attrid = attr.id AND attr.name = ?", Arrays.asList(attrType, id.getType() + "_Mungo"));
81 flexFields.setElement(0, id.getType() + "_Mungo", "cs_ownerid");
82 flexFields.setElement(1, attrType, "name");
83 StatementParam param = flexFields.newParam();
84 param.setLong(0, id.getId());
85 param.setString(1, attrName);
86 Row r = SqlHelper.selectSingle(ics, flexFields, param);
87 if (r == null) {
88 return null;
89 } else {
90 return r.getString("stringvalue");
91 }
92 }
93
94 public Map<String, String> getFlexAttributeValues(AssetId id, String... attrName) {
95
96 if (attrName == null || attrName.length == 0) {
97 throw new IllegalArgumentException("attrName must not be null or zero-length array.");
98 }
99 String attrType = getFlexAttributeType(id);
100 StringBuilder sql = new StringBuilder("SELECT attr.name AS name, cmungo.stringvalue AS stringvalue FROM ")
101 .append(attrType).append(" attr, ").append(id.getType())
102 .append("_Mungo cmungo WHERE cmungo.cs_ownerid = ? AND cmungo.cs_attrid = attr.id AND attr.name IN (");
103
104 for (int num = 0; num < attrName.length; num++) {
105 if (num > 0) {
106 sql.append(",");
107 }
108 sql.append("?");
109 }
110
111 sql.append(")");
112
113 PreparedStmt flexFields = new PreparedStmt(sql.toString(), Arrays.asList(id.getType(), attrType, id.getType() + "_Mungo"));
114 flexFields.setElement(0, id.getType() + "_Mungo", "cs_ownerid");
115 for (int num = 0; num < attrName.length; num++) {
116 flexFields.setElement(num + 1, attrType, "name");
117 }
118
119 StatementParam param = flexFields.newParam();
120 param.setLong(0, id.getId());
121 for (int num = 0; num < attrName.length; num++) {
122 param.setString(num + 1, attrName[num]);
123 }
124 Map<String, String> map = new HashMap<String, String>();
125 for (Row r : SqlHelper.select(ics, flexFields, param)) {
126 map.put(r.getString("name"), r.getString("stringvalue"));
127 }
128 return map;
129 }
130 }