package com.eogren.link_checker.service_layer.schema;

import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.AlreadyExistsException;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
import com.datastax.driver.core.exceptions.QueryExecutionException;
import com.datastax.driver.core.exceptions.QueryValidationException;

public class SchemaManager {
    private final Session session;

    public SchemaManager(Session session) {
        this.session = session;
    }

    /**
     * Create a new Cassandra schema from scratch.
     * [Eventually this will probably need some form of db migration]
     */
    public void createSchema() {
        try {
            executeIgnoreNotExists(session, "CREATE TYPE IF NOT EXISTS " + session.getLoggedKeyspace() + ".found_link ( " +
                "url text, " +
                "anchorText text " +
            ");");

            executeIgnoreNotExists(session, "CREATE TABLE IF NOT EXISTS meta_data ( key text PRIMARY KEY, value text ) WITH comment='config metadata';");
            executeIgnoreNotExists(session, "CREATE TABLE IF NOT EXISTS root_page( " +
                    "url text PRIMARY KEY );");
            executeIgnoreNotExists(session, "CREATE TABLE IF NOT EXISTS crawl_report( " +
                            "url text, " +
                            "date timeuuid, " +
                            "error text, " +
                            "status_code int, " +
                            "links map<text, frozen <found_link>>, " +
                            "PRIMARY KEY (url, date)" +
                            ") WITH CLUSTERING ORDER BY (date DESC);"
            );
            executeIgnoreNotExists(session, "CREATE TABLE IF NOT EXISTS latest_crawl_report( " +
                            "url text, " +
                            "date timeuuid, " +
                            "error text, " +
                            "status_code int, " +
                            "links map<text, frozen <found_link>>, " +
                            "PRIMARY KEY (url)" +
                            ");"
            );

            executeIgnoreNotExists(session, "CREATE INDEX latest_report_index ON latest_crawl_report(KEYS(links))");
            executeIgnoreNotExists(session, "INSERT INTO meta_data (key, value) VALUES ('db_version', '1');");
        } catch (QueryValidationException | QueryExecutionException | NoHostAvailableException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void dropSchema() {
        try {
            executeIgnoreNotExists(session, "DROP TABLE crawl_report");
            executeIgnoreNotExists(session, "DROP TABLE latest_crawl_report");
            executeIgnoreNotExists(session, "DROP TABLE root_page");
            executeIgnoreNotExists(session, "DROP TABLE meta_data");
            executeIgnoreNotExists(session, "DROP TYPE " + session.getLoggedKeyspace() + ".found_link");
        } catch (QueryValidationException | QueryExecutionException | NoHostAvailableException ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void closeSession() {
        session.close();
    }

    protected void executeIgnoreNotExists(Session session, String query) {
        try {
            session.execute(query);
        } catch (AlreadyExistsException ex) {
            System.out.println("Ignoring AlreadyExistsException for " + query);
        }
    }

}

@@ -711,212 +711,502 @@
     
-executeIgnoreNotExists(session, %22CREATE TYPE IF NOT EXISTS %22 + session.getLoggedKeyspace() + %22.found_link ( %22 +%0A                %22url text, %22 +%0A                %22anchorText text %22 +%0A            %22);%22);%0A%0A    
+createMetadataTables();%0A            createMonitoredPageTables();%0A            createCrawlReportTables();%0A        %7D catch (QueryValidationException %7C QueryExecutionException %7C NoHostAvailableException ex) %7B%0A            System.out.println(ex.getMessage());%0A        %7D%0A    %7D%0A%0A    private void createMonitoredPageTables() %7B%0A        executeIgnoreNotExists(session, %22CREATE TABLE IF NOT EXISTS root_page( %22 +%0A                %22url text PRIMARY KEY );%22);%0A    %7D%0A%0A    private void createMetadataTables() %7B%0A
     
@@ -1347,32 +1347,187 @@
 tadata';%22);%0A    
+    executeIgnoreNotExists(session, %22INSERT INTO meta_data (key, value) VALUES ('db_version', '1');%22);%0A    %7D%0A%0A    private void createCrawlReportTables() %7B%0A
         executeI
@@ -1551,35 +1551,34 @@
 ssion, %22CREATE T
-ABL
+YP
 E IF NOT EXISTS 
@@ -1573,34 +1573,72 @@
  NOT EXISTS 
-root_pag
+%22 + session.getLoggedKeyspac
 e(
+) + %22.found_link (
  %22 +%0A       
@@ -1650,44 +1650,84 @@
     
-    %22url text PRIMARY KEY 
+%22url text, %22 +%0A                %22anchorText text %22 +%0A                %22
 );%22);%0A
-    
+%0A
     
@@ -1824,36 +1824,32 @@
                 
-    
 %22url text, %22 +%0A 
@@ -1863,36 +1863,32 @@
                 
-    
 %22date timeuuid, 
@@ -1907,36 +1907,32 @@
                 
-    
 %22error text, %22 +
@@ -1948,36 +1948,32 @@
                 
-    
 %22status_code int
@@ -1970,36 +1970,32 @@
 s_code int, %22 +%0A
-    
                 
@@ -2061,36 +2061,32 @@
                 
-    
 %22PRIMARY KEY (ur
@@ -2093,28 +2093,24 @@
 l, date)%22 +%0A
-    
             
@@ -2159,28 +2159,24 @@
 ate DESC);%22%0A
-    
         );%0A 
@@ -2174,36 +2174,32 @@
      );%0A        
-    
 executeIgnoreNot
@@ -2259,36 +2259,32 @@
 awl_report( %22 +%0A
-    
                 
@@ -2322,36 +2322,32 @@
                 
-    
 %22date timeuuid, 
@@ -2366,36 +2366,32 @@
                 
-    
 %22error text, %22 +
@@ -2407,36 +2407,32 @@
                 
-    
 %22status_code int
@@ -2429,36 +2429,32 @@
 s_code int, %22 +%0A
-    
                 
@@ -2520,36 +2520,32 @@
                 
-    
 %22PRIMARY KEY (ur
@@ -2570,28 +2570,24 @@
             
-    
 %22);%22%0A       
@@ -2591,20 +2591,12 @@
     
-    
 );%0A%0A
-    
     
@@ -2708,279 +2708,8 @@
 %22);%0A
-            executeIgnoreNotExists(session, %22INSERT INTO meta_data (key, value) VALUES ('db_version', '1');%22);%0A        %7D catch (QueryValidationException %7C QueryExecutionException %7C NoHostAvailableException ex) %7B%0A            System.out.println(ex.getMessage());%0A        %7D%0A
     

refactor all schema statements into their own groups