diff --git a/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java b/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java index b7433db..f3536d9 100644 --- a/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java +++ b/src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java @@ -98,6 +98,7 @@ public class OMEROMacroExtension implements PlugIn, MacroExtension { newDescriptor("createDataset", this, ARG_STRING, ARG_STRING, ARG_NUMBER + ARG_OPTIONAL), newDescriptor("createProject", this, ARG_STRING, ARG_STRING), newDescriptor("createTag", this, ARG_STRING, ARG_STRING), + newDescriptor("createKeyValuePair", this, ARG_STRING, ARG_STRING), newDescriptor("link", this, ARG_STRING, ARG_NUMBER, ARG_STRING, ARG_NUMBER), newDescriptor("unlink", this, ARG_STRING, ARG_NUMBER, ARG_STRING, ARG_NUMBER), newDescriptor("addFile", this, ARG_STRING, ARG_NUMBER, ARG_STRING), @@ -922,6 +923,27 @@ public long createTag(String name, String description) { } + /** + * Creates a key-value pair on OMERO. + * + * @param key The key. + * @param value The value. + * + * @return The kv-pair ID. + */ + public long createKeyValuePair(String key, String value) { + long id = -1; + try { + MapAnnotationWrapper pair = new MapAnnotationWrapper(key, value); + pair.saveAndUpdate(client); + id = pair.getId(); + } catch (ServiceException | AccessException | ExecutionException e) { + IJ.error("Could not create kv-pair: " + e.getMessage()); + } + return id; + } + + /** * Creates a project on OMERO. * @@ -1546,7 +1568,6 @@ public String getValue(String type, long id, String key, String defaultValue) { * @return The number of ROIs that were deleted. */ public int removeROIs(long id) { - int removed = 0; try { ImageWrapper image = client.getImage(id); @@ -1678,6 +1699,11 @@ public String handleExtension(String name, Object[] args) { results = String.valueOf(tagId); break; + case "createKeyValuePair": + long pairId = createKeyValuePair((String) args[0], (String) args[1]); + results = String.valueOf(pairId); + break; + case "addToTable": tableName = (String) args[0]; String resultsName = (String) args[1]; diff --git a/src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java b/src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java index 4c96e16..7bf2418 100644 --- a/src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java +++ b/src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java @@ -251,6 +251,20 @@ void testCreateAndLinkTag() { } + @Test + void testCreateAndLinkKVPair() { + final double projectId = 2; + Object[] args = {"TestKey", "TestValue"}; + String result = ext.handleExtension("createKeyValuePair", args); + Double id = Double.parseDouble(result); + Object[] args2 = {"kv-pair", id, "project", projectId}; + ext.handleExtension("link", args2); + Object[] args3 = {"kv-pair", id}; + ext.handleExtension("delete", args3); + assertNotNull(id); + } + + @ParameterizedTest @CsvSource(delimiter = ';', value = {"project;2.0;tag;1.0", "tag;1.0;dataset;3.0",