Adding Business Logic
DashboardViewAssistant
is a helper component that can be used to manage your dashboards. It is a Spring bean with the prototype
scope that gets a managed dashboard instance to its init
method.
The assistant bean is a good choice if you need to allow users to turn some business logic on and off or let users choose one of the predefined business logic providers.
Let’s look at the example of the VisitScheduleAssistant
service that reloads visits:
@Component("sample_VisitScheduleAssistant")
@Scope("prototype") (1)
public class VisitScheduleAssistant implements DashboardViewAssistant { (2)
protected Dashboard dashboard;
@Override
public void init(Dashboard dashboard) { (3)
this.dashboard = dashboard;
}
@EventListener
public void dashboardEventListener(DashboardUpdatedEvent event){ (4)
ScreenFragment widget = dashboard.getWidget("visits-calendar"); (5)
if (widget instanceof VisitsCalendarWidget){ (6)
VisitsCalendarWidget visitsCalendarWidget = (VisitsCalendarWidget) widget;
visitsCalendarWidget.reloadSchedule();
}
}
}
1 | Assistant bean should have the prototype bean scope. |
2 | It should implement the DashboardViewAssistant interface. |
3 | Overrides the init method by saving the dashboard object for later use. |
4 | Subscribes to the DashboardUpdatedEvent - an event that the add-on sends each time the dashboard needs to be updated. |
5 | Checks the id of the VisitsCalendarWidget . |
6 | Gets the VisitsCalendarWidget from the dashboard and calls the reloadSchedule() method. |
Here is the implementation of the reloadSchedule()
method in the VisitsCalendarWidget
that reloads visits:
@Autowired
private DataLoader visitsDl;
public void reloadSchedule(){
visitsDl.load();
}
Then, while configuring a dashboard, you can set the Assistant bean in the Dashboard editor.