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

net.n2oapp.framework.ui.controller.DataController Maven / Gradle / Ivy

package net.n2oapp.framework.ui.controller;

import net.n2oapp.criteria.dataset.DataSet;
import net.n2oapp.framework.api.MetadataEnvironment;
import net.n2oapp.framework.api.metadata.meta.ClientDataProvider;
import net.n2oapp.framework.api.metadata.meta.saga.*;
import net.n2oapp.framework.api.register.route.MetadataRouter;
import net.n2oapp.framework.api.rest.ControllerFactory;
import net.n2oapp.framework.api.rest.GetDataResponse;
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.QueryRequestInfo;
import net.n2oapp.framework.api.ui.QueryResponseInfo;
import net.n2oapp.framework.api.user.UserContext;
import net.n2oapp.framework.config.register.route.RouteUtil;

import java.util.Map;
import java.util.Set;

import static net.n2oapp.framework.engine.util.MappingProcessor.resolveCondition;

/**
 * Контроллер данных
 */
public class DataController extends AbstractController {

    private ControllerFactory controllerFactory;

    public DataController(ControllerFactory controllerFactory,
                          MetadataEnvironment environment) {
        super(environment);
        this.controllerFactory = controllerFactory;
    }

    public DataController(ControllerFactory controllerFactory,
                          MetadataEnvironment environment,
                          MetadataRouter router) {
        super(environment, router);
        this.controllerFactory = controllerFactory;
    }

    public GetDataResponse getData(String path, Map parameters, UserContext user) {
        QueryRequestInfo requestInfo = createQueryRequestInfo(path, parameters, user);
        QueryResponseInfo responseInfo = new QueryResponseInfo();
        return controllerFactory.execute(requestInfo, responseInfo);
    }

    public SetDataResponse setData(String path, Map parameters, Map headers, Object body, UserContext user) {
        ActionRequestInfo requestInfo = createActionRequestInfo(path, parameters, headers, body, user);
        ActionResponseInfo responseInfo = new ActionResponseInfo();
        SetDataResponse result = controllerFactory.execute(requestInfo, responseInfo);
        resolveMeta(requestInfo, result, responseInfo.getSuccess());
        return result;
    }

    private void resolveMeta(ActionRequestInfo requestInfo, SetDataResponse response, Boolean success) {
        if (requestInfo.getPollingEndCondition() != null && !resolveCondition(requestInfo.getPollingEndCondition(), response.getData())) {
            resolvePolling(requestInfo, response);
        } else {
            resolveRedirect(requestInfo, response);
            resolveRefresh(requestInfo, response, success);
            resolveLoading(requestInfo, response);
            resolveClear(requestInfo, response);
        }
    }

    private void resolveClear(ActionRequestInfo requestInfo, SetDataResponse response) {
        if (requestInfo.getClearDatasource() != null)
            response.addClear(requestInfo.getClearDatasource());
    }

    private void resolveLoading(ActionRequestInfo requestInfo, SetDataResponse response) {
        if (requestInfo.getLoading() == null)
            return;
        LoadingSaga resolvedLoading = new LoadingSaga();
        resolvedLoading.setPageId(requestInfo.getLoading().getPageId());
        resolvedLoading.setPosition(requestInfo.getLoading().getPosition());
        resolvedLoading.setActive(requestInfo.getLoading().getActive());
        response.addLoading(resolvedLoading);
    }

    private void resolvePolling(ActionRequestInfo requestInfo, SetDataResponse response) {
        PollingSaga resolvedPolling = new PollingSaga();
        resolvedPolling.setDelay(requestInfo.getPolling().getDelay());
        resolvedPolling.setMaxAttempts(requestInfo.getPolling().getMaxAttempts());
        ClientDataProvider dataProvider = requestInfo.getPolling().getDataProvider();
        if (dataProvider != null && dataProvider.getUrl() != null) {
            String url = dataProvider.getUrl();
            url = RouteUtil.resolveUrlParams(url, requestInfo.getQueryData());
            url = RouteUtil.resolveUrlParams(url, response.getData());
            dataProvider.setUrl(url);
        }
        resolvedPolling.setDataProvider(dataProvider);
        resolvedPolling.setDatasource(requestInfo.getPolling().getDatasource());
        resolvedPolling.setModel(requestInfo.getPolling().getModel());
        response.addPolling(resolvedPolling);
    }

    private void resolveRedirect(ActionRequestInfo requestInfo, SetDataResponse response) {
        if (requestInfo.getRedirect() == null)
            return;
        RedirectSaga redirect = requestInfo.getRedirect();
        if (response.getData() != null) {
            DataSet data = new DataSet(response.getData());
            data.merge(requestInfo.getQueryData());
            String redirectUrl = redirect.getPath();
            Set except = redirect.getPathMapping() != null ? redirect.getPathMapping().keySet() : null;
            redirectUrl = RouteUtil.resolveUrlParams(redirectUrl, requestInfo.getQueryData(), null, except);
            redirectUrl = RouteUtil.resolveUrlParams(redirectUrl, response.getData(), null, except);
            RedirectSaga resolvedRedirect = new RedirectSaga();
            resolvedRedirect.setTarget(redirect.getTarget());
            resolvedRedirect.setPathMapping(redirect.getPathMapping());
            resolvedRedirect.setQueryMapping(redirect.getQueryMapping());
            resolvedRedirect.setPath(redirectUrl);
            response.addRedirect(resolvedRedirect);
        }
    }

    private void resolveRefresh(ActionRequestInfo requestInfo, SetDataResponse response, Boolean success) {
        if (success && requestInfo.getRefresh() != null) {
            RefreshSaga resolvedRefresh = new RefreshSaga();
            resolvedRefresh.setDatasources(requestInfo.getRefresh().getDatasources());
            response.addRefresh(resolvedRefresh);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy