Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #196 from tesler-platform/feature/view-allowed-rol…
Browse files Browse the repository at this point in the history
…es-draft-2

tesler.meta.view-allowed-roles-enabled support. Draft 2
  • Loading branch information
IgorNB authored Aug 29, 2021
2 parents 5eff2d6 + 153e335 commit dcee856
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,21 @@
@Setter
public class ScreenSourceDto {

/**
* used only if tesler.meta.view-allowed-roles-enabled=true (see {@link io.tesler.core.metahotreload.conf.properties.MetaConfigurationProperties#viewAllowedRolesEnabled})
*/
private int order = 0;

/**
* used only if tesler.meta.view-allowed-roles-enabled=true (see {@link io.tesler.core.metahotreload.conf.properties.MetaConfigurationProperties#viewAllowedRolesEnabled})
*/
private String icon;

private String name;

/**
* used only if tesler.meta.view-allowed-roles-enabled=true (see {@link io.tesler.core.metahotreload.conf.properties.MetaConfigurationProperties#viewAllowedRolesEnabled})
*/
private String title;

private String primaryViewName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@
import io.tesler.model.ui.navigation.NavigationGroup;
import io.tesler.model.ui.navigation.NavigationView;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -124,45 +128,61 @@ private void responsibilitiesProcess(List<ScreenSourceDto> screenDtos, List<View
});
});

Map<String, Set<String>> screenToRoles = new HashMap<>();
Map<String, ScreenSourceDto> screenNameToScreen = screenDtos.stream()
.collect(Collectors.toMap(ScreenSourceDto::getName, sd -> sd));

Map<String, Set<ScreenSourceDto>> rolesToScreens = new HashMap<>();
viewDtos.forEach(v -> {
if (viewToScreenMap.containsKey(v.getName())) {
String screenName = viewToScreenMap.get(v.getName());
if (!screenToRoles.containsKey(screenName)) {
screenToRoles.put(screenName, new HashSet<>());
}
Set<String> roles = screenToRoles.get(screenName);
roles.addAll(v.getRolesAllowed());
v.getRolesAllowed().forEach(role -> {
if (!rolesToScreens.containsKey(role)) {
rolesToScreens.put(role, new HashSet<>());
}
rolesToScreens.get(role).add(screenNameToScreen.get(screenName));
});
}
});

Map<String, ScreenSourceDto> screenNameToScreen = screenDtos.stream()
.collect(Collectors.toMap(ScreenSourceDto::getName, sd -> sd));

screenToRoles.forEach((screen, roles) -> {
roles.forEach(role -> {
responsibilities.add(new Responsibilities()
.setResponsibilityType(ResponsibilityType.SCREEN)
.setInternalRoleCD(new LOV(role))
.setView(screen)
.setScreens("[\n"
+ " {\n"
+ " \"id\": \"id1\",\n"
+ " \"name\": \"" + screen + "\",\n"
+ " \"text\": \"" + screenNameToScreen.get(screen).getTitle() + "\",\n"
+ " \"url\": \"/screen/" + screen + "\"\n"
+ " }\n"
+ "]")
.setDepartmentId(defaultDepartmentId));
});
});
for (Entry<String, Set<ScreenSourceDto>> entry : rolesToScreens.entrySet()) {
String role = entry.getKey();
Set<ScreenSourceDto> screens = entry.getValue();
responsibilities.add(new Responsibilities()
.setResponsibilityType(ResponsibilityType.SCREEN)
.setInternalRoleCD(new LOV(role))
.setScreens(mapToScreens(screenNameToScreen, screens))
.setDepartmentId(defaultDepartmentId));
}

jpaDao.delete(Responsibilities.class, (root, query, cb) -> cb.and());
jpaDao.saveAll(responsibilities);

}
}

//TODO>>Draft. Refactor
@NonNull
private String mapToScreens(@NonNull Map<String, ScreenSourceDto> screenNameToScreen, @NonNull Set<ScreenSourceDto> screens) {
StringJoiner joiner = new StringJoiner(",");
List<ScreenSourceDto> orderedScreens = screens
.stream()
.sorted(Comparator.comparing(ScreenSourceDto::getOrder))
.collect(Collectors.toList());
for (int i = 0; i < orderedScreens.size(); i++) {
ScreenSourceDto screen = orderedScreens.get(i);
String s = " {\n"
+ " \"id\": \"id" + i + "\",\n"
+ " \"name\": \"" + screen.getName() + "\",\n"
+ " \"text\": \"" + screen.getTitle() + "\",\n"
+ " \"url\": \"/screen/" + screen.getName() + "\",\n"
+ " \"icon\": \"" + screen.getIcon() + "\"\n"
+ " }";
joiner.add(s);
}
String collect = joiner.toString();
return "[\n" + collect + "\n]";
}

protected void loadMetaPreProcess(List<WidgetSourceDTO> widgetDtos,
List<ViewSourceDTO> viewDtos,
List<ScreenSourceDto> screenDtos) {
Expand Down

0 comments on commit dcee856

Please sign in to comment.