package com.google.job.servlets;

import com.google.job.data.*;
import com.google.utils.FireStoreUtils;
import com.google.utils.ServletUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.time.LocalDate;
import java.util.stream.Collectors;

/** Servlet that handles posting new job posts. */
@WebServlet("/jobs")
public final class NewJobServlet extends HttpServlet {

    private JobsDatabase jobsDatabase;

    @Override
    public void init() {
        this.jobsDatabase = new JobsDatabase();
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            // Gets job post from the form
            Job job = parseJobPost(request);

            // Stores job post into the database
            storeJobPost(job);

            // Sends the success status code in the response
            response.setStatus(HttpServletResponse.SC_OK);
        } catch (IOException | IllegalArgumentException e) {
            // Sends the fail status code in the response
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }

    private Job parseJobPost(HttpServletRequest request) throws IOException, IllegalArgumentException {
        // Parses job object from the POST request
        BufferedReader bufferedReader = request.getReader();
        String jobPostJsonStr = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())).trim();

        if (StringUtils.isBlank(jobPostJsonStr)) {
            throw new IllegalArgumentException("Json for Job object is Empty");
        }

        Job job = ServletUtils.parseFromJsonUsingGson(jobPostJsonStr, Job.class);
        return job;
    }

    private void storeJobPost(Job job) {
        this.jobsDatabase.addJob(job);
    }
}

@@ -23,24 +23,77 @@
 .servlets;%0A%0A
+import com.google.cloud.firestore.DocumentReference;%0A
 import com.g
@@ -110,16 +110,16 @@
 data.*;%0A
-
 import c
@@ -230,24 +230,63 @@
 ringUtils;%0A%0A
+import javax.servlet.ServletException;%0A
 import javax
@@ -536,16 +536,100 @@
 alDate;%0A
+import java.util.concurrent.ExecutionException;%0Aimport java.util.concurrent.Future;%0A
 import j
@@ -1395,16 +1395,35 @@
 ception 
+%7C ServletException 
 e) %7B%0A   
@@ -2188,48 +2188,382 @@
 ob) 
-%7B%0A        this.jobsDatabase.addJob(job);
+throws ServletException %7B%0A        Future%3CDocumentReference%3E future = this.jobsDatabase.addJob(job);%0A%0A        try %7B%0A            // Synchronizes and blocks the operation.%0A            future.get();%0A        %7D catch (InterruptedException e) %7B%0A            throw new ServletException(e);%0A        %7D catch (ExecutionException e) %7B%0A            throw new ServletException(e);%0A        %7D
 %0A   

Synchronize add job post in servlet