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

com.formkiq.server.api.FormsController 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.FormListDTO;
import com.formkiq.server.domain.type.SyncListDTO;
import com.formkiq.server.service.FormService;

/**
 * FormController rest services.
 *
 */
@RestController
public class FormsController extends AbstractRestController {

    /** GET url. */
    public static final String API_FORM_GET = "/api/forms/get";
    /** SYNC url. */
	public static final String API_FORM_SYNC = "/api/forms/sync";
	/** SAVE url. */
	public static final String API_FORM_SAVE = "/api/forms/save";
	/** LIST url. */
    public static final String API_FORM_LIST = "/api/forms/list";
    /** DELETE url. */
    public static final String API_FORM_DELETE = "/api/forms/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_FORM_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.FORM, client, uuid);
        return new ApiMessageResponse("Form Deleted");
    }

	/**
	 * Gets a Form.
	 * @param request {@link HttpServletRequest}
	 * @param response {@link HttpServletResponse}
	 * @param client {@link String}
	 * @param form {@link String}
	 * @throws IOException IOException
	 */
    @Transactional
    @RequestMapping(API_FORM_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 form)
            throws IOException {

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

        UserDetails user = getUserDetails();
        FormDTO dto = this.formservice.findForm(user, client, form);

        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 Forms.
     * @param request {@link HttpServletRequest}
     * @param client {@link String}
     * @param uuid {@link String}
     * @param token {@link String}
     * @return {@link FormListDTO}
     */
    @Transactional
    @Secured({ "ROLE_ADMIN" })
    @RequestMapping(API_FORM_LIST)
    public FormListDTO 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.findForms(client, uuid, token);
        }

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

	/**
	 * Save Form.
	 * @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_FORM_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);

	    ApiMessageResponse msg = new ApiMessageResponse("form saved");

	    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.saveForm(user, client,
                    mpf.getBytes());
            response.addHeader("sha1hash", sha1hash);

	    } else {

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

        return msg;
	}

    /**
	 * Syncs changed forms.
	 * @param request {@link HttpServletRequest}
	 * @param client {@link String}
	 * @param nextToken {@link String}
	 * @return {@link SyncListDTO}
	 */
	@Transactional
	@RequestMapping(API_FORM_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.getSyncList(user, client, nextToken);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy