Commit be8e7caa authored by oleg.pahhomov's avatar oleg.pahhomov

SA0140-611 | add dashboard to widget response for user dashboard

parent 63b90620
package ee.stat.dashboard.service.widget.widget;
import ee.stat.dashboard.config.security.StatUser;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.user.widget.UserGraphType;
import ee.stat.dashboard.model.user.widget.UserWidget;
import ee.stat.dashboard.model.widget.back.GraphType;
......@@ -28,18 +29,17 @@ public class GraphTypeService {
private GraphTypeServiceCache graphTypeServiceCache;
private UserGraphTypeRepository userGraphTypeRepository;
public GraphTypeDto mapToGraphType(Long dashboardId, Long widgetId, StatUser user) {
public GraphTypeDto mapToGraphType(Dashboard dashboard, Long widgetId, StatUser user) {
List<GraphType> graphTypes = graphTypeServiceCache.findAllByWidget(widgetId);
if (isEmpty(graphTypes)) return null;
List<UserGraphType> userGraphTypes = userGraphTypes(dashboardId, widgetId, user);
List<UserGraphType> userGraphTypes = userGraphTypes(dashboard, widgetId, user);
return mapToGraphType(graphTypes, userGraphTypes);
}
private List<UserGraphType> userGraphTypes(Long dashboardId, Long widgetId, StatUser user) {
if (user == null) return null;
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboardId, widgetId);
if (CollectionUtils.isEmpty(userWidgets)) return null;
private List<UserGraphType> userGraphTypes(Dashboard dashboard, Long widgetId, StatUser user) {
if (user == null || dashboard == null) return null;
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboard.getId(), widgetId);
if (isEmpty(userWidgets)) return null;
return userGraphTypeRepository.findAllByUserWidget(StatListUtil.first(userWidgets).getId());
}
......
package ee.stat.dashboard.service.widget.widget;
import com.google.common.collect.Lists;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.widget.back.Widget;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.service.dashboard.DashboardConverter;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.widget.widget.dto.WidgetResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@AllArgsConstructor
public class WidgetConverter {
private WidgetDomainService widgetDomainService;
private DashboardRepository dashboardRepository;
private DashboardConverter dashboardConverter;
public WidgetResponse mapMinimal(Widget widget, Language lang) {
WidgetResponse dto = new WidgetResponse();
......@@ -23,15 +32,19 @@ public class WidgetConverter {
return dto;
}
public void mapWidgetData(Widget widget, Long dashboardId, Language lang, WidgetResponse dto) {
public void mapWidgetData(Widget widget, Dashboard dashboard, Language lang, WidgetResponse dto) {
dto.setTimePeriod(widget.getTimePeriod());
dto.setPrecisionScale(widget.getPrecisionScale());
dto.setNote(widget.getNote(lang));
dto.setUnit(widget.getUnit(lang));
dto.setSource(widget.getSource(lang));
dto.setDescription(widget.getDescription(lang));
if (dashboardId != null) {
dto.setElements(widgetDomainService.getDomains(widget.getId(), dashboardId, lang));
if (dashboard != null) {
if (dashboard.getUserType().isAdmin()) {
dto.setElements(widgetDomainService.getDomains(widget.getId(), dashboard.getId(), lang));
} else {
dto.setDashboards(findAllByWidget(widget.getId(), lang));
}
}
if (widget.getMethodsLink(lang) != null) {
dto.setMethodsLinks(Lists.newArrayList(widget.getMethodsLink(lang)));
......@@ -40,4 +53,9 @@ public class WidgetConverter {
dto.setStatisticianJobLinks(Lists.newArrayList(widget.getStatisticianJobLink(lang)));
}
}
public List<DashboardResponse> findAllByWidget(Long widgetId, Language lang) {
List<Dashboard> dashboards = dashboardRepository.findAllByWidget(widgetId);
return dashboardConverter.mapSimple(dashboards, lang);
}
}
package ee.stat.dashboard.service.widget.widget;
import ee.stat.dashboard.model.classifier.Domain;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.service.element.DomainConverter;
import ee.stat.dashboard.service.element.DomainServiceCache;
......
......@@ -5,6 +5,7 @@ import ee.stat.dashboard.model.widget.back.Widget;
import ee.stat.dashboard.repository.DashboardWidgetRepository;
import ee.stat.dashboard.repository.WidgetRepository;
import ee.stat.dashboard.service.dashboard.DashboardFinder;
import ee.stat.dashboard.service.widget.widget.dto.WidgetDashboardContainer;
import ee.stat.dashboard.util.StatBadRequestException;
import ee.stat.dashboard.util.StatNotFoundRequestException;
import lombok.AllArgsConstructor;
......@@ -30,11 +31,12 @@ public class WidgetFinder {
return widget;
}
public Widget findWidgetForAdmin(Long id) {
return widgetRepository.findById(id).orElseThrow(()-> new StatBadRequestException(widget_is_not_existing));
public WidgetDashboardContainer findWidgetForAdmin(Long id) {
Widget widget = widgetRepository.findById(id).orElseThrow(() -> new StatBadRequestException(widget_is_not_existing));
return new WidgetDashboardContainer(widget);
}
public Widget findWidgetForViewing(Long id, Long dashboardId) {
public WidgetDashboardContainer findWidgetForViewing(Long id, Long dashboardId) {
Dashboard dashboard = dashboardFinder.findById(dashboardId);
if (dashboard.getUserType().isAdmin()) {
existsAdminWidget(id, dashboardId);
......@@ -43,7 +45,7 @@ public class WidgetFinder {
}
Widget widget = findWidgetByIdFromCache(id);
mustBeVisible(widget);
return widget;
return new WidgetDashboardContainer(widget, dashboard);
}
private Widget findWidgetByIdFromCache(Long id) {
......
......@@ -3,6 +3,7 @@ package ee.stat.dashboard.service.widget.widget;
import ee.stat.dashboard.config.security.StatUser;
import ee.stat.dashboard.controller.admin.FilterType;
import ee.stat.dashboard.model.classifier.Element;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.widget.back.Excel;
import ee.stat.dashboard.model.widget.back.GraphType;
import ee.stat.dashboard.model.widget.back.StatDb;
......@@ -18,6 +19,7 @@ import ee.stat.dashboard.service.widget.widget.dto.GraphDto;
import ee.stat.dashboard.service.widget.widget.dto.GraphTypeDto;
import ee.stat.dashboard.service.widget.widget.dto.UserGraphTypeDto;
import ee.stat.dashboard.service.widget.widget.dto.UserWidgetDto;
import ee.stat.dashboard.service.widget.widget.dto.WidgetDashboardContainer;
import ee.stat.dashboard.service.widget.widget.dto.WidgetResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
......@@ -52,20 +54,22 @@ public class WidgetService {
public WidgetResponse findById(Long id, Long dashboardId, Long ehakId, Language lang, String graphType,
StatUser user, Map<String, String> params, FilterType filterType) {
Widget widget = widgetFinder.findWidgetForViewing(id, dashboardId);
return mapWidget(widget, dashboardId, ehakId, lang, graphType, user, params, filterType);
WidgetDashboardContainer widget = widgetFinder.findWidgetForViewing(id, dashboardId);
return mapWidget(widget, ehakId, lang, graphType, user, params, filterType);
}
public WidgetResponse findByWidgetIdOnly(Long id, Language lang, String graphType,
Map<String, String> params, FilterType filterType) {
Widget widget = widgetFinder.findWidgetForAdmin(id);
return mapWidget(widget, null, null, lang, graphType, null, params, filterType);
WidgetDashboardContainer widget = widgetFinder.findWidgetForAdmin(id);
return mapWidget(widget, null, lang, graphType, null, params, filterType);
}
public WidgetResponse mapWidget(Widget widget, Long dashboardId, Long ehakId, Language lang, String graphTypeStr,
StatUser user, Map<String, String> params, FilterType filterType) {
private WidgetResponse mapWidget(WidgetDashboardContainer widgetContainer, Long ehakId, Language lang, String graphTypeStr,
StatUser user, Map<String, String> params, FilterType filterType) {
Widget widget = widgetContainer.getWidget();
Dashboard dashboard = widgetContainer.getDashboard();
WidgetResponse dto = widgetConverter.mapMinimal(widget, lang);
widgetConverter.mapWidgetData(widget, dashboardId, lang, dto);
widgetConverter.mapWidgetData(widget, dashboard, lang, dto);
List<StatDb> statDbs = statDbCache.findByWidget(widget.getId());
statDbs.forEach(api -> addStatMeta(dto, api));
......@@ -76,7 +80,7 @@ public class WidgetService {
.filter(e -> !e.getExcelType().isWidgetMeta())
.forEach(excel -> addExcelMeta(dto, excel));
GraphTypeDto graphTypes = graphTypeService.mapToGraphType(dashboardId, widget.getId(), user);
GraphTypeDto graphTypes = graphTypeService.mapToGraphType(dashboard, widget.getId(), user);
if (graphTypes == null) {
dto.setGraphTypes(GraphTypeDto.empty());
dto.setDiagram(GraphDto.empty());
......
package ee.stat.dashboard.service.widget.widget.dto;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.widget.back.Widget;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class WidgetDashboardContainer {
private Widget widget;
private Dashboard dashboard;
public WidgetDashboardContainer(Widget widget) {
this.widget = widget;
}
}
......@@ -3,6 +3,7 @@ package ee.stat.dashboard.service.widget.widget.dto;
import ee.stat.dashboard.model.widget.back.enums.GraphTypeEnum;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.model.widget.back.enums.TimePeriod;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.element.dto.DomainResponse;
import lombok.Getter;
import lombok.Setter;
......@@ -26,6 +27,7 @@ public class WidgetResponse {
private String unit;
private String source;
private List<DomainResponse> elements;
private List<DashboardResponse> dashboards;
private LocalDateTime dataUpdatedAt;
private List<String> cubes;
private List<String> methodsLinks;
......
......@@ -9,6 +9,7 @@ import ee.stat.dashboard.service.widget.widget.dto.UserFilterDto;
import ee.stat.dashboard.service.widget.widget.dto.UserGraphTypeDto;
import ee.stat.dashboard.service.widget.widget.dto.UserWidgetDto;
import ee.stat.dashboard.service.widget.widget.dto.WidgetResponse;
import ee.stat.dashboard.validator.strategy.DashboardValidationStrategy;
import ee.stat.dashboard.validator.strategy.WidgetValidationStrategy;
import org.junit.Test;
import org.springframework.http.HttpStatus;
......@@ -21,6 +22,7 @@ import static ee.stat.dashboard.AssertUtil.USER_REGULAR;
import static ee.stat.dashboard.AssertUtil.first;
import static ee.stat.dashboard.AssertUtil.getTourismAccomodationWidget;
import static ee.stat.dashboard.AssertUtil.list_size;
import static ee.stat.dashboard.validator.Dashboard_Validator.assertTourismDashboard;
import static ee.stat.dashboard.validator.Widget_AccomodationValidator.assertAccomodationWidget;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
......@@ -81,6 +83,9 @@ public class DashboardWidgetController_SavePreferences_Test extends DashboardWid
WidgetResponse myDashboardWidget = getWidget(myDashboard.getId(), accommodationId, Language.ET, GraphTypeEnum.line, USER_REGULAR);
assertNotNull(myDashboardWidget.getPreferences());
list_size(myDashboardWidget.getPreferences().getGraphs(), 1);
list_size(myDashboardWidget.getDashboards(), 1);
DashboardResponse firstDashboard = first(myDashboardWidget.getDashboards());
assertTourismDashboard(firstDashboard, DashboardValidationStrategy.ROLE_DASHBOARD);
assertEquals(originalWidget.getPreferences().getGraphs(), myDashboardWidget.getPreferences().getGraphs());
//old widget has pinned = true property
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment