/*
 * Copyright 2008 FatWire Corporation. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fatwire.gst.foundation.facade.assetapi;

import COM.FutureTense.Interfaces.ICS;
import com.fatwire.assetapi.data.AssetId;
import com.fatwire.cs.core.db.PreparedStmt;
import com.fatwire.cs.core.db.StatementParam;
import com.fatwire.gst.foundation.facade.sql.Row;
import com.fatwire.gst.foundation.facade.sql.SqlHelper;

import java.util.Arrays;
import java.util.Collections;

/**
 * Backdoor asset API utility that assists with retrieving asset data without using legitimate APIs.
 * Warning: Using this class will bypass security, revision tracking, approval, and compositional
 * dependency management subsystems and should only be used with extreme caution.
 *
 * User: Tony Field
 * Date: 2011-05-07
 */
public final class DirectSqlAccessTools {
    private final ICS ics;
    public DirectSqlAccessTools(ICS ics) {
        this.ics = ics;
    }

    private static final PreparedStmt IS_FLEX = new PreparedStmt("select assettype,logic from AssetType where assettype = ? and logic = 'com.openmarket.assetframework.complexasset.ComplexAsset'", Collections.singletonList("AssetType"));

    static {
        IS_FLEX.setElement(0, "AssetType", "assettype");
    }
    public boolean isFlex(AssetId id) {
        StatementParam param = IS_FLEX.newParam();
        param.setString(0, id.getType());
        return SqlHelper.select(ics, IS_FLEX, param).iterator().hasNext();
    }

    private static final PreparedStmt FLEX_ATTR_TYPE = new PreparedStmt("select assetattr from FlexAssetTypes where assettype = ?", Collections.singletonList("FlexAssetTypes"));

    static {
        FLEX_ATTR_TYPE.setElement(0, "FlexAssetTypes", "assettype");
    }

    public String getFlexAttributeType(AssetId id) {
        if (!isFlex(id)) throw new IllegalArgumentException("Asset " + id + " is not a flex asset!");
        StatementParam param = FLEX_ATTR_TYPE.newParam();
        param.setString(0, id.getType());
        return SqlHelper.selectSingle(ics, FLEX_ATTR_TYPE, param).getString("assetattr");
    }

    public String getFlexAttributeValue(AssetId id, String attrName) {
        // todo: medium: fix as this is very inefficient
        String attrType = getFlexAttributeType(id);
        PreparedStmt flexFields = new PreparedStmt("select attr.name as name, cmungo.stringvalue as stringvalue " +
                "from " + attrType + " attr, " + id.getType() + "_Mungo cmungo " +
                "where cmungo.cs_ownerid = ? " +
                "and cmungo.cs_attrid = attr.id " +
                "and attr.name = ?",
                Arrays.asList(attrType, id.getType() + "_Mungo"));
        flexFields.setElement(0, id.getType() + "_Mungo", "cs_ownerid");
        flexFields.setElement(1, attrType, "name");
        StatementParam param = flexFields.newParam();
        param.setLong(0, id.getId());
        param.setString(1, attrName);
        Row r = SqlHelper.selectSingle(ics, flexFields, param);
        if (r == null) return null;
        else return r.getString("stringvalue");
    }
}

@@ -1086,16 +1086,19 @@
  without
+%0A *
  using l
@@ -1112,19 +1112,16 @@
 te APIs.
-%0A *
  Warning
@@ -1160,16 +1160,19 @@
 ecurity,
+%0A *
  revisio
@@ -1210,19 +1210,16 @@
 sitional
-%0A *
  depende
@@ -1232,16 +1232,19 @@
 nagement
+%0A *
  subsyst
@@ -1291,24 +1291,25 @@
  caution.%0A *
+ 
 %0A * User: To
@@ -1316,19 +1316,16 @@
 ny Field
-%0A *
  Date: 2
@@ -1403,24 +1403,25 @@
 al ICS ics;%0A
+%0A
     public D
@@ -1543,31 +1543,44 @@
 reparedStmt(
-%22select
+%0A            %22SELECT
  assettype,l
@@ -1584,20 +1584,20 @@
 e,logic 
-from
+FROM
  AssetTy
@@ -1599,21 +1599,21 @@
 setType 
-where
+WHERE
  assetty
@@ -1619,19 +1619,19 @@
 ype = ? 
-and
+AND
  logic =
@@ -1690,16 +1690,28 @@
 Asset'%22,
+%0A           
  Collect
@@ -1821,16 +1821,17 @@
 ;%0A    %7D%0A
+%0A
     publ
@@ -2109,23 +2109,36 @@
 redStmt(
-%22select
+%0A            %22SELECT
  assetat
@@ -2140,20 +2140,20 @@
 setattr 
-from
+FROM
  FlexAss
@@ -2160,21 +2160,21 @@
 etTypes 
-where
+WHERE
  assetty
@@ -2395,16 +2395,28 @@
 lex(id))
+%0A           
  throw n
@@ -2914,22 +2914,22 @@
 edStmt(%22
-select
+SELECT
  attr.na
@@ -2931,18 +2931,18 @@
 tr.name 
-as
+AS
  name, c
@@ -2959,18 +2959,18 @@
 ngvalue 
-as
+AS
  stringv
@@ -2975,18 +2975,16 @@
 gvalue %22
- +
 %0A       
@@ -2996,13 +2996,15 @@
     
-%22from
++ %22FROM
  %22 +
@@ -3064,31 +3064,15 @@
  %22 +
-%0A                %22where
+ %22WHERE
  cmu
@@ -3091,18 +3091,16 @@
 id = ? %22
- +
 %0A       
@@ -3108,20 +3108,22 @@
         
-%22and
++ %22AND
  cmungo.
@@ -3149,61 +3149,29 @@
  %22 +
-%0A                %22and attr.name = ?%22,%0A               
+ %22AND attr.name = ?%22,
  Arr
@@ -3195,32 +3195,48 @@
 pe, id.getType()
+%0A               
  + %22_Mungo%22));%0A 
@@ -3575,16 +3575,28 @@
 == null)
+%0A           
  return 
@@ -3613,16 +3613,28 @@
     else
+%0A           
  return 

Change casing to upper for sql keywords