Search in sources :

Example 1 with Parameter

use of io.jmix.dashboards.model.parameter.Parameter in project jmix by jmix-framework.

the class DashboardImpl method refresh.

/**
 * Refreshes widget with passed parameters.
 * Dashboard will be refreshed with merged existed and new parameters.
 * If existed parameter has the same name as one of the param from passed map, it will be overwritten by param from map.
 *
 * @param params map with new dashboard parameters
 */
@Override
public void refresh(Map<String, Object> params) {
    DashboardModel dashboardModel = null;
    if (isNotBlank(jsonPath)) {
        dashboardModel = loadDashboardByJson(jsonPath);
    } else if (isNotBlank(code)) {
        dashboardModel = loadDashboardByCode(code);
    }
    if (MapUtils.isNotEmpty(params) && Objects.nonNull(dashboardModel)) {
        Map<String, Parameter> newParams = params.keySet().stream().map(key -> {
            Parameter parameter = metadata.create(Parameter.class);
            parameter.setName(key);
            parameter.setAlias(key);
            parameter.setValue(parameterTransformer.createParameterValue(params.get(key)));
            return parameter;
        }).collect(Collectors.toMap(Parameter::getName, Function.identity()));
        Map<String, Parameter> paramMap = dashboardModel.getParameters().stream().collect(Collectors.toMap(Parameter::getName, Function.identity()));
        paramMap.putAll(newParams);
        dashboardModel.setParameters(new ArrayList<>(paramMap.values()));
    }
    updateDashboard(dashboardModel);
}
Also used : ScreenFragment(io.jmix.ui.screen.ScreenFragment) PersistentDashboard(io.jmix.dashboards.entity.PersistentDashboard) Dashboard(io.jmix.dashboardsui.component.Dashboard) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ParamsMap(io.jmix.core.common.util.ParamsMap) StringUtils(org.apache.commons.lang3.StringUtils) Messages(io.jmix.core.Messages) DashboardEvent(io.jmix.dashboardsui.event.DashboardEvent) io.jmix.ui(io.jmix.ui) Method(java.lang.reflect.Method) Resource(org.springframework.core.io.Resource) DataManager(io.jmix.core.DataManager) UiEventListenerMethodAdapter(io.jmix.ui.sys.event.UiEventListenerMethodAdapter) ResourceLoader(org.springframework.core.io.ResourceLoader) DataUnit(io.jmix.ui.component.data.DataUnit) LookupWidget(io.jmix.dashboardsui.widget.LookupWidget) UiControllerReflectionInspector(io.jmix.ui.sys.UiControllerReflectionInspector) EventListener(org.springframework.context.event.EventListener) ApplicationListener(org.springframework.context.ApplicationListener) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOUtils(org.apache.commons.io.IOUtils) CanvasLayout(io.jmix.dashboardsui.component.CanvasLayout) Timer(io.jmix.ui.component.Timer) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) io.jmix.ui.component(io.jmix.ui.component) CODE(io.jmix.dashboardsui.screen.dashboard.view.DashboardViewScreen.CODE) AccessConstraintsHelper(io.jmix.dashboardsui.dashboard.tools.AccessConstraintsHelper) java.util(java.util) DashboardUpdatedEvent(io.jmix.dashboardsui.event.DashboardUpdatedEvent) Widget(io.jmix.dashboards.model.Widget) Function(java.util.function.Function) DASHBOARD(io.jmix.dashboardsui.screen.dashboard.editor.canvas.CanvasFragment.DASHBOARD) Parameter(io.jmix.dashboards.model.parameter.Parameter) UiEventsMulticaster(io.jmix.ui.sys.event.UiEventsMulticaster) RefreshableWidget(io.jmix.dashboardsui.widget.RefreshableWidget) MapScreenOptions(io.jmix.ui.screen.MapScreenOptions) Nullable(javax.annotation.Nullable) MapUtils(org.apache.commons.collections4.MapUtils) Logger(org.slf4j.Logger) DashboardException(io.jmix.dashboardsui.DashboardException) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Metadata(io.jmix.core.Metadata) JsonConverter(io.jmix.dashboards.converter.JsonConverter) ItemsSelectedEvent(io.jmix.dashboardsui.event.ItemsSelectedEvent) ParameterTransformer(io.jmix.dashboardsui.transformation.ParameterTransformer) DashboardModel(io.jmix.dashboards.model.DashboardModel) ReflectionUtils(org.springframework.util.ReflectionUtils) BeanFactory(org.springframework.beans.factory.BeanFactory) CanvasFragment(io.jmix.dashboardsui.screen.dashboard.editor.canvas.CanvasFragment) DashboardViewAssistant(io.jmix.dashboardsui.dashboard.assistant.DashboardViewAssistant) DashboardModel(io.jmix.dashboards.model.DashboardModel) Parameter(io.jmix.dashboards.model.parameter.Parameter)

Example 2 with Parameter

use of io.jmix.dashboards.model.parameter.Parameter in project jmix by jmix-framework.

the class WidgetRepositoryImpl method getWidgetParams.

