AuthoringAction
action.
+ *
+ * @author Marcin Cieslak
+ * @see org.lamsfoundation.lams.tool.survey.web.controller.AuthoringAction
+ */
+public class AuthoringConditionAction extends Action {
+
+ @Override
+ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
+ HttpServletResponse response) throws Exception {
+
+ String param = mapping.getParameter();
+
+ if (param.equals("newConditionInit")) {
+ return newConditionInit(mapping, form, request, response);
+ }
+ if (param.equals("editCondition")) {
+ return editCondition(mapping, form, request, response);
+ }
+ if (param.equals("saveOrUpdateCondition")) {
+ return saveOrUpdateCondition(mapping, form, request, response);
+ }
+ if (param.equals("removeCondition")) {
+ return removeCondition(mapping, form, request, response);
+ }
+ if (param.equals("upCondition")) {
+ return upCondition(mapping, form, request, response);
+ }
+ if (param.equals("downCondition")) {
+ return downCondition(mapping, form, request, response);
+ }
+ return null;
+ }
+
+ /**
+ * Display empty page for a new condition.
+ *
+ * @param mapping
+ * @param form
+ * @param request
+ * @param response
+ * @return
+ */
+ private ActionForward newConditionInit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
+ HttpServletResponse response) {
+
+ populateFormWithPossibleItems(form, request);
+ ((SurveyConditionForm) form).setOrderId(-1);
+ return mapping.findForward("addcondition");
+ }
+
+ /**
+ * Display edit page for an existing condition.
+ *
+ * @param mapping
+ * @param form
+ * @param request
+ * @param response
+ * @return
+ */
+ private ActionForward editCondition(ActionMapping mapping, ActionForm form, HttpServletRequest request,
+ HttpServletResponse response) {
+
+ SurveyConditionForm SurveyConditionForm = (SurveyConditionForm) form;
+ String sessionMapID = SurveyConditionForm.getSessionMapID();
+ SessionMap sessionMap = (SessionMap) request.getSession().getAttribute(sessionMapID);
+
+ int orderId = NumberUtils.stringToInt(request.getParameter(SurveyConstants.PARAM_ORDER_ID), -1);
+ SurveyCondition condition = null;
+ if (orderId != -1) {
+ SortedSetHttpSession
+ * condition list. Notice, this save is not persist them into database, just save HttpSession
+ * temporarily. Only they will be persist when the entire authoring page is being persisted.
+ *
+ * @param mapping
+ * @param form
+ * @param request
+ * @param response
+ * @return
+ * @throws ServletException
+ */
+ private ActionForward saveOrUpdateCondition(ActionMapping mapping, ActionForm form, HttpServletRequest request,
+ HttpServletResponse response) {
+
+ SurveyConditionForm conditionForm = (SurveyConditionForm) form;
+ ActionErrors errors = validateSurveyCondition(conditionForm, request);
+
+ if (!errors.isEmpty()) {
+ populateFormWithPossibleItems(form, request);
+ this.addErrors(request, errors);
+ return mapping.findForward("addcondition");
+ }
+
+ try {
+ extractFormToSurveyCondition(request, conditionForm);
+ } catch (Exception e) {
+
+ errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.condition", e.getMessage()));
+ if (!errors.isEmpty()) {
+ populateFormWithPossibleItems(form, request);
+ this.addErrors(request, errors);
+ return mapping.findForward("addcondition");
+ }
+ }
+
+ request.setAttribute(SurveyConstants.ATTR_SESSION_MAP_ID, conditionForm.getSessionMapID());
+
+ return mapping.findForward(SurveyConstants.SUCCESS);
+ }
+
+ /**
+ * Remove condition from HttpSession list and update page display. As authoring rule, all persist only happen when
+ * user submit whole page. So this remove is just impact HttpSession values.
+ *
+ * @param mapping
+ * @param form
+ * @param request
+ * @param response
+ * @return
+ */
+ private ActionForward removeCondition(ActionMapping mapping, ActionForm form, HttpServletRequest request,
+ HttpServletResponse response) {
+
+ // get back sessionMAP
+ String sessionMapID = WebUtil.readStrParam(request, SurveyConstants.ATTR_SESSION_MAP_ID);
+ SessionMap sessionMap = (SessionMap) request.getSession().getAttribute(sessionMapID);
+
+ int orderId = NumberUtils.stringToInt(request.getParameter(SurveyConstants.PARAM_ORDER_ID), -1);
+ if (orderId != -1) {
+ SortedSetjava.util.List
from HttpSession by given name.
+ *
+ * @param request
+ * @param name
+ * @return
+ */
+ private List getListFromSession(SessionMap sessionMap, String name) {
+ List list = (List) sessionMap.get(name);
+ if (list == null) {
+ list = new ArrayList();
+ sessionMap.put(name, list);
+ }
+ return list;
+ }
+
+ /**
+ * This method will populate condition information to its form for edit use.
+ *
+ * @param orderId
+ * @param condition
+ * @param form
+ * @param request
+ */
+ private void populateConditionToForm(int orderId, SurveyCondition condition, SurveyConditionForm form,
+ HttpServletRequest request) {
+ form.populateForm(condition);
+ if (orderId >= 0) {
+ form.setOrderId(orderId + 1);
+ }
+ }
+
+ /**
+ * This method will populate questions to choose to the form for edit use.
+ *
+ * @param sequenceId
+ * @param condition
+ * @param form
+ * @param request
+ */
+ private void populateFormWithPossibleItems(ActionForm form, HttpServletRequest request) {
+ SurveyConditionForm conditionForm = (SurveyConditionForm) form;
+ // get back sessionMAP
+ String sessionMapID = conditionForm.getSessionMapID();
+ SessionMap sessionMap = (SessionMap) request.getSession().getAttribute(sessionMapID);
+
+ SetHttpSession
+ * SurveyItemList. Notice, this save is not persist them into database, just save HttpSession
+ * temporarily. Only they will be persist when the entire authoring page is being persisted.
+ *
+ * @param mapping
+ * @param form
+ * @param request
+ * @param response
+ * @return
+ * @throws ServletException
+ */
+ @RequestMapping("/saveOrUpdateItem")
+ public String saveOrUpdateItem(@ModelAttribute("surveyItemForm") QuestionForm surveyItemForm, Errors errors, HttpServletRequest request)
+ throws Exception {
+ // get instructions:
+ Listjava.util.List
from HttpSession by given name.
+ *
+ * @param request
+ * @param name
+ * @return
+ */
+ private List getListFromSession(SessionMapHttpRequest
+ *
+ * @param request
+ */
+ private List