Skip to content

Commit fbf953a

Browse files
committed
Batch selection is not in sync between search results and record view.
Fixes #8295
1 parent 7c57e12 commit fbf953a

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

core/src/main/java/org/fao/geonet/kernel/SelectionManager.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2001-2016 Food and Agriculture Organization of the
2+
* Copyright (C) 2001-2024 Food and Agriculture Organization of the
33
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
44
* and United Nations Environment Programme (UNEP)
55
*
@@ -50,30 +50,30 @@
5050
* Manage objects selection for a user session.
5151
*/
5252
public class SelectionManager {
53-
5453
public static final String SELECTION_METADATA = "metadata";
55-
public static final String SELECTION_BUCKET = "bucket";
54+
// Bucket name used in the search UI to store the selected the metadata
55+
public static final String SELECTION_BUCKET = "s101";
5656
// used to limit select all if get system setting maxrecords fails or contains value we can't parse
5757
public static final int DEFAULT_MAXHITS = 1000;
5858
public static final String ADD_ALL_SELECTED = "add-all";
5959
public static final String REMOVE_ALL_SELECTED = "remove-all";
6060
public static final String ADD_SELECTED = "add";
6161
public static final String REMOVE_SELECTED = "remove";
6262
public static final String CLEAR_ADD_SELECTED = "clear-add";
63-
private Hashtable<String, Set<String>> selections = null;
63+
private Hashtable<String, Set<String>> selections;
6464

6565
private SelectionManager() {
66-
selections = new Hashtable<String, Set<String>>(0);
66+
selections = new Hashtable<>(0);
6767

6868
Set<String> MDSelection = Collections
69-
.synchronizedSet(new HashSet<String>(0));
69+
.synchronizedSet(new HashSet<>(0));
7070
selections.put(SELECTION_METADATA, MDSelection);
7171
}
7272

7373

7474
public Map<String, Integer> getSelectionsAndSize() {
7575
return selections.entrySet().stream().collect(Collectors.toMap(
76-
e -> e.getKey(),
76+
Map.Entry::getKey,
7777
e -> e.getValue().size()
7878
));
7979
}
@@ -182,7 +182,7 @@ public int updateSelection(String type,
182182
// Get the selection manager or create it
183183
Set<String> selection = this.getSelection(type);
184184
if (selection == null) {
185-
selection = Collections.synchronizedSet(new HashSet<String>());
185+
selection = Collections.synchronizedSet(new HashSet<>());
186186
this.selections.put(type, selection);
187187
}
188188

@@ -191,30 +191,21 @@ public int updateSelection(String type,
191191
this.selectAll(type, context, session);
192192
else if (selected.equals(REMOVE_ALL_SELECTED))
193193
this.close(type);
194-
else if (selected.equals(ADD_SELECTED) && listOfIdentifiers.size() > 0) {
194+
else if (selected.equals(ADD_SELECTED) && !listOfIdentifiers.isEmpty()) {
195195
// TODO ? Should we check that the element exist first ?
196-
for (String paramid : listOfIdentifiers) {
197-
selection.add(paramid);
198-
}
199-
} else if (selected.equals(REMOVE_SELECTED) && listOfIdentifiers.size() > 0) {
196+
selection.addAll(listOfIdentifiers);
197+
} else if (selected.equals(REMOVE_SELECTED) && !listOfIdentifiers.isEmpty()) {
200198
for (String paramid : listOfIdentifiers) {
201199
selection.remove(paramid);
202200
}
203-
} else if (selected.equals(CLEAR_ADD_SELECTED) && listOfIdentifiers.size() > 0) {
201+
} else if (selected.equals(CLEAR_ADD_SELECTED) && !listOfIdentifiers.isEmpty()) {
204202
this.close(type);
205-
for (String paramid : listOfIdentifiers) {
206-
selection.add(paramid);
207-
}
203+
selection.addAll(listOfIdentifiers);
208204
}
209205
}
210206

211207
// Remove empty/null element from the selection
212-
Iterator<String> iter = selection.iterator();
213-
while (iter.hasNext()) {
214-
Object element = iter.next();
215-
if (element == null)
216-
iter.remove();
217-
}
208+
selection.removeIf(Objects::isNull);
218209

219210
return selection.size();
220211
}
@@ -240,14 +231,12 @@ public void selectAll(String type, ServiceContext context, UserSession session)
240231

241232
if (StringUtils.isNotEmpty(type)) {
242233
JsonNode request = (JsonNode) session.getProperty(Geonet.Session.SEARCH_REQUEST + type);
243-
if (request == null) {
244-
return;
245-
} else {
234+
if (request != null) {
246235
final SearchResponse searchResponse;
247236
try {
248237
EsSearchManager searchManager = context.getBean(EsSearchManager.class);
249238
searchResponse = searchManager.query(request.get("query"), FIELDLIST_UUID, 0, maxhits);
250-
List<String> uuidList = new ArrayList();
239+
List<String> uuidList = new ArrayList<>();
251240
for (SearchHit h : Arrays.asList(searchResponse.getHits().getHits())) {
252241
uuidList.add((String) h.getSourceAsMap().get(Geonet.IndexFieldNames.UUID));
253242
}
@@ -291,7 +280,7 @@ public Set<String> getSelection(String type) {
291280
Set<String> sel = selections.get(type);
292281
if (sel == null) {
293282
Set<String> MDSelection = Collections
294-
.synchronizedSet(new HashSet<String>(0));
283+
.synchronizedSet(new HashSet<>(0));
295284
selections.put(type, MDSelection);
296285
}
297286
return selections.get(type);

services/src/main/java/org/fao/geonet/api/es/EsHTTPProxy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ private static boolean hasOperation(ObjectNode doc, ReservedGroup group, Reserve
301301
@ResponseStatus(value = HttpStatus.OK)
302302
@ResponseBody
303303
public void search(
304-
@RequestParam(defaultValue = SelectionManager.SELECTION_METADATA)
305-
String bucket,
304+
@RequestParam(defaultValue = SelectionManager.SELECTION_BUCKET)
305+
String bucket,
306306
@Parameter(description = "Type of related resource. If none, no associated resource returned.",
307307
required = false
308308
)
@@ -391,8 +391,8 @@ public void msearch(
391391
@PreAuthorize("hasAuthority('Administrator')")
392392
@ResponseBody
393393
public void call(
394-
@RequestParam(defaultValue = SelectionManager.SELECTION_METADATA)
395-
String bucket,
394+
@RequestParam(defaultValue = SelectionManager.SELECTION_BUCKET)
395+
String bucket,
396396
@Parameter(description = "'_search' for search service.")
397397
@PathVariable String endPoint,
398398
@Parameter(hidden = true)

0 commit comments

Comments
 (0)