Commit 05248716 authored by OlegPahhomov's avatar OlegPahhomov

Merge branch 'dashboard-roles-additions' into 'master'

SA0140-586, SA0140-587 | order of classifiers, parent option for elements

See merge request statistics-estonia/juhtimislauad/dashboard-backend!7
parents 0f052762 c4c3434d
......@@ -3,6 +3,7 @@ package ee.stat.dashboard.controller.element;
import ee.stat.dashboard.controller.Response;
import ee.stat.dashboard.controller.element.dto.ElementStrategy;
import ee.stat.dashboard.controller.element.dto.ParentStrategy;
import ee.stat.dashboard.model.classifier.ClassifierCode;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.service.element.ElementService;
......@@ -31,11 +32,11 @@ public class ElementController {
@RequestParam(required = false) List<Integer> level,
@RequestParam(required = false) Long parentId,
@RequestParam(required = false) String elementCode,
@RequestParam(defaultValue = "FLAT") ElementStrategy elementStrategy
@RequestParam(defaultValue = "FLAT") ElementStrategy elementStrategy,
@RequestParam(defaultValue = "EXCLUDE") ParentStrategy parentStrategy
) {
if (isNotEmpty(level) || parentId != null || elementCode != null) {
return of(elementService.findAll(clfCode, Language.getValue(language), level, parentId, elementCode, elementStrategy));
return of(elementService.findAll(clfCode, Language.getValue(language), level, parentId, elementCode, elementStrategy, parentStrategy));
}
return of(elementService.findAll(clfCode, Language.getValue(language)));
}
......
package ee.stat.dashboard.controller.element.dto;
public enum ParentStrategy {
INCLUDE, EXCLUDE;
public boolean isIncluded(){
return this == INCLUDE;
}
}
package ee.stat.dashboard.service.dashboard;
import ee.stat.dashboard.controller.element.dto.ElementStrategy;
import ee.stat.dashboard.controller.element.dto.ParentStrategy;
import ee.stat.dashboard.model.classifier.Classifier;
import ee.stat.dashboard.model.classifier.ClassifierCode;
import ee.stat.dashboard.model.classifier.RoleDashboard;
......@@ -57,7 +58,7 @@ public class RoleDashboardService {
private RoleDashboardResponse kovResponse(Classifier clf, Language lang) {
ClassifierResponse classifier = classifierService.mapSimple(clf, lang);
List<RoleDashboard> roleDashboards = roleDashboardRepository.findByRoleClassifier(classifier.getId());
List<ElementResponse> elements = elementService.findAll(clf.getCode(), lang, asList(0, 1), null, null, ElementStrategy.FLAT);
List<ElementResponse> elements = elementService.findAll(clf.getCode(), lang, asList(0, 1), null, null, ElementStrategy.FLAT, ParentStrategy.EXCLUDE);
DashboardResponse dashboard = getDashboard(lang, roleDashboards);
return new RoleDashboardResponse(classifier, elements, dashboard);
}
......
......@@ -2,6 +2,7 @@ package ee.stat.dashboard.service.element;
import ee.stat.dashboard.config.props.ElementConfig;
import ee.stat.dashboard.controller.element.dto.ElementStrategy;
import ee.stat.dashboard.controller.element.dto.ParentStrategy;
import ee.stat.dashboard.model.classifier.ClassifierCode;
import ee.stat.dashboard.model.classifier.Element;
import ee.stat.dashboard.model.widget.back.enums.Language;
......@@ -11,6 +12,7 @@ import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
......@@ -58,8 +60,8 @@ public class ElementService {
.collect(toList());
}
public List<ElementResponse> findAll(ClassifierCode code, Language lang, List<Integer> level, Long parentId, String elementCode, ElementStrategy strategy) {
List<ElementResponse> elements = getDbElements(code, level, parentId, elementCode).stream()
public List<ElementResponse> findAll(ClassifierCode code, Language lang, List<Integer> level, Long parentId, String elementCode, ElementStrategy strategy, ParentStrategy parentStrategy) {
List<ElementResponse> elements = getDbElements(code, level, parentId, elementCode, parentStrategy).stream()
.map(o -> mapSimple(o, lang))
.collect(toList());
if (strategy.isFlat()) {
......@@ -92,9 +94,13 @@ public class ElementService {
return dto;
}
private List<Element> getDbElements(ClassifierCode code, List<Integer> level, Long parentId, String elementCode) {
private List<Element> getDbElements(ClassifierCode code, List<Integer> level, Long parentId, String elementCode, ParentStrategy parentStrategy) {
if (parentId != null) {
return elementServiceCache.findAllToByFrom(parentId);
List<Element> elements = new ArrayList<>(elementServiceCache.findAllToByFrom(parentId));
if (parentStrategy.isIncluded()) {
elementServiceCache.findById(parentId).ifPresent(e -> elements.add(0, e));
}
return elements;
}
if (elementCode != null) {
return elementServiceCache.findAllByCode(code, elementCode);
......
UPDATE classifier set order_nr = 1 where code = 'BUSINESS';
UPDATE classifier set order_nr = 2 where code = 'EHAK';
UPDATE classifier set order_nr = 3 where code = 'GOV';
......@@ -2,10 +2,10 @@ package ee.stat.dashboard.controller.element;
import ee.stat.dashboard.controller.CommonRestTest;
import ee.stat.dashboard.controller.element.dto.ElementStrategy;
import ee.stat.dashboard.controller.element.dto.ParentStrategy;
import ee.stat.dashboard.model.classifier.ClassifierCode;
import ee.stat.dashboard.service.element.dto.ElementResponse;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import java.util.List;
......@@ -13,7 +13,7 @@ import static ee.stat.dashboard.AssertUtil.LIST_OF_ELEMENTS;
import static ee.stat.dashboard.AssertUtil.NR_OF_EHAK_ELEMENTS;
import static ee.stat.dashboard.AssertUtil.list_size;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.springframework.http.HttpMethod.GET;
public class ElementControllerTest extends CommonRestTest {
......@@ -25,12 +25,18 @@ public class ElementControllerTest extends CommonRestTest {
@Test
public void application_returns_ehak_elements_by_level() {
list_size(getFlatElements(0), 1);
list_size(getFlatElements(1), 15);
list_size(getFlatElements(2), 79);
}
@Test
public void application_returns_ehak_elements_by_parentId() {
List<ElementResponse> lvl_0_elements = getFlatElements(0);
List<ElementResponse> lvl_1_elements = getFlatElements(1);
List<ElementResponse> lvl_2_elements = getFlatElements(2);
assertEquals(1, lvl_0_elements.size());
assertEquals(15, lvl_1_elements.size());
assertEquals(79, lvl_2_elements.size());
list_size(lvl_0_elements, 1);
ElementResponse wholeCountry = lvl_0_elements.get(0);
list_size(getChildElements(wholeCountry.getId(), ParentStrategy.INCLUDE), 1 + 15);
list_size(getChildElements(wholeCountry.getId(), ParentStrategy.EXCLUDE), 15);
}
@Test
......@@ -73,8 +79,13 @@ public class ElementControllerTest extends CommonRestTest {
private List<ElementResponse> getFlatElements(Integer level) {
String codeString = "?clfCode=" + ClassifierCode.EHAK.name();
String levelString = "&level=" + level;
return ok(restTemplate.exchange("/element" + codeString + levelString,
HttpMethod.GET, null, LIST_OF_ELEMENTS));
return ok(restTemplate.exchange("/element" + codeString + levelString, GET, null, LIST_OF_ELEMENTS));
}
private List<ElementResponse> getChildElements(Long parentId, ParentStrategy parentStrategy) {
String codeString = "?clfCode=" + ClassifierCode.EHAK.name();
String parentString = "&parentId=" + parentId + "&parentStrategy=" + parentStrategy.name();
return ok(restTemplate.exchange("/element" + codeString + parentString, GET, null, LIST_OF_ELEMENTS));
}
private List<ElementResponse> getElementTree(Integer level) {
......@@ -82,7 +93,7 @@ public class ElementControllerTest extends CommonRestTest {
String levelString = "&level=" + level;
String connectionsString = "&elementStrategy=" + ElementStrategy.TREE.name();
return ok(restTemplate.exchange("/element" + codeString + levelString + connectionsString,
HttpMethod.GET, null, LIST_OF_ELEMENTS));
GET, null, LIST_OF_ELEMENTS));
}
private List<ElementResponse> getElementTree(String elementCode) {
......@@ -90,11 +101,11 @@ public class ElementControllerTest extends CommonRestTest {
String levelString = "&elementCode=" + elementCode;
String connectionsString = "&elementStrategy=" + ElementStrategy.TREE.name();
return ok(restTemplate.exchange("/element" + codeString + levelString + connectionsString,
HttpMethod.GET, null, LIST_OF_ELEMENTS));
GET, null, LIST_OF_ELEMENTS));
}
private List<ElementResponse> getElements(ClassifierCode code) {
return ok(restTemplate.exchange("/element" + "?clfCode=" + code.name(),
HttpMethod.GET, null, LIST_OF_ELEMENTS));
GET, null, LIST_OF_ELEMENTS));
}
}
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