@Override
public Map<String, Object> getWidgetParams(Widget widget) {
    Map<String, Object> widgetParams = new HashMap<>();
    for (Parameter p : widget.getWidgetFields()) {
        Object rawValue = parameterTransformer.transform(p.getValue());
        widgetParams.put(getAlias(p), rawValue);
    }
    if (widget.getDashboard() != null) {
        for (Parameter p : widget.getDashboard().getParameters()) {
            Object rawValue = parameterTransformer.transform(p.getValue());
            widgetParams.put(getAlias(p), rawValue);
        }
    }
    for (Parameter p : widget.getParameters()) {
        Object rawValue = parameterTransformer.transform(p.getValue());
        widgetParams.put(getAlias(p), rawValue);
    }
    return widgetParams;
}
Also used : Parameter(io.jmix.dashboards.model.parameter.Parameter)

Example 3 with Parameter

use of io.jmix.dashboards.model.parameter.Parameter in project jmix by jmix-framework.

the class WidgetRepositoryImpl method serializeWidgetFields.

@Override
public void serializeWidgetFields(ScreenFragment widgetFragment, Widget widget) {
    widget.getWidgetFields().clear();
    List<Field> parameterFields = FieldUtils.getFieldsListWithAnnotation(widgetFragment.getClass(), WidgetParam.class);
    for (Field parameterField : parameterFields) {
        Parameter parameter = metadata.create(Parameter.class);
        parameter.setName(parameterField.getName());
        parameter.setAlias(parameterField.getName());
        ParameterValue parameterValue = parameterTransformer.createParameterValue(parameterField, widgetFragment);
        parameter.setValue(parameterValue);
        widget.getWidgetFields().add(parameter);
    }
}
Also used : Field(java.lang.reflect.Field) ParameterValue(io.jmix.dashboards.model.parameter.type.ParameterValue) Parameter(io.jmix.dashboards.model.parameter.Parameter)

Example 4 with Parameter

use of io.jmix.dashboards.model.parameter.Parameter in project jmix by jmix-framework.

the class WidgetRepositoryImpl method initializeWidgetFields.

@Override
public void initializeWidgetFields(ScreenFragment widgetFragment, Widget widget) {
    List<Parameter> widgetFieldValues = widget.getWidgetFields();
    List<Field> parameterFields = FieldUtils.getFieldsListWithAnnotation(widgetFragment.getClass(), WidgetParam.class);
    for (Field parameterField : parameterFields) {
        Optional<Parameter> fieldValueOpt = widgetFieldValues.stream().filter(p -> p.getName().equals(parameterField.getName())).findFirst();
        if (fieldValueOpt.isPresent()) {
            Parameter p = fieldValueOpt.get();
            Object rawValue = parameterTransformer.transform(p.getValue());
            try {
                FieldUtils.writeField(parameterField, widgetFragment, rawValue, true);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(String.format("Error on widget field %s initialization", p.getAlias()), e);
            }
        }
    }
}
Also used : ScreenFragment(io.jmix.ui.screen.ScreenFragment) DashboardWidget(io.jmix.dashboardsui.annotation.DashboardWidget) java.util(java.util) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Metadata(io.jmix.core.Metadata) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Field(java.lang.reflect.Field) Widget(io.jmix.dashboards.model.Widget) WidgetParam(io.jmix.dashboardsui.annotation.WidgetParam) StringUtils(org.apache.commons.lang3.StringUtils) Messages(io.jmix.core.Messages) ParameterTransformer(io.jmix.dashboardsui.transformation.ParameterTransformer) WindowConfig(io.jmix.ui.WindowConfig) Parameter(io.jmix.dashboards.model.parameter.Parameter) WidgetRepository(io.jmix.dashboardsui.repository.WidgetRepository) Component(org.springframework.stereotype.Component) WindowInfo(io.jmix.ui.WindowInfo) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) ParameterValue(io.jmix.dashboards.model.parameter.type.ParameterValue) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) WidgetTypeInfo(io.jmix.dashboardsui.repository.WidgetTypeInfo) Field(java.lang.reflect.Field) Parameter(io.jmix.dashboards.model.parameter.Parameter)

Example 5 with Parameter

use of io.jmix.dashboards.model.parameter.Parameter in project jmix by jmix-framework.

the class DashboardImpl method dashboardEventListener.

