Skip to content

Commit

Permalink
feat: improved algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter committed Apr 19, 2024
1 parent 666f482 commit 3b1ae8a
Showing 1 changed file with 8 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,14 @@ default UserWorkspaceMenuStructureDTO mapTree(Workspace workspace, Collection<Me
items = entities.stream().filter(m -> m.getParentId() == null)
.collect(Collectors.toSet());
}
items = filterMenu(items, mapping, roles);
items = filterMenu(items, mapping, roles, workspace.getBaseUrl());
if (items.isEmpty()) {
return dto;
}
items.forEach(menuItem -> menuItem = updateUrl(menuItem, workspace.getBaseUrl()));

return dto.menu(items.stream().map(this::mapTreeItem).toList());
}

default MenuItem updateUrl(MenuItem menuItem, String workspaceUrl) {
if (!menuItem.getChildren().isEmpty()) {
menuItem.getChildren().forEach(menuItemChild -> {
menuItemChild.setUrl(updateInternalUrl(workspaceUrl, menuItemChild.getUrl(), menuItemChild.isExternal()));
updateUrl(menuItemChild, workspaceUrl);
});
} else {
if (menuItem.getUrl() != null) {
menuItem.setUrl(updateInternalUrl(workspaceUrl, menuItem.getUrl(), menuItem.isExternal()));
}
}
return menuItem;
}

default String updateInternalUrl(String workspaceUrl, String menuItemUrl, Boolean isExternal) {
if (Boolean.TRUE.equals(isExternal)) {
return menuItemUrl;
Expand All @@ -68,32 +53,34 @@ default String updateInternalUrl(String workspaceUrl, String menuItemUrl, Boolea
}
}

default Set<MenuItem> filterMenu(Set<MenuItem> items, Map<String, Set<String>> mapping, Set<String> roles) {
default Set<MenuItem> filterMenu(Set<MenuItem> items, Map<String, Set<String>> mapping, Set<String> roles,
String workspaceUrl) {
Set<MenuItem> tmp = new HashSet<>(items);
tmp.forEach(m -> {
var mr = mapping.get(m.getId());
if (mr == null || mr.stream().noneMatch(roles::contains)) {
items.remove(m);
} else {
filterChildren(m, mapping, roles);
filterChildren(m, mapping, roles, workspaceUrl);
}
});

return items;
}

default void filterChildren(MenuItem menuItem, Map<String, Set<String>> mapping, Set<String> roles) {
default void filterChildren(MenuItem menuItem, Map<String, Set<String>> mapping, Set<String> roles, String workspaceUrl) {
Set<MenuItem> items = new HashSet<>(menuItem.getChildren());
items.forEach(child -> {
var mr = mapping.get(child.getId());
if (mr != null && mr.stream().noneMatch(roles::contains)) {
menuItem.getChildren().remove(child);
} else {
if (child.getChildren() != null && !child.getChildren().isEmpty()) {
filterChildren(child, mapping, roles);
filterChildren(child, mapping, roles, workspaceUrl);
} else {
child.setUrl(updateInternalUrl(workspaceUrl, child.getUrl(), child.isExternal()));
}
}
});
}

}

0 comments on commit 3b1ae8a

Please sign in to comment.