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

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

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.ClientFormType;
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";
    /** DELETE url. */
    public static final String API_WORKFLOW_DELETE = "/api/workflows/delete";

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

    /** SpringSecurityService. */
    @Autowired
    private SpringSecurityService securityService;

	/**
     * Deletes a Form.
     * @param request {@link HttpServletRequest}
     * @param client {@link String}
     * @param uuid {@link String}
     * @return {@link ApiMessageResponse}
     */
    @Transactional
    @Secured({ "ROLE_ADMIN" })
    @RequestMapping(API_WORKFLOW_DELETE)
    public ApiMessageResponse delete(
            final HttpServletRequest request,
            @RequestParam(value = "client", required = true)
            final String client,
            @RequestParam(value = "uuid", required = true)
            final String uuid) {

        getApiVersion(request);
        this.formservice.deleteClientForm(ClientFormType.WORKFLOW, client,
                uuid);
        return new ApiMessageResponse("Workflow Deleted");
    }

	/**
	 * 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);
        this.securityService.verifyUserHasAccessToClient(client);

        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 uuid {@link String}
     * @param token {@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 = "uuid", required = false)
            final String uuid,
            @RequestParam(value = "token", required = false)
            final String token) {

        getApiVersion(request);

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

        return this.formservice.findWorkflows(client, token);
    }

	/**
	 * Save Workflow.
	 * @param request {@link HttpServletRequest}
	 * @param response {@link HttpServletResponse}
	 * @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,
	        final HttpServletResponse response,
	        @RequestParam(value = "client", required = true)
            final String client,
			final HttpEntity entity)
            throws IOException {

	    getApiVersion(request);
	    this.securityService.verifyUserHasAccessToClient(client);

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

            MultipartHttpServletRequest rr =
                    (MultipartHttpServletRequest) request;

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

        } else {

            String sha1hash = this.formservice.saveWorkflow(user, client,
                    entity.getBody());
            response.addHeader("sha1hash", sha1hash);
        }

        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);
	    this.securityService.verifyUserHasAccessToClient(client);

        UserDetails user = getUserDetails();

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy