Revision 57754
Added by Michele Artini over 4 years ago
OrganizationController.java | ||
---|---|---|
3 | 3 |
import java.util.Arrays; |
4 | 4 |
import java.util.Collection; |
5 | 5 |
import java.util.HashMap; |
6 |
import java.util.LinkedHashSet; |
|
7 | 6 |
import java.util.List; |
8 | 7 |
import java.util.Map; |
9 | 8 |
import java.util.Set; |
10 |
import java.util.TreeMap;
|
|
9 |
import java.util.TreeSet;
|
|
11 | 10 |
import java.util.stream.Collectors; |
12 | 11 |
|
13 | 12 |
import org.apache.commons.lang3.StringUtils; |
... | ... | |
166 | 165 |
@RequestMapping(value = "/conflicts/all", method = RequestMethod.GET) |
167 | 166 |
public Collection<Set<OrganizationSimpleView>> findWarningsById(final Authentication authentication) { |
168 | 167 |
|
169 |
final Map<String, Set<OrganizationSimpleView>> res = new TreeMap<>(); |
|
168 |
int count = 0; |
|
169 |
final Map<Integer, Set<OrganizationSimpleView>> groups = new HashMap<>(); |
|
170 | 170 |
|
171 |
final Map<String, String> roots = new HashMap<>(); |
|
172 |
|
|
173 | 171 |
if (UserInfo.isSuperAdmin(authentication)) { |
174 | 172 |
for (final ConflictView w : conflictViewRepository.findAll()) { |
175 |
final String root = findWarningRoot(w, roots); |
|
176 |
if (!res.containsKey(root)) { |
|
177 |
res.put(root, new LinkedHashSet<OrganizationSimpleView>()); |
|
178 |
res.get(root).add(new OrganizationSimpleView(w.getId1(), w.getName1(), w.getType1(), w.getCity1(), w.getCountry1(), new String[] {})); |
|
173 |
final List<Integer> list = findExistingGroupsForRel(w, groups); |
|
174 |
if (list.size() == 0) { |
|
175 |
groups.put(count, new TreeSet<>()); |
|
176 |
addToGroup(groups, count, w); |
|
177 |
count++; |
|
178 |
} else if (list.size() == 1) { |
|
179 |
addToGroup(groups, list.get(0), w); |
|
180 |
} else { |
|
181 |
final int newPos = count; |
|
182 |
groups.put(count, new TreeSet<>()); |
|
183 |
list.forEach(i -> groups.get(newPos).addAll(groups.get(i))); |
|
184 |
list.forEach(i -> groups.remove(i)); |
|
185 |
addToGroup(groups, newPos, w); |
|
186 |
count++; |
|
179 | 187 |
} |
180 |
res.get(root).add(new OrganizationSimpleView(w.getId2(), w.getName2(), w.getType2(), w.getCity2(), w.getCountry2(), new String[] {})); |
|
181 | 188 |
} |
182 |
return res.values(); |
|
189 |
|
|
190 |
return groups.values(); |
|
183 | 191 |
} else { |
184 | 192 |
throw new RuntimeException("User not authorized"); |
185 | 193 |
} |
186 | 194 |
} |
187 | 195 |
|
188 |
private String findWarningRoot(final ConflictView w, final Map<String, String> roots) { |
|
189 |
if (roots.containsKey(w.getId1())) { |
|
190 |
return roots.get(w.getId1()); |
|
191 |
} else if (roots.containsKey(w.getId2())) { |
|
192 |
return roots.get(w.getId2()); |
|
193 |
} else { |
|
194 |
// id1 is the new root |
|
195 |
roots.put(w.getId1(), w.getId1()); |
|
196 |
roots.put(w.getId2(), w.getId1()); |
|
197 |
return w.getId1(); |
|
198 |
} |
|
196 |
private List<Integer> findExistingGroupsForRel(final ConflictView w, final Map<Integer, Set<OrganizationSimpleView>> groups) { |
|
197 |
return groups.entrySet() |
|
198 |
.stream() |
|
199 |
.filter(e -> { |
|
200 |
return e.getValue().stream().filter(o -> o.getId().equals(w.getId1()) || o.getId().equals(w.getId2())).findFirst().isPresent(); |
|
201 |
}) |
|
202 |
.map(e -> e.getKey()) |
|
203 |
.distinct() |
|
204 |
.collect(Collectors.toList()); |
|
205 |
} |
|
199 | 206 |
|
207 |
private void addToGroup(final Map<Integer, Set<OrganizationSimpleView>> groups, final int count, final ConflictView w) { |
|
208 |
groups.get(count).add(new OrganizationSimpleView(w.getId1(), w.getName1(), w.getType1(), w.getCity1(), w.getCountry1(), new String[] {})); |
|
209 |
groups.get(count).add(new OrganizationSimpleView(w.getId2(), w.getName2(), w.getType2(), w.getCity2(), w.getCountry2(), new String[] {})); |
|
200 | 210 |
} |
201 | 211 |
|
202 | 212 |
@RequestMapping(value = "/enrichments", method = RequestMethod.POST) |
Also available in: Unified diff
bug fix