Commit 73a40708 authored by OlegPahhomov's avatar OlegPahhomov

SA0140-537 | pinned user preferences

parent add3ea76
package ee.stat.dashboard.controller.admin;
public enum FilterType {
FILTER, SKIP_USER, SKIP_USER_AND_FILTER;
FILTER_USER_AND_DATA, FILTER_DATA_ONLY, DO_NOT_FILTER;
public boolean filterUser(){
return this == FILTER;
public boolean filterUserPreferences(){
return this == FILTER_USER_AND_DATA;
}
public boolean filterGraph() {
return !skipFilter();
public boolean filterGraphData() {
return this == FILTER_USER_AND_DATA || this == FILTER_DATA_ONLY;
}
public boolean skipFilter() {
return this == SKIP_USER_AND_FILTER;
}
}
......@@ -3,9 +3,7 @@ package ee.stat.dashboard.controller.admin;
import ee.stat.dashboard.config.security.RoleString;
import ee.stat.dashboard.controller.Response;
import ee.stat.dashboard.service.statdata.StatDimensionsManager;
import ee.stat.dashboard.service.statdata.StatDimensionsService;
import ee.stat.dashboard.service.statdata.StatLinksManager;
import ee.stat.dashboard.service.statdata.StatLinksService;
import ee.stat.dashboard.service.statdata.xml.StatLinksResponse;
import ee.stat.dashboard.service.statdata.xml.XmlDimension;
import lombok.AllArgsConstructor;
......
......@@ -26,8 +26,8 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import static ee.stat.dashboard.controller.Response.of;
import static ee.stat.dashboard.controller.admin.FilterType.SKIP_USER;
import static ee.stat.dashboard.controller.admin.FilterType.SKIP_USER_AND_FILTER;
import static ee.stat.dashboard.controller.admin.FilterType.DO_NOT_FILTER;
import static ee.stat.dashboard.controller.admin.FilterType.FILTER_DATA_ONLY;
import static ee.stat.dashboard.controller.publics.DashboardWidgetController.removeParams;
import static ee.stat.dashboard.util.ResponseUtil.badRequest;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
......@@ -69,7 +69,7 @@ public class WidgetAdminController {
) {
removeParams(params);
WidgetResponse response = widgetService.findByWidgetIdOnly(widgetId, Language.getValue(language), graphType,
params, SKIP_USER);
params, FILTER_DATA_ONLY);
return of(response);
}
......@@ -83,7 +83,7 @@ public class WidgetAdminController {
) {
removeParams(params);
WidgetResponse response = widgetService.findByWidgetIdOnly(widgetId, Language.getValue(language), graphType,
params, SKIP_USER_AND_FILTER);
params, DO_NOT_FILTER);
return of(response);
}
......
......@@ -10,7 +10,6 @@ import ee.stat.dashboard.service.dashboard.DashboardService;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.dashboard.dto.UserDashboardDto;
import ee.stat.dashboard.service.dashboard.dto.UserWidgetsDto;
import ee.stat.dashboard.util.ResponseUtil;
import lombok.AllArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -24,7 +23,6 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import static ee.stat.dashboard.controller.Response.of;
import static ee.stat.dashboard.util.ResponseUtil.badRequest;
@RestController
@RequestMapping("/dashboard")
......
......@@ -6,12 +6,10 @@ import ee.stat.dashboard.controller.Response;
import ee.stat.dashboard.controller.admin.FilterType;
import ee.stat.dashboard.controller.user.UserSessionHolder;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.service.widget.widget.WidgetSaveService;
import ee.stat.dashboard.service.widget.widget.WidgetService;
import ee.stat.dashboard.service.widget.widget.dto.UserWidgetDto;
import ee.stat.dashboard.service.widget.widget.dto.WidgetResponse;
import ee.stat.dashboard.util.ResponseUtil;
import lombok.AllArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -45,7 +43,7 @@ public class DashboardWidgetController {
removeParams(params);
StatUser user = UserSessionHolder.getLoggedInUser();
WidgetResponse response = widgetService.findById(widgetId, dashboardId,
ehakId, Language.getValue(language), graphType, user, params, FilterType.FILTER);
ehakId, Language.getValue(language), graphType, user, params, FilterType.FILTER_USER_AND_DATA);
return of(response);
}
......
......@@ -21,7 +21,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.UUID;
import static java.text.MessageFormat.format;
......@@ -93,4 +92,4 @@ public class LoginController {
log.error("x-real-ip header is blank: {} using remote_addr {}", realIp, request.getRemoteAddr());
return request.getRemoteAddr();
}
}
\ No newline at end of file
}
......@@ -22,13 +22,25 @@ public class UserWidget {
private Long widget;
private Long dashboard;
private Long widgetDomain;
private Long dashboardWidget;
private boolean selected;
private Integer orderNr;
public UserWidget(Long appUser, Long widget, Long dashboard, Long widgetDomain) {
this.appUser = appUser;
this.widget = widget;
this.dashboard = dashboard;
this.widgetDomain = widgetDomain;
public static UserWidget fromWidgetDomain(Long appUser, Long widget, Long dashboard, Long widgetDomain){
UserWidget uw = new UserWidget();
uw.appUser = appUser;
uw.widget = widget;
uw.dashboard = dashboard;
uw.widgetDomain = widgetDomain;
return uw;
}
public static UserWidget fromDashboardWidget(Long appUser, Long widget, Long dashboard, Long dashboardWidget){
UserWidget uw = new UserWidget();
uw.appUser = appUser;
uw.widget = widget;
uw.dashboard = dashboard;
uw.dashboardWidget = dashboardWidget;
return uw;
}
}
......@@ -16,7 +16,7 @@ public interface DashboardRepository extends JpaRepository<Dashboard, Long>, Das
List<Dashboard> findByNameEn(String nameEt);
List<Dashboard> findAllByLevelAndStatus(Integer level, DashboardStatus status);
List<Dashboard> findAllByLevelAndStatusAndUserType(Integer level, DashboardStatus status, DashboardUserType dashboardUserType);
Page<Dashboard> findAllByLevelAndUserType(Integer level, DashboardUserType dashboardUserType, Pageable pageable);
......
......@@ -4,8 +4,11 @@ import ee.stat.dashboard.model.dashboard.DashboardWidget;
import ee.stat.dashboard.repository.custom.DashboardWidgetCustomRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface DashboardWidgetRepository extends JpaRepository<DashboardWidget, Long>, DashboardWidgetCustomRepository {
List<DashboardWidget> findAllByDashboard(Long id);
void deleteByWidget(Long id);
void deleteByDashboard(Long id);
boolean existsByDashboardAndWidget(Long dashboard, Long widget);
......
......@@ -3,7 +3,7 @@ package ee.stat.dashboard.service.dashboard;
import ee.stat.dashboard.config.security.StatUser;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.util.ErrorCode;
import ee.stat.dashboard.service.widget.widget.dto.PinningStrategy;
import ee.stat.dashboard.util.StatBadRequestException;
import ee.stat.dashboard.util.StatNotFoundRequestException;
import lombok.AllArgsConstructor;
......@@ -14,47 +14,92 @@ import java.util.Objects;
import static ee.stat.dashboard.util.ErrorCode.dashboard_is_hidden;
import static ee.stat.dashboard.util.ErrorCode.dashboard_is_not_existing;
import static ee.stat.dashboard.util.ErrorCode.dashboard_must_be_of_admin_type;
import static ee.stat.dashboard.util.ErrorCode.dashboard_must_be_regional;
import static ee.stat.dashboard.util.ErrorCode.my_dashboard_cannot_be_pinned;
import static ee.stat.dashboard.util.ErrorCode.my_dashboard_does_not_exist;
import static ee.stat.dashboard.util.StatListUtil.first;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
@Service
@AllArgsConstructor
public class ValidDashboardFinder {
public class DashboardFinder {
private DashboardServiceCache dashboardServiceCache;
private DashboardRepository dashboardRepository;
public Dashboard findValidDashboard(Long id, StatUser user) {
Dashboard dashboard = dashboardServiceCache.findById(id)
.orElseThrow(() -> new StatBadRequestException(dashboard_is_not_existing));
if (dashboard.getStatus().isHidden()) {
throw new StatBadRequestException(dashboard_is_hidden);
Dashboard dashboard = findById(id);
mustBeVisible(dashboard);
ifTypeIsUserUsersMustMatch(user, dashboard);
return dashboard;
}
public Dashboard dashboardForSaveWidgets(Long id, StatUser user) {
return findValidDashboard(id, user);
}
public Dashboard findValidDashboardForPinning(Long id, PinningStrategy pinning) {
Dashboard dashboard = findById(id);
mustBeVisible(dashboard);
ifTypeIsUserMustBeUnpinOnly(dashboard, pinning);
return dashboard;
}
public Dashboard findValidMyDashboard(StatUser user) {
List<Dashboard> myDashboards = dashboardRepository.findByAppUser(user.getId());
mustHave(myDashboards);
return first(myDashboards);
}
public Dashboard findValidDashboardForSaveRegion(Long id) {
Dashboard dashboard = findById(id);
mustBeVisible(dashboard);
mustBeRegional(dashboard);
mustBeAdminType(dashboard);
return dashboard;
}
public Dashboard findById(Long id) {
return dashboardServiceCache.findById(id)
.orElseThrow(() -> new StatBadRequestException(dashboard_is_not_existing, "Dashboard id" + id));
}
private void ifTypeIsUserMustBeUnpinOnly(Dashboard dashboard, PinningStrategy pinning) {
if (dashboard.getUserType().isUser() && !pinning.unpin()) {
throw new StatBadRequestException(my_dashboard_cannot_be_pinned, "Dashboard id" + dashboard.getId());
}
}
private void mustHave(List<Dashboard> myDashboards) {
if (isEmpty(myDashboards)) {
throw new StatNotFoundRequestException(my_dashboard_does_not_exist);
}
}
private void ifTypeIsUserUsersMustMatch(StatUser user, Dashboard dashboard) {
if (dashboard.getUserType().isUser()) {
if (user == null || !Objects.equals(user.getId(), dashboard.getAppUser())) {
throw new StatNotFoundRequestException(ErrorCode.my_dashboard_does_not_exist);
throw new StatNotFoundRequestException(my_dashboard_does_not_exist);
}
}
return dashboard;
}
public Dashboard findValidDashboardForPinning(Long id) {
Dashboard dashboard = dashboardServiceCache.findById(id)
.orElseThrow(() -> new StatBadRequestException(dashboard_is_not_existing));
if (dashboard.getStatus().isHidden()) {
throw new StatBadRequestException(dashboard_is_hidden);
}
private void mustBeAdminType(Dashboard dashboard) {
if (dashboard.getUserType().isUser()) {
throw new StatBadRequestException(ErrorCode.my_dashboard_cannot_be_pinned);
throw new StatBadRequestException(dashboard_must_be_of_admin_type, "Dashboard id" + dashboard.getId());
}
return dashboard;
}
public Dashboard findValidMyDashboard(StatUser user) {
List<Dashboard> myDashboards = dashboardRepository.findByAppUser(user.getId());
if (isEmpty(myDashboards)) {
throw new StatNotFoundRequestException(ErrorCode.my_dashboard_does_not_exist);
private void mustBeRegional(Dashboard dashboard) {
if (!dashboard.getType().isRegional()) {
throw new StatBadRequestException(dashboard_must_be_regional, "Dashboard id" + dashboard.getId());
}
}
private void mustBeVisible(Dashboard dashboard) {
if (dashboard.getStatus().isHidden()) {
throw new StatBadRequestException(dashboard_is_hidden, "Dashboard id " + dashboard.getId());
}
return first(myDashboards);
}
}
......@@ -6,10 +6,12 @@ import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.dashboard.DashboardStatus;
import ee.stat.dashboard.model.dashboard.DashboardType;
import ee.stat.dashboard.model.dashboard.DashboardUserType;
import ee.stat.dashboard.model.dashboard.DashboardWidget;
import ee.stat.dashboard.model.user.UserDashboard;
import ee.stat.dashboard.model.user.widget.UserWidget;
import ee.stat.dashboard.model.widget.back.WidgetDomain;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.repository.DashboardWidgetRepository;
import ee.stat.dashboard.repository.UserDashboardRepository;
import ee.stat.dashboard.repository.UserFilterRepository;
import ee.stat.dashboard.repository.UserFilterValueRepository;
......@@ -18,7 +20,6 @@ import ee.stat.dashboard.repository.WidgetDomainRepository;
import ee.stat.dashboard.service.dashboard.dto.UserDashboardDto;
import ee.stat.dashboard.service.dashboard.dto.UserWidgetsDto;
import ee.stat.dashboard.service.element.ElementService;
import ee.stat.dashboard.util.StatListUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
......@@ -38,7 +39,6 @@ import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@AllArgsConstructor
public class DashboardSaveService {
private DashboardServiceCache dashboardServiceCache;
private WidgetDomainRepository widgetDomainRepository;
private UserWidgetRepository userWidgetRepository;
private UserDashboardRepository userDashboardRepository;
......@@ -46,11 +46,12 @@ public class DashboardSaveService {
private UserFilterRepository userFilterRepository;
private DashboardRepository dashboardRepository;
private ElementService elementService;
private DashboardFinder dashboardFinder;
private DashboardWidgetRepository widgetRepository;
public Long saveRegion(Long id, UserDashboardDto dto, StatUser user) {
Dashboard dashboard = dashboardServiceCache.findById(id).orElseThrow(badRequest(id, "dashboard"));
if (dto.getRegion() == null) throw badRequest(id, "region").get();
if (dashboard.getType().isGlobal()) throw badRequest(id, "global request").get();
Dashboard dashboard = dashboardFinder.findValidDashboardForSaveRegion(id);
Optional<Element> byIdOp = elementService.findById(dto.getRegion());
if (!byIdOp.isPresent()) throw badRequest(id, "region").get();
......@@ -70,29 +71,40 @@ public class DashboardSaveService {
}
public Long saveWidgetsAndSetVisited(Long dashboardId, UserWidgetsDto dto, StatUser user) {
setVisited(dashboardId, user);
return saveWidgets(dashboardId, dto, user);
Dashboard dashboard = dashboardFinder.dashboardForSaveWidgets(dashboardId, user);
setVisited(dashboard, user);
return saveWidgets(dashboard, user, dto);
}
public Long saveWidgets(Long dashboardId, UserWidgetsDto dto, StatUser user) {
Dashboard dashboard = dashboardServiceCache.findById(dashboardId).orElseThrow(badRequest(dashboardId, "dashboard"));
List<WidgetDomain> widgetDomains = widgetDomainRepository.findAllByDashboard(dashboardId);
if (isNotEmpty(widgetDomains)) {
List<Long> allowedWidgetIds = getWidgetIds(widgetDomains);
dto.getSelectedWidgets().removeIf(e -> !allowedWidgetIds.contains(e));
List<UserWidget> userWidgets = widgetDomains.stream()
.map(widgetDomain -> map(widgetDomain, user, dashboard, dto.getSelectedWidgets()))
.collect(Collectors.toList());
userWidgetRepository.saveAll(userWidgets);
public Long saveWidgets(Dashboard dashboard, StatUser user, UserWidgetsDto dto) {
if (dashboard.getUserType().isAdmin()) {
List<WidgetDomain> widgetDomains = widgetDomainRepository.findAllByDashboard(dashboard.getId());
if (isNotEmpty(widgetDomains)) {
List<Long> allowedWidgetIds = getWidgetIds(widgetDomains);
dto.getSelectedWidgets().removeIf(e -> !allowedWidgetIds.contains(e));
List<UserWidget> userWidgets = widgetDomains.stream()
.map(widgetDomain -> mapFromWidgetDomain(widgetDomain, user, dashboard, dto.getSelectedWidgets()))
.collect(Collectors.toList());
userWidgetRepository.saveAll(userWidgets);
}
} else {
List<DashboardWidget> dashboardWidgets = widgetRepository.findAllByDashboard(dashboard.getId());
if (isNotEmpty(dashboardWidgets)) {
List<UserWidget> userWidgets = dashboardWidgets.stream()
.map(dashboardWidget -> mapFromDashboardWidget(dashboardWidget, user, dashboard, dto.getSelectedWidgets()))
.collect(Collectors.toList());
userWidgetRepository.saveAll(userWidgets);
}
}
return dashboardId;
return dashboard.getId();
}
public void setVisited(Long dashboardId, StatUser user) {
List<UserDashboard> userDashboards = userDashboardRepository.findAllByDashboardAndAndAppUser(dashboardId, user.getId());
UserDashboard userDashboard = userDashboards.stream().findFirst().orElse(new UserDashboard(user.getId(), dashboardId));
public void setVisited(Dashboard dashboard, StatUser user) {
List<UserDashboard> userDashboards = userDashboardRepository.findAllByDashboardAndAndAppUser(dashboard.getId(), user.getId());
UserDashboard userDashboard = userDashboards.stream().findFirst().orElse(new UserDashboard(user.getId(), dashboard.getId()));
if (!userDashboard.isVisited()) {
userDashboard.setVisited(true);
userDashboardRepository.save(userDashboard);
......@@ -103,15 +115,24 @@ public class DashboardSaveService {
return widgetDomains.stream().map(WidgetDomain::getWidget).collect(Collectors.toList());
}
private UserWidget map(WidgetDomain widgetDomain, StatUser user, Dashboard dashboard, List<Long> selectedWidgets) {
private UserWidget mapFromWidgetDomain(WidgetDomain widgetDomain, StatUser user, Dashboard dashboard, List<Long> selectedWidgets) {
UserWidget userWidget = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboard.getId(), widgetDomain.getWidget())
.orElse(new UserWidget(user.getId(), widgetDomain.getWidget(), dashboard.getId(), widgetDomain.getId()));
.orElse(UserWidget.fromWidgetDomain(user.getId(), widgetDomain.getWidget(), dashboard.getId(), widgetDomain.getId()));
userWidget.setSelected(selectedWidgets.contains(widgetDomain.getWidget()));
int indexOf = selectedWidgets.indexOf(widgetDomain.getWidget());
userWidget.setOrderNr(indexOf == -1 ? null : indexOf);
return userWidget;
}
private UserWidget mapFromDashboardWidget(DashboardWidget dashboardWidget, StatUser user, Dashboard dashboard, List<Long> selectedWidgets) {
UserWidget userWidget = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboard.getId(), dashboardWidget.getWidget())
.orElse(UserWidget.fromDashboardWidget(user.getId(), dashboardWidget.getWidget(), dashboard.getId(), dashboardWidget.getId()));
userWidget.setSelected(selectedWidgets.contains(dashboardWidget.getWidget()));
int indexOf = selectedWidgets.indexOf(dashboardWidget.getWidget());
userWidget.setOrderNr(indexOf == -1 ? null : indexOf);
return userWidget;
}
private boolean regionsHaveChanged(Element region, UserDashboard userDashboard) {
return !regionsHaveNotChanged(region, userDashboard);
}
......@@ -120,7 +141,7 @@ public class DashboardSaveService {
return regionsEqual(userDashboard, region) || wholeEstoniaEquals(userDashboard, region);
}
public Dashboard saveNewUserDashboard(Long user){
public Dashboard saveNewUserDashboard(Long user) {
Dashboard dashboard = new Dashboard();
dashboard.setAppUser(user);
dashboard.setStatus(DashboardStatus.VISIBLE);
......
......@@ -4,26 +4,15 @@ package ee.stat.dashboard.service.dashboard;
import ee.stat.dashboard.config.security.StatUser;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.element.DomainService;
import ee.stat.dashboard.service.user.UserDashboardService;
import ee.stat.dashboard.service.user.UserWidgetService;
import ee.stat.dashboard.util.ErrorCode;
import ee.stat.dashboard.util.StatBadRequestException;
import ee.stat.dashboard.util.StatListUtil;
import ee.stat.dashboard.util.StatNotFoundRequestException;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
import static ee.stat.dashboard.util.ErrorCode.dashboard_is_not_existing;
import static ee.stat.dashboard.util.StatListUtil.*;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
@Service
@AllArgsConstructor
......@@ -36,19 +25,19 @@ public class DashboardService {
private DashboardServiceCache dashboardServiceCache;
private UserDashboardService userDashboardService;
private DashboardWidgetService dashboardWidgetService;
private ValidDashboardFinder validDashboardFinder;
private DashboardFinder dashboardFinder;
public List<DashboardResponse> findAll(Language lang, StatUser user) {
return dashboardConverter.mapSimple(dashboardServiceCache.findAllByLevel(), lang, user);
}
public DashboardResponse findMyDashboard(Language lang, StatUser user) {
Dashboard dashboard = validDashboardFinder.findValidMyDashboard(user);
Dashboard dashboard = dashboardFinder.findValidMyDashboard(user);
return map(dashboard, lang, user);
}
public DashboardResponse findById(Long id, Language lang, StatUser user) {
Dashboard dashboard = validDashboardFinder.findValidDashboard(id, user);
Dashboard dashboard = dashboardFinder.findValidDashboard(id, user);
return map(dashboard, lang, user);
}
......@@ -58,11 +47,11 @@ public class DashboardService {
dto.setWidgets(dashboardWidgetService.getWidgets(dashboard, lang));
} else {
dto.setElements(domainService.loadDomainsAndWidgets(dashboard.getId(), lang));
if (user != null) {
dto.setSelectedWidgets(userWidgetService.getSelectedWidgets(dashboard, user));
if (userDashboardService.getFirstVisit(dashboard, user)) {
dto.setFirstVisit(true);
}
}
if (user != null) {
dto.setSelectedWidgets(userWidgetService.getSelectedWidgets(dashboard, user));
if (userDashboardService.getFirstVisit(dashboard, user)) {
dto.setFirstVisit(true);
}
}
return dto;
......
......@@ -2,6 +2,7 @@ package ee.stat.dashboard.service.dashboard;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.dashboard.DashboardStatus;
import ee.stat.dashboard.model.dashboard.DashboardUserType;
import ee.stat.dashboard.repository.DashboardRepository;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
......@@ -23,7 +24,7 @@ public class DashboardServiceCache {
@Cacheable("DashboardServiceCache_findAllByLevel")
public List<Dashboard> findAllByLevel() {
return dashboardRepository.findAllByLevelAndStatus(0, DashboardStatus.VISIBLE);
return dashboardRepository.findAllByLevelAndStatusAndUserType(0, DashboardStatus.VISIBLE, DashboardUserType.ADMIN);
}
@Cacheable(value = "DashboardServiceCache_findAllFromByTo", key = "#dashboardId")
......
package ee.stat.dashboard.service.widget.widget;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.dashboard.DashboardWidget;
import ee.stat.dashboard.model.user.widget.UserFilter;
import ee.stat.dashboard.model.user.widget.UserFilterValue;
import ee.stat.dashboard.model.user.widget.UserGraphType;
......@@ -30,8 +31,8 @@ public class UserWidgetCopyService {
private UserLegendRepository userLegendRepository;
private UserLegendValueRepository userLegendValueRepository;
public void saveAsCopy(UserWidget userWidget, Dashboard myDashboard) {
UserWidget newUserWidget = copy(userWidget, myDashboard);
public void saveAsCopy(UserWidget userWidget, Dashboard myDashboard, DashboardWidget dashboardWidget) {
UserWidget newUserWidget = copy(userWidget, myDashboard, dashboardWidget);
List<UserGraphType> userGraphTypes = userGraphTypeRepository.findAllByUserWidget(userWidget.getId());
for (UserGraphType userGraphType : userGraphTypes) {
UserGraphType newUserGraph = copy(userGraphType, newUserWidget);
......@@ -50,12 +51,13 @@ public class UserWidgetCopyService {
}
}
private UserWidget copy(UserWidget existing, Dashboard reference) {
private UserWidget copy(UserWidget existing, Dashboard reference, DashboardWidget dashboardWidget) {
UserWidget newItem = new UserWidget();
BeanUtils.copyProperties(existing, newItem);
newItem.setId(null);
newItem.setDashboard(reference.getId());
newItem.setWidgetDomain(null);
newItem.setDashboardWidget(dashboardWidget.getId());
return userWidgetRepository.save(newItem);
}
......
......@@ -5,57 +5,66 @@ import ee.stat.dashboard.model.dashboard.Dashboard;
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.dashboard.DashboardServiceCache;
import ee.stat.dashboard.util.ErrorCode;
import ee.stat.dashboard.util.StatBadRequestException;
import ee.stat.dashboard.util.StatNotFoundRequestException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.Optional;
import static ee.stat.dashboard.util.ErrorCode.*;
import static ee.stat.dashboard.util.ErrorCode.widget_is_hidden;
import static ee.stat.dashboard.util.ErrorCode.widget_is_not_existing_or_added_to_dashboard;
import static ee.stat.dashboard.util.ResponseUtil.badRequest;
@Service
@AllArgsConstructor
public class ValidWidgetFinder {
public class WidgetFinder {
private WidgetServiceCache widgetServiceCache;
private DashboardServiceCache dashboardServiceCache;
private DashboardFinder dashboardFinder;
private WidgetRepository widgetRepository;
private DashboardWidgetRepository dashboardWidgetRepository;
public Widget findWidgetForPinning(Long id){
Widget widget = findWidgetByIdFromCache(id);
mustBeVisible(widget);
return widget;
}
public Widget findWidgetForAdmin(Long id) {
return widgetRepository.findById(id).orElseThrow(()-> new StatBadRequestException(widget_is_not_existing));
}