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

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

/*
 * Copyright (C) 2016 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.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.core.userdetails.UserDetails;
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.UserDTO;
import com.formkiq.server.service.FolderService;
import com.formkiq.server.service.SpringSecurityService;
import com.formkiq.server.service.UserService;

/**
 * Workflows Rest services.
 *
 */
@Deprecated
@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";

    /** FolderService. */
    @Autowired
    private FolderService folderservice;

    /** UserService. */
    @Autowired
    private UserService userservice;

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

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

        getApiVersion(request);

        UserDetails user = this.securityService.getUserDetails();
        UserDTO userDTO = this.userservice.findUser(user.getUsername(), false);
        String folder = userDTO.getFolders().get(0).getFolder();

        FormDTO dto = this.folderservice.findForm(user, folder, workflow);

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

	/**
	 * Save Workflow.
	 * @param request {@link HttpServletRequest}
	 * @param response {@link HttpServletResponse}
	 * @param entity HttpEntity<byte[]>
	 * @param lastSha1hash {@link String}
	 * @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 = "sha1hash", required = false)
            final String lastSha1hash,
			final HttpEntity entity)
            throws IOException {

	    getApiVersion(request);

	    UserDetails user = this.securityService.getUserDetails();
        UserDTO userDTO = this.userservice.findUser(user.getUsername(), false);
        String folder = userDTO.getFolders().get(0).getFolder();

        if (request instanceof MultipartHttpServletRequest) {

            MultipartHttpServletRequest rr =
                    (MultipartHttpServletRequest) request;

            Iterator itr = rr.getFileNames();
            MultipartFile mpf = rr.getFile(itr.next());
            String sha1hash = this.folderservice.saveForm(user, folder,
                    mpf.getBytes(), lastSha1hash);
            response.addHeader("sha1hash", sha1hash);

        } else {

            String sha1hash = this.folderservice.saveForm(user, folder,
                    entity.getBody(), lastSha1hash);
            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);

	    UserDetails user = this.securityService.getUserDetails();

        UserDTO userDTO = this.userservice.findUser(user.getUsername(), false);
        String folder = userDTO.getFolders().get(0).getFolder();

        return this.folderservice.getSyncList(user, ClientFormType.WORKFLOW,
                folder, nextToken);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy