Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch selection is not in sync between search results and record view. #8492

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions core/src/main/java/org/fao/geonet/kernel/SelectionManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2016 Food and Agriculture Organization of the
* Copyright (C) 2001-2024 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -51,30 +51,30 @@
* Manage objects selection for a user session.
*/
public class SelectionManager {

public static final String SELECTION_METADATA = "metadata";
public static final String SELECTION_BUCKET = "bucket";
// Bucket name used in the search UI to store the selected the metadata
public static final String SELECTION_BUCKET = "s101";
// used to limit select all if get system setting maxrecords fails or contains value we can't parse
public static final int DEFAULT_MAXHITS = 1000;
public static final String ADD_ALL_SELECTED = "add-all";
public static final String REMOVE_ALL_SELECTED = "remove-all";
public static final String ADD_SELECTED = "add";
public static final String REMOVE_SELECTED = "remove";
public static final String CLEAR_ADD_SELECTED = "clear-add";
private Hashtable<String, Set<String>> selections = null;
private Hashtable<String, Set<String>> selections;

private SelectionManager() {
selections = new Hashtable<String, Set<String>>(0);
selections = new Hashtable<>(0);

Set<String> MDSelection = Collections
.synchronizedSet(new HashSet<String>(0));
.synchronizedSet(new HashSet<>(0));
selections.put(SELECTION_METADATA, MDSelection);
}


public Map<String, Integer> getSelectionsAndSize() {
return selections.entrySet().stream().collect(Collectors.toMap(
e -> e.getKey(),
Map.Entry::getKey,
e -> e.getValue().size()
));
}
Expand Down Expand Up @@ -183,7 +183,7 @@ public int updateSelection(String type,
// Get the selection manager or create it
Set<String> selection = this.getSelection(type);
if (selection == null) {
selection = Collections.synchronizedSet(new HashSet<String>());
selection = Collections.synchronizedSet(new HashSet<>());
this.selections.put(type, selection);
}

Expand All @@ -192,30 +192,21 @@ public int updateSelection(String type,
this.selectAll(type, context, session);
else if (selected.equals(REMOVE_ALL_SELECTED))
this.close(type);
else if (selected.equals(ADD_SELECTED) && listOfIdentifiers.size() > 0) {
else if (selected.equals(ADD_SELECTED) && !listOfIdentifiers.isEmpty()) {
// TODO ? Should we check that the element exist first ?
for (String paramid : listOfIdentifiers) {
selection.add(paramid);
}
} else if (selected.equals(REMOVE_SELECTED) && listOfIdentifiers.size() > 0) {
selection.addAll(listOfIdentifiers);
} else if (selected.equals(REMOVE_SELECTED) && !listOfIdentifiers.isEmpty()) {
for (String paramid : listOfIdentifiers) {
selection.remove(paramid);
}
} else if (selected.equals(CLEAR_ADD_SELECTED) && listOfIdentifiers.size() > 0) {
} else if (selected.equals(CLEAR_ADD_SELECTED) && !listOfIdentifiers.isEmpty()) {
this.close(type);
for (String paramid : listOfIdentifiers) {
selection.add(paramid);
}
selection.addAll(listOfIdentifiers);
}
}

// Remove empty/null element from the selection
Iterator<String> iter = selection.iterator();
while (iter.hasNext()) {
Object element = iter.next();
if (element == null)
iter.remove();
}
selection.removeIf(Objects::isNull);

return selection.size();
}
Expand All @@ -241,14 +232,12 @@ public void selectAll(String type, ServiceContext context, UserSession session)

if (StringUtils.isNotEmpty(type)) {
JsonNode request = (JsonNode) session.getProperty(Geonet.Session.SEARCH_REQUEST + type);
if (request == null) {
return;
} else {
if (request != null) {
final SearchResponse searchResponse;
try {
EsSearchManager searchManager = context.getBean(EsSearchManager.class);
searchResponse = searchManager.query(request.get("query"), FIELDLIST_UUID, 0, maxhits);
List<String> uuidList = new ArrayList();
List<String> uuidList = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper();
for (Hit h : (List<Hit>) searchResponse.hits().hits()) {
uuidList.add((String) objectMapper.convertValue(h.source(), Map.class).get(Geonet.IndexFieldNames.UUID));
Expand Down Expand Up @@ -293,7 +282,7 @@ public Set<String> getSelection(String type) {
Set<String> sel = selections.get(type);
if (sel == null) {
Set<String> MDSelection = Collections
.synchronizedSet(new HashSet<String>(0));
.synchronizedSet(new HashSet<>(0));
selections.put(type, MDSelection);
}
return selections.get(type);
Expand Down
4 changes: 2 additions & 2 deletions services/src/main/java/org/fao/geonet/api/es/EsHTTPProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private static boolean hasOperation(ObjectNode doc, ReservedGroup group, Reserve
@ResponseStatus(value = HttpStatus.OK)
@ResponseBody
public void search(
@RequestParam(defaultValue = SelectionManager.SELECTION_METADATA)
@RequestParam(defaultValue = SelectionManager.SELECTION_BUCKET)
String bucket,
@Parameter(description = "Type of related resource. If none, no associated resource returned.",
required = false
Expand Down Expand Up @@ -387,7 +387,7 @@ public void msearch(
@PreAuthorize("hasAuthority('Administrator')")
@ResponseBody
public void call(
@RequestParam(defaultValue = SelectionManager.SELECTION_METADATA)
@RequestParam(defaultValue = SelectionManager.SELECTION_BUCKET)
String bucket,
@Parameter(description = "'_search' for search service.")
@PathVariable String endPoint,
Expand Down
Loading