package com.signavio.workflow.connector.example;

import com.signavio.workflow.connector.example.customer.Customer;
import com.signavio.workflow.connector.example.customer.CustomerOption;
import com.signavio.workflow.connector.example.customer.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.*;
import spark.utils.IOUtils;

import java.io.IOException;

import static spark.Spark.*;

/**
 * Starts an embedded HTTP server on {@link Server#PORT} which provides the connector interface.
 */
public class Server {

  private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);

  /**
   * Defines the port the embedded HTTP server will bind to.
   */
  private static final int PORT = 5000;
  private static CustomerService customerService = new CustomerService();
  private static String descriptor;

  public static void main(String[] arguments) {
    try {
      customerService.loadData("data.json");
      descriptor = IOUtils.toString(Server.class.getClassLoader().getResourceAsStream("descriptor.json"));
    } catch (IOException exception) {
      LOGGER.error("Couldn't initialize the data.", exception);
      return;
    }

    // Server configuration
    port(PORT);
    ResponseTransformer responseTransformer = new JsonTransformer();

    // Request handlers
    get("/", (request, response) -> descriptor);
    get("/customer/options", Server::serveOptions, responseTransformer);
    get("/customer/options/:id", Server::serveOption, responseTransformer);
    get("/customer/:id", Server::serveRecord, responseTransformer);

    // Map a Java exception type to an HTTP response status.
    exception(NotFoundException.class, (error, request, response) -> {
      response.status(404);
    });

    // Define the response content type for all endpoints.
    after((request, response) -> {
      response.type("application/json;charset=UTF-8");
    });
  }

  /**
   * Serves <a href="https://docs.signavio.com/userguide/workflow/en/integration/connectors.html#record-details">Record details</a>.
   */
  private static Object serveRecord(Request request, Response response) throws NotFoundException {
      String id = request.params("id");
      LOGGER.info("Fetching customer with ID: " + id);
      Customer customer = customerService.find(id);
      if (customer == null) {
        LOGGER.info("Did not find customer.");
        throw new NotFoundException();
      }
      return customer;
  }

  /**
   * Serves a <a href="https://docs.signavio.com/userguide/workflow/en/integration/connectors.html#record-type-option-single-option">Record type option (single option)</a>.
   */
  private static Object serveOption(Request request, Response response) throws NotFoundException {
      String id = request.params("id");
      LOGGER.info("Fetching customer option with ID: " + id);
      CustomerOption option = customerService.findOneOption(id);
      if (option == null) {
        LOGGER.info("Did not find option.");
        throw new NotFoundException();
      }
      return option;
  }

  /**
   * Serves <a href="https://docs.signavio.com/userguide/workflow/en/integration/connectors.html#connector-type-options">Record type options</a>.
   */
  private static Object serveOptions(Request request, Response response) {
    String filter = request.queryParams("filter");
    return customerService.findOptions(filter);
  }

  private static class NotFoundException extends Exception {

  }
}

@@ -54,139 +54,30 @@
 ort 
-com.signavio.workflow.connector.example.customer.Customer;%0Aimport com.signavio.workflow.connector.example.customer.CustomerO
+java.io.IOExce
 ption;%0A
+%0A
 impo
@@ -128,31 +128,17 @@
 ustomer.
-CustomerService
+*
 ;%0Aimport
@@ -237,37 +237,8 @@
 s;%0A%0A
-import java.io.IOException;%0A%0A
 impo
@@ -1443,158 +1443,125 @@
  // 
-Map a Java exception type to an HTTP response status.%0A    exception(NotFoundException.class, (error, request, response) -%3E %7B%0A      response.status(404
+Log requests.%0A    before((request, response) -%3E %7B%0A      LOGGER.info(%22%E2%86%92 %7B%7D %7B%7D%22, request.requestMethod(), request.uri()
 );%0A 
@@ -1724,16 +1724,389 @@
 %0A    %7D);
+%0A%0A    // Log responses.%0A    afterAfter((request, response) -%3E %7B%0A      LOGGER.info(%22%E2%86%90 HTTP %7B%7D%22, response.status());%0A    %7D);%0A%0A    // Map a Java exception type to an HTTP response status.%0A    exception(NotFoundException.class, (error, request, response) -%3E %7B%0A      response.status(404);%0A      response.type(%22application/json;charset=UTF-8%22);%0A      response.body(%22%7B%7D%22);%0A    %7D);
 %0A  %7D%0A%0A  
@@ -2343,34 +2343,32 @@
 Exception %7B%0A    
-  
 String id = requ
@@ -2389,65 +2389,8 @@
 %22);%0A
-      LOGGER.info(%22Fetching customer with ID: %22 + id);%0A  
     
@@ -2435,26 +2435,24 @@
 nd(id);%0A    
-  
 if (customer
@@ -2467,57 +2467,8 @@
 ) %7B%0A
-        LOGGER.info(%22Did not find customer.%22);%0A  
     
@@ -2496,38 +2496,34 @@
 xception();%0A    
-  %7D%0A  
+%7D%0A
     return custo
@@ -2820,18 +2820,16 @@
 n %7B%0A    
-  
 String i
@@ -2858,72 +2858,8 @@
 %22);%0A
-      LOGGER.info(%22Fetching customer option with ID: %22 + id);%0A  
     
@@ -2921,18 +2921,16 @@
 d);%0A    
-  
 if (opti
@@ -2947,55 +2947,8 @@
 ) %7B%0A
-        LOGGER.info(%22Did not find option.%22);%0A  
     
@@ -2984,22 +2984,18 @@
 ();%0A    
-  %7D%0A  
+%7D%0A
     retu
@@ -3408,15 +3408,14 @@
 ption %7B%0A
-%0A
   %7D%0A%7D%0A

Consolidate logging