package org.atlasapi.system;

import com.google.api.client.repackaged.com.google.common.base.Strings;
import com.google.common.base.Optional;

import org.atlasapi.application.sources.SourceIdCodec;
import org.atlasapi.application.writers.SourceWithIdWriter;
import org.atlasapi.content.QueryParseException;
import org.atlasapi.media.entity.Publisher;
import org.atlasapi.output.EntityListWriter;
import org.atlasapi.output.ErrorResultWriter;
import org.atlasapi.output.ErrorSummary;
import org.atlasapi.output.OutputContext;
import org.atlasapi.output.ResponseWriter;
import org.atlasapi.output.ResponseWriterFactory;
import org.atlasapi.output.writers.SourceWriter;
import org.atlasapi.query.common.context.QueryContext;
import org.atlasapi.query.common.exceptions.QueryExecutionException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import java.io.IOException;

@Controller
public class SystemSourcesController {

    private final ResponseWriterFactory writerResolver;
    private final EntityListWriter<Publisher> sourcesWriter;
    private final SourceIdCodec sourceIdCodec;

    private SystemSourcesController(SourceIdCodec sourceIdCodec) {
        this.sourceIdCodec = sourceIdCodec;
        sourcesWriter = new SourceWithIdWriter(sourceIdCodec, "source", "sources");
        writerResolver = new ResponseWriterFactory();
    }

    public static SystemSourcesController create(SourceIdCodec sourceIdCodec) {
        return new SystemSourcesController(sourceIdCodec);
    }

    @RequestMapping({ "/system/sources/{sourceId}.*", "/system/sources.*" })
    public void listSources(
            HttpServletRequest request,
            HttpServletResponse response,
            @PathVariable String sourceId
    ) throws QueryParseException, QueryExecutionException, IOException {

        ResponseWriter writer = writerResolver.writerFor(request, response);
        try {
            writer.startResponse();
            if(Strings.isNullOrEmpty(sourceId)) {
                listAllSources(writer, request);
            } else {
                listSingleSource(writer, request, sourceId);
            }
            writer.finishResponse();
        } catch (Exception e) {
            ErrorSummary summary = ErrorSummary.forException(e);
            new ErrorResultWriter().write(summary, writer, request, response);
        }
    }

    private void listAllSources(
            ResponseWriter writer,
            HttpServletRequest request
    ) throws IOException {
        writer.writeList(
                sourcesWriter,
                Publisher.all(),
                OutputContext.valueOf(QueryContext.standard(request))
        );
    }

    private void listSingleSource(
            ResponseWriter writer,
            HttpServletRequest request,
            String sourceId
    ) throws IOException {
        Optional<Publisher> publisher = sourceIdCodec.decode(sourceId);
        if(!publisher.isPresent()) {
            throw new IOException("Publisher ID not found: " + sourceId);
        }

        writer.writeObject(
                sourcesWriter,
                sourceIdCodec.decode(sourceId).get(),
                OutputContext.valueOf(QueryContext.standard(request))
        );
    }

}

@@ -27,80 +27,8 @@
 m;%0A%0A
-import com.google.api.client.repackaged.com.google.common.base.Strings;%0A
 impo
@@ -63,17 +63,16 @@
 tional;%0A
-%0A
 import o
@@ -542,57 +542,8 @@
 ry;%0A
-import org.atlasapi.output.writers.SourceWriter;%0A
 impo
@@ -934,94 +934,8 @@
 se;%0A
-import javax.ws.rs.Path;%0Aimport javax.ws.rs.PathParam;%0Aimport javax.ws.rs.QueryParam;%0A
 impo
@@ -1634,29 +1634,8 @@
 %7D.*%22
-, %22/system/sources.*%22
  %7D)%0A
@@ -1992,132 +1992,8 @@
 ();%0A
-            if(Strings.isNullOrEmpty(sourceId)) %7B%0A                listAllSources(writer, request);%0A            %7D else %7B%0A    
     
@@ -2049,22 +2049,8 @@
 d);%0A
-            %7D%0A
     
@@ -2250,32 +2250,32 @@
 est, response);%0A
-
         %7D%0A    %7D%0A
@@ -2267,32 +2267,666 @@
        %7D%0A    %7D%0A%0A
+    @RequestMapping(%7B%22/system/sources.*%22%7D)%0A    public void listAllSources(%0A            HttpServletRequest request,%0A            HttpServletResponse response%0A    ) throws QueryParseException, QueryExecutionException, IOException %7B%0A%0A        ResponseWriter writer = writerResolver.writerFor(request, response);%0A%0A        try %7B%0A            writer.startResponse();%0A            listAllSources(writer, request);%0A            writer.finishResponse();%0A        %7D catch (Exception e) %7B%0A            ErrorSummary summary = ErrorSummary.forException(e);%0A            new ErrorResultWriter().write(summary, writer, request, response);%0A        %7D%0A%0A    %7D%0A%0A
     private void

fix path variable mapping on sources endpoint