From fca51aaf70170a26104be6ec6da56fd73a4063cd Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 5 Apr 2024 13:58:57 +0200 Subject: [PATCH 1/2] Bump version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f17bef96..afe2c4c5 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ fr.igred simple-omero-client - 5.18.0 + 5.19.0-SNAPSHOT jar Simple OMERO Client From 819a320b07ce683f31394a885169023a2634ecfd Mon Sep 17 00:00:00 2001 From: Pierre Pouchin Date: Fri, 5 Apr 2024 10:33:38 +0200 Subject: [PATCH 2/2] Add methods to MapAnnotationWrapper to wrap NamedValue --- .../fr/igred/omero/AnnotatableWrapper.java | 6 +- .../annotations/MapAnnotationWrapper.java | 129 +++++++++++++++++- .../omero/annotations/MapAnnotationTest.java | 50 +++++++ 3 files changed, 179 insertions(+), 6 deletions(-) diff --git a/src/main/java/fr/igred/omero/AnnotatableWrapper.java b/src/main/java/fr/igred/omero/AnnotatableWrapper.java index dccf299d..136a7ab8 100644 --- a/src/main/java/fr/igred/omero/AnnotatableWrapper.java +++ b/src/main/java/fr/igred/omero/AnnotatableWrapper.java @@ -29,7 +29,6 @@ import fr.igred.omero.exception.OMEROServerError; import fr.igred.omero.exception.ServiceException; import fr.igred.omero.repository.GenericRepositoryObjectWrapper.ReplacePolicy; -import omero.constants.metadata.NSCLIENTMAPANNOTATION; import omero.gateway.facility.TablesFacility; import omero.gateway.model.AnnotationData; import omero.gateway.model.DataObject; @@ -39,7 +38,6 @@ import omero.gateway.model.TableData; import omero.gateway.model.TagAnnotationData; import omero.model.IObject; -import omero.model.NamedValue; import omero.model.TagAnnotationI; import omero.sys.ParametersI; @@ -366,9 +364,7 @@ public void addPairKeyValue(Client client, String key, String value) */ public void addKeyValuePair(Client client, String key, String value) throws ServiceException, AccessException, ExecutionException { - List kv = singletonList(new NamedValue(key, value)); - MapAnnotationWrapper pkv = new MapAnnotationWrapper(kv); - pkv.setNameSpace(NSCLIENTMAPANNOTATION.value); + MapAnnotationWrapper pkv = new MapAnnotationWrapper(key, value); link(client, pkv); } diff --git a/src/main/java/fr/igred/omero/annotations/MapAnnotationWrapper.java b/src/main/java/fr/igred/omero/annotations/MapAnnotationWrapper.java index 4d9b0b80..035c6cb1 100644 --- a/src/main/java/fr/igred/omero/annotations/MapAnnotationWrapper.java +++ b/src/main/java/fr/igred/omero/annotations/MapAnnotationWrapper.java @@ -21,7 +21,16 @@ import omero.gateway.model.MapAnnotationData; import omero.model.NamedValue; +import java.util.AbstractMap; +import java.util.Collection; +import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.mapping; +import static java.util.stream.Collectors.toList; /** @@ -30,6 +39,11 @@ */ public class MapAnnotationWrapper extends GenericAnnotationWrapper { + /** + * The name space used to identify MapAnnotations created be the user + */ + public static final String NS_USER_CREATED = MapAnnotationData.NS_CLIENT_CREATED; + /** * Constructor of the MapAnnotationWrapper class. @@ -45,13 +59,44 @@ public MapAnnotationWrapper(MapAnnotationData data) { * Constructor of the MapAnnotationWrapper class. Sets the content of the MapAnnotationData * * @param result List of NamedValue(Key-Value pair). + * + * @deprecated This constructor will be removed in a future version. + *

Use {@link #MapAnnotationWrapper(Collection)} instead. */ + @Deprecated public MapAnnotationWrapper(List result) { super(new MapAnnotationData()); data.setContent(result); } + /** + * Constructor of the MapAnnotationWrapper class. Sets the content of the map annotation. + * + * @param pairs Collection of Key-Value pairs. + */ + public MapAnnotationWrapper(Collection> pairs) { + super(new MapAnnotationData()); + List nv = pairs.stream() + .map(e -> new NamedValue(e.getKey(), e.getValue())) + .collect(toList()); + data.setContent(nv); + } + + + /** + * Constructor of the MapAnnotationWrapper class. Sets the content to a single key-value pair. + *

Initializes the namespace to {@link #NS_USER_CREATED}.

+ * + * @param key The key. + * @param value The value. + */ + public MapAnnotationWrapper(String key, String value) { + this(Collections.singletonList(new AbstractMap.SimpleEntry<>(key, value))); + data.setNameSpace(NS_USER_CREATED); + } + + /** * Constructor of the MapAnnotationWrapper class. */ @@ -60,11 +105,38 @@ public MapAnnotationWrapper() { } + /** + * Converts a {@link NamedValue} to a {@link Entry}. + * + * @param namedValue The {@link NamedValue}. + * + * @return See above. + */ + private static Entry toMapEntry(NamedValue namedValue) { + return new AbstractMap.SimpleEntry<>(namedValue.name, namedValue.value); + } + + + /** + * Converts a {@link Entry} to a {@link NamedValue}. + * + * @param entry The {@link Entry}. + * + * @return See above. + */ + private static NamedValue toNamedValue(Entry entry) { + return new NamedValue(entry.getKey(), entry.getValue()); + } + + /** * Gets the List of NamedValue contained in the map annotation. * * @return MapAnnotationData content. + * + * @deprecated This method will be removed in a future version. */ + @Deprecated @SuppressWarnings("unchecked") public List getContent() { return (List) data.getContent(); @@ -75,13 +147,68 @@ public List getContent() { * Sets the content of the map annotation. * * @param result List of NamedValue(Key-Value pair). + * + * @deprecated This method will be replaced by {@link #setContent(Collection)} in a future version. */ + @Deprecated public void setContent(List result) { - data = new MapAnnotationData(); data.setContent(result); } + /** + * Sets the content of the map annotation. + * + * @param pairs Collection of Key-Value pairs. + */ + public void setContent(Collection> pairs) { + List nv = pairs.stream() + .map(MapAnnotationWrapper::toNamedValue) + .collect(toList()); + data.setContent(nv); + } + + + /** + * Gets the List of Key-Value pairs contained in the map annotation. + * + * @return MapAnnotationData content. + * + * @deprecated This method will be renamed to {@link #getContent()} in a future version. + */ + @Deprecated + @SuppressWarnings("unchecked") + public List> getContentAsEntryList() { + return ((Collection) data.getContent()).stream() + .map(MapAnnotationWrapper::toMapEntry) + .collect(toList()); + } + + + /** + * Gets the List of Key-Value pairs contained in the map annotation as a map. + *

As keys may not be unique, the map contains values as a list.

+ * + * @return See above. + */ + public Map> getContentAsMap() { + return this.getContentAsEntryList() + .stream() + .collect(groupingBy(Entry::getKey, + mapping(Entry::getValue, toList()))); + } + + + /** + * Gets the content of the map annotation as a string. + * + * @return See above. + */ + public String getContentAsString() { + return data.getContentAsString(); + } + + /** * @return the {@link MapAnnotationData} contained. * diff --git a/src/test/java/fr/igred/omero/annotations/MapAnnotationTest.java b/src/test/java/fr/igred/omero/annotations/MapAnnotationTest.java index df328610..7db2e39c 100644 --- a/src/test/java/fr/igred/omero/annotations/MapAnnotationTest.java +++ b/src/test/java/fr/igred/omero/annotations/MapAnnotationTest.java @@ -19,9 +19,15 @@ import fr.igred.omero.UserTest; +import omero.model.NamedValue; import org.junit.jupiter.api.Test; +import java.util.AbstractMap.SimpleEntry; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -61,4 +67,48 @@ void testGetSingleMapAnnotationByKeyAndValue() throws Exception { assertEquals(1, maps.size()); } + + @Test + void testGetContentAsMap() throws Exception { + MapAnnotationWrapper map = client.getMapAnnotation(4L); + Map> content = map.getContentAsMap(); + assertEquals("testValue1", content.get("testKey1").get(0)); + assertEquals("20", content.get("testKey2").get(0)); + } + + + @Test + void testGetContentAsString() throws Exception { + MapAnnotationWrapper map = client.getMapAnnotation(4L); + + String expected = "testKey1=testValue1;testKey2=20"; + assertEquals(expected, map.getContentAsString()); + } + + + @Test + void testSetContent() throws Exception { + MapAnnotationWrapper map = client.getMapAnnotation(4L); + List content = map.getContent(); + + Collection> pairs = new ArrayList<>(2); + pairs.add(new SimpleEntry<>("testKey2", "testValue2")); + pairs.add(new SimpleEntry<>("testKey3", "testValue3")); + + map.setContent(pairs); + map.saveAndUpdate(client); + + int size = map.getContent().size(); + String expected = "testKey2=testValue2;testKey3=testValue3"; + String actual = map.getContentAsString(); + + map.setContent(content); + map.saveAndUpdate(client); + + assertEquals(2, size); + assertEquals(expected, actual); + assertEquals("testKey1", map.getContent().get(0).name); + assertEquals("testValue1", map.getContent().get(0).value); + } + }