package com.twilio.survey.models;

import com.twilio.survey.Server;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.query.UpdateOperations;

import java.util.List;

public class SurveyService {
  // MongoClient and Morphia instances must be accessible to the entire object, so a Datastore can
  // be built.
  MongoClient mongoClient;
  Morphia morphia;

  // An instance of Datastore must be accessible to the entire object, so all instance methods can
  // persist to and read from the datastore.
  final Datastore datastore;

  // Constructor
  public SurveyService() {
    try {
      // Create MongoDB drivers
      mongoClient = new MongoClient();
      morphia = new Morphia();

      // Ask the Morphia driver to scan the Models package for models.
      morphia.mapPackage("com.twilio.survey.models");

    } catch (Exception e) {
      // Catch any MongoDB configuration errors, and pass them back to STDERR.
      System.err.println(e.getMessage());
    } finally {
      // TODO: Remember to make sure the name of the datastore matches the MongoLab datastore name.
      datastore = morphia.createDatastore(mongoClient, "survey-java");
    }
  }

  public SurveyService(MongoClientURI mongoURI) {
    try {
      // Create MongoDB drivers
      mongoClient = new MongoClient(mongoURI);
      morphia = new Morphia();

      // Ask the Morphia driver to scan the Models package for models.
      morphia.mapPackage("com.twilio.survey.models");

    } catch (Exception e) {
      // Catch any MongoDB configuration errors, and pass them back to STDERR.
      System.err.println(e.getMessage());
    } finally {
      // Create a datastore with the database name provided.
      datastore = morphia.createDatastore(mongoClient, Server.config.getMongoDBName());
    }
  }


  // Find, Update, and Create -- database operations.
  public Survey getSurvey(String phone) {
    return datastore.find(Survey.class).field("phone").equal(phone).get();
  }

  public void updateSurvey(Survey survey) {
    UpdateOperations<Survey> updates = datastore.createUpdateOperations(Survey.class);
    updates.set("index", survey.getIndex());
    updates.set("responses", survey.getResponses());
    updates.set("done", survey.isDone());
    datastore.update(survey, updates);
  }

  public Survey createSurvey(String phone) {
    Survey existingSurvey = getSurvey(phone);
    if (existingSurvey == null) {
      Survey survey = new Survey(phone);
      datastore.save(survey);
      return survey;
    } else {
      return existingSurvey;
    }
  }

  public List<Survey> findAllFinishedSurveys() {
    return datastore.find(Survey.class).field("done").equal(true).asList();
  }
}

@@ -619,14 +619,8 @@
 .%0A  
-final 
 Data
@@ -636,16 +636,16 @@
 astore;%0A
+
 %0A  // Co
@@ -911,33 +911,120 @@
 urvey.models%22);%0A
+      datastore = morphia.createDatastore(mongoClient, Server.config.getMongoDBName());
 %0A
-
     %7D catch (Exc
@@ -1106,32 +1106,32 @@
 back to STDERR.%0A
+
       System.err
@@ -1160,195 +1160,8 @@
 ));%0A
-    %7D finally %7B%0A      // TODO: Remember to make sure the name of the datastore matches the MongoLab datastore name.%0A      datastore = morphia.createDatastore(mongoClient, %22survey-java%22);%0A
     

Fix datastore exception handling, closes #3