package com.mongodb.dibs;

import com.mongodb.MongoClient;
import com.mongodb.dibs.model.Order;
import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.views.ViewBundle;
import org.eclipse.jetty.server.session.SessionHandler;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;

public class DibsApplication extends Application<DibsConfiguration> {
    private MongoClient mongo;
    private Morphia morphia;

    @Override
    public void initialize(final Bootstrap<DibsConfiguration> bootstrap) {
        bootstrap.addBundle(new ViewBundle());
        bootstrap.addBundle(new AssetsBundle("/assets", "/assets", null, "assets"));
        bootstrap.addBundle(new AssetsBundle("/META-INF/resources/webjars", "/webjars", null, "webjars"));
    }

    @Override
    public void run(final DibsConfiguration configuration, final Environment environment) throws Exception {
        morphia = new Morphia();
        morphia.mapPackage(Order.class.getPackage().getName());

        mongo = new MongoClient();
        
        environment.getApplicationContext().setSessionsEnabled(true);
        environment.getApplicationContext().setSessionHandler(new SessionHandler());

        Datastore datastore = morphia.createDatastore(mongo, "mongo-dibs");
        datastore.ensureIndexes();
        
        environment.jersey().register(new DibsResource(datastore));
    }

    public static void main(String[] args) throws Exception {
        new DibsApplication().run(args);
    }
}

@@ -20,16 +20,50 @@
 .dibs;%0A%0A
+import com.mongodb.BasicDBObject;%0A
 import c
@@ -82,24 +82,24 @@
 ongoClient;%0A
-
 import com.m
@@ -368,24 +368,55 @@
 ionHandler;%0A
+import org.joda.time.DateTime;%0A
 import org.m
@@ -1196,33 +1196,24 @@
 goClient();%0A
-        %0A
         envi
@@ -1467,24 +1467,16 @@
 exes();%0A
-        
 %0A       
@@ -1535,16 +1535,882 @@
 store));
+%0A%0A        String testdata = System.getProperty(%22testdata%22);%0A        if (testdata != null) %7B%0A            System.out.println(%22***  Generating test data ***%22);%0A            datastore.getCollection(Order.class).remove(new BasicDBObject());%0A            for (int i = 0; i %3C 100; i++) %7B%0A                createTestOrder(datastore, i, %22Vendor %22 + (i %25 5), true);%0A            %7D%0A            for (int i = 0; i %3C 10; i++) %7B%0A                createTestOrder(datastore, i, %22Awesome Vendor%22, false);%0A            %7D%0A        %7D%0A%0A    %7D%0A%0A    private void createTestOrder(final Datastore ds, final int count, final String vendor, final boolean group) %7B%0A        Order order = new Order();%0A        order.setVendor(vendor);%0A        order.setGroup(group);%0A        order.setExpectedAt(new DateTime(2014, 7, 10, 11, 45).toDate());%0A        order.setContents(%22yum %22 + count);%0A        ds.save(order);
 %0A    %7D%0A%0A

generate test data with -Dtestdata