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

com.formkiq.server.api.FormsController 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.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";

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

	/**
	 * 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);

        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 form {@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 = "form", required = false)
            final String form) {

        getApiVersion(request);

        if (!StringUtils.isEmpty(form)) {
            return this.formservice.findForms(client, form);
        }

        return this.formservice.findForms(client);
    }

	/**
	 * Save Form.
	 * @param request {@link HttpServletRequest}
	 * @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,
	        @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.saveForm(user, client, mpf.getBytes());

	    } else {

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

        return new ApiMessageResponse("form saved");
	}

    /**
	 * 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);

        UserDetails user = getUserDetails();

        int next = convertToken(nextToken);

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy