Skip to content

Commit

Permalink
feat(engine-rest): Add process instance comments rest api
Browse files Browse the repository at this point in the history
Related to CAM-14446, camunda#1834
  • Loading branch information
d-michail authored Apr 20, 2022
1 parent c833617 commit 6b30af9
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<#macro endpoint_macro docsUrl="">
{

<@lib.endpointInfo
id = "getProcessInstanceComments"
tag = "Process Instance"
summary = "Get Process Instance Comments"
desc = "Gets the comments for a process instance by id." />

"parameters" : [

<@lib.parameter
name = "id"
location = "path"
type = "string"
required = true
last = true
desc = "The id of the process instance to retrieve the comments for." />

],

"responses" : {

<@lib.response
code = "200"
dto = "CommentDto"
array = true
desc = "Request successful."
examples = ['"example-1": {
"summary": "GET /process-instance/aProcessInstanceId/comment",
"value": [
{
"id": "commentId",
"userId": "userId",
"taskId": "aTaskId",
"processInstanceId": "aProcessInstanceId",
"time": "2013-01-02T21:37:03.764+0200",
"message": "message",
"removalTime": "2018-02-10T14:33:19.000+0200",
"rootProcessInstanceId": "aRootProcessInstanceId"
},
{
"id": "anotherCommentId",
"userId": "anotherUserId",
"taskId": "aTaskId",
"processInstanceId": "aProcessInstanceId",
"time": "2013-02-23T20:37:43.975+0200",
"message": "anotherMessage",
"removalTime": "2018-02-10T14:33:19.000+0200",
"rootProcessInstanceId": "aRootProcessInstanceId"
},
{
"id": "yetAnotherCommentId",
"userId": "yetAnotherUserId",
"taskId": "aTaskId",
"processInstanceId": "aProcessInstanceId",
"time": "2013-04-21T10:15:23.764+0200",
"message": "yetAnotherMessage",
"removalTime": "2018-02-10T14:33:19.000+0200",
"rootProcessInstanceId": "aRootProcessInstanceId"
}
]
}'
] />

<@lib.response
code = "404"
dto = "ExceptionDto"
last = true
desc = "No process instance exists for the given process instance id. See the
[Introduction](${docsUrl}/reference/rest/overview/#error-handling)
for the error response format." />

}
}

</#macro>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.engine.rest.sub.runtime;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.camunda.bpm.engine.rest.dto.task.CommentDto;

public interface ProcessInstanceCommentResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
List<CommentDto> getComments();

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,7 @@ void deleteProcessInstance(@QueryParam("skipCustomListeners") @DefaultValue("fal
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
BatchDto modifyProcessInstanceAsync(ProcessInstanceModificationDto dto);

@Path("/comment")
ProcessInstanceCommentResource getProcessInstanceCommentResource();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.engine.rest.sub.runtime.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.ws.rs.core.Response.Status;

import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.identity.Authentication;
import org.camunda.bpm.engine.rest.dto.task.CommentDto;
import org.camunda.bpm.engine.rest.exception.InvalidRequestException;
import org.camunda.bpm.engine.rest.sub.runtime.ProcessInstanceCommentResource;
import org.camunda.bpm.engine.task.Comment;

public class ProcessInstanceCommentResourceImpl implements ProcessInstanceCommentResource {

private ProcessEngine engine;
private String processInstanceId;

public ProcessInstanceCommentResourceImpl(ProcessEngine engine, String processInstanceId) {
this.engine = engine;
this.processInstanceId = processInstanceId;
}

public List<CommentDto> getComments() {
if (!isHistoryEnabled()) {
return Collections.emptyList();
}

ensureProcessInstanceExists(Status.NOT_FOUND);

List<Comment> processInstanceComments = engine.getTaskService().getProcessInstanceComments(processInstanceId);

List<CommentDto> comments = new ArrayList<CommentDto>();
for (Comment comment : processInstanceComments) {
comments.add(CommentDto.fromComment(comment));
}

return comments;
}

private boolean isHistoryEnabled() {
IdentityService identityService = engine.getIdentityService();
Authentication currentAuthentication = identityService.getCurrentAuthentication();
try {
identityService.clearAuthentication();
int historyLevel = engine.getManagementService().getHistoryLevel();
return historyLevel > ProcessEngineConfigurationImpl.HISTORYLEVEL_NONE;
} finally {
identityService.setAuthentication(currentAuthentication);
}
}

private void ensureProcessInstanceExists(Status status) {
HistoricProcessInstance historicProcessInstance = engine.getHistoryService().createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId).singleResult();
if (historicProcessInstance == null) {
throw new InvalidRequestException(status, "No process instance found for id " + processInstanceId);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.camunda.bpm.engine.rest.dto.runtime.modification.ProcessInstanceModificationDto;
import org.camunda.bpm.engine.rest.exception.InvalidRequestException;
import org.camunda.bpm.engine.rest.sub.VariableResource;
import org.camunda.bpm.engine.rest.sub.runtime.ProcessInstanceCommentResource;
import org.camunda.bpm.engine.rest.sub.runtime.ProcessInstanceResource;
import org.camunda.bpm.engine.runtime.ActivityInstance;
import org.camunda.bpm.engine.runtime.ProcessInstance;
Expand Down Expand Up @@ -154,4 +155,10 @@ public BatchDto modifyProcessInstanceAsync(ProcessInstanceModificationDto dto) {

throw new InvalidRequestException(Status.BAD_REQUEST, "The provided instuctions are invalid.");
}

@Override
public ProcessInstanceCommentResource getProcessInstanceCommentResource() {
return new ProcessInstanceCommentResourceImpl(engine, processInstanceId);
}

}
Loading

0 comments on commit 6b30af9

Please sign in to comment.