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

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

There is a newer version: 0.6.1
Show newest version
/*
 * 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.util.UUID;

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

import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.formkiq.server.domain.type.ClientFormType;
import com.formkiq.server.domain.type.FolderDTO;
import com.formkiq.server.domain.type.FolderListDTO;
import com.formkiq.server.domain.type.SyncListDTO;
import com.formkiq.server.service.FolderService;
import com.formkiq.server.service.SpringSecurityService;

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

    /** GET url. */
    public static final String API_FOLDER_GET      = "/api/folders/get";
    /** SAVE url. */
    public static final String API_FOLDER_SAVE      = "/api/folders/save";
    /** Sync Folder url. */
    public static final String API_FOLDER_SYNC      = "/api/folders/sync";
    /** List Folder url. */
    public static final String API_FOLDER_LIST      = "/api/folders/list";
    /** Delete Folder url. */
    public static final String API_FOLDER_DELETE      = "/api/folders/delete";

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

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

    /**
     * Delete Folder.
     * @param request {@link HttpServletRequest}
     * @param folder {@link String}
     * @return {@link ApiMessageResponse}
     */
    @Transactional
    @RequestMapping(API_FOLDER_DELETE)
    public ApiMessageResponse deleteFolder(
            final HttpServletRequest request,
            @RequestParam(value = "folder", required = true)
            final String folder) {

        getApiVersion(request);

        UserDetails user = this.securityService.getUserDetails();
        this.folderservice.deleteFolder(user, folder);

        return new ApiMessageResponse("Folder deleted");
    }

    /**
     * Get Folder.
     * @param request {@link HttpServletRequest}
     * @param email {@link String}
     * @param folder {@link String}
     * @return {@link ApiMessageResponse}
     */
    @Transactional
    @RequestMapping(API_FOLDER_GET)
    public FolderDTO get(
            final HttpServletRequest request,
            @RequestParam(value = "email", required = false)
            final String email,
            @RequestParam(value = "folder", required = true)
            final String folder) {

        getApiVersion(request);

        UserDetails user = this.securityService.getUserDetailsByEmail(email);
        return this.folderservice.findFolder(user, folder);
    }

    /**
     * Get folder list for user.
     * @param request {@link HttpServletRequest}
     * @param email {@link String}
     * @param nextToken {@link String}
     * @return {@link FolderListDTO}
     */
    @Transactional
    @RequestMapping(API_FOLDER_LIST)
    public FolderListDTO list(
            final HttpServletRequest request,
            @RequestParam(value = "email", required = false)
            final String email,
            @RequestParam(value = "nexttoken", required = false)
            final String nextToken) {

        getApiVersion(request);

        UserDetails user = this.securityService.getUserDetailsByEmail(email);

        return this.folderservice.getFolderList(user, nextToken);
    }

    /**
     * Add Folder.
     * @param request {@link HttpServletRequest}
     * @param email {@link String}
     * @param folder {@link String}
     * @param foldername {@link String}
     * @return {@link ApiMessageResponse}
     */
    @Transactional
    @RequestMapping(API_FOLDER_SAVE)
    public ApiMessageResponse saveFolder(
            final HttpServletRequest request,
            @RequestParam(value = "email", required = false)
            final String email,
            @RequestParam(value = "folder", required = false)
            final String folder,
            @RequestParam(value = "foldername", required = true)
            final String foldername) {

        getApiVersion(request);

        String msg = StringUtils.isEmpty(folder) ? "Folder added"
                : "Folder updated";

        UserDetails user = this.securityService.getUserDetailsByEmail(email);

        UUID folderUUID = this.folderservice.saveFolder(user, folder,
                foldername);

        return new ApiMessageResponse(msg, folderUUID.toString());
    }

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

        getApiVersion(request);

        UserDetails user = this.securityService.getUserDetails();

        return this.folderservice.getSyncList(user,
                ClientFormType.valueOf(type.toUpperCase()), folder, nextToken);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy