<%@ page extends="oneit.servlets.process.AJAXProcessJSP" %>

<%@ page import="oneit.servlets.jsp.*,oneit.servlets.orm.*,oneit.servlets.objstore.*,oneit.servlets.jsp.tabs.*,oneit.logging.*,oneit.sql.*,oneit.objstore.services.*" %>
<%@ page import="performa.orm.*,performa.orm.types.*,performa.utils.*, java.util.*, oneit.objstore.*, oneit.servlets.process.*, oneit.utils.*" %>
<%@ page import="oneit.utils.filter.Filter, oneit.objstore.rdbms.filters.EqualsFilter" %>

<%@ include file="../../../setuprequest.jsp" %>

<%! protected String getName (ServletConfig config) { return "save_job_match_answers_jsp"; } %>
<oneit:form method="POST">
<%
    ORMProcessState     process         =   (ORMProcessState)ProcessDecorator.getDefaultProcess(request);
    ObjectTransaction   objTran         =   ObjectTransaction.getTransaction();
    int                 questionNumber  =   Integer.valueOf(request.getParameter ("questionNumber")) - 1;
    int                 actualNumber    =   Integer.valueOf(request.getParameter ("actualNumber"));
    int                 answerNo        =   Integer.valueOf(request.getParameter ("answerNo"));
    boolean             complete        =   Boolean.valueOf(request.getParameter ("completeApplication"));
    Long                jobAppId        =   (Long) process.getAttribute("JobApplicationID");
    JobApplication      jobApplication  =   JobApplication.getJobApplicationByID(objTran, jobAppId);
    List<Question>      allQuestions    =   (List<Question>) process.getAttribute("AllQuestions");

    
    jobApplication  =   (JobApplication) jobApplication.getInTransaction (objTran);

    Debug.assertion (jobApplication.getObjectWriteability() == FieldWriteability.TRUE, "Must be able to edit");

    Question        question    =   (Question) allQuestions.get(questionNumber).getInTransaction (objTran);
    Filter<Answer>  filter      =   Answer.SearchByAll().andQuestion(new EqualsFilter<>(question));
    Answer          answer      =   jobApplication.pipelineJobApplication().toCandidate().toProfileAssessmentAnswers(filter).val();

    DataMap  dataMap  = DataMap.getDataMap(request, true);
    dataMap.removeORMHandlersForObject(answer);

    answer.setAnswerNo(answerNo);

    if(complete)
    {   
//        jobApplication.setApplicationStatus(ApplicationStatus.SUBMITTED);
//        jobApplication.setSubmittedDate(new Date());
//        
//        AnalysisEngine.analyseAnswers(jobApplication.getCandidate(), jobApplication.getJob().getLevel(), jobApplication.getCompletedAnswers());


//        completeProcessRestartAndRestoreAttribs(process, request);
//        
//        jobApplication  = (JobApplication) process.getAttribute("JobApplication");
//
//        // Update company in intercom
//        if(jobApplication != null && jobApplication.getJob().getCompanyUser() != null)
//        {
//            IntercomUtils.updateCompany(jobApplication.getJob().getCompanyUser().getCompany());
//        }
    }
    else
    {
        request.getSession().setAttribute ("historyQstNo", questionNumber + 1);
        request.getSession().setAttribute ("historyActualNo", actualNumber);
    }
%>
</oneit:form>