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 com.fatwire.gst.foundation.facade.assetapi;
18  
19  import java.util.Arrays;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import COM.FutureTense.Interfaces.ICS;
24  
25  import com.fatwire.assetapi.data.AssetId;
26  import com.fatwire.cs.core.db.PreparedStmt;
27  import com.fatwire.cs.core.db.StatementParam;
28  import com.fatwire.gst.foundation.facade.sql.Row;
29  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
30  
31  /**
32   * Backdoor asset API utility that assists with retrieving asset data without
33   * using legitimate APIs. Warning: Using this class will bypass security,
34   * revision tracking, approval, and compositional dependency management
35   * subsystems and should only be used with extreme caution.
36   * 
37   * @author Tony Field 
38   * @author Dolf Dijkstra
39  
40   * @since 2011-05-07
41   */
42  public final class DirectSqlAccessTools {
43      private final ICS ics;
44  
45      public DirectSqlAccessTools(ICS ics) {
46          this.ics = ics;
47      }
48  
49      public boolean isFlex(AssetId id) {
50          StatementParam param = FLEX_ATTR_TYPE.newParam();
51          param.setString(0, id.getType());
52          param.setString(1, id.getType());
53          return SqlHelper.selectSingle(ics, FLEX_ATTR_TYPE, param) != null;
54      }
55  
56      private static final PreparedStmt FLEX_ATTR_TYPE = new PreparedStmt(
57              "SELECT assetattr FROM FlexAssetTypes WHERE assettype = ? UNION SELECT assetattr FROM FlexGroupTypes WHERE assettype = ?",
58              Arrays.asList("FlexAssetTypes", "FlexGroupTypes"));
59  
60      static {
61          FLEX_ATTR_TYPE.setElement(0, "FlexAssetTypes", "assettype");
62          FLEX_ATTR_TYPE.setElement(1, "FlexGroupTypes", "assettype");
63      }
64  
65      public String getFlexAttributeType(AssetId id) {
66          StatementParam param = FLEX_ATTR_TYPE.newParam();
67          param.setString(0, id.getType());
68          param.setString(1, id.getType());
69          Row row = SqlHelper.selectSingle(ics, FLEX_ATTR_TYPE, param);
70          if (row == null) {
71              throw new IllegalArgumentException("Asset " + id + " is not a flex asset!");
72          }
73          return row.getString("assetattr");
74      }
75    
76  
77  
78      public String getFlexAttributeValue(AssetId id, String attrName) {
79          // todo: medium: fix as this is very inefficient
80          String attrType = getFlexAttributeType(id);
81          PreparedStmt flexFields = new PreparedStmt("SELECT attr.name AS name, cmungo.stringvalue AS stringvalue "
82                  + "FROM " + attrType + " attr, " + id.getType() + "_Mungo cmungo " + "WHERE cmungo.cs_ownerid = ? "
83                  + "AND cmungo.cs_attrid = attr.id AND attr.name = ?", Arrays.asList(attrType, id.getType() + "_Mungo"));
84          flexFields.setElement(0, id.getType() + "_Mungo", "cs_ownerid");
85          flexFields.setElement(1, attrType, "name");
86          StatementParam param = flexFields.newParam();
87          param.setLong(0, id.getId());
88          param.setString(1, attrName);
89          Row r = SqlHelper.selectSingle(ics, flexFields, param);
90          if (r == null)
91              return null;
92          else
93              return r.getString("stringvalue");
94      }
95  
96      public Map<String, String> getFlexAttributeValues(AssetId id, String... attrName) {
97          // todo: medium: fix as this is very inefficient
98          if (attrName == null || attrName.length == 0)
99              throw new IllegalArgumentException("attrName must not be null or zero-length array.");
100         String attrType = getFlexAttributeType(id);
101         StringBuilder sql = new StringBuilder("SELECT attr.name AS name, cmungo.stringvalue AS stringvalue FROM ")
102                 .append(attrType).append(" attr, ").append(id.getType())
103                 .append("_Mungo cmungo WHERE cmungo.cs_ownerid = ? AND cmungo.cs_attrid = attr.id AND attr.name IN (");
104 
105         for (int num = 0; num < attrName.length; num++) {
106             if (num > 0)
107                 sql.append(",");
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 }