/*******************************************************************************
 * Copyright 2013 DIMA Research Group, TU Berlin (http://www.dima.tu-berlin.de)
 * 
 * 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.tu_berlin.dima.oligos.db.handler.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.log4j.Logger;

import de.tu_berlin.dima.oligos.db.constraints.ForeignKey;
import de.tu_berlin.dima.oligos.db.reference.ColumnRef;
import de.tu_berlin.dima.oligos.db.reference.TableRef;
import static de.tu_berlin.dima.oligos.db.JdbcConstants.Keys.*;

/**
 * {@link org.apache.commons.dbutils.ResultSetHandler ResultSetHandler}
 * implementation that converts the given <code>ResultSet</code> into a
 * <code>Set</code> of <code>ForeignKey</code>s. This class is intended to be a
 * utility class when working with JDBC and its <code>DatabaseMetaData</code>
 * object.
 * 
 * @author Christoph Brücke (christoph.bruecke@campus.tu-berlin.de)
 *
 */
public class ForeignKeysHandler implements ResultSetHandler<Set<ForeignKey>> {
  
  private static final Logger LOGGER = Logger.getLogger(ForeignKeysHandler.class);

  private boolean isPart;
  
  /**
   * Converts the given <code>ResultSet</code> into a <code>Set</code> of
   * <code>ForeignKey</code>s.
   * @param rs Untouched <code>ResultSet</code> with at least the following
   *   columns: 
   *   <ol>
   *     <li>PKTABLE_SCHEM String => primary key table schema being imported (may be null)</li>
   *     <li>PKTABLE_NAME String => primary key table name being imported</li>
   *     <li>PKCOLUMN_NAME String => primary key column name being imported</li>
   *     <li>FKTABLE_SCHEM String => foreign key table schema (may be null)</li>
   *     <li>FKTABLE_NAME String => foreign key table name</li>
   *     <li>FKCOLUMN_NAME String => foreign key column name</li>
   *     <li>KEY_SEQ short => sequence number within a foreign key</li>
   *   </ol>
   * @return A <code>Set</code> containing no, one, or more <code>ForeignKey</code>s
   */
  @Override
  public Set<ForeignKey> handle(ResultSet rs) throws SQLException {
    Set<ForeignKey> foreignKeys = new HashSet<ForeignKey>();
    ForeignKey last = null;
    ForeignKey current = null;
    while (rs.next()) {
      current = handleRow(rs);
      if (isPart) {
        Collection<ColumnRef> childCols = current.getChildColumns();
        Collection<ColumnRef> parenCols = current.getParentColumns();
        last.addColumns(childCols, parenCols);
      } else {
        foreignKeys.add(current);
      }
      last = current;
    }
    return foreignKeys;
  }

  private ForeignKey handleRow(ResultSet rs) throws SQLException {
    Collection<String> parentColumns = new ArrayList<String>();
    Collection<String> childColumns = new ArrayList<String>();
    String parentSchema = rs.getString(PARENT_SCHEMA);
    if (parentSchema == null) parentSchema = "";
    String childSchema = rs.getString(CHILD_SCHEMA);
    if (childSchema == null) childSchema = "";
    TableRef parentTable = new TableRef(parentSchema, rs.getString(PARENT_TABLE));
    parentColumns.add(rs.getString(PARENT_COLUMN));
    TableRef childTable = new TableRef(childSchema, rs.getString(CHILD_TABLE));
    childColumns.add(rs.getString(CHILD_COLUMN));
    short seq = rs.getShort(SEQUENCE_NUMBER);
    isPart = seq > 1;
    ForeignKey foreignKey =
        new ForeignKey("", childTable, childColumns, parentTable, parentColumns);
    LOGGER.debug("Found foreign key: " + foreignKey);
    return foreignKey;
  }

}

@@ -1008,16 +1008,51 @@
 l.Set;%0A%0A
+import javax.annotation.Nullable;%0A%0A
 import o
@@ -1128,16 +1128,58 @@
 ogger;%0A%0A
+import com.google.common.base.Predicate;%0A%0A
 import d
@@ -1981,23 +1981,372 @@
 ate 
-boolean isPart;
+final Predicate%3CResultSet%3E predicate;%0A%0A  private boolean isPart;%0A%0A  public ForeignKeysHandler() %7B%0A    this.predicate = new Predicate%3CResultSet%3E() %7B%0A      @Override%0A      public boolean apply(@Nullable ResultSet input) %7B%0A        return true;%0A      %7D%0A    %7D;%0A  %7D%0A%0A  public ForeignKeysHandler(final Predicate%3CResultSet%3E predicate) %7B%0A    this.predicate = predicate;%0A  %7D
 %0A  %0A
@@ -3424,16 +3424,39 @@
 s.next()
+ && predicate.apply(rs)
 ) %7B%0A    

Add predicate to ForeignKeysHandler.