package org.consumersunion.stories.server.solr.story;

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

import javax.inject.Inject;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.consumersunion.stories.server.persistence.PersistenceUtil;
import org.consumersunion.stories.server.solr.IndexChecker;
import org.consumersunion.stories.server.solr.story.documents.IndexedStoryDocument;
import org.springframework.stereotype.Component;

@Component
public class StoriesIndexChecker implements IndexChecker {
    private final FullStoryIndexer fullStoryIndexer;

    @Inject
    public StoriesIndexChecker(FullStoryIndexer fullStoryIndexer) {
        this.fullStoryIndexer = fullStoryIndexer;
    }

    @Override
    public void check(SolrServer solrStoryServer, SolrServer solrCollectionServer, SolrServer solrPersonServer)
            throws Exception {
        SolrQuery query = new SolrQuery("type:" + IndexedStoryDocument.class.getSimpleName());
        QueryResponse result = solrStoryServer.query(query);

        Long indexedStories = result.getResults().getNumFound();
        Long existingStories = storiesCount();

        if (indexedStories.longValue() != existingStories.longValue()) {
            fullStoryIndexer.index(solrStoryServer, solrCollectionServer, solrPersonServer);
        }
    }

    private Long storiesCount() throws Exception {
        Connection connection = null;
        try {
            connection = PersistenceUtil.getConnection();
            PreparedStatement countStmt = connection.prepareStatement("SELECT count(s.id) FROM document d "
                    + "JOIN profile p ON p.id=d.primaryAuthor " + "JOIN documentText t ON d.id=t.documentId "
                    + "JOIN story s ON d.systemEntity = s.id " + "JOIN systemEntity e ON s.id=e.id "
                    + "LEFT OUTER JOIN documentText dt2 ON (t.documentId=dt2.documentId AND dt2.version > t.version) "
                    + "WHERE d.systemEntityRelation='BODY' AND dt2.documentId IS NULL");
            ResultSet resultSet = countStmt.executeQuery();
            resultSet.next();

            return resultSet.getLong(1);
        } catch (SQLException e) {
            throw e;
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }
}

@@ -1731,19 +1731,98 @@
 ECT 
-count(s.id)
+COUNT(*)%22 +%0A                    %22 FROM (SELECT DISTINCT s.id%22 +%0A                    %22     
  FRO
@@ -1833,18 +1833,19 @@
 cument d
- 
 %22
+ +
 %0A       
@@ -1853,27 +1853,33 @@
             
-+ %22
+%22        
 JOIN profile
@@ -1888,17 +1888,19 @@
  ON p.id
-=
+ = 
 d.primar
@@ -1906,22 +1906,49 @@
 ryAuthor
- 
 %22 +
+%0A                    %22       
  
-%22
 JOIN doc
@@ -1952,20 +1952,16 @@
 document
-Text
  t ON d.
@@ -1966,23 +1966,18 @@
 d.id
-=t.documentId %22
+ = t.id%22 +
 %0A   
@@ -1993,19 +1993,25 @@
         
-+ %22
+%22        
 JOIN sto
@@ -2043,14 +2043,41 @@
 s.id
- 
 %22 +
+%0A                    %22       
  
-%22
 JOIN
@@ -2103,15 +2103,18 @@
 s.id
-=
+ = 
 e.id
- 
 %22
+ +
 %0A   
@@ -2130,19 +2130,25 @@
         
-+ %22
+%22        
 LEFT OUT
@@ -2167,12 +2167,8 @@
 ment
-Text
  dt2
@@ -2178,32 +2178,18 @@
  (t.
-documentId=dt2.documentI
+id = dt2.i
 d AN
@@ -2214,18 +2214,19 @@
 version)
- 
 %22
+ +
 %0A       
@@ -2242,11 +2242,15 @@
     
-+ %22
+%22      
 WHER
@@ -2273,17 +2273,19 @@
 Relation
-=
+ = 
 'BODY' A
@@ -2295,17 +2295,9 @@
 dt2.
-documentI
+i
 d IS
@@ -2301,16 +2301,19 @@
  IS NULL
+) A
 %22);%0A    

Fix StoriesIndexChecker