Commit 2286ada3 authored by OlegPahhomov's avatar OlegPahhomov

SA0140-589 | remove dashboard level logic

parent 05248716
......@@ -38,8 +38,6 @@ spring:
cache-names:
- DashboardServiceCache_findById
- DashboardServiceCache_findAllByLevel
- DashboardServiceCache_findAllFromByTo
- DashboardServiceCache_findAllToByFrom
- DiagramServiceCache_findDiagram
- DiagramServiceCache_findDiagramGraph
- DomainService_loadDomainsAndWidgets
......
......@@ -46,7 +46,7 @@ public class DashboardAdminController {
}
@GetMapping(path = "/{dashboardId}")
// @Secured(RoleString.ADMIN)
@Secured(RoleString.ADMIN)
public Response<DashboardAdminResponse> findById(@PathVariable Long dashboardId) {
return of(dashboardAdminService.findById(dashboardId));
}
......
package ee.stat.dashboard.model.classifier;
import ee.stat.dashboard.model.dashboard.Dashboard;
public enum DashboardRule {
ONE_DASHBOARD, DASHBOARDS, SUB_DASHBOARDS;
ONE_DASHBOARD, DASHBOARDS;
public boolean isDashboards(){
return this == DASHBOARDS;
}
public boolean isSubDashboards(){
return this == SUB_DASHBOARDS;
}
public boolean isOneDashboards(){
return this == ONE_DASHBOARD;
}
......
......@@ -32,7 +32,6 @@ public class Dashboard {
private DashboardUserType userType;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private Integer level;
public String getName(Language lang) {
return lang.isEt() ? nameEt : nameEn;
......
package ee.stat.dashboard.model.dashboard;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Getter
@Setter
@Entity
public class DashboardConnection {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long fromId;
private Long toId;
public DashboardConnection() {
}
public DashboardConnection(Long fromId, Long toId) {
this.fromId = fromId;
this.toId = toId;
}
}
package ee.stat.dashboard.repository;
import ee.stat.dashboard.model.dashboard.DashboardConnection;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface DashboardConnectionRepository extends JpaRepository<DashboardConnection, Long> {
void deleteByToId(Long toId);
List<DashboardConnection> findByToId(Long toId);
void deleteByFromId(Long toId);
}
......@@ -16,9 +16,9 @@ public interface DashboardRepository extends JpaRepository<Dashboard, Long>, Das
List<Dashboard> findByNameEn(String nameEt);
List<Dashboard> findAllByLevelAndStatusAndUserType(Integer level, DashboardStatus status, DashboardUserType dashboardUserType);
Page<Dashboard> findAllByUserType(DashboardUserType dashboardUserType, Pageable pageable);
Page<Dashboard> findAllByLevelAndUserType(Integer level, DashboardUserType dashboardUserType, Pageable pageable);
List<Dashboard> findAllByStatusAndUserType(DashboardStatus status, DashboardUserType dashboardUserType);
List<Dashboard> findByAppUser(Long user);
}
......@@ -8,16 +8,6 @@ import java.util.List;
public interface DashboardCustomRepository {
/**
* children
*/
List<Dashboard> findAllToByFrom(Long from);
/**
* parents
*/
List<Dashboard> findAllFromByTo(Long to);
List<Dashboard> findAllByWidget(Long widget);
Page<Dashboard> findAllByNameEtIsLikeAndLevel(String name, Integer level, Pageable pageable);
......
......@@ -16,26 +16,6 @@ public class DashboardCustomRepositoryImpl implements DashboardCustomRepository
@PersistenceContext
private EntityManager entityManager;
@Override
public List<Dashboard> findAllFromByTo(Long to) {
return entityManager.createNativeQuery("" +
"SELECT e.* from Dashboard e\n" +
"join dashboard_connection ec on ec.from_id = e.id\n" +
"where ec.to_id = :to", Dashboard.class)
.setParameter("to", to)
.getResultList();
}
@Override
public List<Dashboard> findAllToByFrom(Long from) {
return entityManager.createNativeQuery("" +
"SELECT e.* from Dashboard e\n" +
"join dashboard_connection ec on ec.to_id = e.id\n" +
"where ec.from_id = :to", Dashboard.class)
.setParameter("to", from)
.getResultList();
}
@Override
public List<Dashboard> findAllByWidget(Long widget) {
return entityManager.createNativeQuery("" +
......@@ -46,18 +26,14 @@ public class DashboardCustomRepositoryImpl implements DashboardCustomRepository
.getResultList();
}
//todo
@Override
public Page<Dashboard> findAllByNameEtIsLikeAndLevel(String name, Integer level, Pageable pageable) {
String searchName = "%" + name.toUpperCase() + "%";
List<Dashboard> content = entityManager.createNativeQuery("" +
"select distinct d0.* from dashboard d0\n" +
"left join dashboard_connection dc on d0.id = dc.from_id\n" +
"left join dashboard d1 on dc.to_id = d1.id\n" +
"where d0.level = 0\n" +
"and (upper(d0.name_et) like :name\n" +
" or upper(d1.name_et) like :name)\n " +
"and (d0.user_type = :userType \n" +
" or d1.user_type = :userType) \n " +
"where upper(d0.name_et) like :name\n" +
"and d0.user_type = :userType \n" +
"", Dashboard.class)
.setParameter("name", searchName)
.setParameter("userType", DashboardUserType.ADMIN.name())
......@@ -68,11 +44,7 @@ public class DashboardCustomRepositoryImpl implements DashboardCustomRepository
BigInteger count = (BigInteger) entityManager.createNativeQuery("" +
"select count(a.id) from (\n" +
"select distinct d0.* from dashboard d0\n" +
" left join dashboard_connection dc on d0.id = dc.from_id\n" +
" left join dashboard d1 on dc.to_id = d1.id\n" +
"where d0.level = 0\n" +
" and (upper(d0.name_et) like :name\n" +
" or d1.name_et like :name)\n" +
"where upper(d0.name_et) like :name\n" +
" ) a")
.setParameter("name", searchName)
.getSingleResult();
......
......@@ -51,7 +51,7 @@ public class ElementCustomRepositoryImpl implements ElementCustomRepository {
public List<Element> findAllByClfCodeAndRoleOrderByName(ClassifierCode code, RoleStrategy role, Language language) {
String roleExistsCondition = role.hasDashboard() ? "and rd.id is not null \n" : "and rd.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 e.*\n" +
String select = "SELECT DISTINCT e.*\n" +
"from Element e\n" +
" join Classifier c on c.id = e.classifier\n" +
" left join role_dashboard rd on e.id = rd.role_element\n" +
......
......@@ -7,7 +7,6 @@ import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.service.admin.dashboard.DashboardCacheManager;
import ee.stat.dashboard.service.admin.dashboard.DashboardDeleteService;
import ee.stat.dashboard.service.admin.dashboard.DashboardDomainService;
import ee.stat.dashboard.service.admin.dashboard.DashboardParentService;
import ee.stat.dashboard.service.admin.dashboard.DashboardRegionService;
import ee.stat.dashboard.service.admin.dashboard.DashboardValidator;
import ee.stat.dashboard.service.admin.dashboard.dto.DashboardAdminResponse;
......@@ -32,7 +31,6 @@ public class DashboardAdminSaveService {
private DashboardValidator dashboardValidator;
private DashboardDeleteService dashboardDeleteService;
private DashboardDomainService dashboardDomainService;
private DashboardParentService dashboardParentService;
private DashboardRegionService dashboardRegionService;
private RoleDashboardAdminService roleDashboardAdminService;
private DashboardRepository dashboardRepository;
......@@ -68,7 +66,6 @@ public class DashboardAdminSaveService {
public void delete(Long id) {
Dashboard dashboard = dashboardRepository.findById(id).orElseThrow(badRequest(id, "dashboard"));
dashboardValidator.validateDelete(dashboard);
dashboardDeleteService.delete(dashboard);
dashboardCacheManager.delete(id);
}
......@@ -79,7 +76,6 @@ public class DashboardAdminSaveService {
roleDashboardAdminService.saveRole(dashboard);
dashboardRegionService.updateRegions(dashboard);
dashboardDomainService.updateDomains(dashboard, saving);
dashboardParentService.updateParent(dashboard);
return saved;
}
......@@ -87,7 +83,6 @@ public class DashboardAdminSaveService {
entity.setCode(dto.getCode());
entity.setNameEt(dto.getNameEt());
entity.setNameEn(dto.getNameEn());
entity.setLevel(dto.getLevel());
entity.setStatus(dto.getStatus());
entity.setType(dto.getType());
entity.setUpdatedAt(LocalDateTime.now());
......
......@@ -49,7 +49,7 @@ public class DashboardAdminService {
private List<DashboardAdminResponse> convertAll(Page<Dashboard> all, DashboardStrategy dashboardStrategy) {
List<DashboardAdminResponse> results = new ArrayList<>();
for (Dashboard dashboard : all.getContent()) {
DashboardAdminResponse dashboardAdminResponse = dashboardAdminConverter.mapWChildren(dashboard);
DashboardAdminResponse dashboardAdminResponse = dashboardAdminConverter.mapSimple(dashboard);
if (dashboardStrategy.withElements()) {
addElementsToDashboard(dashboard, dashboardAdminResponse);
}
......@@ -73,7 +73,7 @@ public class DashboardAdminService {
if (dashboard.getUserType().isUser()){
throw new StatBadRequestException(ErrorCode.user_dashboards_are_not_visible_to_admin);
}
DashboardAdminResponse dto = dashboardAdminConverter.mapWRegionsWChildrenWParent(dashboard);
DashboardAdminResponse dto = dashboardAdminConverter.mapWRegions(dashboard);
dto.setElements(domainAdminService.loadDomainsAndWidgets(dashboard.getId()));
RoleResponse role = roleDashboardAdminService.getRole(dashboard.getId());
if (role != null){
......@@ -87,7 +87,7 @@ public class DashboardAdminService {
if (StringUtils.isNotBlank(name) && name.trim().length() >= 3) {
return dashboardRepository.findAllByNameEtIsLikeAndLevel("%" + name + "%", 0, of);
} else {
return dashboardRepository.findAllByLevelAndUserType(0, DashboardUserType.ADMIN, of);
return dashboardRepository.findAllByUserType(DashboardUserType.ADMIN, of);
}
}
}
......@@ -85,10 +85,8 @@ public class RoleDashboardAdminService {
private DashboardRule clfToRule(ClassifierCode code) {
if (code == ClassifierCode.EHAK) {
return DashboardRule.ONE_DASHBOARD;
} else if (code == ClassifierCode.BUSINESS) {
} else if (code == ClassifierCode.BUSINESS || code == ClassifierCode.GOV) {
return DashboardRule.DASHBOARDS;
} else if (code == ClassifierCode.GOV) {
return DashboardRule.SUB_DASHBOARDS;
}
throw new UnsupportedOperationException("unknown clf: " + code.name());
}
......
package ee.stat.dashboard.service.admin.dashboard;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.service.admin.dashboard.dto.DashboardAdminResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@Service
@AllArgsConstructor
public class DashboardAdminConverter {
private DashboardRepository dashboardRepository;
private ElementAdminService elementAdminService;
public DashboardAdminResponse mapWRegionsWChildrenWParent(Dashboard dashboard) {
DashboardAdminResponse dto = mapMainData(dashboard);
if (dashboard.getLevel() == 0) {
mapChildren(dto, dashboard.getId());
}
if (dashboard.getLevel() > 0) {
mapParent(dto, dashboard.getId());
}
public DashboardAdminResponse mapWRegions(Dashboard dashboard) {
DashboardAdminResponse dto = mapSimple(dashboard);
if (dashboard.getType().isRegional()) {
dto.setRegions(elementAdminService.findAllDashboardRegions(dashboard.getId()));
}
return dto;
}
public DashboardAdminResponse mapWParent(Dashboard dashboard) {
DashboardAdminResponse dto = mapMainData(dashboard);
if (dashboard.getLevel() > 0) {
mapParent(dto, dashboard.getId());
}
return dto;
}
public DashboardAdminResponse mapWChildren(Dashboard dashboard) {
DashboardAdminResponse dto = mapMainData(dashboard);
if (dashboard.getLevel() == 0) {
mapChildren(dto, dashboard.getId());
}
return dto;
}
public DashboardAdminResponse mapMainData(Dashboard dashboard) {
public DashboardAdminResponse mapSimple(Dashboard dashboard) {
DashboardAdminResponse dto = new DashboardAdminResponse();
dto.setId(dashboard.getId());
dto.setCode(dashboard.getCode());
......@@ -56,30 +27,6 @@ public class DashboardAdminConverter {
dto.setNameEt(dashboard.getNameEt());
dto.setType(dashboard.getType());
dto.setStatus(dashboard.getStatus());
dto.setLevel(dashboard.getLevel());
return dto;
}
private void mapParent(DashboardAdminResponse dto, Long id) {
List<Dashboard> parents = dashboardRepository.findAllFromByTo(id);
if (isNotEmpty(parents)) {
dto.setParentId(parents.get(0).getId());
dto.setParent(mapMainData(parents.get(0)));
}
}
private void mapChildren(DashboardAdminResponse dto, Long id) {
List<Dashboard> children = dashboardRepository.findAllToByFrom(id);
if (isNotEmpty(children)) {
dto.setSubDashboards(children.stream()
.map(child -> mapMainDataAndParentId(child, id))
.collect(Collectors.toList()));
}
}
private DashboardAdminResponse mapMainDataAndParentId(Dashboard child, Long parentId) {
DashboardAdminResponse childDto = mapMainData(child);
childDto.setParentId(parentId);
return childDto;
}
}
......@@ -20,10 +20,6 @@ public class DashboardCacheManager {
@Caching(evict = {
@CacheEvict(value = "DashboardServiceCache_findAllByLevel", allEntries = true),
@CacheEvict(value = "DashboardServiceCache_findById", key = "#dashboard.id"),
@CacheEvict(value = "DashboardServiceCache_findAllFromByTo", key = "#dashboard.id"),
@CacheEvict(value = "DashboardServiceCache_findAllFromByTo", condition = "#dashboard.parentId != null", key = "#dashboard.parentId"),
@CacheEvict(value = "DashboardServiceCache_findAllToByFrom", key = "#dashboard.id"),
@CacheEvict(value = "DashboardServiceCache_findAllToByFrom", condition = "#dashboard.parentId != null", key = "#dashboard.parentId"),
@CacheEvict(value = "ElementServiceCache_findAllDashboardRegions", key = "#dashboard.id"),
})
public void update(DashboardAdminResponse dashboard) {
......@@ -47,8 +43,6 @@ public class DashboardCacheManager {
@Caching(evict = {
@CacheEvict(value = "DashboardServiceCache_findAllByLevel", allEntries = true),
@CacheEvict(value = "DashboardServiceCache_findAllFromByTo", condition = "#dashboard.parentId != null", key = "#dashboard.parentId"),
@CacheEvict(value = "DashboardServiceCache_findAllToByFrom", condition = "#dashboard.parentId != null", key = "#dashboard.parentId"),
})
public void save(DashboardAdminResponse dashboard) {
......@@ -57,8 +51,6 @@ public class DashboardCacheManager {
@Caching(evict = {
@CacheEvict(value = "DashboardServiceCache_findById", key = "#dashboardId"),
@CacheEvict(value = "DashboardServiceCache_findAllByLevel", allEntries = true),
@CacheEvict(value = "DashboardServiceCache_findAllFromByTo", allEntries = true),
@CacheEvict(value = "DashboardServiceCache_findAllToByFrom", allEntries = true),
})
public void delete(Long dashboardId) {
......
......@@ -4,7 +4,6 @@ package ee.stat.dashboard.service.admin.dashboard;
import ee.stat.dashboard.model.classifier.Domain;
import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.widget.back.WidgetDomain;
import ee.stat.dashboard.repository.DashboardConnectionRepository;
import ee.stat.dashboard.repository.DashboardRegionRepository;
import ee.stat.dashboard.repository.DashboardRepository;
import ee.stat.dashboard.repository.DashboardWidgetRepository;
......@@ -28,7 +27,6 @@ import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
public class DashboardDeleteService {
private DashboardRepository dashboardRepository;
private DashboardConnectionRepository dashboardConnectionRepository;
private DashboardWidgetRepository dashboardWidgetRepository;
private DashboardRegionRepository dashboardRegionRepository;
private UserWidgetDeleteService userWidgetDeleteService;
......@@ -38,7 +36,6 @@ public class DashboardDeleteService {
private RoleDashboardRepository roleDashboardRepository;
public void delete(Dashboard dashboard) {
dashboardConnectionRepository.deleteByToId(dashboard.getId());
dashboardRegionRepository.deleteByDashboard(dashboard.getId());
userWidgetDeleteService.deleteUserWidgets(dashboard);
......
package ee.stat.dashboard.service.admin.dashboard;
import ee.stat.dashboard.model.dashboard.DashboardConnection;
import ee.stat.dashboard.repository.DashboardConnectionRepository;
import ee.stat.dashboard.service.admin.dashboard.dto.DashboardAdminResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@Service
@AllArgsConstructor
@Transactional
public class DashboardParentService {
private DashboardConnectionRepository dashboardConnectionRepository;
public void updateParent(DashboardAdminResponse dashboard) {
List<DashboardConnection> existingConnections = dashboardConnectionRepository.findByToId(dashboard.getId());
Optional<DashboardConnection> connectionOp = existingConnection(existingConnections, dashboard);
if (connectionOp.isPresent()) {
return; //nothing to change
}
if (isNotEmpty(existingConnections)) {
dashboardConnectionRepository.deleteByToId(dashboard.getId());
}
if (dashboard.getParentId() != null) {
dashboardConnectionRepository.save(new DashboardConnection(dashboard.getParentId(), dashboard.getId()));
}
}
private Optional<DashboardConnection> existingConnection(List<DashboardConnection> existingConnections, DashboardAdminResponse dashboard) {
return existingConnections.stream()
.filter(c -> c.getFromId().equals(dashboard.getParentId()))
.filter(c -> c.getToId().equals(dashboard.getId()))
.findAny();
}
}
......@@ -37,12 +37,6 @@ public class DashboardValidator {
if (dashboard.getStatus() == null) {
throw badRequest(DASHBOARD, "no status").get();
}
if (dashboard.getLevel() == null) {
throw badRequest(DASHBOARD, "no level").get();
}
if (dashboard.getLevel() > 0) {
validateParent(dashboard);
}
if (dashboard.getType().isRegional()) {
validateRegions(dashboard);
}
......@@ -70,23 +64,6 @@ public class DashboardValidator {
}
}
private void validateParent(DashboardAdminResponse dashboard) {
if (dashboard.getParentId() == null) {
throw badRequest(DASHBOARD, "no parent").get();
}
Optional<Dashboard> byId = dashboardRepository.findById(dashboard.getParentId());
if (!byId.isPresent()) {
throw badRequest(DASHBOARD, "parent not found").get();
}
}
public void validateDelete(Dashboard dashboard) {
List<Dashboard> subDashboards = dashboardRepository.findAllToByFrom(dashboard.getId());
if (isNotEmpty(subDashboards)) {
throw badRequest(DASHBOARD, "dashboard has sub dashboards").get();
}
}
private void validateName(DashboardAdminResponse dashboard, ValidationStrategy strategy, String name, String fieldName, Function<String, List<Dashboard>> findByName) {
if (name == null) {
throw badRequest(DASHBOARD, fieldName + " missingE").get();
......
......@@ -14,8 +14,6 @@ import java.util.List;
public class DashboardAdminResponse {
private Long id;
private DashboardAdminResponse parent;
private Long parentId;
private String code;
private String nameEt;
private String nameEn;
......@@ -23,7 +21,6 @@ public class DashboardAdminResponse {
private DashboardType type;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private Integer level;
private List<DashboardAdminResponse> subDashboards;
private List<ElementAdminResponse> regions;
......
......@@ -37,7 +37,7 @@ public class DomainAdminConverter {
public DomainAdminResponse mapWDashboard(Domain domain) {
Dashboard dashboard = dashboardRepository.findById(domain.getDashboard()).orElseThrow(RuntimeException::new);
DomainAdminResponse domainAdminResponse = mapMainData(domain);
domainAdminResponse.setDashboard(dashboardAdminConverter.mapWParent(dashboard));
domainAdminResponse.setDashboard(dashboardAdminConverter.mapSimple(dashboard));
return domainAdminResponse;
}
}
......@@ -75,7 +75,7 @@ public class WidgetAdminConverter {
}
private List<DashboardAdminResponse> mapDashboards(Widget widget) {
return dashboardRepository.findAllByWidget(widget.getId()).stream().map(dashboardAdminConverter::mapWParent).collect(Collectors.toList());
return dashboardRepository.findAllByWidget(widget.getId()).stream().map(dashboardAdminConverter::mapSimple).collect(Collectors.toList());
}
public void mapDetailedData(WidgetAdminResponse dto, Widget widget) {
......
......@@ -5,18 +5,15 @@ import ee.stat.dashboard.model.dashboard.Dashboard;
import ee.stat.dashboard.model.user.UserDashboard;
import ee.stat.dashboard.model.widget.back.enums.Language;
import ee.stat.dashboard.repository.UserDashboardRepository;
import ee.stat.dashboard.service.dashboard.dto.DashboardLoadStrategy;
import ee.stat.dashboard.service.dashboard.dto.DashboardResponse;
import ee.stat.dashboard.service.element.ElementService;
import ee.stat.dashboard.service.element.dto.ElementResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static ee.stat.dashboard.service.dashboard.dto.DashboardLoadStrategy.*;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
@Service
......@@ -24,7 +21,6 @@ import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;
public class DashboardConverter {
private ElementService elementService;
private DashboardServiceCache dashboardServiceCache;
private UserDashboardRepository userDashboardRepository;
public List<DashboardResponse> mapSimpleWRegion(List<Dashboard> dashboard, Language lang, StatUser user) {
......@@ -36,14 +32,11 @@ public class DashboardConverter {
}
public DashboardResponse mapSimple(Dashboard dashboard, Language lang) {
DashboardResponse dto = mapByParent(dashboard, null, lang, SKIP_CHILDREN);
mapParentId(dashboard, dto);
return dto;
return mapSimpleInner(dashboard, lang);
}
public DashboardResponse mapSimpleWRegion(Dashboard dashboard, Language lang, StatUser user) {
DashboardResponse dto = mapByParent(dashboard, null, lang, LOAD_CHILDREN);
mapParentId(dashboard, dto);
DashboardResponse dto = mapSimpleInner(dashboard, lang);
mapRegion(dashboard, lang, user, dto);
return dto;
}
......@@ -60,37 +53,13 @@ public class DashboardConverter {
}
}
private void mapParentId(Dashboard dashboard, DashboardResponse dto) {
if (dashboard.getLevel() > 0) {
List<Dashboard> parents = dashboardServiceCache.findAllFromByTo(dashboard.getId());
if (isNotEmpty(parents)) {
dto.setParentId(parents.get(0).getId());
}
}