Skip to content

Commit

Permalink
Merge pull request #2485 from opencb/TASK-6568
Browse files Browse the repository at this point in the history
TASK-6568 - missing tagsAction in notes/update
  • Loading branch information
pfurio authored Aug 5, 2024
2 parents cd4faac + 2015f69 commit 00cc8a3
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.opencb.opencga.app.cli.main.options.OrganizationsCommandOptions;
import org.opencb.opencga.catalog.exceptions.CatalogAuthenticationException;
import org.opencb.opencga.catalog.utils.ParamUtils.AddRemoveAction;
import org.opencb.opencga.catalog.utils.ParamUtils.BasicUpdateAction;
import org.opencb.opencga.catalog.utils.ParamUtils.UpdateAction;
import org.opencb.opencga.client.exceptions.ClientException;
import org.opencb.opencga.core.common.JacksonUtils;
Expand Down Expand Up @@ -214,6 +215,7 @@ private RestResponse<Note> updateNotes() throws Exception {
ObjectMap queryParams = new ObjectMap();
queryParams.putIfNotEmpty("include", commandOptions.include);
queryParams.putIfNotEmpty("exclude", commandOptions.exclude);
queryParams.putIfNotNull("tagsAction", commandOptions.tagsAction);
queryParams.putIfNotNull("includeResult", commandOptions.includeResult);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ private RestResponse<Note> updateNotes() throws Exception {
ObjectMap queryParams = new ObjectMap();
queryParams.putIfNotEmpty("include", commandOptions.include);
queryParams.putIfNotEmpty("exclude", commandOptions.exclude);
queryParams.putIfNotNull("tagsAction", commandOptions.tagsAction);
queryParams.putIfNotNull("includeResult", commandOptions.includeResult);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ public class UpdateNotesCommandOptions {
@Parameter(names = {"--id"}, description = "Note unique identifier.", required = true, arity = 1)
public String id;

@Parameter(names = {"--tags-action"}, description = "Action to be performed if the array of tags is being updated.", required = false, arity = 1)
public String tagsAction = "ADD";

@Parameter(names = {"--include-result"}, description = "Flag indicating to include the created or updated document result in the response", required = false, help = true, arity = 0)
public boolean includeResult = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,9 @@ public class UpdateNotesCommandOptions {
@Parameter(names = {"--id"}, description = "Note unique identifier.", required = true, arity = 1)
public String id;

@Parameter(names = {"--tags-action"}, description = "Action to be performed if the array of tags is being updated.", required = false, arity = 1)
public String tagsAction = "ADD";

@Parameter(names = {"--include-result"}, description = "Flag indicating to include the created or updated document result in the response", required = false, help = true, arity = 0)
public boolean includeResult = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ private UpdateDocument parseAndValidateUpdateParams(Document note, ObjectMap par
filterStringListParams(parameters, document.getSet(), tagsParams);
break;
case REMOVE:
filterStringListParams(parameters, document.getPull(), tagsParams);
filterStringListParams(parameters, document.getPullAll(), tagsParams);
break;
case ADD:
filterStringListParams(parameters, document.getAddToSet(), tagsParams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,7 @@ private OpenCGAResult<Note> update(long noteUid, NoteUpdateParams noteUpdatePara
// Write who's performing the update
updateMap.put(NoteDBAdaptor.QueryParams.USER_ID.key(), tokenPayload.getUserId());

OpenCGAResult<Note> update = catalogDBAdaptorFactory.getCatalogNoteDBAdaptor(organizationId).update(noteUid, updateMap,
QueryOptions.empty());
OpenCGAResult<Note> update = catalogDBAdaptorFactory.getCatalogNoteDBAdaptor(organizationId).update(noteUid, updateMap, options);
if (options.getBoolean(ParamConstants.INCLUDE_RESULT_PARAM)) {
// Fetch updated note
OpenCGAResult<Note> result = catalogDBAdaptorFactory.getCatalogNoteDBAdaptor(organizationId).get(noteUid, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.opencb.opencga.catalog.db.api.NoteDBAdaptor;
import org.opencb.opencga.catalog.exceptions.CatalogAuthorizationException;
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.utils.Constants;
import org.opencb.opencga.catalog.utils.ParamUtils;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.models.notes.Note;
import org.opencb.opencga.core.models.notes.NoteCreateParams;
import org.opencb.opencga.core.models.notes.NoteUpdateParams;
Expand All @@ -16,6 +19,8 @@
import org.opencb.opencga.core.testclassification.duration.MediumTests;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -190,6 +195,45 @@ public void updateStudyNoteTest() throws CatalogException {
catalogManager.getNotesManager().updateStudyNote(studyFqn, note.getId(), noteUpdateParams, INCLUDE_RESULT, normalToken1);
}

@Test
public void noteTagsUpdateTest() throws CatalogException {
NoteCreateParams noteCreateParams = new NoteCreateParams()
.setId("note1")
.setVisibility(Note.Visibility.PRIVATE)
.setValueType(Note.Type.STRING)
.setTags(Arrays.asList("tag1", "tag2"))
.setValue("hello");
Note note = catalogManager.getNotesManager().createStudyNote(studyFqn, noteCreateParams, INCLUDE_RESULT, ownerToken).first();
assertEquals(2, note.getTags().size());
assertArrayEquals(Arrays.asList("tag1", "tag2").toArray(), note.getTags().toArray());

QueryOptions queryOptions = new QueryOptions();
Map<String, Object> actionMap = new HashMap<>();
actionMap.put(NoteDBAdaptor.QueryParams.TAGS.key(), ParamUtils.BasicUpdateAction.ADD);
queryOptions.put(Constants.ACTIONS, actionMap);
queryOptions.put(ParamConstants.INCLUDE_RESULT_PARAM, true);
NoteUpdateParams updateParams = new NoteUpdateParams().setTags(Arrays.asList("tag3", "tag1"));
note = catalogManager.getNotesManager().updateStudyNote(studyFqn, note.getId(), updateParams, queryOptions, ownerToken).first();
assertEquals(3, note.getTags().size());
assertArrayEquals(Arrays.asList("tag1", "tag2", "tag3").toArray(), note.getTags().toArray());

// Remove tag1 and tag2
actionMap.put(NoteDBAdaptor.QueryParams.TAGS.key(), ParamUtils.BasicUpdateAction.REMOVE);
queryOptions.put(Constants.ACTIONS, actionMap);
updateParams = new NoteUpdateParams().setTags(Arrays.asList("tag1", "tag2"));
note = catalogManager.getNotesManager().updateStudyNote(studyFqn, note.getId(), updateParams, queryOptions, ownerToken).first();
assertEquals(1, note.getTags().size());
assertArrayEquals(Arrays.asList("tag3").toArray(), note.getTags().toArray());

// Set new list of tags
actionMap.put(NoteDBAdaptor.QueryParams.TAGS.key(), ParamUtils.BasicUpdateAction.SET);
queryOptions.put(Constants.ACTIONS, actionMap);
updateParams = new NoteUpdateParams().setTags(Arrays.asList("tag4", "tag5"));
note = catalogManager.getNotesManager().updateStudyNote(studyFqn, note.getId(), updateParams, queryOptions, ownerToken).first();
assertEquals(2, note.getTags().size());
assertArrayEquals(Arrays.asList("tag4", "tag5").toArray(), note.getTags().toArray());
}

@Test
public void getStudyNoteTest() throws CatalogException {
NoteCreateParams noteCreateParams1 = new NoteCreateParams()
Expand Down
3 changes: 2 additions & 1 deletion opencga-client/src/main/R/R/Organization-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' | createNotes | /{apiVersion}/organizations/notes/create | include, exclude, includeResult, body[*] |
#' | searchNotes | /{apiVersion}/organizations/notes/search | include, exclude, creationDate, modificationDate, id, scope, visibility, uuid, userId, tags, version |
#' | deleteNotes | /{apiVersion}/organizations/notes/{id}/delete | id[*], includeResult |
#' | updateNotes | /{apiVersion}/organizations/notes/{id}/update | include, exclude, id[*], includeResult, body[*] |
#' | updateNotes | /{apiVersion}/organizations/notes/{id}/update | include, exclude, id[*], tagsAction, includeResult, body[*] |
#' | userUpdateStatus | /{apiVersion}/organizations/user/{user}/status/update | include, exclude, user[*], organization, includeResult, body[*] |
#' | updateUser | /{apiVersion}/organizations/user/{user}/update | include, exclude, user[*], organization, includeResult, body[*] |
#' | updateConfiguration | /{apiVersion}/organizations/{organization}/configuration/update | include, exclude, organization[*], includeResult, authenticationOriginsAction, body[*] |
Expand Down Expand Up @@ -85,6 +85,7 @@ setMethod("organizationClient", "OpencgaR", function(OpencgaR, id, organization,
#' @param include Fields included in the response, whole JSON path must be provided.
#' @param exclude Fields excluded in the response, whole JSON path must be provided.
#' @param id Note unique identifier.
#' @param tagsAction Action to be performed if the array of tags is being updated. Allowed values: ['ADD SET REMOVE']
#' @param includeResult Flag indicating to include the created or updated document result in the response.
#' @param data JSON containing the Note fields to be updated.
updateNotes=fetchOpenCGA(object=OpencgaR, category="organizations", categoryId=NULL, subcategory="notes",
Expand Down
3 changes: 2 additions & 1 deletion opencga-client/src/main/R/R/Study-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' | createNotes | /{apiVersion}/studies/{study}/notes/create | include, exclude, study[*], includeResult, body[*] |
#' | searchNotes | /{apiVersion}/studies/{study}/notes/search | include, exclude, study[*], creationDate, modificationDate, id, uuid, userId, tags, visibility, version |
#' | deleteNotes | /{apiVersion}/studies/{study}/notes/{id}/delete | study[*], id[*], includeResult |
#' | updateNotes | /{apiVersion}/studies/{study}/notes/{id}/update | include, exclude, study[*], id[*], includeResult, body[*] |
#' | updateNotes | /{apiVersion}/studies/{study}/notes/{id}/update | include, exclude, study[*], id[*], tagsAction, includeResult, body[*] |
#' | permissionRules | /{apiVersion}/studies/{study}/permissionRules | study[*], entity[*] |
#' | updatePermissionRules | /{apiVersion}/studies/{study}/permissionRules/update | study[*], entity[*], action, body[*] |
#' | runTemplates | /{apiVersion}/studies/{study}/templates/run | study[*], jobId, jobDependsOn, jobDescription, jobTags, jobScheduledStartTime, jobPriority, jobDryRun, body[*] |
Expand Down Expand Up @@ -191,6 +191,7 @@ setMethod("studyClient", "OpencgaR", function(OpencgaR, group, id, members, stud
#' @param exclude Fields excluded in the response, whole JSON path must be provided.
#' @param study Study [[organization@]project:]study where study and project can be either the ID or UUID.
#' @param id Note unique identifier.
#' @param tagsAction Action to be performed if the array of tags is being updated. Allowed values: ['ADD SET REMOVE']
#' @param includeResult Flag indicating to include the created or updated document result in the response.
#' @param data JSON containing the Note fields to be updated.
updateNotes=fetchOpenCGA(object=OpencgaR, category="studies", categoryId=study, subcategory="notes",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public RestResponse<Note> deleteNotes(String id, ObjectMap params) throws Client
* @param params Map containing any of the following optional parameters.
* include: Fields included in the response, whole JSON path must be provided.
* exclude: Fields excluded in the response, whole JSON path must be provided.
* tagsAction: Action to be performed if the array of tags is being updated.
* includeResult: Flag indicating to include the created or updated document result in the response.
* @return a RestResponse object.
* @throws ClientException ClientException if there is any server error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public RestResponse<Note> deleteNotes(String study, String id, ObjectMap params)
* @param params Map containing any of the following optional parameters.
* include: Fields included in the response, whole JSON path must be provided.
* exclude: Fields excluded in the response, whole JSON path must be provided.
* tagsAction: Action to be performed if the array of tags is being updated.
* includeResult: Flag indicating to include the created or updated document result in the response.
* @return a RestResponse object.
* @throws ClientException ClientException if there is any server error.
Expand Down
2 changes: 2 additions & 0 deletions opencga-client/src/main/javascript/Organization.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export default class Organization extends OpenCGAParentClass {
* @param {Object} [params] - The Object containing the following optional parameters:
* @param {String} [params.include] - Fields included in the response, whole JSON path must be provided.
* @param {String} [params.exclude] - Fields excluded in the response, whole JSON path must be provided.
* @param {"ADD SET REMOVE"} [params.tagsAction = "ADD"] - Action to be performed if the array of tags is being updated. The default
* value is ADD.
* @param {Boolean} [params.includeResult = "false"] - Flag indicating to include the created or updated document result in the response.
* The default value is false.
* @returns {Promise} Promise object in the form of RestResponse instance.
Expand Down
2 changes: 2 additions & 0 deletions opencga-client/src/main/javascript/Study.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ export default class Study extends OpenCGAParentClass {
* @param {Object} [params] - The Object containing the following optional parameters:
* @param {String} [params.include] - Fields included in the response, whole JSON path must be provided.
* @param {String} [params.exclude] - Fields excluded in the response, whole JSON path must be provided.
* @param {"ADD SET REMOVE"} [params.tagsAction = "ADD"] - Action to be performed if the array of tags is being updated. The default
* value is ADD.
* @param {Boolean} [params.includeResult = "false"] - Flag indicating to include the created or updated document result in the response.
* The default value is false.
* @returns {Promise} Promise object in the form of RestResponse instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ def update_notes(self, id, data=None, **options):
must be provided.
:param str exclude: Fields excluded in the response, whole JSON path
must be provided.
:param str tags_action: Action to be performed if the array of tags is
being updated. Allowed values: ['ADD SET REMOVE']
:param bool include_result: Flag indicating to include the created or
updated document result in the response.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ def update_notes(self, study, id, data=None, **options):
must be provided.
:param str exclude: Fields excluded in the response, whole JSON path
must be provided.
:param str tags_action: Action to be performed if the array of tags is
being updated. Allowed values: ['ADD SET REMOVE']
:param bool include_result: Flag indicating to include the created or
updated document result in the response.
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.opencb.opencga.server.rest;

import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.opencga.catalog.db.api.NoteDBAdaptor;
import org.opencb.opencga.catalog.db.api.OrganizationDBAdaptor;
import org.opencb.opencga.catalog.utils.Constants;
import org.opencb.opencga.catalog.utils.ParamUtils;
Expand Down Expand Up @@ -204,9 +205,17 @@ public Response createNote(
})
public Response updateNote(
@ApiParam(value = FieldConstants.NOTES_ID_DESCRIPTION) @PathParam(FieldConstants.NOTES_ID_PARAM) String noteId,
@ApiParam(value = "Action to be performed if the array of tags is being updated.", allowableValues = "ADD,REMOVE,SET", defaultValue = "ADD")
@QueryParam("tagsAction") ParamUtils.BasicUpdateAction tagsAction,
@ApiParam(value = ParamConstants.INCLUDE_RESULT_DESCRIPTION, defaultValue = "false") @QueryParam(ParamConstants.INCLUDE_RESULT_PARAM) boolean includeResult,
@ApiParam(value = "JSON containing the Note fields to be updated.", required = true) NoteUpdateParams parameters) {
try {
if (tagsAction == null) {
tagsAction = ParamUtils.BasicUpdateAction.ADD;
}
Map<String, Object> actionMap = new HashMap<>();
actionMap.put(NoteDBAdaptor.QueryParams.TAGS.key(), tagsAction);
queryOptions.put(Constants.ACTIONS, actionMap);
OpenCGAResult<Note> result = catalogManager.getNotesManager().updateOrganizationNote(noteId, parameters, queryOptions, token);
return createOkResponse(result);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opencb.commons.datastore.core.DataResult;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.opencga.analysis.templates.TemplateRunner;
import org.opencb.opencga.catalog.db.api.NoteDBAdaptor;
import org.opencb.opencga.catalog.db.api.StudyDBAdaptor;
import org.opencb.opencga.catalog.managers.StudyManager;
import org.opencb.opencga.catalog.utils.Constants;
Expand All @@ -46,7 +47,9 @@
import javax.ws.rs.core.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.opencb.opencga.core.api.ParamConstants.JOB_DEPENDS_ON;

Expand Down Expand Up @@ -613,9 +616,17 @@ public Response createNote(
public Response updateNote(
@ApiParam(value = ParamConstants.STUDY_DESCRIPTION) @PathParam(ParamConstants.STUDY_PARAM) String studyStr,
@ApiParam(value = FieldConstants.NOTES_ID_DESCRIPTION) @PathParam(FieldConstants.NOTES_ID_PARAM) String noteId,
@ApiParam(value = "Action to be performed if the array of tags is being updated.", allowableValues = "ADD,REMOVE,SET", defaultValue = "ADD")
@QueryParam("tagsAction") ParamUtils.BasicUpdateAction tagsAction,
@ApiParam(value = ParamConstants.INCLUDE_RESULT_DESCRIPTION, defaultValue = "false") @QueryParam(ParamConstants.INCLUDE_RESULT_PARAM) boolean includeResult,
@ApiParam(value = "JSON containing the Note fields to be updated.", required = true) NoteUpdateParams parameters) {
try {
if (tagsAction == null) {
tagsAction = ParamUtils.BasicUpdateAction.ADD;
}
Map<String, Object> actionMap = new HashMap<>();
actionMap.put(NoteDBAdaptor.QueryParams.TAGS.key(), tagsAction);
queryOptions.put(Constants.ACTIONS, actionMap);
OpenCGAResult<Note> result = catalogManager.getNotesManager().updateStudyNote(studyStr, noteId, parameters, queryOptions, token);
return createOkResponse(result);
} catch (Exception e) {
Expand Down

0 comments on commit 00cc8a3

Please sign in to comment.