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

net.n2oapp.framework.ui.controller.action.OperationController Maven / Gradle / Ivy

package net.n2oapp.framework.ui.controller.action;

import net.n2oapp.criteria.dataset.DataSet;
import net.n2oapp.framework.api.MetadataEnvironment;
import net.n2oapp.framework.api.exception.N2oException;
import net.n2oapp.framework.api.metadata.global.view.page.N2oDialog;
import net.n2oapp.framework.api.metadata.meta.page.Dialog;
import net.n2oapp.framework.api.rest.ControllerType;
import net.n2oapp.framework.api.rest.SetDataResponse;
import net.n2oapp.framework.api.ui.ActionRequestInfo;
import net.n2oapp.framework.api.ui.ActionResponseInfo;
import net.n2oapp.framework.api.ui.AlertMessageBuilder;
import net.n2oapp.framework.api.ui.AlertMessagesConstructor;
import net.n2oapp.framework.config.compile.pipeline.N2oPipelineSupport;
import net.n2oapp.framework.config.metadata.compile.context.ActionContext;
import net.n2oapp.framework.config.metadata.compile.context.DialogContext;
import net.n2oapp.framework.engine.data.N2oOperationProcessor;
import net.n2oapp.framework.engine.modules.stack.DataProcessingStack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;

import static net.n2oapp.framework.config.register.route.RouteUtil.normalize;

/**
 * Выполняет действия(action), пришедшие с клиента
 */
@Controller
public class OperationController extends SetController {

    private AlertMessageBuilder messageBuilder;
    private AlertMessagesConstructor messagesConstructor;
    private static final Logger logger = LoggerFactory.getLogger(OperationController.class);
    private MetadataEnvironment environment;

    public OperationController(DataProcessingStack dataProcessingStack,
                               N2oOperationProcessor operationProcessor,
                               AlertMessageBuilder messageBuilder,
                               MetadataEnvironment environment,
                               AlertMessagesConstructor messagesConstructor) {
        super(dataProcessingStack, operationProcessor, environment);
        this.messageBuilder = messageBuilder;
        this.environment = environment;
        this.messagesConstructor = messagesConstructor;
    }


    @Override
    @SuppressWarnings("unchecked")
    public SetDataResponse execute(ActionRequestInfo requestInfo, ActionResponseInfo responseInfo) {
        return executeRequest(requestInfo, responseInfo);
    }

    protected SetDataResponse executeRequest(ActionRequestInfo requestInfo,
                                             ActionResponseInfo responseInfo) {
        try {
            DataSet data = handleActionRequest(requestInfo, responseInfo);
            SetDataResponse response = constructSuccessSetDataResponse(data, requestInfo, responseInfo);
            responseInfo.setSuccess(true);
            return response;
        } catch (N2oException e) {
            SetDataResponse response = constructFailSetDataResponse(e, requestInfo);
            responseInfo.setSuccess(false);
            logger.error(String.format("Error response %d %s: %s", response.getStatus(), e.getSeverity(),
                    e.getUserMessage() != null ? e.getUserMessage() : e.getMessage()), e);
            return response;
        }
    }

    private SetDataResponse constructFailSetDataResponse(N2oException e, ActionRequestInfo requestInfo) {
        SetDataResponse response = new SetDataResponse();
        if (e.getDialog() != null) {
            response.setDialog(compileDialog(e.getDialog(), requestInfo));
        } else {
            if (requestInfo.isMessageOnFail()) {
                response.addResponseMessages(messagesConstructor.constructMessages(e, requestInfo), requestInfo.getMessagesForm());
            }
        }
        response.setStatus(e.getHttpStatus());
        return response;
    }

    private SetDataResponse constructSuccessSetDataResponse(DataSet data,
                                                            ActionRequestInfo requestInfo,
                                                            ActionResponseInfo responseInfo) {
        SetDataResponse response = new SetDataResponse();
        response.setResponseMessages(responseInfo.getMessageList(), requestInfo.getMessagesForm());
        response.setData(data);
        if (responseInfo.getDialog() != null)
            response.setDialog(compileDialog(responseInfo.getDialog(), requestInfo));
        else if (requestInfo.isMessageOnSuccess())
            response.addResponseMessage(
                    messageBuilder.buildSuccessMessage(requestInfo, data),
                    requestInfo.getMessagesForm());
        return response;
    }

    /**
     * Компиляция диалога подтверждения действия
     *
     * @param n2oDialog   Исходная модель диалога подтверждения действия
     * @param requestInfo Информация о запросе вызова операции
     * @return Клиентская модель диалога подтверждения действия
     */
    private Dialog compileDialog(N2oDialog n2oDialog, ActionRequestInfo requestInfo) {
        String route = requestInfo.getContext().getRoute(null) + normalize(
                n2oDialog.getRoute() != null ? n2oDialog.getRoute() : n2oDialog.getId());
        DialogContext context = new DialogContext(route, n2oDialog.getId());
        ActionContext actionContext = (ActionContext) requestInfo.getContext();
        context.setPathRouteMapping(actionContext.getPathRouteMapping());
        context.setQueryRouteMapping(actionContext.getQueryRouteMapping());
        context.setParentPageId(actionContext.getParentPageId());
        context.setParentSourceDatasourceId(actionContext.getParentSourceDatasourceId());
        context.setParentClientWidgetId(actionContext.getParentClientWidgetId());
        context.setParentRedirect(actionContext.getRedirect());
        context.setParentRefresh(actionContext.getRefresh());
        if (requestInfo.getObject() != null)
            context.setObjectId(requestInfo.getObject().getId());
        N2oPipelineSupport pipelineSupport = new N2oPipelineSupport(environment);
        Dialog dialog = environment.getCompilePipelineFunction()
                .apply(pipelineSupport)
                .get(n2oDialog, context);
        dialog = environment.getBindPipelineFunction().apply(pipelineSupport)
                .get(dialog, context, requestInfo.getQueryData());
        return dialog;
    }

    @Override
    public ControllerType getControllerType() {
        return ControllerType.operation;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy