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

com.formkiq.server.api.ActionsController 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.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.formkiq.server.service.SaveActionException;
import com.formkiq.server.service.SaveActionService;
import com.formkiq.server.service.SpringSecurityService;
import com.formkiq.server.service.dto.SaveActionRequest;
import com.formkiq.server.service.dto.SaveActionResponse;

/**
 * Action Controller handles Save actions.
 *
 */
@RestController
public class ActionsController extends AbstractRestController {

    /** Save Actions List. */
    public static final String API_ACTIONS_LIST = "/api/actions/savelist";

    /** Save Action. */
    public static final String API_ACTIONS_SAVE = "/api/actions/save";

    /** ApplicationContext. */
    @Autowired
    private ApplicationContext context;

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

    /**
     * Perform a Save Action.
     * @param request {@link HttpServletRequest}
     * @param saveRequest {@link SaveActionRequest}
     * @return {@link ApiActionListResponse}
     * @throws SaveActionException SaveActionException
     */
    @Transactional
    @RequestMapping(value = API_ACTIONS_SAVE, method = RequestMethod.POST)
    public SaveActionResponse save(final HttpServletRequest request,
            @RequestBody final SaveActionRequest saveRequest)
                    throws SaveActionException {

        getApiVersion(request);
        this.securityService
                .verifyUserHasAccessToClient(saveRequest.getClientid());

        List results = new ArrayList();

        Map beans = this.context
                .getBeansOfType(SaveActionService.class);

        for (SaveActionService action : beans.values()) {

            if (action.isSupported(saveRequest)) {
                SaveActionResponse result = action.perform(saveRequest);
                results.add(result);
            }
        }

        SaveActionResponse result = unique(results);

        if (results.isEmpty()) {
            result.setMessage("no save actions found.");
        }

        return result;
    }

    /**
     * Return a list of save actions.
     * @param request HttpServletRequest
     * @return ApiActionListResponse
     */
    @Transactional
    @RequestMapping(API_ACTIONS_LIST)
    public ApiActionListResponse savelist(final HttpServletRequest request) {

        getApiVersion(request);

        List actions = new ArrayList();
        Map beans = this.context
                .getBeansOfType(SaveActionService.class);

        for (SaveActionService action : beans.values()) {
            actions.add(action.getName());
        }

        Collections.sort(actions);

        return new ApiActionListResponse(actions);
    }

    /**
     * Transform a list of SaveActionResults into one object.
     * @param results List
     * @return SaveActionResult
     */
    private SaveActionResponse unique(final List results) {

        StringBuilder sb = new StringBuilder();
        SaveActionResponse result = new SaveActionResponse();

        for (SaveActionResponse r : results) {

            String msg = r.getMessage();

            if (!StringUtils.isEmpty(msg)) {

                if (sb.length() > 0) {
                    sb.append("\n");
                }

                sb.append(msg);
            }

            result.putValidators(r.getValidators());
        }

        result.setMessage(sb.toString());
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy