1
1
/*
2
- * Copyright (C) 2001-2016 Food and Agriculture Organization of the
2
+ * Copyright (C) 2001-2024 Food and Agriculture Organization of the
3
3
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
4
4
* and United Nations Environment Programme (UNEP)
5
5
*
50
50
* Manage objects selection for a user session.
51
51
*/
52
52
public class SelectionManager {
53
-
54
53
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" ;
56
56
// used to limit select all if get system setting maxrecords fails or contains value we can't parse
57
57
public static final int DEFAULT_MAXHITS = 1000 ;
58
58
public static final String ADD_ALL_SELECTED = "add-all" ;
59
59
public static final String REMOVE_ALL_SELECTED = "remove-all" ;
60
60
public static final String ADD_SELECTED = "add" ;
61
61
public static final String REMOVE_SELECTED = "remove" ;
62
62
public static final String CLEAR_ADD_SELECTED = "clear-add" ;
63
- private Hashtable <String , Set <String >> selections = null ;
63
+ private Hashtable <String , Set <String >> selections ;
64
64
65
65
private SelectionManager () {
66
- selections = new Hashtable <String , Set < String > >(0 );
66
+ selections = new Hashtable <>(0 );
67
67
68
68
Set <String > MDSelection = Collections
69
- .synchronizedSet (new HashSet <String >(0 ));
69
+ .synchronizedSet (new HashSet <>(0 ));
70
70
selections .put (SELECTION_METADATA , MDSelection );
71
71
}
72
72
73
73
74
74
public Map <String , Integer > getSelectionsAndSize () {
75
75
return selections .entrySet ().stream ().collect (Collectors .toMap (
76
- e -> e . getKey () ,
76
+ Map . Entry :: getKey ,
77
77
e -> e .getValue ().size ()
78
78
));
79
79
}
@@ -182,7 +182,7 @@ public int updateSelection(String type,
182
182
// Get the selection manager or create it
183
183
Set <String > selection = this .getSelection (type );
184
184
if (selection == null ) {
185
- selection = Collections .synchronizedSet (new HashSet <String >());
185
+ selection = Collections .synchronizedSet (new HashSet <>());
186
186
this .selections .put (type , selection );
187
187
}
188
188
@@ -191,30 +191,21 @@ public int updateSelection(String type,
191
191
this .selectAll (type , context , session );
192
192
else if (selected .equals (REMOVE_ALL_SELECTED ))
193
193
this .close (type );
194
- else if (selected .equals (ADD_SELECTED ) && listOfIdentifiers .size () > 0 ) {
194
+ else if (selected .equals (ADD_SELECTED ) && ! listOfIdentifiers .isEmpty () ) {
195
195
// 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 ()) {
200
198
for (String paramid : listOfIdentifiers ) {
201
199
selection .remove (paramid );
202
200
}
203
- } else if (selected .equals (CLEAR_ADD_SELECTED ) && listOfIdentifiers .size () > 0 ) {
201
+ } else if (selected .equals (CLEAR_ADD_SELECTED ) && ! listOfIdentifiers .isEmpty () ) {
204
202
this .close (type );
205
- for (String paramid : listOfIdentifiers ) {
206
- selection .add (paramid );
207
- }
203
+ selection .addAll (listOfIdentifiers );
208
204
}
209
205
}
210
206
211
207
// 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 );
218
209
219
210
return selection .size ();
220
211
}
@@ -240,14 +231,12 @@ public void selectAll(String type, ServiceContext context, UserSession session)
240
231
241
232
if (StringUtils .isNotEmpty (type )) {
242
233
JsonNode request = (JsonNode ) session .getProperty (Geonet .Session .SEARCH_REQUEST + type );
243
- if (request == null ) {
244
- return ;
245
- } else {
234
+ if (request != null ) {
246
235
final SearchResponse searchResponse ;
247
236
try {
248
237
EsSearchManager searchManager = context .getBean (EsSearchManager .class );
249
238
searchResponse = searchManager .query (request .get ("query" ), FIELDLIST_UUID , 0 , maxhits );
250
- List <String > uuidList = new ArrayList ();
239
+ List <String > uuidList = new ArrayList <> ();
251
240
for (SearchHit h : Arrays .asList (searchResponse .getHits ().getHits ())) {
252
241
uuidList .add ((String ) h .getSourceAsMap ().get (Geonet .IndexFieldNames .UUID ));
253
242
}
@@ -291,7 +280,7 @@ public Set<String> getSelection(String type) {
291
280
Set <String > sel = selections .get (type );
292
281
if (sel == null ) {
293
282
Set <String > MDSelection = Collections
294
- .synchronizedSet (new HashSet <String >(0 ));
283
+ .synchronizedSet (new HashSet <>(0 ));
295
284
selections .put (type , MDSelection );
296
285
}
297
286
return selections .get (type );
0 commit comments