diff --git a/domain/src/main/java/org/fao/geonet/domain/page/Page.java b/domain/src/main/java/org/fao/geonet/domain/page/Page.java index eca6ca25e1e..563e58663fa 100644 --- a/domain/src/main/java/org/fao/geonet/domain/page/Page.java +++ b/domain/src/main/java/org/fao/geonet/domain/page/Page.java @@ -41,7 +41,7 @@ import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.Lob; -import javax.persistence.OneToMany; +import javax.persistence.ManyToMany; import javax.persistence.Table; import org.fao.geonet.domain.GeonetEntity; @@ -160,9 +160,9 @@ public String getIcon() { * * @return all the page's groups. */ - @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST, orphanRemoval = false) + @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH}) @JoinTable(name = "spg_page_group", joinColumns = {@JoinColumn(name = "language"), @JoinColumn(name = "linktext")}, - inverseJoinColumns = {@JoinColumn(name = "groupid", referencedColumnName = "id", unique = true)}) + inverseJoinColumns = {@JoinColumn(name = "groupid", referencedColumnName = "id", unique = false)}) public Set getGroups() { return groups; } diff --git a/domain/src/main/java/org/fao/geonet/repository/page/PageRepository.java b/domain/src/main/java/org/fao/geonet/repository/page/PageRepository.java index b76094c770f..1245a752d99 100644 --- a/domain/src/main/java/org/fao/geonet/repository/page/PageRepository.java +++ b/domain/src/main/java/org/fao/geonet/repository/page/PageRepository.java @@ -29,7 +29,9 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface PageRepository extends JpaRepository { - + List findByPageIdentityLanguage(String language); + List findPageByStatus(Page.PageStatus status); + } diff --git a/services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java b/services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java index 317edf6d6cd..ac931e0c5fe 100644 --- a/services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java +++ b/services/src/main/java/org/fao/geonet/api/groups/GroupsApi.java @@ -80,6 +80,7 @@ import java.nio.file.attribute.FileTime; import java.sql.SQLException; import java.util.*; +import java.util.stream.Collectors; import static org.springframework.data.jpa.domain.Specification.where; @@ -547,19 +548,17 @@ public void deleteGroup( )); } - List staticPages = pageRepository.findAll(); + List staticPages = pageRepository.findPageByStatus(Page.PageStatus.GROUPS); + List staticPagesAssignedToGroup = + staticPages.stream().filter(p -> + !p.getGroups().stream().filter(g -> g.getId() == groupIdentifier).collect(Collectors.toList()).isEmpty()) + .collect(Collectors.toList()); - for (Page page: staticPages) { - if (CollectionUtils.isNotEmpty(page.getGroups())) { - for (Group pageGroup : page.getGroups()) { - if (pageGroup.getId() == groupIdentifier) { - throw new NotAllowedException(String.format( - "Group %s is associated with '%s' static page(s). Please remove the static page(s) associated with that group first.", - group.get().getName(), page.getLabel() - )); - } - } - } + if (!staticPagesAssignedToGroup.isEmpty()) { + throw new NotAllowedException(String.format( + "Group %s is associated with '%s' static page(s). Please remove the static page(s) associated with that group first.", + group.get().getName(), staticPagesAssignedToGroup.stream().map(p -> p.getLabel()).collect(Collectors.joining()) + )); } groupRepository.deleteById(groupIdentifier);