Commit 9eb220d8 authored by oleg.pahhomov's avatar oleg.pahhomov

SA0140-609 | update dashboard order

parent 8cd5c5f7
package ee.stat.dashboard.repository.custom;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.widget.back.enums.Language;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -12,4 +13,8 @@ public interface DashboardCustomRepository {
Page<Dashboard> findAllByNameEtIsLikeAndLevel(String name, Integer level, Pageable pageable);
List<Dashboard> findByRoleElement(Long roleElement, Language lang);
List<Dashboard> findByRoleClassifier(Long roleClassifier, Language lang);
}
package ee.stat.dashboard.repository.custom;
import ee.stat.dashboard.model.classifier.DashboardRule;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.dashboard.DashboardUserType;
import ee.stat.dashboard.model.widget.back.enums.Language;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
......@@ -26,7 +28,6 @@ public class DashboardCustomRepositoryImpl implements DashboardCustomRepository
.getResultList();
}
//todo
@Override
public Page<Dashboard> findAllByNameEtIsLikeAndLevel(String name, Integer level, Pageable pageable) {
String searchName = "%" + name.toUpperCase() + "%";
......@@ -51,5 +52,33 @@ public class DashboardCustomRepositoryImpl implements DashboardCustomRepository
return new PageImpl<>(content, pageable, count.longValue());
}
@Override
public List<Dashboard> findByRoleElement(Long roleElement, Language lang) {
String order = lang.isEt() ? "order by d.name_et" : "order by d.name_en";
return entityManager.createNativeQuery("\n" +
"select d.*\n" +
"from dashboard d\n" +
" join role_dashboard rd on d.id = rd.dashboard\n" +
"where rd.role_element = :roleElement\n" +
" and rd.dashboard_rule = :dashboardRule\n" +
order, Dashboard.class)
.setParameter("roleElement", roleElement)
.setParameter("dashboardRule", DashboardRule.DASHBOARDS.name())
.getResultList();
}
@Override
public List<Dashboard> findByRoleClassifier(Long roleClassifier, Language lang) {
String order = lang.isEt() ? "order by d.name_et" : "order by d.name_en";
return entityManager.createNativeQuery("\n" +
"select d.*\n" +
"from dashboard d\n" +
" join role_dashboard rd on d.id = rd.dashboard\n" +
"where rd.role_classifier = :roleClassifier\n" +
" and rd.dashboard_rule = :dashboardRule\n" +
order, Dashboard.class)
.setParameter("roleClassifier", roleClassifier)
.setParameter("dashboardRule", DashboardRule.ONE_DASHBOARD.name())
.getResultList();
}
}
......@@ -4,10 +4,9 @@ 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;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.repository.RoleDashboardRepository;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.dashboard.dto.RoleDashboardResponse;
import ee.stat.dashboard.service.element.ClassifierService;
......@@ -19,7 +18,6 @@ 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.stream.Collectors;
......@@ -36,9 +34,8 @@ import static org.apache.commons.collections4.CollectionUtils.isEmpty;
public class RoleDashboardService {
private ElementService elementService;
private RoleDashboardRepository roleDashboardRepository;
private DashboardRepository dashboardRepository;
private ClassifierServiceCache classifierServiceCache;
private DashboardServiceCache dashboardServiceCache;
private DashboardConverter dashboardConverter;
private ClassifierService classifierService;
......@@ -57,9 +54,8 @@ 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, ParentStrategy.EXCLUDE);
DashboardResponse dashboard = getDashboard(lang, roleDashboards);
DashboardResponse dashboard = getDashboard(clf, lang);
return new RoleDashboardResponse(classifier, elements, dashboard);
}
......@@ -70,38 +66,23 @@ public class RoleDashboardService {
return new RoleDashboardResponse(classifier, union(elementsWithDash, elementsWithoutDash));
}
private DashboardResponse getDashboard(Language lang, List<RoleDashboard> roleDashboards) {
if (isEmpty(roleDashboards)) {
private DashboardResponse getDashboard(Classifier clf, Language lang) {
List<Dashboard> dashboards = dashboardRepository.findByRoleClassifier(clf.getId(), lang);
if (isEmpty(dashboards)) {
return null;
}
return dashboardServiceCache.findById(first(roleDashboards).getDashboard())
.map(dashboard -> dashboardConverter.mapSimple(dashboard, lang))
.orElse(null);
return dashboardConverter.mapSimple(first(dashboards), lang);
}
private List<ElementResponse> elementsWithDash(ClassifierCode code, Language lang) {
List<ElementResponse> elementsWithDash = elementService.findAll(code, HAS_DASHBOARDS, lang);
for (ElementResponse element : elementsWithDash) {
element.setDashboards(getDashboards(code, lang, element));
List<Dashboard> dashboards = dashboardRepository.findByRoleElement(element.getId(), lang);
element.setDashboards(dashboardConverter.mapSimple(dashboards, lang));
}
return elementsWithDash;
}
private List<DashboardResponse> getDashboards(ClassifierCode code, Language lang, ElementResponse element) {
List<Dashboard> dashboards = roleElement(code, element).stream()
.filter(roleDashboard -> roleDashboard.getDashboardRule().isDashboards())
.map(roleDashboard -> dashboardServiceCache.findById(roleDashboard.getDashboard()).orElseThrow(RuntimeException::new))
.collect(Collectors.toList());
return dashboardConverter.mapSimple(dashboards, lang);
}
private List<RoleDashboard> roleElement(ClassifierCode code, ElementResponse element) {
if (code == ClassifierCode.EHAK) {
return roleDashboardRepository.findByRoleClassifier(element.getId());
}
return roleDashboardRepository.findByRoleElement(element.getId());
}
private List<ElementResponse> elementsWithoutDashboard(ClassifierCode code, Language lang) {
return elementService.findAll(code, HAS_NO_DASHBOARDS, lang);
}
......
......@@ -266,6 +266,10 @@ public class AssertUtil {
return series.get(0);
}
public static <T> T second(List<T> series) {
return series.get(1);
}
public static <T> T nth(List<T> series, int index) {
return series.get(index);
}
......
......@@ -19,6 +19,8 @@ import static ee.stat.dashboard.validator.Dashboard_Validator.assertRegionalStat
import static ee.stat.dashboard.validator.Dashboard_Validator.assertSocialMinistryDashboard;
import static ee.stat.dashboard.validator.Dashboard_Validator.assertTourismDashboard;
import static ee.stat.dashboard.validator.Dashboard_Validator.assertWelfareDevelopmentPlanDashboard;
import static ee.stat.dashboard.validator.strategy.DashboardValidationStrategy.DASHBOARD_META;
import static ee.stat.dashboard.validator.strategy.DashboardValidationStrategy.DASHBOARD_META_AND_DATA;
public class DashboardControllerTest extends DashboardController_Common {
......@@ -30,8 +32,8 @@ public class DashboardControllerTest extends DashboardController_Common {
@Test
public void application_returns_tourism_dashboard() {
assertTourismDashboard(getDashboard(DASHBOARD_1, Language.ET), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertTourismDashboard(getDashboard(DASHBOARD_1, Language.EN), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertTourismDashboard(getDashboard(DASHBOARD_1, Language.ET), DASHBOARD_META_AND_DATA);
assertTourismDashboard(getDashboard(DASHBOARD_1, Language.EN), DASHBOARD_META_AND_DATA);
}
@Test
......@@ -42,33 +44,33 @@ public class DashboardControllerTest extends DashboardController_Common {
@Test
public void user_can_save_widgets_for_dashboard_1() {
DashboardResponse dashboard = getDashboard(DASHBOARD_1, Language.ET);
assertTourismDashboard(dashboard, DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertTourismDashboard(dashboard, DASHBOARD_META_AND_DATA);
saveDashboard(dashboard.getRegions().get(1), null);
}
@Test
public void user_can_save_region_for_dashboard_1() {
DashboardResponse dashboard = getDashboard(DASHBOARD_1, Language.ET);
assertTourismDashboard(dashboard, DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertTourismDashboard(dashboard, DASHBOARD_META_AND_DATA);
WidgetResponse widget = getTourismAccomodationWidget(dashboard);
saveDashboard(widget, null);
}
@Test
public void application_returns_social_min_dashboard() {
assertWelfareDevelopmentPlanDashboard(getDashboard(DASHBOARD_2, Language.ET), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertWelfareDevelopmentPlanDashboard(getDashboard(DASHBOARD_2, Language.EN), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertWelfareDevelopmentPlanDashboard(getDashboard(DASHBOARD_2, Language.ET), DASHBOARD_META_AND_DATA);
assertWelfareDevelopmentPlanDashboard(getDashboard(DASHBOARD_2, Language.EN), DASHBOARD_META_AND_DATA);
}
@Test
public void application_returns_regional_dashboard() {
assertRegionalStatDashboard(getDashboard(DASHBOARD_3, Language.ET), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertRegionalStatDashboard(getDashboard(DASHBOARD_3, Language.EN), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertRegionalStatDashboard(getDashboard(DASHBOARD_3, Language.ET), DASHBOARD_META_AND_DATA);
assertRegionalStatDashboard(getDashboard(DASHBOARD_3, Language.EN), DASHBOARD_META_AND_DATA);
}
@Test
public void application_returns_social_ministry_main_dashboard() {
assertSocialMinistryDashboard(getDashboard(DASHBOARD_4, Language.ET));
assertSocialMinistryDashboard(getDashboard(DASHBOARD_4, Language.EN));
assertSocialMinistryDashboard(getDashboard(DASHBOARD_4, Language.ET), DASHBOARD_META);
assertSocialMinistryDashboard(getDashboard(DASHBOARD_4, Language.EN), DASHBOARD_META);
}
}
......@@ -4,7 +4,6 @@ import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.dashboard.dto.RoleDashboardResponse;
import ee.stat.dashboard.service.widget.widget.dto.WidgetResponse;
import ee.stat.dashboard.validator.strategy.DashboardValidationStrategy;
import org.junit.Test;
import org.springframework.http.HttpMethod;
......@@ -22,19 +21,21 @@ import static ee.stat.dashboard.validator.Dashboard_Validator.assertSocialMinist
import static ee.stat.dashboard.validator.Dashboard_Validator.assertTourismDashboard;
import static ee.stat.dashboard.validator.Dashboard_Validator.assertWelfareDevelopmentPlanDashboard;
import static ee.stat.dashboard.validator.RoleDashboardValidator.validateRoles;
import static ee.stat.dashboard.validator.strategy.DashboardValidationStrategy.*;
import static ee.stat.dashboard.validator.strategy.DashboardValidationStrategy.DASHBOARD_META;
public class DashboardV2ControllerTest extends DashboardController_Common {
@Test
public void application_returns_a_list_of_dashboards() {
validateRoles(getRoleDashboards(Language.ET));
validateRoles(getRoleDashboards(Language.EN));
validateRoles(getRoleDashboards(Language.ET), Language.ET);
validateRoles(getRoleDashboards(Language.EN), Language.EN);
}
@Test
public void application_returns_tourism_dashboard() {
assertTourismDashboard(getDashboard(DASHBOARD_1, Language.ET, "/v2"), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertTourismDashboard(getDashboard(DASHBOARD_1, Language.EN, "/v2"), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertTourismDashboard(getDashboard(DASHBOARD_1, Language.ET, "/v2"), DASHBOARD_META_AND_DATA);
assertTourismDashboard(getDashboard(DASHBOARD_1, Language.EN, "/v2"), DASHBOARD_META_AND_DATA);
}
@Test
......@@ -45,34 +46,34 @@ public class DashboardV2ControllerTest extends DashboardController_Common {
@Test
public void user_can_save_widgets_for_dashboard_1() {
DashboardResponse dashboard = getDashboard(DASHBOARD_1, Language.ET, "/v2");
assertTourismDashboard(dashboard, DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertTourismDashboard(dashboard, DASHBOARD_META_AND_DATA);
saveDashboard(dashboard.getRegions().get(1), "/v2");
}
@Test
public void user_can_save_region_for_dashboard_1() {
DashboardResponse dashboard = getDashboard(DASHBOARD_1, Language.ET, "/v2");
assertTourismDashboard(dashboard, DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertTourismDashboard(dashboard, DASHBOARD_META_AND_DATA);
WidgetResponse widget = getTourismAccomodationWidget(dashboard);
saveDashboard(widget, "/v2");
}
@Test
public void application_returns_social_min_dashboard() {
assertWelfareDevelopmentPlanDashboard(getDashboard(DASHBOARD_2, Language.ET, "/v2"), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertWelfareDevelopmentPlanDashboard(getDashboard(DASHBOARD_2, Language.EN, "/v2"), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertWelfareDevelopmentPlanDashboard(getDashboard(DASHBOARD_2, Language.ET, "/v2"), DASHBOARD_META_AND_DATA);
assertWelfareDevelopmentPlanDashboard(getDashboard(DASHBOARD_2, Language.EN, "/v2"), DASHBOARD_META_AND_DATA);
}
@Test
public void application_returns_regional_dashboard() {
assertRegionalStatDashboard(getDashboard(DASHBOARD_3, Language.ET, "/v2"), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertRegionalStatDashboard(getDashboard(DASHBOARD_3, Language.EN, "/v2"), DashboardValidationStrategy.DASHBOARD_META_AND_DATA);
assertRegionalStatDashboard(getDashboard(DASHBOARD_3, Language.ET, "/v2"), DASHBOARD_META_AND_DATA);
assertRegionalStatDashboard(getDashboard(DASHBOARD_3, Language.EN, "/v2"), DASHBOARD_META_AND_DATA);
}
@Test
public void application_returns_social_ministry_main_dashboard() {
assertSocialMinistryDashboard(getDashboard(DASHBOARD_4, Language.ET, "/v2"));
assertSocialMinistryDashboard(getDashboard(DASHBOARD_4, Language.EN, "/v2"));
assertSocialMinistryDashboard(getDashboard(DASHBOARD_4, Language.ET, "/v2"), DASHBOARD_META);
assertSocialMinistryDashboard(getDashboard(DASHBOARD_4, Language.EN, "/v2"), DASHBOARD_META);
}
private List<RoleDashboardResponse> getRoleDashboards(Language lang) {
......
......@@ -49,12 +49,16 @@ public class Dashboard_Validator {
assertTravellingWidget(widget, VALIDATION_STRATEGY);
}
public static void assertSocialMinistryDashboard(DashboardResponse dashboard) {
public static void assertSocialMinistryDashboard(DashboardResponse dashboard, DashboardValidationStrategy validationStrategy) {
assertCommon(dashboard);
assertEquals(GLOBAL, dashboard.getType());
assertNull(dashboard.getRegions());
assertEquals(SOCIAL_MIN_CODE, dashboard.getCode());
assertEquals(dashboard.getLang().isEt() ? SOCIAL_MIN_NAME_ET : SOCIAL_MIN_NAME_EN, dashboard.getName());
if (validationStrategy.roleDashboard()){
return;
}
list_size(dashboard.getElements(), 0);
}
......@@ -64,7 +68,7 @@ public class Dashboard_Validator {
assertEquals(WELFARE_SOCIAL_MIN_CODE, dashboard.getCode());
assertEquals(dashboard.getLang().isEt() ? WELFARE_SOCIAL_MIN_NAME_ET : WELFARE_SOCIAL_MIN_NAME_EN, dashboard.getName());
if (validationStrategy.onlyMeta()){
if (validationStrategy.roleDashboard() || validationStrategy.onlyMeta()){
return;
}
......
package ee.stat.dashboard.validator;
import ee.stat.dashboard.model.classifier.ClassifierCode;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.dashboard.dto.RoleDashboardResponse;
import ee.stat.dashboard.service.element.dto.ElementResponse;
import ee.stat.dashboard.validator.strategy.DashboardValidationStrategy;
import java.util.List;
......@@ -12,24 +12,26 @@ import static ee.stat.dashboard.AssertUtil.NR_OF_ROLE_CLASSIFIERS;
import static ee.stat.dashboard.AssertUtil.first;
import static ee.stat.dashboard.AssertUtil.list_size;
import static ee.stat.dashboard.AssertUtil.nth;
import static ee.stat.dashboard.AssertUtil.second;
import static ee.stat.dashboard.validator.Dashboard_Validator.assertRegionalStatDashboard;
import static ee.stat.dashboard.validator.Dashboard_Validator.assertSocialMinistryDashboard;
import static ee.stat.dashboard.validator.Dashboard_Validator.assertTourismDashboard;
import static ee.stat.dashboard.validator.Dashboard_Validator.assertWelfareDevelopmentPlanDashboard;
import static ee.stat.dashboard.validator.strategy.DashboardValidationStrategy.ROLE_DASHBOARD;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class RoleDashboardValidator {
public static void validateRoles(List<RoleDashboardResponse> roleDashboards) {
public static void validateRoles(List<RoleDashboardResponse> roleDashboards, Language lang) {
list_size(roleDashboards, NR_OF_ROLE_CLASSIFIERS);
assertBusiness(nth(roleDashboards, 0));
assertKov(nth(roleDashboards, 1));
assertGov(nth(roleDashboards, 2));
assertGov(nth(roleDashboards, 2), lang);
}
private static void assertGov(RoleDashboardResponse role) {
private static void assertGov(RoleDashboardResponse role, Language lang) {
assertEquals(ClassifierCode.GOV, role.getMainRole().getCode());
list_size(role.getSubRoles(), 12);
assertNull(role.getDashboard());
......@@ -38,7 +40,11 @@ public class RoleDashboardValidator {
list_size(subRole.getDashboards(), 2);
List<DashboardResponse> dashboards = subRole.getDashboards();
assertWelfareDevelopmentPlanDashboard(first(dashboards), DashboardValidationStrategy.DASHBOARD_META);
DashboardResponse first = first(dashboards);
DashboardResponse second = second(dashboards);
assertWelfareDevelopmentPlanDashboard(lang.isEt() ? first : second, ROLE_DASHBOARD);
assertSocialMinistryDashboard(lang.isEt() ? second : first, ROLE_DASHBOARD);
for (ElementResponse otherRole : role.getSubRoles().subList(1, role.getSubRoles().size())) {
assertNull(otherRole.getDashboards());
......@@ -54,7 +60,7 @@ public class RoleDashboardValidator {
list_size(subRole.getDashboards(), 1);
List<DashboardResponse> dashboards = subRole.getDashboards();
assertTourismDashboard(first(dashboards), DashboardValidationStrategy.ROLE_DASHBOARD);
assertTourismDashboard(first(dashboards), ROLE_DASHBOARD);
for (ElementResponse otherRole : role.getSubRoles().subList(1, role.getSubRoles().size())) {
assertNull(otherRole.getDashboards());
......@@ -67,7 +73,7 @@ public class RoleDashboardValidator {
assertNotNull(role.getDashboard());
DashboardResponse dashboard = role.getDashboard();
assertRegionalStatDashboard(dashboard, DashboardValidationStrategy.ROLE_DASHBOARD);
assertRegionalStatDashboard(dashboard, ROLE_DASHBOARD);
for (ElementResponse otherRole : role.getSubRoles()) {
assertNull(otherRole.getDashboards());
......
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