Commit 78b1dca7 authored by oleg.pahhomov's avatar oleg.pahhomov

SA0140-537 | quick pinning causing causing duplicate rows

parent 2d6884db
......@@ -14,7 +14,7 @@ public interface UserWidgetRepository extends JpaRepository<UserWidget, Long> {
List<UserWidget> findAllByWidget(Long widget);
Optional<UserWidget> findByAppUserAndDashboardAndWidget(Long user, Long dashboard, Long widget);
List<UserWidget> findByAppUserAndDashboardAndWidget(Long user, Long dashboard, Long widget);
Optional<UserWidget> findByWidgetDomainAndAppUser(Long widgetDomain, Long user);
......
......@@ -31,6 +31,7 @@ import java.util.Optional;
import java.util.stream.Collectors;
import static ee.stat.dashboard.util.ResponseUtil.badRequest;
import static ee.stat.dashboard.util.StatListUtil.first;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@Slf4j
......@@ -92,7 +93,6 @@ public class DashboardSaveService {
} 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());
......@@ -116,8 +116,7 @@ public class DashboardSaveService {
}
private UserWidget mapFromWidgetDomain(WidgetDomain widgetDomain, StatUser user, Dashboard dashboard, List<Long> selectedWidgets) {
UserWidget userWidget = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboard.getId(), widgetDomain.getWidget())
.orElse(UserWidget.fromWidgetDomain(user.getId(), widgetDomain.getWidget(), dashboard.getId(), widgetDomain.getId()));
UserWidget userWidget = existingOrNewWidgetDomain(widgetDomain, user, dashboard);
userWidget.setSelected(selectedWidgets.contains(widgetDomain.getWidget()));
int indexOf = selectedWidgets.indexOf(widgetDomain.getWidget());
userWidget.setOrderNr(indexOf == -1 ? null : indexOf);
......@@ -125,14 +124,23 @@ public class DashboardSaveService {
}
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 userWidget = existingOrNewDashboardWidget(dashboardWidget, user, dashboard);
userWidget.setSelected(selectedWidgets.contains(dashboardWidget.getWidget()));
int indexOf = selectedWidgets.indexOf(dashboardWidget.getWidget());
userWidget.setOrderNr(indexOf == -1 ? null : indexOf);
return userWidget;
}
private UserWidget existingOrNewDashboardWidget(DashboardWidget dashboardWidget, StatUser user, Dashboard dashboard) {
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboard.getId(), dashboardWidget.getWidget());
return isNotEmpty(userWidgets) ? first(userWidgets) : UserWidget.fromDashboardWidget(user.getId(), dashboardWidget.getWidget(), dashboard.getId(), dashboardWidget.getId());
}
private UserWidget existingOrNewWidgetDomain(WidgetDomain widgetDomain, StatUser user, Dashboard dashboard) {
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboard.getId(), widgetDomain.getWidget());
return isNotEmpty(userWidgets) ? first(userWidgets) : UserWidget.fromWidgetDomain(user.getId(), widgetDomain.getWidget(), dashboard.getId(), widgetDomain.getId());
}
private boolean regionsHaveChanged(Element region, UserDashboard userDashboard) {
return !regionsHaveNotChanged(region, userDashboard);
}
......
......@@ -2,12 +2,15 @@ package ee.stat.dashboard.service.widget.widget;
import ee.stat.dashboard.config.security.StatUser;
import ee.stat.dashboard.model.user.widget.UserGraphType;
import ee.stat.dashboard.model.user.widget.UserWidget;
import ee.stat.dashboard.model.widget.back.GraphType;
import ee.stat.dashboard.model.widget.back.enums.GraphTypeEnum;
import ee.stat.dashboard.repository.UserGraphTypeRepository;
import ee.stat.dashboard.repository.UserWidgetRepository;
import ee.stat.dashboard.service.widget.widget.dto.GraphTypeDto;
import ee.stat.dashboard.util.StatListUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import java.util.List;
......@@ -35,9 +38,9 @@ public class GraphTypeService {
private List<UserGraphType> userGraphTypes(Long dashboardId, Long widgetId, StatUser user) {
if (user == null) return null;
return userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboardId, widgetId)
.map(uw -> userGraphTypeRepository.findAllByUserWidget(uw.getId()))
.orElse(null);
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboardId, widgetId);
if (CollectionUtils.isEmpty(userWidgets)) return null;
return userGraphTypeRepository.findAllByUserWidget(StatListUtil.first(userWidgets).getId());
}
private GraphTypeDto mapToGraphType(List<GraphType> graphTypes, List<UserGraphType> userGraphTypes) {
......
......@@ -13,8 +13,10 @@ import ee.stat.dashboard.service.dashboard.DashboardSaveService;
import ee.stat.dashboard.service.dashboard.DashboardFinder;
import ee.stat.dashboard.service.widget.widget.dto.PinningStrategy;
import ee.stat.dashboard.service.widget.widget.dto.UserWidgetDto;
import ee.stat.dashboard.util.StatListUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -42,9 +44,11 @@ public class WidgetSaveService {
private WidgetFinder widgetFinder;
public void savePreferences(Long widgetId, Long dashboardId, StatUser user, UserWidgetDto preferences) {
UserWidget userWidget = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboardId, widgetId)
.orElseThrow(badRequest(widgetId, "user widget missing, widget id"));
userWidgetPreferencesSaveService.savePreferences(userWidget, preferences);
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboardId, widgetId);
if (CollectionUtils.isEmpty(userWidgets)){
throw badRequest(widgetId, "user widget missing, widget id").get();
}
userWidgetPreferencesSaveService.savePreferences(first(userWidgets), preferences);
}
public void savePin(Long requestWidgetId, Long requestDashboardId, StatUser user, UserWidgetDto dto) {
......@@ -63,11 +67,13 @@ public class WidgetSaveService {
if (!dashboardWidgetRepository.existsByDashboardAndWidget(myDashboard.getId(), widget.getId())) {
return;
}
Optional<UserWidget> userWidgetOp = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), myDashboard.getId(), widget.getId());
userWidgetOp.ifPresent(userWidget -> {
userWidgetDeleteService.deleteUserData(userWidget);
userWidgetRepository.deleteById(userWidget.getId());
});
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), myDashboard.getId(), widget.getId());
if (CollectionUtils.isNotEmpty(userWidgets)) {
userWidgets.forEach(userWidget -> {
userWidgetDeleteService.deleteUserData(userWidget);
userWidgetRepository.deleteById(userWidget.getId());
});
}
dashboardWidgetRepository.deleteByDashboardAndWidget(myDashboard.getId(), widget.getId());
}
......@@ -77,11 +83,12 @@ public class WidgetSaveService {
return;
}
DashboardWidget dashboardWidget = saveDashboardWidget(widget, myDashboard);
Optional<UserWidget> userWidgetOp = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), myDashboard.getId(), widget.getId());
if (!userWidgetOp.isPresent()) {
Optional<UserWidget> userWidgetOfPrevDashboardOp =
userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboard.getId(), widget.getId());
userWidgetOfPrevDashboardOp.ifPresent(userWidget -> userWidgetCopyService.saveAsCopy(userWidget, myDashboard, dashboardWidget));
List<UserWidget> myDashboardUserWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), myDashboard.getId(), widget.getId());
if (CollectionUtils.isEmpty(myDashboardUserWidgets)) {
List<UserWidget> userWidgetsOfPrevDashboard = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboard.getId(), widget.getId());
if (CollectionUtils.isNotEmpty(userWidgetsOfPrevDashboard)) {
userWidgetCopyService.saveAsCopy(first(userWidgetsOfPrevDashboard), myDashboard, dashboardWidget);
}
}
}
......
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