Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package it.inaf.ia2.gms.service;
import it.inaf.ia2.gms.persistence.GroupsDAO;
import it.inaf.ia2.gms.persistence.model.GroupEntity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Utility class for retrieving the complete names (including parents) from a
* set of group paths.
*/
@Service
public class GroupNameService {
@Autowired
private GroupsDAO groupsDAO;
/**
* @param groupsIdPath map having group id as keys and group paths as values
* @return map having group id as keys and group names as values
*/
public Map<String, List<String>> getNames(List<Map.Entry<String, String>> groupsIdPath) {
Set<String> allIdentifiers = new HashSet<>();
for (Map.Entry<String, String> entry : groupsIdPath) {
allIdentifiers.addAll(getIdentifiers(entry.getValue()));
}
Map<String, String> groupSingleNamesMap = getGroupSingleNamesMap(allIdentifiers);
Map<String, List<String>> groupCompleteNamesMap = new HashMap<>();
for (Map.Entry<String, String> entry : groupsIdPath) {
List<String> groupCompleteName = getGroupCompleteName(groupSingleNamesMap, entry.getValue());
groupCompleteNamesMap.put(entry.getKey(), groupCompleteName);
}
return groupCompleteNamesMap;
}
private Map<String, String> getGroupSingleNamesMap(Set<String> allIdentifiers) {
Map<String, String> groupNamesMap = new HashMap<>();
for (GroupEntity group : groupsDAO.findGroupsByIds(allIdentifiers)) {
groupNamesMap.put(group.getId(), group.getName());
}
return groupNamesMap;
}
private List<String> getGroupCompleteName(Map<String, String> groupNamesMap, String groupPath) {
List<String> names = new ArrayList<>();
if (groupPath.isEmpty()) {
names.add("Root");
} else {
List<String> identifiers = getIdentifiers(groupPath);
for (String groupId : identifiers) {
names.add(groupNamesMap.get(groupId));
}
}
return names;
}
/**
* Returns the list of all identifiers including parent ones.
*/
private List<String> getIdentifiers(String groupPath) {
List<String> identifiers = new ArrayList<>();
if (!groupPath.isEmpty()) {
for (String id : groupPath.split(Pattern.quote("."))) {
identifiers.add(id);
}
}
return identifiers;
}
}