@SuppressWarnings("unchecked")
@EventListener
public void dashboardEventListener(DashboardEvent dashboardEvent) throws InvocationTargetException, IllegalAccessException {
    refreshWidgets(dashboardEvent);
    if (dashboardViewAssistant == null) {
        return;
    }
    Class eventClass = dashboardEvent.getClass();
    Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(dashboardViewAssistant.getClass());
    List<Method> eventListenerMethods = Arrays.stream(methods).filter(m -> m.getAnnotation(EventListener.class) != null).filter(m -> m.getParameterCount() == 1).collect(Collectors.toList());
    for (Method method : eventListenerMethods) {
        java.lang.reflect.Parameter[] parameters = method.getParameters();
        java.lang.reflect.Parameter parameter = parameters[0];
        Class methodEventTypeArg = parameter.getType();
        if (methodEventTypeArg.isAssignableFrom(eventClass)) {
            method.invoke(dashboardViewAssistant, dashboardEvent);
        }
    }
}
Also used : ScreenFragment(io.jmix.ui.screen.ScreenFragment) PersistentDashboard(io.jmix.dashboards.entity.PersistentDashboard) Dashboard(io.jmix.dashboardsui.component.Dashboard) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ParamsMap(io.jmix.core.common.util.ParamsMap) StringUtils(org.apache.commons.lang3.StringUtils) Messages(io.jmix.core.Messages) DashboardEvent(io.jmix.dashboardsui.event.DashboardEvent) io.jmix.ui(io.jmix.ui) Method(java.lang.reflect.Method) Resource(org.springframework.core.io.Resource) DataManager(io.jmix.core.DataManager) UiEventListenerMethodAdapter(io.jmix.ui.sys.event.UiEventListenerMethodAdapter) ResourceLoader(org.springframework.core.io.ResourceLoader) DataUnit(io.jmix.ui.component.data.DataUnit) LookupWidget(io.jmix.dashboardsui.widget.LookupWidget) UiControllerReflectionInspector(io.jmix.ui.sys.UiControllerReflectionInspector) EventListener(org.springframework.context.event.EventListener) ApplicationListener(org.springframework.context.ApplicationListener) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOUtils(org.apache.commons.io.IOUtils) CanvasLayout(io.jmix.dashboardsui.component.CanvasLayout) Timer(io.jmix.ui.component.Timer) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) io.jmix.ui.component(io.jmix.ui.component) CODE(io.jmix.dashboardsui.screen.dashboard.view.DashboardViewScreen.CODE) AccessConstraintsHelper(io.jmix.dashboardsui.dashboard.tools.AccessConstraintsHelper) java.util(java.util) DashboardUpdatedEvent(io.jmix.dashboardsui.event.DashboardUpdatedEvent) Widget(io.jmix.dashboards.model.Widget) Function(java.util.function.Function) DASHBOARD(io.jmix.dashboardsui.screen.dashboard.editor.canvas.CanvasFragment.DASHBOARD) Parameter(io.jmix.dashboards.model.parameter.Parameter) UiEventsMulticaster(io.jmix.ui.sys.event.UiEventsMulticaster) RefreshableWidget(io.jmix.dashboardsui.widget.RefreshableWidget) MapScreenOptions(io.jmix.ui.screen.MapScreenOptions) Nullable(javax.annotation.Nullable) MapUtils(org.apache.commons.collections4.MapUtils) Logger(org.slf4j.Logger) DashboardException(io.jmix.dashboardsui.DashboardException) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Metadata(io.jmix.core.Metadata) JsonConverter(io.jmix.dashboards.converter.JsonConverter) ItemsSelectedEvent(io.jmix.dashboardsui.event.ItemsSelectedEvent) ParameterTransformer(io.jmix.dashboardsui.transformation.ParameterTransformer) DashboardModel(io.jmix.dashboards.model.DashboardModel) ReflectionUtils(org.springframework.util.ReflectionUtils) BeanFactory(org.springframework.beans.factory.BeanFactory) CanvasFragment(io.jmix.dashboardsui.screen.dashboard.editor.canvas.CanvasFragment) DashboardViewAssistant(io.jmix.dashboardsui.dashboard.assistant.DashboardViewAssistant) Parameter(io.jmix.dashboards.model.parameter.Parameter) Method(java.lang.reflect.Method) EventListener(org.springframework.context.event.EventListener)

Aggregations

Parameter (io.jmix.dashboards.model.parameter.Parameter)6 Messages (io.jmix.core.Messages)3 Metadata (io.jmix.core.Metadata)3 Widget (io.jmix.dashboards.model.Widget)3 ParameterTransformer (io.jmix.dashboardsui.transformation.ParameterTransformer)3 ScreenFragment (io.jmix.ui.screen.ScreenFragment)3 java.util (java.util)3 DataManager (io.jmix.core.DataManager)2 ParamsMap (io.jmix.core.common.util.ParamsMap)2 JsonConverter (io.jmix.dashboards.converter.JsonConverter)2 PersistentDashboard (io.jmix.dashboards.entity.PersistentDashboard)2 DashboardModel (io.jmix.dashboards.model.DashboardModel)2 ParameterValue (io.jmix.dashboards.model.parameter.type.ParameterValue)2 DashboardException (io.jmix.dashboardsui.DashboardException)2 CanvasLayout (io.jmix.dashboardsui.component.CanvasLayout)2 Dashboard (io.jmix.dashboardsui.component.Dashboard)2 DashboardViewAssistant (io.jmix.dashboardsui.dashboard.assistant.DashboardViewAssistant)2 AccessConstraintsHelper (io.jmix.dashboardsui.dashboard.tools.AccessConstraintsHelper)2 DashboardEvent (io.jmix.dashboardsui.event.DashboardEvent)2 DashboardUpdatedEvent (io.jmix.dashboardsui.event.DashboardUpdatedEvent)2