Package com.vst.webapp.action

Source Code of com.vst.webapp.action.DocumentationQuestionFormController

package com.vst.webapp.action;

import com.vst.model.Answer;
import com.vst.model.BuildingObject;
import com.vst.model.ObjectAnswer;
import com.vst.model.Question;
import com.vst.service.AnswerManager;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ObjectAnswerManager;
import com.vst.service.QuestionManager;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
* Created by IntelliJ IDEA.
* User: ALEXEI
* Date: 11.02.2008
* Time: 12:13:52
* To change this template use File | Settings | File Templates.
*/
public class DocumentationQuestionFormController extends BaseFormController {
    private BuildingObjectManager buildingObjectManager = null;
    private QuestionManager questionManager = null;
    private AnswerManager answerManager = null;
    private ObjectAnswerManager objectAnswerManager = null;

    public void setAnswerManager(AnswerManager answerManager) {
        this.answerManager = answerManager;
    }

    public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
        this.buildingObjectManager = buildingObjectManager;
    }

    public void setQuestionManager(QuestionManager questionManager) {
        this.questionManager = questionManager;
    }

    public void setObjectAnswerManager(ObjectAnswerManager objectAnswerManager) {
        this.objectAnswerManager = objectAnswerManager;
    }

    public DocumentationQuestionFormController() {
        setCommandName("buildingObject");
        setCommandClass(BuildingObject.class);
    }

    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {
        //   request.setAttribute("authentificationElementList",authentificationElementManager.getAuthentificationElements(null));
        if (!isFormSubmission(request)) {
            String objectId = request.getParameter("objectId");
            boolean forDocuments = request.getParameter("forDocuments") != null;
            if (forDocuments) {
                request.setAttribute("addition", "&forDocuments");
            }

            buildingObjectManager.savePreparedDocumentationQuestions(new Integer(objectId));

            BuildingObject buildingObject =
              buildingObjectManager.getBuildingObjectForList(new Integer(objectId));

            //checking if the objectanswer list is not empty. Otherwise filling it by the documenttation questions containig in the base

//            if (buildingObject.getDocumentationQuestions().size() == 0) {
//                //filling the list from the database
//                List objectAnswerList = buildingObjectManager.getDocumentationQuestions();
//                for (int i = 0; i < objectAnswerList.size(); i++) {
//                    Question question = (Question) objectAnswerList.get(i);
//                    question = questionManager.getQuestion(question.getQuestionId().toString());
//                    ObjectAnswer objectAnswer = new ObjectAnswer();
//                    objectAnswer.setObjectConstruction(null);
//                    objectAnswer.setQuestion(question);
//                    if (!question.isFewAnswers()) {
//                        Answer answer = new Answer();
//                        answer.setAnswerId(new Integer(-1));
//                        objectAnswer.getAnswers().add(answer);
//                    } else {
//                        for (int j = 0; j < question.getAnswers().size(); j++) {
//                            Answer answer = new Answer();
//                            answer.setAnswerId(new Integer(-1));
//                            objectAnswer.getAnswers().add(answer);
//                        }
//                    }
//                    objectAnswer.setObjectConstruction(null);
//                    objectAnswer.setBuildingObject(buildingObject);
//                    buildingObject.getDocumentationQuestions().add(objectAnswer);
//                }
//                buildingObjectManager.saveBuildingObject(buildingObject);
//            }
            int num = 0;
            String questionId = request.getParameter("questionId");
            Integer currentQuestionId = null;
            if (questionId != null && !questionId.equals("null")) {
                //trying to retrieve the current answer from url
                currentQuestionId = new Integer(questionId);
            }

            //getting the number of question in the list with the current ID to be asked the first in the current list
            int currentPosition = 0;
            if (currentQuestionId != null) {
                for (int i = 0; i < buildingObject.getDocumentationQuestions().size(); i++) {
                    ObjectAnswer o = (ObjectAnswer) buildingObject.getDocumentationQuestions().get(i);
                    if (o.getQuestion().getQuestionId().equals(currentQuestionId)) {
                        currentPosition = i;
                        break;
                    }
                }
            }

            //putting all current questions to the list
            for (int i = currentPosition; i < buildingObject.getDocumentationQuestions().size(); i++) {

                ObjectAnswer o = (ObjectAnswer) buildingObject.getDocumentationQuestions().get(i);

                if ((!o.getQuestion().isForObjectStructure() && forDocuments) || (o.getQuestion().isForObjectStructure() && !forDocuments)) {
                    //getting the hint for the question if it exists
                    o.setHasHint(questionManager.hasHint(o.getQuestion().getQuestionId().toString()));
                    buildingObject.getCurrentDocumentationQuestions().add(o);
                    num++;
                    if (num == buildingObject.getQuestionNumber()) {
                        break;
                    }
                }
            }

            //getting previous and next questions
            List currentAnswerList = buildingObject.getCurrentDocumentationQuestions();
            Integer lastQuestionId = null;
            Integer firstQuestionId = null;
            if (currentAnswerList.size() != 0) {
                lastQuestionId = ((ObjectAnswer) currentAnswerList.get(currentAnswerList.size() - 1)).getQuestion().getQuestionId();
                firstQuestionId = ((ObjectAnswer) currentAnswerList.get(0)).getQuestion().getQuestionId();
            }

            if (lastQuestionId != null) {
                ObjectAnswer nextObjectAnswer = (buildingObjectManager.getNextQestion(buildingObject.getObjectId(), lastQuestionId, !forDocuments));
                if (nextObjectAnswer != null) {
                    request.setAttribute("nextId", nextObjectAnswer.getQuestion().getQuestionId());
                    buildingObject.setNextId(nextObjectAnswer.getQuestion().getQuestionId());
                }

            }

            if (firstQuestionId != null) {
                ObjectAnswer prevObjectAnswer = (buildingObjectManager.getPrevQestion(buildingObject.getObjectId(), firstQuestionId, !forDocuments));
                if (prevObjectAnswer != null) {
                    request.setAttribute("prevId", prevObjectAnswer.getQuestion().getQuestionId());
                    buildingObject.setPrevId(prevObjectAnswer.getQuestion().getQuestionId());
                }

            }


            if (buildingObject.getDocumentationQuestions().size() == 0) {
                request.setAttribute("noQuestions", "true");
            }

            if (currentQuestionId == null || currentQuestionId.equals(new Integer(0))) {
                request.setAttribute("theFirst", "true");

            }
            buildingObject.setNoValidation(false);
           // buildingObjectManager.evict(buildingObject);
            return buildingObject;
        }
        return super.formBackingObject(request);
    }

    public ModelAndView onSubmit(HttpServletRequest request,
                                 HttpServletResponse response, Object command,
                                 BindException errors)
            throws Exception {

        if (log.isDebugEnabled()) {
            log.debug("entering 'onSubmit' method...");
        }
        boolean forDocuments = request.getParameter("forDocuments") != null;
        String forDocumentsParameter = "";
        if (forDocuments) {
            forDocumentsParameter = "&forDocuments";
        }
        BuildingObject buildingObject = (BuildingObject) command;
        // boolean isNew = (buildingObject.getDefectZoneId() == null);
        Locale locale = request.getLocale();
        Integer nextId = null;
        Integer prevId = null;

        List currentAnswerList = buildingObject.getCurrentDocumentationQuestions();
        Integer lastQuestionId = null;
        Integer firstQuestionId = null;
        if (currentAnswerList.size() != 0) {
            lastQuestionId = ((ObjectAnswer) currentAnswerList.get(currentAnswerList.size() - 1)).getQuestion().getQuestionId();
            firstQuestionId = ((ObjectAnswer) currentAnswerList.get(0)).getQuestion().getQuestionId();
        }

        //getting previous and next qestion blocks
        ObjectAnswer nextObjectAnswer = null;
        ObjectAnswer prevObjectAnswer = null;
        if (lastQuestionId != null) {
            nextObjectAnswer = (buildingObjectManager.getNextQestion(buildingObject.getObjectId(), lastQuestionId, !forDocuments));
        }
        if (firstQuestionId != null) {
            prevObjectAnswer = (buildingObjectManager.getPrevQestion(buildingObject.getObjectId(), firstQuestionId, !forDocuments));
        }
        if (nextObjectAnswer != null) {
            nextId = nextObjectAnswer.getQuestion().getQuestionId();
        }
        if (prevObjectAnswer != null) {
            prevId = prevObjectAnswer.getQuestion().getQuestionId();
        }
        request.setAttribute("nextId", nextId);
        request.setAttribute("prevId", prevId);


        if (request.getParameter("save") != null || request.getParameter("saveAndGo") != null) {
            //saving and leaving the page
            for (int i = 0; i < currentAnswerList.size(); i++) {
              String contents = null;
              String comment = null;
              List<Answer> templateAswers = new ArrayList<Answer>();

                ObjectAnswer o = (ObjectAnswer) currentAnswerList.get(i);


                if (o.getQuestion().getAnswers().size() != 0) {
                    if (!o.getQuestion().isFewAnswers()) {
                      Answer answer = (Answer) o.getAnswers().get(0);
                        if (answer.getAnswerId() != null && answer.getAnswerId()!=-1) {
                            templateAswers.add(answer);
                        }
                    }

                    if (o.getQuestion().isFewAnswers()) {
                        List answerList = o.getAnswers();
                        for (int j = 0; j < answerList.size(); j++) {
                            Answer answer = (Answer) answerList.get(j);
                            if (answer.getAnswerId() != null || answer.getAnswerId() != -1) {
                              templateAswers.add(answer);
                            }
                        }


                    }
                } else {
                    contents = o.getAnswerContents();
                }
                comment = o.getAnswerComment();

                objectAnswerManager
                  .updateObjectAnswerValues(
                      o.getObjectAnswerId(),
                      templateAswers, contents, comment);
//                buildingObjectManager.saveBuildingObject(buildingObject);
            }
            //System.out.println("prevId="+prevId);
            //System.out.println("nextId="+nextId);
            if (request.getParameter("save") != null) {
                return new ModelAndView("redirect:buildingObjects.html");
            }

            if (request.getParameter("saveAndGo") != null) {
                return new ModelAndView("redirect:documentQuestions.html?objectId=" + buildingObject.getObjectId() + "&questionId=" + nextId + forDocumentsParameter);
            }


        }

        if (request.getParameter("back") != null) {

            return new ModelAndView("redirect:documentQuestions.html?objectId=" + buildingObject.getObjectId() + "&questionId=" + prevId + forDocumentsParameter);
        }

        return new ModelAndView(getSuccessView());
    }


    public ModelAndView processFormSubmission(HttpServletRequest request,
                                              HttpServletResponse response,
                                              Object command,
                                              BindException errors)
            throws Exception {


        return super.processFormSubmission(request,
                response,
                command,
                errors);
    }


    protected void onBind(HttpServletRequest request, Object command)
            throws Exception {
        // if the user is being deleted, turn off validation
        BuildingObject buildingObject = (BuildingObject) command;
        if (buildingObject.isNoValidation() || request.getParameter("back") != null) {
            super.setValidateOnBinding(false);
        } else {
            super.setValidateOnBinding(true);
        }
    }

}
TOP

Related Classes of com.vst.webapp.action.DocumentationQuestionFormController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.