/*
 * Copyright [1999-2015] Wellcome Trust Sanger Institute and the
 * EMBL-European Bioinformatics Institute
 * Copyright [2016] EMBL-European Bioinformatics Institute
 *
 * 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 org.ensembl.healthcheck.testcase.funcgen;

import org.ensembl.healthcheck.DatabaseRegistryEntry;
import org.ensembl.healthcheck.ReportManager;
import org.ensembl.healthcheck.Team;
import org.ensembl.healthcheck.testcase.SingleDatabaseTestCase;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Check that every distinct experiment - epigenome - feature_type
 * combination is linked to a feature_set.
 *
 * @author ilavidas
 */


public class ExperimentHasFeatureSet extends SingleDatabaseTestCase {

    public ExperimentHasFeatureSet() {
        setTeamResponsible(Team.FUNCGEN);
        setDescription("Check that every distinct experiment - epigenome - "
                + "feature_type combination is linked to a feature_set.");
    }

    @Override
    public boolean run(DatabaseRegistryEntry dbre) {

        boolean result = true;
        Connection con = dbre.getConnection();

        try {
            //fetch all distinct experiment - epigenome - feature_type
            // combinations from experiment table
            Statement stmt = con.createStatement();
            ResultSet combos = stmt.executeQuery("SELECT DISTINCT " +
                    "experiment_id, epigenome_id, feature_type_id FROM " +
                    "experiment");

            while (combos != null && combos.next()) {
                int experimentID = combos.getInt(1);
                int epigenomeID = combos.getInt(2);
                int featureTypeID = combos.getInt(3);

                //fetch feature_sets for every combination
                Statement newStmt = con.createStatement();
                ResultSet featureSets = newStmt.executeQuery("SELECT " +
                        "feature_set_id FROM feature_set WHERE " +
                        "experiment_id=" +experimentID +" AND epigenome_id="
                        + epigenomeID + " AND feature_type_id=" +featureTypeID);


                if (!featureSets.next()) {
                    ReportManager.problem(this, con, "There is no feature_set" +
                            " for this combination: experiment_id = " +
                            experimentID + ", epigenome_id = " + epigenomeID
                            + ", feature_type_id = " + featureTypeID);

                    result = false;
                }
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }

        return result;
    }
}


@@ -1827,17 +1827,39 @@
 nt table
+. Ignore control files
 %0A
-
         
@@ -1968,16 +1968,18 @@
 ISTINCT 
+ex
 %22 +%0A    
@@ -1987,32 +1987,33 @@
                %22
+.
 experiment_id, e
@@ -2011,16 +2011,19 @@
 ent_id, 
+ex.
 epigenom
@@ -2028,16 +2028,19 @@
 ome_id, 
+ex.
 feature_
@@ -2050,49 +2050,232 @@
 e_id
- FROM %22 +%0A                    %22experiment
+, ep%22 +%0A                    %22.display_label, ft.name FROM experiment ex JOIN %22 +%0A                    %22feature_type ft USING(feature_type_id) JOIN epigenome ep%22 +%0A                    %22 USING(epigenome_id) WHERE ft.name!='WCE'
 %22);%0A
@@ -2487,16 +2487,138 @@
 tInt(3);
+%0A                String epigenomeName = combos.getString(4);%0A                String featureTypeName = combos.getString(5);
 %0A%0A      
@@ -3337,45 +3337,128 @@
  + %22
-, feature_type_id = %22 + featureTypeID
+ (%22 + epigenomeName + %22), feature_type_id = %22%0A                            + featureTypeID + %22 (%22 + featureTypeName + %22)%22
 );%0A%0A

improve ExperimentHasFeatureSet