package com.google.job.servlets;

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

import javax.servlet.ServletException;
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.concurrent.ExecutionException;
import java.util.concurrent.Future;
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 | ServletException 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) throws ServletException {
        Future<DocumentReference> future = this.jobsDatabase.addJob(job);

        try {
            // Synchronizes and blocks the operation.
            future.get();
        } catch (InterruptedException e) {
            throw new ServletException(e);
        } catch (ExecutionException e) {
            throw new ServletException(e);
        }
    }
}

@@ -2044,25 +2044,28 @@
         Job 
-j
+rawJ
 ob = Servlet
@@ -2120,16 +2120,511 @@
 .class);
+%0A%0A        Job job = Job.newBuilder()%0A                .setJobStatus(JobStatus.ACTIVE)%0A                .setJobTitle(rawJob.getJobTitle())%0A                .setLocation(rawJob.getLocation())%0A                .setJobDescription(rawJob.getJobDescription())%0A                .setJobPay(rawJob.getJobPay())%0A                .setRequirements(rawJob.getRequirements())%0A                .setPostExpiry(rawJob.getPostExpiry())%0A                .setJobDuration(rawJob.getJobDuration())%0A                .build();%0A%0A
 %0A       

Make newly created job post active