package uk.ac.ebi.pride.spectracluster.repo.dao.stats;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import uk.ac.ebi.pride.spectracluster.repo.model.ClusterRepoStatistics;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Reader for accessing statistics of the cluster repository
 *
 * @author Rui Wang
 * @version $Id$
 */
public class ClusterRepoStatisticsReader implements IClusterRepoStatisticsReadDao {

    private final JdbcTemplate template;

    public ClusterRepoStatisticsReader(DataSource dataSource) {
        this.template = new JdbcTemplate(dataSource);
    }

    @Override
    public List<ClusterRepoStatistics> getGeneralStatistics() {
        final List<ClusterRepoStatistics> clusterRepoStatistics = new ArrayList<ClusterRepoStatistics>();


        final String SELECT_QUERY = "SELECT * FROM cluster_statistics WHERE name NOT LIKE '%-%'";

        template.query(SELECT_QUERY, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                ClusterRepoStatistics statistics = new ClusterRepoStatistics();

                statistics.setName(rs.getString("name"));
                statistics.setValue(rs.getLong("value"));

                clusterRepoStatistics.add(statistics);
            }
        });

        return clusterRepoStatistics;
    }

    @Override
    public List<ClusterRepoStatistics> getStatisticsByPrefix(String prefix) {
        final List<ClusterRepoStatistics> clusterRepoStatistics = new ArrayList<ClusterRepoStatistics>();


        final String SELECT_QUERY = "SELECT * FROM cluster_statistics WHERE name LIKE '" + prefix + "%-%'";

        template.query(SELECT_QUERY, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet rs) throws SQLException {
                ClusterRepoStatistics statistics = new ClusterRepoStatistics();

                statistics.setName(rs.getString("name"));
                statistics.setValue(rs.getLong("value"));

                clusterRepoStatistics.add(statistics);
            }
        });

        return clusterRepoStatistics;
    }
}

@@ -562,16 +562,72 @@
 dDao %7B%0A%0A
+    private static final String FIELD_SEPARATOR = %22%7C%22;%0A%0A
     priv
@@ -1063,17 +1063,39 @@
  LIKE '%25
--
+%22 + FIELD_SEPARATOR + %22
 %25'%22;%0A%0A  
@@ -1880,9 +1880,30 @@
 + %22%25
--
+%22 + FIELD_SEPARATOR+ %22
 %25'%22;

change field separator for getting statistics