@Override
public void handle(Request request, Response response) throws Exception {
DbSession dbSession = dbClient.openSession(false);
try {
Integer userId = UserSession.get().userId();
DashboardDto dashboard = dbClient.dashboardDao().getAllowedByKey(dbSession, request.mandatoryParamAsLong(PARAM_KEY),
userId != null ? userId.longValue() : null);
if (dashboard == null) {
throw new NotFoundException();
}
JsonWriter json = response.newJsonWriter();
json.beginObject();
json.prop("key", dashboard.getKey());
json.prop("name", dashboard.getName());
json.prop("layout", dashboard.getColumnLayout());
json.prop("desc", dashboard.getDescription());
json.prop("global", dashboard.getGlobal());
json.prop("shared", dashboard.getShared());
if (dashboard.getUserId() != null) {
UserDto user = dbClient.userDao().getUser(dashboard.getUserId());
if (user != null) {
json.name("owner").beginObject();
// TODO to be shared and extracted from here
json.prop("login", user.getLogin());
json.prop("name", user.getName());
json.endObject();
}
}
// load widgets and related properties
json.name("widgets").beginArray();
Collection<WidgetDto> widgets = dbClient.widgetDao().findByDashboard(dbSession, dashboard.getKey());
ListMultimap<Long, WidgetPropertyDto> propertiesByWidget = WidgetPropertyDto.groupByWidgetId(
dbClient.widgetPropertyDao().findByDashboard(dbSession, dashboard.getKey()));
for (WidgetDto widget : widgets) {
json.beginObject();
json.prop("id", widget.getId());
json.prop("key", widget.getWidgetKey());
json.prop("name", widget.getName());