package dk.statsbiblioteket.newspaper.processmonitor.backend;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Service class to expose retrieval of Batch (@see Batch) and Events (@see Event) objects for monitoring progress and state
 */

@Component
@Path("/")
public class Batches {

    @Autowired
    private DataSourceCombiner dataSource;

    /**
     * Retrieves a list of all known Batch objects (@see Batch).
     *
     * @param details If true, will also include the available details for each event in the Batch objects. Defaults to false.
     * @return List<Batch> as JSON data.
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Batch> getBatches(@QueryParam("details") @DefaultValue("false") boolean details) {
        return convertBatchList(dataSource.getAsOneDataSource().getBatches(details, null));
    }

    private List<Batch> convertBatchList(List<dk.statsbiblioteket.newspaper.processmonitor.datasources.Batch> batches) {
        ArrayList<Batch> result = new ArrayList<Batch>(batches.size());
        for (dk.statsbiblioteket.newspaper.processmonitor.datasources.Batch batch : batches) {
            result.add(convert(batch));
        }
        return result;
    }

    /**
     * Retrieves a specific batch given it's ID.
     *
     * @param batchID The ID of the specific batch
     * @param details If true, will also include the available details for each event in the Batch. Defaults to false.
     * @return Batch as JSON Object
     */
    @GET
    @Path("{batchID}")
    @Produces(MediaType.APPLICATION_JSON)
    public Batch getSpecificBatch(@PathParam("batchID") String batchID,
                                  @QueryParam("details") @DefaultValue("false") boolean details) {
        return convert(dataSource.getAsOneDataSource().getBatch(batchID, details));
    }

    private Batch convert(dk.statsbiblioteket.newspaper.processmonitor.datasources.Batch batch) {
        Batch result = new Batch();
        result.setBatchID(batch.getBatchID());
        result.setEvents(convert(batch.getEventList()));
        return result;
    }

    private Map<String, Event> convert(List<dk.statsbiblioteket.newspaper.processmonitor.datasources.Event> eventList) {
        Map<String, Event> result = new HashMap<String, Event>(eventList.size());
        for (dk.statsbiblioteket.newspaper.processmonitor.datasources.Event event : eventList) {
            result.put(event.getEventID(), convert(event));
        }
        return result;
    }

    /**
     * Retrieves a specific Event for a specific Batch.
     *
     * @param batchID The ID of the specific batch
     * @param eventID The ID of the specific event
     * @param details If true, will also include the available details. Defaults to false.
     */
    @GET
    @Path("{batchID}/{eventID}")
    @Produces(MediaType.APPLICATION_JSON)
    public Event getSpecificBatchEvent(@PathParam("batchID") String batchID, @PathParam("eventID") String eventID,
                                       @QueryParam("details") @DefaultValue("false") boolean details) {
        return convert(dataSource.getAsOneDataSource().getBatchEvent(batchID, eventID, details));
    }

    private Event convert(dk.statsbiblioteket.newspaper.processmonitor.datasources.Event batchEvent) {
        Event result = new Event();
        result.setDetails(batchEvent.getDetails());
        result.setSuccess(batchEvent.isSuccess());
        return result;
    }

}

@@ -169,16 +169,100 @@
 onent;%0A%0A
+import dk.statsbiblioteket.newspaper.processmonitor.datasources.NotFoundException;%0A%0A
 import j
@@ -448,23 +448,102 @@
 .rs.
-core.MediaType;
+WebApplicationException;%0Aimport javax.ws.rs.core.MediaType;%0Aimport javax.ws.rs.core.Response;%0A
 %0Aimp
@@ -2267,32 +2267,50 @@
 lean details) %7B%0A
+        try %7B%0A    
         return c
@@ -2369,32 +2369,321 @@
 hID, details));%0A
+        %7D catch (NotFoundException e) %7B%0A            throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)%0A                    .entity(%22Failed to get batch with ID: %22 + batchID)%0A                    .type(MediaType.TEXT_PLAIN)%0A                    .build());%0A        %7D%0A
     %7D%0A%0A    priva
@@ -3908,32 +3908,50 @@
 lean details) %7B%0A
+        try %7B%0A    
         return c
@@ -4028,24 +4028,349 @@
  details));%0A
+        %7D catch (NotFoundException e) %7B%0A            throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND)%0A                    .entity(%22Failed to get event with ID: %22 + eventID + %22 from batch with ID: %22 + batchID)%0A                    .type(MediaType.TEXT_PLAIN)%0A                    .build());%0A        %7D%0A
     %7D%0A%0A    p

handle NotFoundExceptions from backend in webservice class