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

SA0140-447 widget connection validations

parent 3d03ea8b
......@@ -16,9 +16,12 @@ import ee.stat.dashboard.service.widget.importer.WidgetImportUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static ee.stat.dashboard.service.widget.importer.importer.WidgetLocationImporter.DOMAIN_EN;
......@@ -29,6 +32,8 @@ import static ee.stat.dashboard.service.widget.importer.importer.WidgetLocationI
import static ee.stat.dashboard.service.widget.importer.importer.WidgetLocationImporter.PROGRAM_ET;
import static ee.stat.dashboard.service.widget.importer.importer.WidgetLocationImporter.RESULT_DOMAIN_EN;
import static ee.stat.dashboard.service.widget.importer.importer.WidgetLocationImporter.RESULT_DOMAIN_ET;
import static ee.stat.dashboard.util.ResponseUtil.badRequest;
import static java.lang.String.format;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@Service
......@@ -54,10 +59,11 @@ public class DashboardDomainService {
List<WidgetDomain> existingWidgetDomains = widgetDomainRepository.findAllByDashboard(dashboard.getId());
if (isNotEmpty(dashboard.getElements())) {
Map<Long, String> uniqueWidgets = new HashMap<>();
for (DomainAdminResponse level0 : dashboard.getElements()) {
order = saveDomain(dashboard, order, domainsToSave, level0);
saveWidgets(dashboard, widgetDomainsToSave, existingWidgetDomains, level0);
order = saveChildren(domainsToSave, widgetDomainsToSave, existingWidgetDomains, dashboard, level0, 1, order);
saveWidgets(dashboard, widgetDomainsToSave, existingWidgetDomains, level0, uniqueWidgets);
order = saveChildren(domainsToSave, widgetDomainsToSave, existingWidgetDomains, dashboard, level0, 1, order, uniqueWidgets);
}
}
......@@ -84,12 +90,12 @@ public class DashboardDomainService {
return order;
}
private int saveChildren(List<Domain> domainsToSave, List<WidgetDomain> widgetDomainsToSave, List<WidgetDomain> existingWidgetDomains, DashboardAdminResponse dashboard, DomainAdminResponse parent, int depth, int order) {
private int saveChildren(List<Domain> domainsToSave, List<WidgetDomain> widgetDomainsToSave, List<WidgetDomain> existingWidgetDomains, DashboardAdminResponse dashboard, DomainAdminResponse parent, int depth, int order, Map<Long, String> uniqueWidgets) {
if (isNotEmpty(parent.getSubElements())) {
for (DomainAdminResponse child : parent.getSubElements()) {
order = saveChildDomain(domainsToSave, dashboard, parent, depth, order, child);
saveWidgets(dashboard, widgetDomainsToSave, existingWidgetDomains, child);
order = saveChildren(domainsToSave, widgetDomainsToSave, existingWidgetDomains, dashboard, child, depth + 1, order);
saveWidgets(dashboard, widgetDomainsToSave, existingWidgetDomains, child, uniqueWidgets);
order = saveChildren(domainsToSave, widgetDomainsToSave, existingWidgetDomains, dashboard, child, depth + 1, order, uniqueWidgets);
}
}
return order;
......@@ -103,9 +109,15 @@ public class DashboardDomainService {
return order;
}
private void saveWidgets(DashboardAdminResponse dashboard, List<WidgetDomain> widgetDomainsToSave, List<WidgetDomain> existingWidgetDomains, DomainAdminResponse domain) {
private void saveWidgets(DashboardAdminResponse dashboard, List<WidgetDomain> widgetDomainsToSave,
List<WidgetDomain> existingWidgetDomains, DomainAdminResponse domain, Map<Long, String> uniqueWidgets) {
if (isNotEmpty(domain.getWidgets())) {
for (WidgetAdminResponse widget : domain.getWidgets()) {
String domainName = uniqueWidgets.get(widget.getId());
if (domainName != null) {
throw widgetExistsWithAnotherConnection(domain, widget, domainName);
}
uniqueWidgets.put(widget.getId(), domain.getNameEt());
Optional<WidgetDomain> existingWidgetDomain = existingWidgetDomain(existingWidgetDomains, widget, dashboard, domain);
widgetDomainsToSave.add(existingWidgetDomain
.orElseGet(() -> new WidgetDomain(widget.getId(), domain.getId(), dashboard.getId())));
......@@ -156,4 +168,10 @@ public class DashboardDomainService {
}
throw new IllegalStateException("unknown levelname: " + levelNameEt);
}
private ResponseStatusException widgetExistsWithAnotherConnection(DomainAdminResponse domain, WidgetAdminResponse widget, String domainName) {
return badRequest("widget",
format("widget %s already exists with another connection c1 %s c2 %s",
widget.getNameEn(), domainName, domain.getNameEn())).get();
}
}
......@@ -10,11 +10,16 @@ import ee.stat.dashboard.service.admin.widget.dto.WidgetAdminResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static ee.stat.dashboard.util.ResponseUtil.badRequest;
import static java.lang.String.format;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@Service
......@@ -30,9 +35,15 @@ public class WidgetDashboardService {
List<WidgetDomain> existingWidgetDomains = widgetDomainRepository.findAllByWidget(widget.getId());
List<WidgetDomain> newWidgetDomains = new ArrayList<>();
if (isNotEmpty(widget.getElements())) {
Map<Long, String> uniqueDashboards = new HashMap<>();
for (DomainAdminResponse element : widget.getElements()) {
DashboardAdminResponse dashboard = element.getDashboard();
String domainName = uniqueDashboards.get(dashboard.getId());
if (domainName != null) {
throw dashboardExistsWithAnotherConnection(element, dashboard, domainName);
}
uniqueDashboards.put(dashboard.getId(), element.getNameEt());
WidgetDomain newWidgetDomain = existingWidgetDomains.stream()
.filter(wd -> wd.getDomain().equals(element.getId()))
.filter(wd -> wd.getWidget().equals(widget.getId()))
......@@ -57,4 +68,10 @@ public class WidgetDashboardService {
oldDashboardIds.addAll(newDashboardIds);
return oldDashboardIds;
}
private ResponseStatusException dashboardExistsWithAnotherConnection(DomainAdminResponse element, DashboardAdminResponse dashboard, String domainName) {
return badRequest("dashboard",
format("dashboard %s already exists with another connection c1: %s c2: %s",
dashboard.getNameEt(), domainName, element.getNameEt())).get();
}
}
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