use of de.metas.ui.web.menu.MenuTree in project metasfresh-webui-api by metasfresh.
the class JSONDocumentReferencesGroupList method of.
public static //
JSONDocumentReferencesGroupList of(//
final Collection<DocumentReference> documentReferences, //
final MenuTree menuTree, //
final String othersMenuCaption, //
final JSONOptions jsonOpts) {
if (documentReferences.isEmpty()) {
return EMPTY;
}
final Map<String, JSONDocumentReferencesGroupBuilder> groupsBuilders = new HashMap<>();
final String othersGroupId = "_others_" + UUID.randomUUID();
for (final DocumentReference documentReference : documentReferences) {
final JSONDocumentReference jsonDocumentReference = JSONDocumentReference.of(documentReference, jsonOpts);
if (jsonDocumentReference == null) {
continue;
}
final MenuNode topLevelMenuGroup = menuTree.getTopLevelMenuGroupOrNull(documentReference.getWindowId());
final String topLevelMenuGroupId = topLevelMenuGroup != null ? topLevelMenuGroup.getId() : othersGroupId;
final JSONDocumentReferencesGroupBuilder groupBuilder = groupsBuilders.computeIfAbsent(topLevelMenuGroupId, k -> {
final boolean isMiscGroup = topLevelMenuGroup == null;
final String caption = topLevelMenuGroup != null ? topLevelMenuGroup.getCaption() : othersMenuCaption;
return JSONDocumentReferencesGroup.builder().caption(caption).isMiscGroup(isMiscGroup);
});
groupBuilder.reference(jsonDocumentReference);
}
// Sort by Caption, but keep the "misc group" last
Comparator<JSONDocumentReferencesGroup> sorting = Comparator.<JSONDocumentReferencesGroup>comparingInt(group -> group.isMiscGroup() ? 1 : 0).thenComparing(JSONDocumentReferencesGroup::getCaption);
final List<JSONDocumentReferencesGroup> groups = groupsBuilders.values().stream().map(groupBuilder -> groupBuilder.build()).filter(group -> !group.isEmpty()).sorted(sorting).collect(ImmutableList.toImmutableList());
return new JSONDocumentReferencesGroupList(groups);
}
Aggregations