Commit e65be6d9 authored by Andrus Aru's avatar Andrus Aru

repo update 31.01.2029

parent 454c2f24
......@@ -5,7 +5,7 @@
<groupId>ee.stat</groupId>
<artifactId>stat-dashboard-backend</artifactId>
<version>0.16.3</version>
<version>1.0.0</version>
<packaging>jar</packaging>
<!--<packaging>${packaging}</packaging>-->
<!-- mvn clean package -Dpackaging=jar/war-->
......@@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
......
......@@ -52,14 +52,4 @@ public class Element {
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Element{");
sb.append("id=").append(id);
sb.append(", code='").append(code).append('\'');
sb.append(", nameEt='").append(nameEt).append('\'');
sb.append('}');
return sb.toString();
}
}
......@@ -49,17 +49,4 @@ public class GraphType {
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("GraphType{");
sb.append("id=").append(id);
sb.append(", widget=").append(widget);
sb.append(", type=").append(type);
sb.append(", defaultType=").append(defaultType);
sb.append(", mapRule=").append(mapRule);
sb.append(", verticalRule=").append(verticalRule);
sb.append('}');
return sb.toString();
}
}
......@@ -8,4 +8,6 @@ import java.util.List;
public interface UserDashboardRepository extends JpaRepository<UserDashboard, Long> {
List<UserDashboard> findAllByDashboardAndAndAppUser(Long dashboard, Long appUser);
void deleteByDashboard(Long dashboard);
}
......@@ -10,9 +10,9 @@ public interface UserGraphTypeRepository extends JpaRepository<UserGraphType, Lo
List<UserGraphType> findAllByUserWidget(Long userWidget);
List<UserGraphType> findAllByGraphTypeIn(List<Long> graphType);
List<UserGraphType> findAllByUserWidgetIn(List<Long> userWidget);
void deleteByIdIn(List<Long> ids);
void deleteByGraphTypeIn(List<Long> ids);
}
......@@ -13,4 +13,6 @@ public interface UserLegendRepository extends JpaRepository<UserLegend, Long> {
List<UserLegend> findAllByUserGraphTypeIn(List<Long> userGraphType);
void deleteByIdIn(List<Long> ids);
void deleteByUserGraphTypeIn(List<Long> ids);
}
......@@ -51,7 +51,7 @@ public class ElementCustomRepositoryImpl implements ElementCustomRepository {
@Override
public List<Element> findAllByClfCodeAndRoleOrderByName(ClassifierCode code, RoleStrategy role, Language language, DashboardStatus status, DashboardUserType userType) {
String roleExistsCondition = role.hasDashboard() ? " and rd.id is not null \n" : " and rd.id is null \n";
String roleExistsCondition = role.hasDashboard() ? " and d.id is not null \n" : " and d.id is null \n";
String order = language.isEt() ? " order by e.name_et asc \n" : " order by e.name_en asc \n";
String select = "SELECT DISTINCT e.*\n" +
"from Element e\n" +
......
......@@ -10,6 +10,7 @@ import ee.stat.dashboard.repository.DashboardWidgetRepository;
import ee.stat.dashboard.repository.DomainConnectionRepository;
import ee.stat.dashboard.repository.DomainRepository;
import ee.stat.dashboard.repository.RoleDashboardRepository;
import ee.stat.dashboard.repository.UserDashboardRepository;
import ee.stat.dashboard.repository.WidgetDomainRepository;
import ee.stat.dashboard.service.admin.widget.UserWidgetDeleteService;
import lombok.AllArgsConstructor;
......@@ -34,10 +35,12 @@ public class DashboardDeleteService {
private DomainConnectionRepository domainConnectionRepository;
private DomainRepository domainRepository;
private RoleDashboardRepository roleDashboardRepository;
private UserDashboardRepository userDashboardRepository;
public void delete(Dashboard dashboard) {
dashboardRegionRepository.deleteByDashboard(dashboard.getId());
userWidgetDeleteService.deleteUserWidgets(dashboard);
userDashboardRepository.deleteByDashboard(dashboard.getId());
widgetDomainRepository.deleteByDashboard(dashboard.getId());
dashboardWidgetRepository.deleteByDashboard(dashboard.getId());
......
......@@ -77,7 +77,7 @@ public class DashboardDomainService {
List<Domain> existingDomains = domainRepository.findByDashboard(dashboard.getId());
existingDomains.removeAll(domainsToSave);
if (isNotEmpty(existingWidgetDomains)) {
if (isNotEmpty(existingDomains)) {
domainRepository.deleteAll(existingDomains);
}
}
......
......@@ -23,10 +23,8 @@ import ee.stat.dashboard.service.statdata.xml.XmlValue;
import ee.stat.dashboard.service.statsync.StatDbUpdater;
import ee.stat.dashboard.service.statsync.dto.StatDbResponse;
import ee.stat.dashboard.service.statsync.dto.StatDbUpdaterException;
import ee.stat.dashboard.util.StatListUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;
......@@ -129,7 +127,7 @@ public class GraphTypeAdminService {
existingFilterValues.removeAll(savedFilterValues);
if (isNotEmpty(existingFilterValues)) {
widgetDeleteService.deleteFilterValues(existingFilterValues, true);
widgetDeleteService.deleteFilterValues(existingFilterValues);
}
existingFilters.removeAll(savedFilters);
if (isNotEmpty(existingFilters)) {
......@@ -138,10 +136,10 @@ public class GraphTypeAdminService {
userLegendValueRepository.deleteByFilterValueIn(ids);
}
widgetDeleteService.deleteFilters(existingFilters, true);
widgetDeleteService.deleteFilters(existingFilters);
existingGraphTypes.removeAll(newGraphTypes);
widgetCacheManager.updateGraphType(graphTypeIdsForCache(existingGraphTypes, newGraphTypes));
widgetDeleteService.deleteGraphTypes(existingGraphTypes, true);
widgetDeleteService.deleteGraphTypes(existingGraphTypes);
}
private GraphType graphType(WidgetAdminResponse widget, GraphTypeAdminDto graphTypeAdminDto) {
......
......@@ -59,17 +59,6 @@ public class UserWidgetDeleteService {
deleteUserData(Arrays.asList(userWidget));
}
public void deleteUserLegends(List<Long> userGraphTypeIds) {
List<UserLegend> userLegends = userGraphTypeIds.isEmpty() ? new ArrayList<>() : userLegendRepository.findAllByUserGraphTypeIn(userGraphTypeIds);
List<Long> userLegendIds = userLegends.stream().map(UserLegend::getId).collect(Collectors.toList());
deleteUserLegendValues(userLegendIds);
if (isNotEmpty(userLegendIds)) {
userLegendRepository.deleteByIdIn(userLegendIds);
}
}
public void deleteUserFilters(List<Long> userGraphTypeIds) {
List<UserFilter> userFilters = userGraphTypeIds.isEmpty() ? new ArrayList<>() : userFilterRepository.findAllByUserGraphTypeIn(userGraphTypeIds);
List<Long> userFilterIds = userFilters.stream().map(UserFilter::getId).collect(Collectors.toList());
......@@ -81,23 +70,6 @@ public class UserWidgetDeleteService {
}
}
public void deleteUserLegendValues(List<Long> userLegendIds) {
List<UserLegendValue> legendValues = userLegendIds.isEmpty() ? new ArrayList<>() : userLegendValueRepository.findAllByUserLegendIn(userLegendIds);
List<Long> userLegendValueIds = legendValues.stream().map(UserLegendValue::getId).collect(Collectors.toList());
if (isNotEmpty(userLegendValueIds)) {
userLegendValueRepository.deleteByIdIn(userLegendValueIds);
}
}
public void deleteUserFilterValues(List<Long> userFilterIds) {
List<UserFilterValue> userFilterValues = userFilterIds.isEmpty() ? new ArrayList<>() : userFilterValueRepository.findAllByUserFilterIn(userFilterIds);
List<Long> userFilterValuesIds = userFilterValues.stream().map(UserFilterValue::getId).collect(Collectors.toList());
if (isNotEmpty(userFilterValuesIds)) {
userFilterValueRepository.deleteByIdIn(userFilterValuesIds);
}
}
public void deleteUserFiltersByFilter(List<Long> filterIds) {
if (isNotEmpty(filterIds)) {
userFilterRepository.deleteByFilterIn(filterIds);
......@@ -105,13 +77,24 @@ public class UserWidgetDeleteService {
}
}
public void deleteUserFilterValuesByValue(List<Long> filterValuesIds) {
public void deleteUserFilterValuesByFilterValue(List<Long> filterValuesIds) {
if (isNotEmpty(filterValuesIds)) {
userFilterValueRepository.deleteByFilterValueIn(filterValuesIds);
userLegendValueRepository.deleteByFilterValueIn(filterValuesIds);
}
}
private void deleteUserLegends(List<Long> userGraphTypeIds) {
List<UserLegend> userLegends = userGraphTypeIds.isEmpty() ? new ArrayList<>() : userLegendRepository.findAllByUserGraphTypeIn(userGraphTypeIds);
List<Long> userLegendIds = userLegends.stream().map(UserLegend::getId).collect(Collectors.toList());
deleteUserLegendValues(userLegendIds);
if (isNotEmpty(userLegendIds)) {
userLegendRepository.deleteByIdIn(userLegendIds);
}
}
private void deleteUserData(List<UserWidget> userWidgets) {
List<Long> userWidgetsIds = userWidgets.stream().map(UserWidget::getId).collect(Collectors.toList());
List<UserGraphType> allByUserWidget = userWidgets.isEmpty() ? new ArrayList<>() : userGraphTypeRepository.findAllByUserWidgetIn(userWidgetsIds);
......@@ -124,4 +107,21 @@ public class UserWidgetDeleteService {
userGraphTypeRepository.deleteByIdIn(userGraphTypeIds);
}
}
private void deleteUserLegendValues(List<Long> userLegendIds) {
List<UserLegendValue> legendValues = userLegendIds.isEmpty() ? new ArrayList<>() : userLegendValueRepository.findAllByUserLegendIn(userLegendIds);
List<Long> userLegendValueIds = legendValues.stream().map(UserLegendValue::getId).collect(Collectors.toList());
if (isNotEmpty(userLegendValueIds)) {
userLegendValueRepository.deleteByIdIn(userLegendValueIds);
}
}
private void deleteUserFilterValues(List<Long> userFilterIds) {
List<UserFilterValue> userFilterValues = userFilterIds.isEmpty() ? new ArrayList<>() : userFilterValueRepository.findAllByUserFilterIn(userFilterIds);
List<Long> userFilterValuesIds = userFilterValues.stream().map(UserFilterValue::getId).collect(Collectors.toList());
if (isNotEmpty(userFilterValuesIds)) {
userFilterValueRepository.deleteByIdIn(userFilterValuesIds);
}
}
}
package ee.stat.dashboard.service.admin.widget;
import ee.stat.dashboard.model.user.widget.UserGraphType;
import ee.stat.dashboard.model.widget.back.Excel;
import ee.stat.dashboard.model.widget.back.Filter;
import ee.stat.dashboard.model.widget.back.FilterValue;
......@@ -18,6 +19,7 @@ import ee.stat.dashboard.repository.GraphTypeRepository;
import ee.stat.dashboard.repository.StatDbDataRepository;
import ee.stat.dashboard.repository.StatDbRepository;
import ee.stat.dashboard.repository.UserGraphTypeRepository;
import ee.stat.dashboard.repository.UserLegendRepository;
import ee.stat.dashboard.repository.WidgetDomainRepository;
import ee.stat.dashboard.repository.WidgetExcelImportRepository;
import ee.stat.dashboard.repository.WidgetRepository;
......@@ -49,6 +51,7 @@ public class WidgetDeleteService {
private DiagramDataRepository diagramDataRepository;
private WidgetExcelImportRepository widgetExcelImportRepository;
private UserGraphTypeRepository userGraphTypeRepository;
private UserLegendRepository userLegendRepository;
private DashboardWidgetRepository dashboardWidgetRepository;
private UserWidgetDeleteService userWidgetDeleteService;
......@@ -72,7 +75,7 @@ public class WidgetDeleteService {
dashboardWidgetRepository.deleteByWidget(widget.getId());
userWidgetDeleteService.deleteUserWidgets(widget);
deleteGraphTypes(graphTypeRepository.findAllByWidgetOrderByIdAsc(widget.getId()), false);
deleteGraphTypes(graphTypeRepository.findAllByWidgetOrderByIdAsc(widget.getId()));
List<StatDb> statDbs = statDbRepository.findByWidget(widget.getId());
if (isNotEmpty(statDbs)) {
......@@ -85,42 +88,40 @@ public class WidgetDeleteService {
widgetRepository.deleteById(widget.getId());
}
public void deleteGraphTypes(List<GraphType> existingGraphTypes, boolean deleteUser) {
public void deleteGraphTypes(List<GraphType> existingGraphTypes) {
List<Long> graphTypeIds = existingGraphTypes.stream().map(GraphType::getId).collect(Collectors.toList());
List<Filter> filters = graphTypeIds.isEmpty() ? new ArrayList<>() : filterRepository.findAllByGraphTypeInOrderByIdAsc(graphTypeIds);
deleteFilters(filters, deleteUser);
if (deleteUser){
userGraphTypeRepository.deleteByGraphTypeIn(graphTypeIds);
}
deleteFilters(filters);
if (isNotEmpty(graphTypeIds)) {
List<UserGraphType> userGraphTypes = userGraphTypeRepository.findAllByGraphTypeIn(graphTypeIds);
if (isNotEmpty(userGraphTypes)){
List<Long> userGraphTypeIds = userGraphTypes.stream().map(UserGraphType::getId).collect(Collectors.toList());
userLegendRepository.deleteByUserGraphTypeIn(userGraphTypeIds);
userGraphTypeRepository.deleteByIdIn(userGraphTypeIds);
}
diagramDataRepository.deleteByGraphTypeIn(graphTypeIds);
statDbDataRepository.deleteByGraphTypeIn(graphTypeIds);
graphTypeRepository.deleteByIdIn(graphTypeIds);
}
}
public void deleteFilters(List<Filter> filters, boolean deleteUser) {
public void deleteFilters(List<Filter> filters) {
List<Long> filterIds = filters.stream().map(Filter::getId).collect(Collectors.toList());
List<FilterValue> filterValues = filters.isEmpty() ? new ArrayList<>() : filterValueRepository.findAllByFilterIn(filterIds);
deleteFilterValues(filterValues, deleteUser);
if (deleteUser){
userWidgetDeleteService.deleteUserFiltersByFilter(filterIds);
}
deleteFilterValues(filterValues);
userWidgetDeleteService.deleteUserFiltersByFilter(filterIds);
if (isNotEmpty(filterIds)) {
filterRepository.deleteByIdIn(filterIds);
}
}
public void deleteFilterValues(List<FilterValue> filterValues, boolean deleteUser) {
public void deleteFilterValues(List<FilterValue> filterValues) {
List<Long> filterValuesIds = filterValues.stream().map(FilterValue::getId).collect(Collectors.toList());
if (deleteUser){
userWidgetDeleteService.deleteUserFilterValuesByValue(filterValuesIds);
}
userWidgetDeleteService.deleteUserFilterValuesByFilterValue(filterValuesIds);
if (isNotEmpty(filterValuesIds)) {
filterValueRepository.deleteByIdIn(filterValuesIds);
......
......@@ -50,6 +50,12 @@ public class DashboardSaveService {
private DashboardFinder dashboardFinder;
private DashboardWidgetRepository widgetRepository;
public Long saveWidgetsAndSetVisited(Long dashboardId, UserWidgetsDto dto, StatUser user) {
Dashboard dashboard = dashboardFinder.dashboardForSaveWidgets(dashboardId, user);
setVisited(dashboard, user);
return saveWidgets(dashboard, user, dto);
}
public Long saveRegion(Long id, UserDashboardDto dto, StatUser user) {
if (dto.getRegion() == null) throw badRequest(id, "region").get();
Dashboard dashboard = dashboardFinder.findValidDashboardForSaveRegion(id);
......@@ -71,12 +77,6 @@ public class DashboardSaveService {
return id;
}
public Long saveWidgetsAndSetVisited(Long dashboardId, UserWidgetsDto dto, StatUser user) {
Dashboard dashboard = dashboardFinder.dashboardForSaveWidgets(dashboardId, user);
setVisited(dashboard, user);
return saveWidgets(dashboard, user, dto);
}
public Long saveWidgets(Dashboard dashboard, StatUser user, UserWidgetsDto dto) {
if (dashboard.getUserType().isAdmin()) {
List<WidgetDomain> widgetDomains = widgetDomainRepository.findAllByDashboard(dashboard.getId());
......
......@@ -38,10 +38,10 @@ public class DashboardService {
public DashboardResponse map(Dashboard dashboard, Language lang, StatUser user) {
DashboardResponse dto = dashboardConverter.mapSimpleWRegion(dashboard, lang, user);
if (dashboard.getUserType().isUser()) {
dto.setWidgets(dashboardWidgetService.getWidgets(dashboard, lang));
} else {
if (dashboard.getUserType().isAdmin()) {
dto.setElements(domainService.loadDomainsAndWidgets(dashboard.getId(), lang));
} else {
dto.setWidgets(dashboardWidgetService.getWidgets(dashboard, lang));
}
if (user != null) {
dto.setSelectedWidgets(userWidgetService.getSelectedWidgets(dashboard, user));
......
......@@ -56,7 +56,7 @@ public class TimeFilterManagement {
value.setOrderNr(i);
value.setSelected(i == 0);
}
widgetDeleteService.deleteFilterValues(valuesToRemove, true);
widgetDeleteService.deleteFilterValues(valuesToRemove);
filterValueRepository.saveAll(existingTimeFilterValues);
filterValueRepository.flush();
......
......@@ -23,7 +23,7 @@ public class DiagramService {
if (diagramOp.isPresent()) {
return diagramOp.get();
} else {
log.error("Diagram is missing, should not happen {}", widget.getId());
log.error("Diagram is missing, should not happen for widget {}", widget.getId());
return null;
}
}
......
......@@ -4,7 +4,6 @@ import ee.stat.dashboard.model.classifier.Element;
import ee.stat.dashboard.model.widget.back.Filter;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.model.widget.back.enums.MapType;
import ee.stat.dashboard.model.widget.front.Diagram;
import ee.stat.dashboard.model.widget.front.DiagramData;
import ee.stat.dashboard.model.widget.front.FilterSerie;
import ee.stat.dashboard.model.widget.front.Graph;
......@@ -15,7 +14,6 @@ import ee.stat.dashboard.service.widget.widget.dto.Filters;
import ee.stat.dashboard.service.widget.widget.dto.GraphDto;
import ee.stat.dashboard.service.widget.widget.dto.UserFilterDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
......@@ -28,7 +26,6 @@ import java.util.stream.Collectors;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@Service
@Slf4j
@AllArgsConstructor
public class GraphService {
......@@ -36,21 +33,20 @@ public class GraphService {
private ElementService elementService;
private FilterService filterService;
@Cacheable(value = "GraphService_filterData", key = "\"\".concat(#diagramData.hashCode()).concat(#config.hashCode())")
public GraphDto filterData(DiagramData diagramData, Filters config) {
log.info("filtering now");
// @Cacheable(value = "GraphService_filterData", key = "\"\".concat(#data.hashCode()).concat(\"-\").concat(#config.hashCode())")
public GraphDto filterData(DiagramData data, Filters config) {
Language lang = config.getLang();
List<FilterDto> filterDtos = getFilters(lang, diagramData);
List<FilterDto> filterDtos = getFilters(lang, data);
updateRegionFiltersByEhak(filterDtos, config);
updateFiltersByUserPreferences(filterDtos, config);
Graph filteredGraph = graphFiltering.filterGraph(diagramData.getGraph(lang), filterDtos, config);
Graph filteredGraph = graphFiltering.filterGraph(data.getGraph(lang), filterDtos, config);
GraphDto graphDto = convert(filteredGraph);
graphDto.setFilters(frontEndFilters(filterDtos));
graphDto.setMapType(getMapType(config));
graphDto.setDiagramDataId(diagramData.getId());
graphDto.setDiagramCreatedAt(diagramData.getDiagramObj().getCreatedAt());
graphDto.setDiagramDataId(data.getId());
graphDto.setDiagramCreatedAt(data.getDiagramObj().getCreatedAt());
return graphDto;
}
......@@ -73,7 +69,6 @@ public class GraphService {
for (FilterDto filter : filterDtos) {
Optional<UserFilterDto> userFilterOp = config.getUserFilters().stream().filter(v -> v.getFilter().equals(filter.getId())).findAny();
if (userFilterOp.isPresent()) {
log.info("userfilter preferences: {}", userFilterOp.get());
UserFilterDto userFilter = userFilterOp.get();
filter.getValues().forEach(v -> v.setSelected(userFilter.getValues().contains(v.getId())));
filter.recalcDefaultOptions();
......@@ -85,7 +80,6 @@ public class GraphService {
private void updateRegionFiltersByEhak(List<FilterDto> filterDtos, Filters config) {
for (FilterDto filter : filterDtos) {
if (BooleanUtils.isTrue(filter.getRegion())) {
log.info("ehak setup: {}", config.getEhakName());
filter.getValues().forEach(v -> v.setSelected(v.getOption().equalsIgnoreCase(config.getEhakName()) ? true : null));
filter.recalcDefaultOptions();
}
......
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.dashboard.DashboardWidget;
import ee.stat.dashboard.model.user.widget.UserWidget;
import ee.stat.dashboard.repository.DashboardWidgetRepository;
import ee.stat.dashboard.repository.UserWidgetRepository;
import ee.stat.dashboard.service.admin.widget.UserWidgetDeleteService;
import ee.stat.dashboard.service.dashboard.DashboardSaveService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import static ee.stat.dashboard.util.StatListUtil.first;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@Service
@AllArgsConstructor
public class WidgetPinService {
private UserWidgetRepository userWidgetRepository;
private DashboardWidgetRepository dashboardWidgetRepository;
private DashboardSaveService dashboardSaveService;
private UserWidgetDeleteService userWidgetDeleteService;
private UserWidgetCopyService userWidgetCopyService;
public void unpinFlow(StatUser user, Long widgetId) {
Dashboard myDashboard = dashboardSaveService.findExistingOrCreateNew(user);
if (!dashboardWidgetRepository.existsByDashboardAndWidget(myDashboard.getId(), widgetId)) {
return;
}
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), myDashboard.getId(), widgetId);
if (isNotEmpty(userWidgets)) {
userWidgets.forEach(userWidget -> {
userWidgetDeleteService.deleteUserData(userWidget);
userWidgetRepository.deleteById(userWidget.getId());
});
}
dashboardWidgetRepository.deleteByDashboardAndWidget(myDashboard.getId(), widgetId);
}
public void pinFlow(StatUser user, Long dashboardId, Long widgetId) {
Dashboard myDashboard = dashboardSaveService.findExistingOrCreateNew(user);
if (dashboardWidgetRepository.existsByDashboardAndWidget(myDashboard.getId(), widgetId)) {
return;
}
DashboardWidget dashboardWidget = saveDashboardWidget(myDashboard, widgetId);
List<UserWidget> myDashboardUserWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), myDashboard.getId(), widgetId);
if (isEmpty(myDashboardUserWidgets)) {
List<UserWidget> userWidgetsOfPrevDashboard = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboardId, widgetId);
if (isNotEmpty(userWidgetsOfPrevDashboard)) {
List<UserWidget> currentNrOfWidgets = userWidgetRepository.findByAppUserAndDashboard(user.getId(), myDashboard.getId());
userWidgetCopyService.saveAsCopy(first(userWidgetsOfPrevDashboard), myDashboard, dashboardWidget, currentNrOfWidgets.size() * -1);
}
}
}
private DashboardWidget saveDashboardWidget(Dashboard myDashboard, Long widgetId) {
DashboardWidget dashboardWidget = new DashboardWidget(myDashboard.getId(), widgetId);
dashboardWidgetRepository.save(dashboardWidget);
return dashboardWidget;
}
}
......@@ -2,30 +2,22 @@ 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.dashboard.DashboardWidget;
import ee.stat.dashboard.model.user.widget.UserWidget;
import ee.stat.dashboard.model.widget.back.Widget;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.repository.DashboardWidgetRepository;
import ee.stat.dashboard.repository.UserWidgetRepository;
import ee.stat.dashboard.service.admin.widget.UserWidgetDeleteService;
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;
import java.util.List;
import java.util.Optional;
import static ee.stat.dashboard.util.ResponseUtil.badRequest;
import static ee.stat.dashboard.util.StatListUtil.first;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
import static org.apache.commons.collections4.CollectionUtils.isEmpty;
@Slf4j
@Service
......@@ -34,17 +26,14 @@ import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
public class WidgetSaveService {
private UserWidgetRepository userWidgetRepository;
private DashboardWidgetRepository dashboardWidgetRepository;
private DashboardSaveService dashboardSaveService;
private UserWidgetDeleteService userWidgetDeleteService;
private UserWidgetPreferencesSaveService userWidgetPreferencesSaveService;
private UserWidgetCopyService userWidgetCopyService;
private DashboardFinder dashboardFinder;
private WidgetFinder widgetFinder;
private WidgetPinService widgetPinService;
public void savePreferences(Long widgetId, Long dashboardId, StatUser user, UserWidgetDto preferences) {
List<UserWidget> userWidgets = userWidgetRepository.findByAppUserAndDashboardAndWidget(user.getId(), dashboardId, widgetId);
if (CollectionUtils.isEmpty(userWidgets)){
if (isEmpty(userWidgets)){
throw badRequest(widgetId, "user widget missing, widget id").get();
}
userWidgetPreferencesSaveService.savePreferences(first(userWidgets), preferences);
......@@ -55,46 +44,9 @@ public class WidgetSaveService {
Dashboard dashboard = dashboardFinder.findValidDashboardForPinning(requestDashboardId, pinningStrategy);
Widget widget = widgetFinder.findWidgetForPinning(requestWidgetId);
if (pinningStrategy.pin()) {
pinFlow(user, dashboard, widget);
widgetPinService.pinFlow(user, dashboard.getId(), widget.getId());
} else {
unpinFlow(user, widget);
widgetPinService.unpinFlow(user, widget.getId());
}
}
private void unpinFlow(StatUser user, Widget widget) {
Dashboard myDashboard = dashboardSaveService.findExistingOrCreateNew(user);