package com.xeiam.xdropwizard.manager;

import io.dropwizard.lifecycle.Managed;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xeiam.xdropwizard.XDropWizardApplicationConfiguration.YankConfiguration;
import com.xeiam.xdropwizard.business.Book;
import com.xeiam.xdropwizard.business.BooksDAO;
import com.xeiam.yank.DBConnectionManager;
import com.xeiam.yank.PropertiesUtils;

/**
 * <p>
 * This is where Yank is bound to the main DropWizard thread.
 * </p>
 * 
 * @author timmolter
 */
public class YankManager implements Managed {

  private final Logger logger = LoggerFactory.getLogger(YankManager.class);

  private YankConfiguration yankConfiguration;

  /**
   * Constructor
   */
  public YankManager(YankConfiguration yankConfiguration) {

    this.yankConfiguration = yankConfiguration;
  }

  @Override
  public void start() throws Exception {

    logger.info("initializing Yank...");

    if (yankConfiguration.getSqlPropsFileName() == null || yankConfiguration.getSqlPropsFileName().trim().length() < 1) {
      DBConnectionManager.INSTANCE.init(PropertiesUtils.getPropertiesFromClasspath(yankConfiguration.getDbPropsFileName()));
    }
    else {
      DBConnectionManager.INSTANCE.init(PropertiesUtils.getPropertiesFromClasspath(yankConfiguration.getDbPropsFileName()), PropertiesUtils.getPropertiesFromClasspath(yankConfiguration
          .getSqlPropsFileName()));
    }

    logger.info("Yank started successfully.");

    // Create an in-memory table and fill it with Book Objects. Normally the database would already exist.
    logger.info("Creating Books table and adding data to it...");

    BooksDAO.createBooksTable();

    Book book = new Book();
    book.setTitle("Cryptonomicon");
    book.setAuthor("Neal Stephenson");
    book.setPrice(23.99);
    BooksDAO.insertBook(book);

    book = new Book();
    book.setTitle("Harry Potter");
    book.setAuthor("Joanne K. Rowling");
    book.setPrice(11.99);
    BooksDAO.insertBook(book);

    book = new Book();
    book.setTitle("Don Quijote");
    book.setAuthor("Cervantes");
    book.setPrice(21.99);
    BooksDAO.insertBook(book);

    logger.info("Creating Books table complete.");
  }

  @Override
  public void stop() throws Exception {

    logger.info("shutting down Yank...");

    DBConnectionManager.INSTANCE.release();

    logger.info("Yank shutdown successfully.");
  }

}

@@ -220,100 +220,8 @@
 on;%0A
-import com.xeiam.xdropwizard.business.Book;%0Aimport com.xeiam.xdropwizard.business.BooksDAO;%0A
 impo
@@ -255,24 +255,24 @@
 ionManager;%0A
+
 import com.x
@@ -382,17 +382,16 @@
  %3C/p%3E%0A *
- 
 %0A * @aut
@@ -542,16 +542,22 @@
 private 
+final 
 YankConf
@@ -846,35 +846,34 @@
 onfiguration.get
-Sql
+Db
 PropsFileName() 
@@ -884,10 +884,10 @@
 ull 
-%7C%7C
+&&
  yan
@@ -926,28 +926,188 @@
 me()
-.trim().length() %3C 1
+ == null) %7B%0A      DBConnectionManager.INSTANCE.init(PropertiesUtils.getPropertiesFromClasspath(%22DB.properties%22));%0A    %7D%0A    else if (yankConfiguration.getSqlPropsFileName() == null
 ) %7B%0A
@@ -1528,733 +1528,8 @@
 );%0A%0A
-    // Create an in-memory table and fill it with Book Objects. Normally the database would already exist.%0A    logger.info(%22Creating Books table and adding data to it...%22);%0A%0A    BooksDAO.createBooksTable();%0A%0A    Book book = new Book();%0A    book.setTitle(%22Cryptonomicon%22);%0A    book.setAuthor(%22Neal Stephenson%22);%0A    book.setPrice(23.99);%0A    BooksDAO.insertBook(book);%0A%0A    book = new Book();%0A    book.setTitle(%22Harry Potter%22);%0A    book.setAuthor(%22Joanne K. Rowling%22);%0A    book.setPrice(11.99);%0A    BooksDAO.insertBook(book);%0A%0A    book = new Book();%0A    book.setTitle(%22Don Quijote%22);%0A    book.setAuthor(%22Cervantes%22);%0A    book.setPrice(21.99);%0A    BooksDAO.insertBook(book);%0A%0A    logger.info(%22Creating Books table complete.%22);%0A
   %7D%0A

update YankManager