Skip to content

Commit

Permalink
Add method to create kv-pair
Browse files Browse the repository at this point in the history
  • Loading branch information
ppouchin committed Nov 15, 2024
1 parent c556360 commit c18bf62
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/main/java/fr/igred/ij/plugin/OMEROMacroExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/fr/igred/ij/plugin/OMEROExtensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c18bf62

Please sign in to comment.