package com.google.job.servlets;

import com.google.cloud.firestore.DocumentReference;
import com.google.job.data.Job;
import com.google.job.data.JobsDatabase;
import com.google.utils.ServletUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/** Servlet that handles updating an existing job posts. */
@WebServlet("/jobs")
public final class UpdateJobServlet extends MyServlet {
    private static final String JOB_ID_FIELD = "jobId";

    private JobsDatabase jobsDatabase;

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

    @Override
    public void doPatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // Gets the target job post id
            String jobId = getJobId(request, JOB_ID_FIELD, "");

            // Gets job post from the form
            Job job = ServletUtils.parseJobPost(request);

            // Stores job post into the database
            storeJobPost(jobId, 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 String getJobId(HttpServletRequest request, String paramName, String defaultValue)
            throws ServletException, IOException {
        String jobId = ServletUtils.getStringParameter(request, paramName, defaultValue);

        if (jobId.isEmpty()) {
            throw new IllegalArgumentException("Empty Job Id");
        }

        try {
            if (JobsDatabase.isJobIdExist(jobId)) {
                return jobId;
            }

            throw new IllegalArgumentException("Invalid Job Id");
        } catch (InterruptedException e) {
            throw new ServletException(e);
        } catch (ExecutionException e) {
            System.err.println("Error occur: " + e.getCause());
            throw new IOException("Error occur");
        }
    }

    private void storeJobPost(String jobId, Job job) throws ServletException {
        Future<DocumentReference> future = this.jobsDatabase.addJob(job);

        try {
            // Synchronizes and blocks the operation.
            future.get();
            this.jobsDatabase.updateJobId(jobId);
        } catch (InterruptedException e) {
            throw new ServletException(e);
        } catch (ExecutionException e) {
            System.err.println("Error occur: " + e.getCause());
        }
    }
}

@@ -1048,26 +1048,8 @@
 uest
-, JOB_ID_FIELD, %22%22
 );%0A%0A
@@ -1639,47 +1639,8 @@
 uest
-, String paramName, String defaultValue
 )%0A  
@@ -1756,18 +1756,24 @@
 st, 
-paramName,
+JOB_ID_FIELD, /*
  def
@@ -1781,16 +1781,23 @@
 ultValue
+= */ %22%22
 );%0A%0A    

Refactor code