/*
  Copyright 2010-2017 BusinessCode GmbH, Germany

  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 de.businesscode.bcdui.wrs.load;

import java.util.ArrayList;
import java.util.Collection;

import org.w3c.dom.Element;

import de.businesscode.bcdui.binding.BindingItem;
import de.businesscode.bcdui.binding.BindingSet;
import de.businesscode.bcdui.binding.Bindings;
import de.businesscode.bcdui.binding.exc.BindingNotFoundException;

public class SQLStatementWithParams {

  private String statement;
  private Collection<Element> filterItems;
  private Collection<String> filterValues;
  private Collection<BindingItem> filterBindingItems;

  public SQLStatementWithParams(String statement, Collection<Element> filterItems, BindingSet bindingSet) throws BindingNotFoundException {
    super();
    this.statement = statement;
    this.filterItems = filterItems;
    this.filterValues = new ArrayList<String>();
    this.filterBindingItems = new ArrayList<BindingItem>();
    for (Element e : filterItems) {
      filterValues.add(e.getAttribute("value"));
      if( !e.getAttribute("bRef").isEmpty() )
        filterBindingItems.add(bindingSet.get(e.getAttribute("bRef")));
      // If we want to enforce a numeric type (needed for example for NULLIF(v,0)), we need to create a dummy BinsingItem here as a carrier for the data type
      else if( "NUMERIC".equals(e.getAttribute(Bindings.jdbcDataTypeNameAttribute)) ) {
        BindingItem dummyBi = new BindingItem(null, "null", false, bindingSet);
        try {
          dummyBi.setJDBCDataTypeName("NUMERIC");
        } catch(Exception exc) { // We know, it does exist, because its value is hard-coded here
        }
        filterBindingItems.add(dummyBi);
      } else
        filterBindingItems.add(null);
    }
  }

  public String getStatement() {
    return statement;
  }

  public Collection<Element> getFilterItems() {
    return filterItems;
  }

  public Collection<String> getFilterValues() {
    return filterValues;
  }

  public Collection<BindingItem> getFilterBindingItems() {
    return filterBindingItems;
  }
}
@@ -656,16 +656,39 @@
 ayList;%0A
+import java.util.List;%0A
 import j
@@ -1029,26 +1029,20 @@
 private 
-Collection
+List
 %3CElement
@@ -1066,26 +1066,20 @@
 private 
-Collection
+List
 %3CString%3E
@@ -1103,26 +1103,20 @@
 private 
-Collection
+List
 %3CBinding
@@ -1192,26 +1192,20 @@
 tement, 
-Collection
+List
 %3CElement
@@ -2338,26 +2338,20 @@
  public 
-Collection
+List
 %3CElement
@@ -2409,26 +2409,20 @@
  public 
-Collection
+List
 %3CString%3E
@@ -2481,26 +2481,20 @@
  public 
-Collection
+List
 %3CBinding
@@ -2559,10 +2559,573 @@
 ems;%0A  %7D
+%0A%0A  /**%0A   * Provides the statement with values filled in instead of showing '?' for bound variables%0A   * @return%0A   */%0A  public String getStatementWithParams() %7B%0A    StringBuffer stmt = new StringBuffer();%0A    String%5B%5D parts = getStatement().split(%22%5C%5C?%22);%0A    for( int i=0; i%3Cparts.length && i%3CfilterValues.size(); i++ ) %7B%0A      if (filterBindingItems.get(i).isNumeric())%0A        stmt.append(parts%5Bi%5D).append(filterValues.get(i));%0A      else%0A        stmt.append(parts%5Bi%5D).append(%22'%22).append(filterValues.get(i)).append(%22'%22);%0A    %7D%0A    return stmt.toString();%0A  %7D
 %0A%7D

Allow to get SQL statement wiht parameters filled in