refactor: reformat code(menu)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.egovframe.cloud.portalservice.api.menu.dto;
|
||||
|
||||
import java.util.Objects;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -50,4 +51,8 @@ public class MenuDnDRequestDto {
|
||||
this.icon = icon;
|
||||
this.children = children == null ? null : new ArrayList<>(children);
|
||||
}
|
||||
|
||||
public boolean hasParentId() {
|
||||
return Objects.nonNull(parentId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.egovframe.cloud.portalservice.api.menu.dto;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -56,4 +57,8 @@ public class MenuRoleRequestDto {
|
||||
this.level = level;
|
||||
this.children = children == null ? null : new ArrayList<>(children);
|
||||
}
|
||||
|
||||
public boolean hasMenuRoleId() {
|
||||
return Objects.nonNull(menuRoleId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.egovframe.cloud.portalservice.api.menu.dto;
|
||||
|
||||
import java.util.Optional;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
@@ -54,20 +55,21 @@ public class MenuRoleResponseDto {
|
||||
*/
|
||||
public MenuRoleResponseDto (Menu menu, String roleId) {
|
||||
|
||||
MenuRole menuRole = menu.getMenuRole(roleId);
|
||||
if (menuRole == null) {
|
||||
this.isChecked = false;
|
||||
this.roleId = roleId;
|
||||
}else {
|
||||
this.menuRoleId = menuRole.getId();
|
||||
this.roleId = menuRole.getRoleId();
|
||||
Optional<MenuRole> menuRole = menu.getMenuRole(roleId);
|
||||
|
||||
this.isChecked = false;
|
||||
this.roleId = roleId;
|
||||
|
||||
if (menuRole.isPresent()) {
|
||||
this.menuRoleId = menuRole.get().getId();
|
||||
this.roleId = menuRole.get().getRoleId();
|
||||
this.isChecked = true;
|
||||
}
|
||||
|
||||
this.id = menu.getId();
|
||||
this.korName = menu.getMenuKorName();
|
||||
this.engName = menu.getMenuEngName();
|
||||
if (menu.getParent() != null) {
|
||||
if (menu.hasParent()) {
|
||||
this.parentId = menu.getParent().getId();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.egovframe.cloud.portalservice.api.menu.dto;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
@@ -7,6 +9,7 @@ import org.egovframe.cloud.portalservice.domain.menu.Menu;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.MenuRole;
|
||||
|
||||
/**
|
||||
* org.egovframe.cloud.portalservice.api.menu.dto.MenuSideResponseDto
|
||||
@@ -65,8 +68,8 @@ public class MenuSideResponseDto {
|
||||
this.isShow = menu.getIsShow();
|
||||
|
||||
this.children = menu.getChildren().stream()
|
||||
.filter(children -> children.getIsUse())
|
||||
.filter(children -> children.getMenuRole(roleId) != null)
|
||||
.filter(Menu::getIsUse)
|
||||
.filter(children -> children.getMenuRole(roleId).isPresent())
|
||||
.map(children -> new MenuSideResponseDto(children, roleId))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
@@ -79,4 +82,12 @@ public class MenuSideResponseDto {
|
||||
public void setUrlPath(String urlPath) {
|
||||
this.urlPath = urlPath;
|
||||
}
|
||||
|
||||
public boolean hasChildren() {
|
||||
return Objects.nonNull(children) || children.size() > 0;
|
||||
}
|
||||
|
||||
public boolean isRequiredUrlPath() {
|
||||
return "board".equals(menuType) || "contents".equals(menuType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.egovframe.cloud.portalservice.api.menu.dto;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -8,6 +10,7 @@ import org.egovframe.cloud.portalservice.domain.menu.Menu;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.Site;
|
||||
|
||||
/**
|
||||
* org.egovframe.cloud.portalservice.api.menu.dto.MenuTreeRequestDto
|
||||
@@ -48,4 +51,16 @@ public class MenuTreeRequestDto {
|
||||
this.isShow = isShow;
|
||||
this.isUse = isUse;
|
||||
}
|
||||
|
||||
public Menu toEntity(Optional<Menu> parent, Site site) {
|
||||
return Menu.builder()
|
||||
.parent(parent)
|
||||
.site(site)
|
||||
.menuKorName(name)
|
||||
.sortSeq(sortSeq)
|
||||
.level(level)
|
||||
.isShow(isShow)
|
||||
.isUse(isUse)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package org.egovframe.cloud.portalservice.api.menu.dto;
|
||||
|
||||
import java.util.Objects;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import org.egovframe.cloud.common.exception.BusinessMessageException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* org.egovframe.cloud.portalservice.api.menu.dto.MenuUpdateRequestDto
|
||||
@@ -58,4 +61,18 @@ public class MenuUpdateRequestDto {
|
||||
this.description = description;
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public boolean hasConnectId() {
|
||||
if ("contents".equals(menuType) || "board".equals(menuType)) {
|
||||
return Objects.nonNull(connectId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean hasUrlPath() {
|
||||
if ("inside".equals(menuType) || "outside".equals(menuType)) {
|
||||
return Objects.nonNull(urlPath) || StringUtils.hasText(urlPath);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.egovframe.cloud.portalservice.domain.menu;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -96,14 +98,16 @@ public class Menu extends BaseEntity {
|
||||
|
||||
|
||||
@Builder
|
||||
public Menu(String menuKorName, Menu parent, Integer sortSeq, Site site, Integer level, Boolean isUse, Boolean isShow) {
|
||||
public Menu(String menuKorName, Optional<Menu> parent, Integer sortSeq, Site site, Integer level, Boolean isUse, Boolean isShow) {
|
||||
this.menuKorName = menuKorName;
|
||||
this.parent = parent;
|
||||
this.sortSeq = sortSeq;
|
||||
this.site = site;
|
||||
this.level = level;
|
||||
this.isShow = isShow;
|
||||
this.isUse = isUse;
|
||||
if (Objects.nonNull(parent)) {
|
||||
parent.ifPresent(it -> this.parent = it);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,11 +128,11 @@ public class Menu extends BaseEntity {
|
||||
* @param sortSeq
|
||||
* @return
|
||||
*/
|
||||
public Menu updateDnD(Menu parent, Integer sortSeq, Integer level) {
|
||||
public Menu updateDnD(Optional<Menu> parent, Integer sortSeq, Integer level) {
|
||||
this.sortSeq = sortSeq;
|
||||
this.level = level;
|
||||
|
||||
if (parent == null) {
|
||||
if (!parent.isPresent()) {
|
||||
return updateOldParent();
|
||||
}
|
||||
|
||||
@@ -136,22 +140,21 @@ public class Menu extends BaseEntity {
|
||||
return this;
|
||||
}
|
||||
|
||||
this.parent = parent;
|
||||
parent.getChildren().add(this);
|
||||
this.parent = parent.get();
|
||||
parent.get().getChildren().add(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
private Menu updateOldParent() {
|
||||
Menu oldParent = this.getParent();
|
||||
|
||||
if (oldParent == null) {
|
||||
if (Objects.isNull(this.parent)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
Menu old = oldParent.getChildren().stream().filter(item -> item.getId().equals(this.id)).findAny().orElse(null);
|
||||
if (old != null) {
|
||||
oldParent.getChildren().remove(old);
|
||||
}
|
||||
Optional<Menu> oldMenu = this.parent.getChildren().stream()
|
||||
.filter(it -> it.getId().equals(this.id))
|
||||
.findAny();
|
||||
|
||||
oldMenu.ifPresent(it -> this.parent.getChildren().remove(it));
|
||||
this.parent = null;
|
||||
return this;
|
||||
}
|
||||
@@ -195,12 +198,16 @@ public class Menu extends BaseEntity {
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
public MenuRole getMenuRole(String roleId) {
|
||||
public Optional<MenuRole> getMenuRole(String roleId) {
|
||||
return this.getMenuRoles()
|
||||
.stream()
|
||||
.filter(menuRole ->
|
||||
menuRole.getRoleId().equals(roleId))
|
||||
.findAny().orElse(null);
|
||||
.findAny();
|
||||
}
|
||||
|
||||
public boolean hasParent() {
|
||||
return Objects.nonNull(this.parent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package org.egovframe.cloud.portalservice.service.menu;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.egovframe.cloud.common.exception.EntityNotFoundException;
|
||||
@@ -9,11 +13,13 @@ import org.egovframe.cloud.portalservice.api.menu.dto.MenuRoleResponseDto;
|
||||
import org.egovframe.cloud.portalservice.api.menu.dto.MenuSideResponseDto;
|
||||
import org.egovframe.cloud.portalservice.client.BoardServiceClient;
|
||||
import org.egovframe.cloud.portalservice.client.dto.BoardResponseDto;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.*;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.Menu;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.MenuRepository;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.MenuRole;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.MenuRoleRepository;
|
||||
import org.egovframe.cloud.portalservice.domain.user.Role;
|
||||
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
|
||||
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
@@ -21,9 +27,6 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* org.egovframe.cloud.portalservice.service.menu.MenuRoleService
|
||||
* <p>
|
||||
@@ -63,55 +66,6 @@ public class MenuRoleService extends AbstractService {
|
||||
return menuRoleRepository.findTree(roleId, siteId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 권한별 메뉴 저장
|
||||
* children 데이터 재귀 호출
|
||||
* checked 인 경우 저장
|
||||
* unchecked 인 경우 삭제
|
||||
*
|
||||
* @param menuRoleRequestDto
|
||||
*/
|
||||
private void recursiveSave( MenuRoleRequestDto menuRoleRequestDto) {
|
||||
if (menuRoleRequestDto.getIsChecked()) {
|
||||
// checked 인 경우 menuRole 저장
|
||||
|
||||
Menu menu = menuRepository.findById(menuRoleRequestDto.getId())
|
||||
.orElseThrow(() -> new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= " + menuRoleRequestDto.getId()));
|
||||
|
||||
if (menuRoleRequestDto.getMenuRoleId() == null) {
|
||||
MenuRole menuRole = MenuRole.builder()
|
||||
.roleId(menuRoleRequestDto.getRoleId())
|
||||
.menu(menu)
|
||||
.build();
|
||||
menuRole.setMenu(menu);
|
||||
menuRoleRepository.save(menuRole);
|
||||
}else {
|
||||
MenuRole menuRole = menuRoleRepository.findById(menuRoleRequestDto.getMenuRoleId()).orElse(null);
|
||||
|
||||
if (menuRole != null) {
|
||||
menuRole.setMenu(menu);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//unchecked 인 경우 menurole 삭제
|
||||
if (menuRoleRequestDto.getMenuRoleId() != null) {
|
||||
MenuRole menuRole = menuRoleRepository.findById(menuRoleRequestDto.getMenuRoleId()).orElse(null);
|
||||
if (menuRole != null) {
|
||||
menuRoleRepository.delete(menuRole);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (menuRoleRequestDto.getChildren() == null || menuRoleRequestDto.getChildren().size() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < menuRoleRequestDto.getChildren().size(); i++) {
|
||||
MenuRoleRequestDto child = menuRoleRequestDto.getChildren().get(i);
|
||||
recursiveSave( child);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 권한별 메뉴 저장
|
||||
*
|
||||
@@ -121,62 +75,13 @@ public class MenuRoleService extends AbstractService {
|
||||
@Transactional
|
||||
public String save(List<MenuRoleRequestDto> menuRoleRequestDtoList) {
|
||||
|
||||
for (MenuRoleRequestDto menuRoleRequestDto: menuRoleRequestDtoList) {
|
||||
recursiveSave( menuRoleRequestDto);
|
||||
for (MenuRoleRequestDto menuRoleRequestDto : menuRoleRequestDtoList) {
|
||||
recursiveSave(menuRoleRequestDto);
|
||||
}
|
||||
|
||||
return "Success";
|
||||
}
|
||||
|
||||
/**
|
||||
* 계층구조 메뉴 조회
|
||||
* 로그인 사용자의 권한으로 조회하고
|
||||
* 로그인 사용자가 없는 경우 손님(ROLE_ANONYMOUS) 로 조회한다.
|
||||
*
|
||||
* @param siteId
|
||||
* @return
|
||||
*/
|
||||
private List<MenuSideResponseDto> findMenu(Long siteId) {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null || !authentication.isAuthenticated() || authentication instanceof AnonymousAuthenticationToken) {
|
||||
return menuRoleRepository.findMenu(Role.ANONYMOUS.getKey(), siteId);
|
||||
}
|
||||
String role = authentication.getAuthorities().stream().map(GrantedAuthority::toString).collect(Collectors.toList()).get(0);
|
||||
return menuRoleRepository.findMenu(role, siteId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 메뉴 유형이 게시판인 경우
|
||||
* 해당 게시판의 스킨타입으로 url을 만들어 준다.
|
||||
*
|
||||
* @param menuSideResponseDto
|
||||
*/
|
||||
private void recursiveSetUrlPath(MenuSideResponseDto menuSideResponseDto) {
|
||||
if (menuSideResponseDto.getConnectId() != null) {
|
||||
if ("board".equals(menuSideResponseDto.getMenuType())) {
|
||||
//connectid 로 board 조회
|
||||
//board 의 skinTypeCode로 url 지정
|
||||
// BoardResponseDto board = boardServiceClient.findById(menuSideResponseDto.getConnectId());
|
||||
CircuitBreaker circuitBreaker = circuitBreakerFactory.create("board");
|
||||
BoardResponseDto board = circuitBreaker.run(() ->
|
||||
boardServiceClient.findById(menuSideResponseDto.getConnectId()),
|
||||
throwable -> new BoardResponseDto());
|
||||
|
||||
menuSideResponseDto.setUrlPath("/board/"+board.getSkinTypeCode()+"/"+menuSideResponseDto.getConnectId());
|
||||
} else if ("contents".equals(menuSideResponseDto.getMenuType())) {
|
||||
menuSideResponseDto.setUrlPath("/content/"+menuSideResponseDto.getConnectId());
|
||||
}
|
||||
}
|
||||
|
||||
if (menuSideResponseDto.getChildren() == null || menuSideResponseDto.getChildren().size() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < menuSideResponseDto.getChildren().size(); i++) {
|
||||
MenuSideResponseDto child = menuSideResponseDto.getChildren().get(i);
|
||||
recursiveSetUrlPath(child);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 로그인한 사용자의 권한에 맞는 메뉴 조회
|
||||
@@ -187,10 +92,139 @@ public class MenuRoleService extends AbstractService {
|
||||
public List<MenuSideResponseDto> findMenus(Long siteId) {
|
||||
List<MenuSideResponseDto> menuSideResponseDtoList = findMenu(siteId);
|
||||
|
||||
for (MenuSideResponseDto menuSideResponseDto: menuSideResponseDtoList) {
|
||||
for (MenuSideResponseDto menuSideResponseDto : menuSideResponseDtoList) {
|
||||
recursiveSetUrlPath(menuSideResponseDto);
|
||||
}
|
||||
|
||||
return menuSideResponseDtoList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 계층구조 메뉴 조회 로그인 사용자의 권한으로 조회하고 로그인 사용자가 없는 경우 손님(ROLE_ANONYMOUS) 로 조회한다.
|
||||
*
|
||||
* @param siteId
|
||||
* @return
|
||||
*/
|
||||
private List<MenuSideResponseDto> findMenu(Long siteId) {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null || !authentication.isAuthenticated()
|
||||
|| authentication instanceof AnonymousAuthenticationToken) {
|
||||
return menuRoleRepository.findMenu(Role.ANONYMOUS.getKey(), siteId);
|
||||
}
|
||||
String role = authentication.getAuthorities().stream()
|
||||
.map(GrantedAuthority::toString)
|
||||
.collect(Collectors.toList())
|
||||
.get(0);
|
||||
return menuRoleRepository.findMenu(role, siteId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 메뉴 유형이 게시판인 경우 해당 게시판의 스킨타입으로 url을 만들어 준다.
|
||||
*
|
||||
* @param menuSideResponseDto
|
||||
*/
|
||||
private void recursiveSetUrlPath(MenuSideResponseDto menuSideResponseDto) {
|
||||
if (Objects.nonNull(menuSideResponseDto.getConnectId()) &&
|
||||
menuSideResponseDto.isRequiredUrlPath()) {
|
||||
menuSideResponseDto.setUrlPath(getUrlPath(menuSideResponseDto));
|
||||
}
|
||||
|
||||
if (!menuSideResponseDto.hasChildren()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (MenuSideResponseDto child : menuSideResponseDto.getChildren()) {
|
||||
recursiveSetUrlPath(child);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 권한별 메뉴 저장 children 데이터 재귀 호출 checked 인 경우 저장 unchecked 인 경우 삭제
|
||||
*
|
||||
* @param menuRoleRequestDto
|
||||
*/
|
||||
private void recursiveSave(MenuRoleRequestDto menuRoleRequestDto) {
|
||||
saveMenu(menuRoleRequestDto);
|
||||
|
||||
if (Objects.isNull(menuRoleRequestDto.getChildren())
|
||||
|| menuRoleRequestDto.getChildren().size() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < menuRoleRequestDto.getChildren().size(); i++) {
|
||||
MenuRoleRequestDto child = menuRoleRequestDto.getChildren().get(i);
|
||||
recursiveSave(child);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* urlPath 설정
|
||||
*
|
||||
* @param responseDto
|
||||
* @return
|
||||
*/
|
||||
private String getUrlPath(MenuSideResponseDto responseDto) {
|
||||
if ("contents".equals(responseDto.getMenuType())) {
|
||||
return "/content/" + responseDto.getConnectId();
|
||||
}
|
||||
|
||||
CircuitBreaker circuitBreaker = circuitBreakerFactory.create("board");
|
||||
BoardResponseDto board = circuitBreaker.run(() ->
|
||||
boardServiceClient.findById(responseDto.getConnectId()),
|
||||
throwable -> new BoardResponseDto());
|
||||
|
||||
return "/board/" + board.getSkinTypeCode() + "/" + responseDto.getConnectId();
|
||||
|
||||
}
|
||||
|
||||
private Menu findMenuById(Long id) {
|
||||
return menuRepository.findById(id)
|
||||
.orElseThrow(() -> new EntityNotFoundException(
|
||||
getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= "
|
||||
+ id));
|
||||
}
|
||||
|
||||
/**
|
||||
* menuRole 저장
|
||||
*
|
||||
* @param menuRoleRequestDto
|
||||
*/
|
||||
private void saveMenu(MenuRoleRequestDto menuRoleRequestDto) {
|
||||
if (menuRoleRequestDto.getIsChecked()) {
|
||||
saveCheckedMenu(menuRoleRequestDto);
|
||||
return;
|
||||
}
|
||||
|
||||
//unchecked 인 경우 menurole 삭제
|
||||
if (menuRoleRequestDto.hasMenuRoleId()) {
|
||||
Optional<MenuRole> menuRole = menuRoleRepository
|
||||
.findById(menuRoleRequestDto.getMenuRoleId());
|
||||
menuRole.ifPresent(it -> menuRoleRepository.delete(it));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* checked 인 경우 menuRole 저장
|
||||
*
|
||||
* @param menuRoleRequestDto
|
||||
*/
|
||||
private void saveCheckedMenu(MenuRoleRequestDto menuRoleRequestDto) {
|
||||
Menu menu = findMenuById(menuRoleRequestDto.getId());
|
||||
|
||||
if (!menuRoleRequestDto.hasMenuRoleId()) {
|
||||
MenuRole menuRole = MenuRole.builder()
|
||||
.roleId(menuRoleRequestDto.getRoleId())
|
||||
.menu(menu)
|
||||
.build();
|
||||
menuRole.setMenu(menu);
|
||||
menuRoleRepository.save(menuRole);
|
||||
return;
|
||||
}
|
||||
|
||||
Optional<MenuRole> menuRole = menuRoleRepository
|
||||
.findById(menuRoleRequestDto.getMenuRoleId());
|
||||
menuRole.ifPresent(it -> it.setMenu(menu));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
package org.egovframe.cloud.portalservice.service.menu;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.egovframe.cloud.common.exception.BusinessException;
|
||||
import org.egovframe.cloud.common.exception.BusinessMessageException;
|
||||
import org.egovframe.cloud.common.exception.EntityNotFoundException;
|
||||
import org.egovframe.cloud.common.service.AbstractService;
|
||||
import org.egovframe.cloud.portalservice.api.menu.dto.*;
|
||||
import org.egovframe.cloud.portalservice.api.menu.dto.MenuDnDRequestDto;
|
||||
import org.egovframe.cloud.portalservice.api.menu.dto.MenuResponseDto;
|
||||
import org.egovframe.cloud.portalservice.api.menu.dto.MenuTreeRequestDto;
|
||||
import org.egovframe.cloud.portalservice.api.menu.dto.MenuTreeResponseDto;
|
||||
import org.egovframe.cloud.portalservice.api.menu.dto.MenuUpdateRequestDto;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.Menu;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.MenuRepository;
|
||||
import org.egovframe.cloud.portalservice.domain.menu.Site;
|
||||
@@ -14,8 +20,6 @@ import org.egovframe.cloud.portalservice.domain.menu.SiteRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* org.egovframe.cloud.portalservice.service.menu.MenuService
|
||||
* <p>
|
||||
@@ -71,25 +75,11 @@ public class MenuService extends AbstractService {
|
||||
* @return
|
||||
*/
|
||||
public MenuTreeResponseDto save(MenuTreeRequestDto menuTreeRequestDto) {
|
||||
Site site = siteRepository.findById(menuTreeRequestDto.getSiteId())
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu.site")}) + " ID= " + menuTreeRequestDto.getSiteId()));
|
||||
Site site = findSite(menuTreeRequestDto.getSiteId());
|
||||
|
||||
Menu parent = null;
|
||||
Optional<Menu> parentMenu = findParentMenu(menuTreeRequestDto.getParentId());
|
||||
|
||||
if (menuTreeRequestDto.getParentId() != null) {
|
||||
parent = findById(menuTreeRequestDto.getParentId());
|
||||
}
|
||||
|
||||
Menu menu = menuRepository.save(Menu.builder()
|
||||
.parent(parent)
|
||||
.site(site)
|
||||
.menuKorName(menuTreeRequestDto.getName())
|
||||
.sortSeq(menuTreeRequestDto.getSortSeq())
|
||||
.level(menuTreeRequestDto.getLevel())
|
||||
.isShow(menuTreeRequestDto.getIsShow())
|
||||
.isUse(menuTreeRequestDto.getIsUse())
|
||||
.build());
|
||||
Menu menu = menuRepository.save(menuTreeRequestDto.toEntity(parentMenu, site));
|
||||
return MenuTreeResponseDto.builder()
|
||||
.entity(menu).build();
|
||||
}
|
||||
@@ -120,20 +110,7 @@ public class MenuService extends AbstractService {
|
||||
public MenuResponseDto update(Long menuId, MenuUpdateRequestDto updateRequestDto) throws EntityNotFoundException, BusinessMessageException {
|
||||
Menu menu = findById(menuId);
|
||||
|
||||
//컨텐츠 or 게시판인 경우 connectId 필수
|
||||
if ("contents".equals(updateRequestDto.getMenuType()) || "board".equals(updateRequestDto.getMenuType())) {
|
||||
if (updateRequestDto.getConnectId() == null || updateRequestDto.getConnectId().equals("")) {
|
||||
//컨텐츠 or 게시판을 선택해 주세요
|
||||
throw new BusinessMessageException(getMessage("valid.selection.format", new Object[]{updateRequestDto.getMenuTypeName()}));
|
||||
}
|
||||
}else if ("inside".equals(updateRequestDto.getMenuType()) || "outside".equals(updateRequestDto.getMenuType())) {
|
||||
// 내부링크 or 외부링크인 경우 링크 url 필수
|
||||
if (updateRequestDto.getUrlPath() == null || updateRequestDto.getUrlPath().equals("")) {
|
||||
//링크 Url 값은 필수 입니다.
|
||||
throw new BusinessMessageException(getMessage("valid.required", new Object[]{getMessage("menu.url_path")}));
|
||||
|
||||
}
|
||||
}
|
||||
validateUpdate(updateRequestDto);
|
||||
|
||||
menu.updateDetail(updateRequestDto);
|
||||
|
||||
@@ -147,31 +124,7 @@ public class MenuService extends AbstractService {
|
||||
* @param menuId
|
||||
*/
|
||||
public void delete(Long menuId) {
|
||||
Menu menu = findById(menuId);
|
||||
menuRepository.delete(menu);
|
||||
}
|
||||
|
||||
/**
|
||||
* 트리 드래그 앤드 드랍 시 children 데이터 재귀호출 저장
|
||||
*
|
||||
* @param dto
|
||||
* @param parent
|
||||
* @param sortSeq
|
||||
* @param level
|
||||
*/
|
||||
private void recursive(MenuDnDRequestDto dto, Menu parent, Integer sortSeq, Integer level) {
|
||||
Menu menu = findById(dto.getMenuId());
|
||||
|
||||
menu.updateDnD(parent, sortSeq, level);
|
||||
|
||||
if (dto.getChildren() == null || dto.getChildren().size() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < dto.getChildren().size(); i++) {
|
||||
MenuDnDRequestDto child = dto.getChildren().get(i);
|
||||
recursive(child, menu, child.getSortSeq(), menu.getLevel()+1);
|
||||
}
|
||||
menuRepository.delete(findById(menuId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,18 +137,71 @@ public class MenuService extends AbstractService {
|
||||
public Long updateDnD(Long siteId, List<MenuDnDRequestDto> menuDnDRequestDtoList) {
|
||||
for (int i = 0; i < menuDnDRequestDtoList.size(); i++) {
|
||||
MenuDnDRequestDto requestDto = menuDnDRequestDtoList.get(i);
|
||||
Menu parent = null;
|
||||
if (requestDto.getParentId() != null) {
|
||||
parent = findById(requestDto.getParentId());
|
||||
}
|
||||
recursive(requestDto, parent, requestDto.getSortSeq(), requestDto.getLevel());
|
||||
Optional<Menu> parentMenu = findParentMenu(requestDto.getParentId());
|
||||
|
||||
recursive(requestDto, parentMenu, requestDto.getSortSeq(), requestDto.getLevel());
|
||||
}
|
||||
return siteId;
|
||||
}
|
||||
|
||||
private Optional<Menu> findParentMenu(Long parentId) {
|
||||
if (Objects.isNull(parentId)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return menuRepository.findById(parentId);
|
||||
}
|
||||
|
||||
private Menu findById(Long id) {
|
||||
return menuRepository.findById(id)
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu")}) + " ID= " + id));
|
||||
}
|
||||
|
||||
private Site findSite(Long id) {
|
||||
return siteRepository.findById(id)
|
||||
.orElseThrow(() ->
|
||||
new EntityNotFoundException(getMessage("valid.notexists.format", new Object[]{getMessage("menu.site")}) + " ID= " + id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 메뉴 정합성 체크
|
||||
*
|
||||
* @param updateRequestDto
|
||||
*/
|
||||
private void validateUpdate(MenuUpdateRequestDto updateRequestDto) {
|
||||
//컨텐츠 or 게시판인 경우 connectId 필수
|
||||
if (!updateRequestDto.hasConnectId()) {
|
||||
//컨텐츠 or 게시판을 선택해 주세요
|
||||
throw new BusinessMessageException(getMessage("valid.selection.format", new Object[]{updateRequestDto.getMenuTypeName()}));
|
||||
}
|
||||
|
||||
// 내부링크 or 외부링크인 경우 링크 url 필수
|
||||
if (!updateRequestDto.hasUrlPath()) {
|
||||
//링크 Url 값은 필수 입니다.
|
||||
throw new BusinessMessageException(getMessage("valid.required", new Object[]{getMessage("menu.url_path")}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 트리 드래그 앤드 드랍 시 children 데이터 재귀호출 저장
|
||||
*
|
||||
* @param dto
|
||||
* @param parent
|
||||
* @param sortSeq
|
||||
* @param level
|
||||
*/
|
||||
private void recursive(MenuDnDRequestDto dto, Optional<Menu> parent, Integer sortSeq, Integer level) {
|
||||
Menu menu = findById(dto.getMenuId());
|
||||
|
||||
menu.updateDnD(parent, sortSeq, level);
|
||||
|
||||
if (Objects.isNull(dto.getChildren()) || dto.getChildren().size() <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < dto.getChildren().size(); i++) {
|
||||
MenuDnDRequestDto child = dto.getChildren().get(i);
|
||||
recursive(child, Optional.of(menu), child.getSortSeq(), menu.getLevel()+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user