package com.indeed.status.sample;

import com.indeed.status.core.CheckResultSet;
import com.indeed.status.core.Dependency;
import com.mongodb.MongoClient;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author pitz@indeed.com (Jeremy Pitzeruse)
 */
public class Application {
    private static final ObjectWriter WRITER = new ObjectMapper().writer()
            .withDefaultPrettyPrinter();

    private static final String DATABASE_NAME = "test";

    private CheckResultSet resultSet;

    public Application() {}

    public synchronized CheckResultSet getResultSet() {
        return resultSet;
    }

    public synchronized void setResultSet(final CheckResultSet resultSet) {
        this.resultSet = resultSet;
    }



    public static void main(final String[] args) throws IOException, InterruptedException {
        final MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setDatabaseName(DATABASE_NAME);
        final JdbcTemplate testdb = new JdbcTemplate(dataSource);
        final MongoClient client = new MongoClient("localhost");

        final Dependency mysqlDatabaseDependency = new MySqlDatabaseDependency(DATABASE_NAME, testdb);
        final Dependency dbDependency = new MongoDBDatabaseDependency(DATABASE_NAME, client);
        final Dependency onDiskFileDependency = new OnDiskFileDependency("file");

        final DependencyManager dependencyManager = new DependencyManager();
        dependencyManager.addDependency(mysqlDatabaseDependency);
        dependencyManager.addDependency(dbDependency);
        dependencyManager.addDependency(onDiskFileDependency);

        final Application application = new Application();
        Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                application.setResultSet(dependencyManager.evaluate());
            }
        }, 0L, 3L, TimeUnit.SECONDS);


        while (true) {
            final CheckResultSet resultSet = application.getResultSet();
            if (application.getResultSet() == null) {
                continue;
            }

            Thread.sleep(1000L);

            final StringWriter stringWriter = new StringWriter();
            WRITER.writeValue(stringWriter, resultSet.summarize(true));
            System.out.println(stringWriter);
        }
    }
}

@@ -24,24 +24,128 @@
 us.sample;%0A%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.fasterxml.jackson.databind.ObjectWriter;%0A
 import com.i
@@ -273,27 +273,15 @@
 sql.
-jdb
 c
+j
 .jdbc
-2.optional
 .Mys
@@ -298,112 +298,8 @@
 ce;%0A
-import com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.fasterxml.jackson.databind.ObjectWriter;%0A
 impo
@@ -378,36 +378,8 @@
 on;%0A
-import java.io.PrintStream;%0A
 impo

Fix compilation against new mysql data source