package co.je.conductor;

import io.dropwizard.Application;
import io.dropwizard.java8.Java8Bundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration.Dynamic;

import org.eclipse.jetty.servlets.CrossOriginFilter;

import co.je.conductor.api.resources.JobRequestResource;
import co.je.conductor.domain.business.JobBusiness;
import co.je.conductor.infrastructure.config.ConductorConfig;
import co.je.conductor.persistence.daos.JobRequestDAO;
import co.je.conductor.persistence.daos.JobResultDAO;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.mongodb.DB;
import com.mongodb.MongoClient;

public class Conductor extends Application<ConductorConfig> {

    @Override
    public void initialize(Bootstrap<ConductorConfig> bootstrap) {

        bootstrap.addBundle(new Java8Bundle());
    }

    private void addCORSSupport(Environment environment) {

        Dynamic filter = environment.servlets().addFilter("CORS", CrossOriginFilter.class);
        filter.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");
        filter.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, "GET,PUT,POST,DELETE,OPTIONS,PATCH");
        filter.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
        filter.setInitParameter(CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
        filter.setInitParameter("allowedHeaders", "Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin");
        filter.setInitParameter("allowCredentials", "true");
    }

    private JobRequestResource getJobRequestResource(DB mongoDB, ObjectMapper objectMapper) {

        JobRequestDAO jobRequestDAO = new JobRequestDAO(objectMapper);
        JobResultDAO jobResultDAO = new JobResultDAO(objectMapper);
        JobBusiness jobBusiness = new JobBusiness(mongoDB, jobRequestDAO, jobResultDAO);

        return new JobRequestResource(jobBusiness);
    }

    @Override
    public void run(ConductorConfig conductorConfig, Environment environment) throws Exception {

        // add CORS support
        addCORSSupport(environment);
        
        environment.getObjectMapper().configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

        MongoClient mongoClient = new MongoClient();
        DB mongoDB = mongoClient.getDB(conductorConfig.getMongoDBConfig().getDbName());

        ObjectMapper objectMapper = environment.getObjectMapper();
        JobRequestResource jobRequestResource = getJobRequestResource(mongoDB, objectMapper);

        environment.jersey().register(jobRequestResource);
    }

    public static void main(String[] args) {

        try {

            Conductor conductor = new Conductor();
            conductor.run(args);

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}
@@ -15,24 +15,203 @@
 conductor;%0A%0A
+import com.fasterxml.jackson.databind.DeserializationFeature;%0Aimport com.fasterxml.jackson.databind.util.ISO8601DateFormat;%0Aimport com.fasterxml.jackson.datatype.joda.JodaModule;%0A
 import io.dr
@@ -1879,32 +1879,706 @@
 %22true%22);%0A    %7D%0A%0A
+    public static ObjectMapper configureJackson(ObjectMapper objectMapper) %7B%0A%0A        objectMapper.registerModule(new JodaModule());%0A        objectMapper.setDateFormat(new ISO8601DateFormat());%0A        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);%0A        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);%0A        objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);%0A        objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);%0A        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);%0A%0A        return objectMapper;%0A    %7D%0A%0A
     private JobR
@@ -3129,24 +3129,43 @@
         
-%0A       
+ObjectMapper objectMapper =
  environ
@@ -3190,69 +3190,69 @@
 er()
-.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);%0A
+;%0A        configureJackson(objectMapper);%0A%0A        // MongoDB
 %0A   
@@ -3394,75 +3394,8 @@
 );%0A%0A
-        ObjectMapper objectMapper = environment.getObjectMapper();%0A
     
@@ -3476,33 +3476,32 @@
  objectMapper);%0A
-%0A
         environm
@@ -3610,17 +3610,16 @@
   try %7B%0A
-%0A
         
@@ -3694,17 +3694,16 @@
 (args);%0A
-%0A
         
@@ -3722,25 +3722,24 @@
 eption e) %7B%0A
-%0A
             

Configure the ObjectMapper.