Newer
Older
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;
public List<String> getGroupsNamesFromIdentifiers(Set<String> groupIdentifiers) {
return getGroupsNames(groupsDAO.findGroupsByIds(groupIdentifiers));
}
public String getGroupCompleteName(GroupEntity group) {
return getGroupsNames(Collections.singletonList(group)).get(0);
}
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
82
83
84
85
86
87
88
89
90
91
92
/**
* Returns the list of the group complete names, given a list of GroupEntity
* objects.
*/
public List<String> getGroupsNames(List<GroupEntity> groups) {
// We need to return the complete group name, so it is necessary to load
// all the parents too.
Map<String, String> idNameMap = new HashMap<>();
Set<String> allIdentifiers = getAllIdentifiers(groups);
for (GroupEntity group : groupsDAO.findGroupsByIds(allIdentifiers)) {
idNameMap.put(group.getId(), group.getName());
}
List<String> names = new ArrayList<>();
for (GroupEntity group : groups) {
names.add(getGroupCompleteName(group, idNameMap));
}
return names;
}
private Set<String> getAllIdentifiers(List<GroupEntity> groups) {
Set<String> allIdentifiers = new HashSet<>();
for (GroupEntity group : groups) {
if (!"".equals(group.getPath())) {
String[] ids = group.getPath().split("\\.");
for (String id : ids) {
allIdentifiers.add(id);
}
}
}
return allIdentifiers;
}
private String getGroupCompleteName(GroupEntity group, Map<String, String> idNameMap) {
if ("ROOT".equals(group.getId())) {
return group.getName();
}
List<String> names = new ArrayList<>();
for (String groupId : group.getPath().split("\\.")) {
String groupName = idNameMap.get(groupId);
// Dot inside names is considered a special character (because it is
// used to separate the group from its parents), so we use a
// backslash to escape it (client apps need to be aware of this).
groupName = groupName.replace("\\.", "\\\\.");
names.add(groupName);
}
return String.join(".", names);
}
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @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;
}
}