Package com.vst.webapp.action

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

package com.vst.webapp.action;

import com.lowagie.text.rtf.RtfWriter2;
import com.vst.model.Answer;
import com.vst.model.ConstructionType;
import com.vst.model.Question;
import com.vst.service.*;
import org.apache.commons.lang.StringUtils;
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;

public class QuestionFormController extends BaseFormController {
    private QuestionManager questionManager = null;
    private ConstructionTypeManager constructionTypeManager = null;
    private QuestionTypeManager questionTypeManager = null;
    private AnswerManager answerManager = null;
    private DocumentTypeManager documentTypeManager = null;

    public void setDocumentTypeManager(DocumentTypeManager documentTypeManager) {
        this.documentTypeManager = documentTypeManager;
    }

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

    public void setConstructionTypeManager(ConstructionTypeManager constructionTypeManager) {
        this.constructionTypeManager = constructionTypeManager;
    }

    public void setQuestionTypeManager(QuestionTypeManager questionTypeManager) {
        this.questionTypeManager = questionTypeManager;
    }

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

    public QuestionFormController() {
        setCommandName("question");
        setCommandClass(Question.class);
    }

    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {
        

        request.setAttribute("questionVisualTypeList", questionManager.getQuestionVisualList());
        request.setAttribute("constructionTypeList", constructionTypeManager.getConstructionTypes(null));
        request.setAttribute("questionTypeList", questionTypeManager.getQuestionTypes(null));
        request.setAttribute("documentTypeList", documentTypeManager.getDocumentTypes(null));

        if (!isFormSubmission(request)) {
            String questionId = request.getParameter("questionId");
            Question question = null;

            if (!StringUtils.isEmpty(questionId)) {
                question = questionManager.getQuestion(questionId);
                questionManager.evict(question);
                questionManager.setQuestionVisualType(question);
                questionManager.makeNotNull(question);
            } else {
                question = new Question();
            }
            if (request.getParameter("edited") != null) {
                request.setAttribute("addition", "?edited=1");
                question.setEdited(true);
            }
            question.setDocLocation(request.getParameter("docLocation"));

            return question;
        }
        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...");
        }

        Question question = (Question) command;
        boolean isNew = (question.getQuestionId() == null);
        Locale locale = request.getLocale();

        if (request.getParameter("delete") != null) {
            questionManager.removeQuestion(question.getQuestionId().toString());

            saveMessage(request, getText("question.deleted", locale));
        } else {
            Integer questionTypeId = question.getQuestionType().getQuestionTypeId();
            if (!questionTypeId.equals(new Integer(-1))) {
                question.setQuestionType(questionTypeManager.getQuestionType(questionTypeId.toString()));
            }

            Integer documentTypeId = question.getDocumentType().getDocumentTypeId();
            if (!documentTypeId.equals(new Integer(-1))) {
                question.setDocumentType(documentTypeManager.getDocumentType(documentTypeId.toString()));
            } else {
                question.setDocumentType(null);
            }


            if (question.getQuestionType().getQuestionTypeId().equals(new Integer(0))) {
                List constructionTypeList = new ArrayList();
                List constructionTypeListFromForm = question.getConstructionTypes();
                for (int i = 0; i < constructionTypeListFromForm.size(); i++) {
                    ConstructionType constructionType = (ConstructionType) constructionTypeListFromForm.get(i);
                    constructionTypeList.add(constructionTypeManager.getConstructionType(constructionType.getConstructionTypeId().toString()));
                }
                question.setConstructionTypes(constructionTypeList);
            }


            List answerListFromForm = question.getAnswers();
            for (int i = 0; i < answerListFromForm.size(); i++) {
                Answer answer = (Answer) answerListFromForm.get(i);
                answer.setQuestion(null);
                answerManager.saveAnswer(answer);
            }
            questionManager.saveQuestion(question);
            for (int i = 0; i < answerListFromForm.size(); i++) {
                Answer answer = (Answer) answerListFromForm.get(i);
                answer.setQuestion(question);
                answerManager.saveAnswer(answer);
            }
            String key = (isNew) ? "question.added" : "question.updated";
            saveMessage(request, getText(key, locale));
            if (question.isEdited()) {
                return new ModelAndView("redirect:updating.html?id=" + question.getQuestionId() + "&fieldId=" + request.getParameter("fieldId"));
            }
        }

        return new ModelAndView("redirect:" + question.getDocLocation());
    }

    public ModelAndView processFormSubmission(HttpServletRequest request,
                                              HttpServletResponse response,
                                              Object command,
                                              BindException errors)
            throws Exception {
        Question question = (Question) command;
        //System.out.println("QuestiontypeId=="+question.getQuestionTypeId());
        if (request.getParameter("addConstructionType") != null) {
            question.getConstructionTypes().add(new ConstructionType());
            return new ModelAndView("questionForm", "question", question);
        }
        if (request.getParameter("deleteConstructionType") != null) {
            question.getConstructionTypes().remove(question.getConstructionNumber());
            return new ModelAndView("questionForm", "question", question);
        }

        if (request.getParameter("addAnswer") != null) {
            question.getAnswers().add(new Answer());
            return new ModelAndView("questionForm", "question", question);
        }
        if (request.getParameter("deleteAnswer") != null) {
            question.getAnswers().remove(question.getAnswerNumber());
            return new ModelAndView("questionForm", "question", question);
        }

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

}
TOP

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

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.