com.formkiq.core.controller.user.DesignerController.java Source code

Java tutorial

Introduction

Here is the source code for com.formkiq.core.controller.user.DesignerController.java

Source

/*
 * Copyright (C) 2017 FormKiQ Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.formkiq.core.controller.user;

import static org.springframework.util.StringUtils.hasText;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.formkiq.core.controller.ErrorHandlingController;
import com.formkiq.core.service.workflow.WorkflowEditorService;
import com.formkiq.core.webflow.FlowManager;
import com.formkiq.core.webflow.WebFlow;
import com.formkiq.core.webflow.service.FlowEventMethodNotFound;
import com.formkiq.core.webflow.service.FlowEventService;

/**
 * User Designer Controller.
 *
 */
@Controller
@RequestMapping(value = "/user/designer")
public class DesignerController extends ErrorHandlingController {

    /** Base URL. */
    public static final String BASE_URL = "/user/designer";

    /** WorkflowEditService. */
    @Autowired
    private WorkflowEditorService workflowEditService;

    /** {@link FlowEventService}. */
    @Autowired
    private FlowEventService flowEventService;

    /**
     * User Workflow Edit page.
     * @param request {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @param execution {@link String}
     * @return {@link ModelAndView}
     * @throws Exception Exception
     */
    @RequestMapping(value = { "/edit" }, method = RequestMethod.GET)
    public ModelAndView edit(final HttpServletRequest request, final HttpServletResponse response,
            final @RequestParam(name = "execution", required = false) String execution) throws Exception {

        WebFlow flow = FlowManager.get(request);

        this.flowEventService.processGetRequest(flow, request);

        try {
            Object result = this.flowEventService.processRequest(request);

            if (result != null && result instanceof ModelAndView) {
                ModelAndView mav = (ModelAndView) result;
                if (!mav.getModel().containsKey("flow")) {
                    return mav;
                }
            }
        } catch (FlowEventMethodNotFound e) {
            // ignore
        }

        if (!flow.getSessionKey().equals(execution)) {

            Pair<String, Integer> split = FlowManager.getExecutionSessionKey(execution);

            Integer eventId = split.getRight();
            if (eventId != null) {
                flow.setEventId(eventId.intValue());
            }
        }

        return new ModelAndView("user/designer/edit", "flow", flow);
    }

    /**
     * User Workflow Edit page.
     * @param request {@link HttpServletRequest}
     * @param folder {@link String}
     * @param uuid {@link String}
     * @param execution {@link String}
     * @return {@link ModelAndView}
     * @throws IOException IOException
     */
    @Transactional
    @RequestMapping(value = { "/edit/{folder}", "/edit/{folder}/{uuid}" }, method = RequestMethod.GET)
    public ModelAndView edit(final HttpServletRequest request,
            final @PathVariable(name = "folder", required = false) String folder,
            final @PathVariable(name = "uuid", required = false) String uuid,
            final @RequestParam(name = "execution", required = false) String execution) throws IOException {

        ModelAndView view = new ModelAndView("/user/dashboard");

        if (hasText(folder)) {

            view = this.workflowEditService.startEdit(request, folder, uuid).getLeft();
        }

        return view;
    }

    /**
     * User Workflow Edit page.
     * @param request {@link HttpServletRequest}
     * @return {@link ModelAndView}
     * @throws Exception Exception
     */
    @Transactional
    @RequestMapping(value = { "/edit" }, method = RequestMethod.POST)
    public ModelAndView editPost(final HttpServletRequest request) throws Exception {
        // TODO test..

        try {
            Object result = this.flowEventService.processRequest(request);
            if (result != null && result instanceof ModelAndView) {
                ModelAndView mav = (ModelAndView) result;
                if (!mav.getModel().containsKey("flow")) {
                    return mav;
                }
            }
        } catch (FlowEventMethodNotFound e) {
            // ignore
            // TODO remove...
        }

        String view = "redirect:/user/dashboard";
        Map<String, String> errors = this.workflowEditService.save(request);

        if (!errors.isEmpty()) {
            view = "redirect:" + request.getRequestURI() + "?" + request.getQueryString();
        }

        return new ModelAndView(view);
    }
}