All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.formkiq.server.api.WorkflowsController Maven / Gradle / Ivy

There is a newer version: 0.6.1
Show newest version
package com.formkiq.server.api;

import java.io.IOException;
import java.util.Iterator;

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

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
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.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.formkiq.server.domain.type.FormDTO;
import com.formkiq.server.domain.type.SyncListDTO;
import com.formkiq.server.domain.type.WorkflowListDTO;
import com.formkiq.server.service.FormService;

/**
 * Workflows Rest services.
 *
 */
@RestController
public class WorkflowsController extends AbstractRestController {

    /** GET url. */
    public static final String API_WORKFLOW_GET = "/api/workflows/get";
    /** SYNC url. */
	public static final String API_WORKFLOW_SYNC = "/api/workflows/sync";
	/** SAVE url. */
	public static final String API_WORKFLOW_SAVE = "/api/workflows/save";
	/** LIST url. */
    public static final String API_WORKFLOW_LIST = "/api/workflows/list";

	/** FormService. */
	@Autowired
	private FormService formservice;

	/**
	 * Gets a Workflow.
	 * @param request {@link HttpServletRequest}
	 * @param response {@link HttpServletResponse}
	 * @param client {@link String}
	 * @param workflow {@link String}
	 * @throws IOException IOException
	 */
    @Transactional
    @RequestMapping(API_WORKFLOW_GET)
    public void get(
            final HttpServletRequest request,
            final HttpServletResponse response,
            @RequestParam(value = "client", required = true)
            final String client,
            @RequestParam(value = "uuid", required = true)
            final String workflow)
            throws IOException {

        getApiVersion(request);

        UserDetails user = getUserDetails();
        FormDTO dto = this.formservice.findWorkflow(user, client, workflow);

        response.addHeader("sha1hash", dto.getSha1hash());
        response.addHeader("permission", dto.getPermission().name());
        response.setContentType("application/zip");
        response.setContentLengthLong(dto.getData().length);
        IOUtils.write(dto.getData(), response.getOutputStream());
    }

	/**
     * Lists Workflows.
     * @param request {@link HttpServletRequest}
     * @param client {@link String}
     * @param workflow {@link String}
     * @return {@link WorkflowListDTO}
     */
    @Transactional
    @Secured({ "ROLE_ADMIN" })
    @RequestMapping(API_WORKFLOW_LIST)
    public WorkflowListDTO list(final HttpServletRequest request,
            @RequestParam(value = "client", required = true)
            final String client,
            @RequestParam(value = "workflow", required = false)
            final String workflow) {

        getApiVersion(request);

        if (!StringUtils.isEmpty(workflow)) {
            return this.formservice.findWorkflows(client, workflow);
        }

        return this.formservice.findWorkflows(client);
    }

	/**
	 * Save Workflow.
	 * @param request {@link HttpServletRequest}
	 * @param client {@link String}
	 * @param entity HttpEntity<byte[]>
	 * @return {@link ApiMessageResponse}
	 * @throws IOException IOException
	 */
	@Transactional
	@RequestMapping(value = API_WORKFLOW_SAVE, method = RequestMethod.POST)
	public ApiMessageResponse save(
	        final HttpServletRequest request,
	        @RequestParam(value = "client", required = true)
            final String client,
			final HttpEntity entity)
            throws IOException {

	    getApiVersion(request);

		UserDetails user = getUserDetails();
        if (request instanceof MultipartHttpServletRequest) {

            MultipartHttpServletRequest rr =
                    (MultipartHttpServletRequest) request;

            Iterator itr = rr.getFileNames();
            MultipartFile mpf = rr.getFile(itr.next());
            this.formservice.saveWorkflow(user, client, mpf.getBytes());

        } else {

            this.formservice.saveWorkflow(user, client, entity.getBody());
        }

        return new ApiMessageResponse("workflow saved");
	}

    /**
	 * Syncs changed forms.
	 * @param request {@link HttpServletRequest}
	 * @param client {@link String}
	 * @param nextToken {@link String}
	 * @return {@link SyncListDTO}
	 */
	@Transactional
	@RequestMapping(API_WORKFLOW_SYNC)
    public SyncListDTO sync(
            final HttpServletRequest request,
            @RequestParam(value = "client", required = true)
            final String client,
            @RequestParam(value = "nexttoken", required = false)
            final String nextToken) {

	    getApiVersion(request);

        UserDetails user = getUserDetails();

        int next = convertToken(nextToken);

        return this.formservice.getSyncWorkflowList(user, client, next);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy