/*
 * Copyright [1999-2015] Wellcome Trust Sanger Institute and the 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.variation;

import java.sql.Connection;
import java.util.List;
import org.ensembl.healthcheck.DatabaseRegistryEntry;
import org.ensembl.healthcheck.ReportManager;
import org.ensembl.healthcheck.Team;
import org.ensembl.healthcheck.testcase.SingleDatabaseTestCase;
import org.ensembl.healthcheck.util.DBUtils;
import org.ensembl.healthcheck.util.RowMapper;
import org.ensembl.healthcheck.util.SqlTemplate;
import org.ensembl.healthcheck.Species;

/**
 * Checks populations are entered as expected
 */
public class Population extends SingleDatabaseTestCase {

	/**
	 * Creates a new instance of Population
	 */
	public Population() {

		addToGroup("variation-release");
		setDescription("Check the Populations are entered as expected");
		setTeamResponsible(Team.VARIATION);

	}

	/**
	 * 
	 * @param dbre
	 *          The database to check.
	 * @return True if the test passed
	 */
         public boolean run(final DatabaseRegistryEntry dbre) {
	     boolean result = true;

		Connection con = dbre.getConnection();

	
		try {				
			// Check for populations without sizes
			String size_stmt = "select count(*) from population pop, sample_population sp where pop.population_id = sp.population_id and pop.size is null;";
			
                        int size_rows = DBUtils.getRowCount(con,size_stmt);
                        if (size_rows > 0) {
			    result = false;
			    ReportManager.problem(this, con, String.valueOf(size_rows) + " populations have no stored size");
                        }

			// Check for populations with freqs_from_gts set for mouse and human
			 if (dbre.getSpecies() == Species.MUS_MUSCULUS || dbre.getSpecies() == Species.HOMO_SAPIENS) {

			     // Check for populations without sizes
			     String freq_stmt = "select count(*) from population pop where pop.freqs_from_gts = 1;";
			
			     int freq_rows = DBUtils.getRowCount(con,freq_stmt);
			     if (freq_rows == 0) {
				 result = false;
				 ReportManager.problem(this, con, "No populations have freqs_from_gts set ");
			     }
			 }
			 
		} catch (Exception e) {
			ReportManager.problem(this, con, "HealthCheck caused an exception: " + e.getMessage());
			result = false;
		}
		

		return result;
	 } 
    
}

@@ -1341,17 +1341,16 @@
 ion() %7B%0A
-%0A
 %09%09addToG
@@ -1481,17 +1481,16 @@
 ATION);%0A
-%0A
 %09%7D%0A%0A%09/**
@@ -1588,23 +1588,16 @@
 ed%0A%09 */%0A
-       
   public
@@ -1645,18 +1645,16 @@
 dbre) %7B%0A
-%09 
     bool
@@ -1672,19 +1672,20 @@
 = true;%0A
-%0A%09%09
+    
 Connecti
@@ -1716,20 +1716,20 @@
 ion();%0A%0A
-%09%0A%09%09
+    
 try %7B%09%09%09
@@ -1730,19 +1730,22 @@
 y %7B%09%09%09%09%0A
-%09%09%09
+      
 // Check
@@ -1775,19 +1775,22 @@
 t sizes%0A
-%09%09%09
+      
 String s
@@ -1930,30 +1930,9 @@
 ;%22;%0A
-%09%09%09
 %0A
-                  
     
@@ -1985,34 +1985,16 @@
 _stmt);%0A
-                  
       if
@@ -2012,19 +2012,20 @@
  %3E 0) %7B%0A
-%09%09%09
+    
     resu
@@ -2036,19 +2036,20 @@
  false;%0A
-%09%09%09
+    
     Repo
@@ -2152,32 +2152,17 @@
     
+%7D%0A%0A
       
-            %7D%0A%0A%09%09%09
 // C
@@ -2226,19 +2226,21 @@
 d human%0A
-%09%09%09
+     
  if (dbr
@@ -2326,25 +2326,24 @@
 IENS) %7B%0A
-%0A%09%09%09
+  
      
+ 
 // Check
@@ -2373,19 +2373,19 @@
 t sizes%0A
-%09%09%09
+   
      Str
@@ -2473,15 +2473,11 @@
 ;%22;%0A
-%09%09%09%0A%09%09%09
+   
     
@@ -2529,19 +2529,19 @@
 _stmt);%0A
-%09%09%09
+   
      if 
@@ -2559,20 +2559,25 @@
 == 0) %7B%0A
-%09%09%09%09
+         
  result 
@@ -2585,20 +2585,25 @@
  false;%0A
-%09%09%09%09
+         
  ReportM
@@ -2676,31 +2676,30 @@
 %22);%0A
-%09%09%09
+   
      %7D%0A
-%09%09%09 %7D%0A%09%09%09 %0A%09%09
+      %7D%0A    
 %7D ca
@@ -2718,19 +2718,22 @@
 on e) %7B%0A
-%09%09%09
+      
 ReportMa
@@ -2812,19 +2812,22 @@
 age());%0A
-%09%09%09
+      
 result =
@@ -2838,18 +2838,18 @@
 se;%0A
-%09%09%7D%0A%09%09%0A%0A%09%09
+    %7D%0A    
 retu
@@ -2863,16 +2863,11 @@
 lt;%0A
-%09 %7D %0A 
   
+%7D
  %0A%7D%0A

Fix indentation