@Delete public void deleteTask() { if (!authenticate()) { return; } Form query = getQuery(); Boolean cascadeHistory = getQueryParameterAsBoolean("cascadeHistory", query); String deleteReason = getQueryParameter("deleteReason", query); Task taskToDelete = getTaskFromRequest(); if (taskToDelete.getExecutionId() != null) { // Can't delete a task that is part of a process instance throw new ResourceException( new Status( Status.CLIENT_ERROR_FORBIDDEN.getCode(), "Cannot delete a task that is part of a process-instance.", null, null)); } if (cascadeHistory != null) { // Ignore delete-reason since the task-history (where the reason is recorded) will be deleted // anyway ActivitiUtil.getTaskService().deleteTask(taskToDelete.getId(), cascadeHistory); } else { // Delete with delete-reason ActivitiUtil.getTaskService().deleteTask(taskToDelete.getId(), deleteReason); } getResponse().setStatus(Status.SUCCESS_NO_CONTENT); }
@Put public TaskResponse updateTask(TaskRequest taskRequest) { if (!authenticate()) { return null; } if (taskRequest == null) { throw new ResourceException( new Status( Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode(), "A request body was expected when updating the task.", null, null)); } Task task = getTaskFromRequest(); // Populate the task properties based on the request populateTaskFromRequest(task, taskRequest); // Save the task and fetch agian, it's possible that an assignment-listener has updated // fields after it was saved so we can't use the in-memory task ActivitiUtil.getTaskService().saveTask(task); task = ActivitiUtil.getTaskService().createTaskQuery().taskId(task.getId()).singleResult(); return getApplication(ActivitiRestServicesApplication.class) .getRestResponseFactory() .createTaskReponse(this, task); }
public RestVariable getVariableFromRequest(boolean includeBinary) { String taskId = getAttribute("taskId"); if (taskId == null) { throw new ActivitiIllegalArgumentException("The taskId cannot be null"); } String variableName = getAttribute("variableName"); if (variableName == null) { throw new ActivitiIllegalArgumentException("The variableName cannot be null"); } boolean variableFound = false; Object value = null; RestVariableScope variableScope = RestVariable.getScopeFromString(getQueryParameter("scope", getQuery())); if (variableScope == null) { // First, check local variables (which have precedence when no scope is supplied) if (ActivitiUtil.getTaskService().hasVariableLocal(taskId, variableName)) { value = ActivitiUtil.getTaskService().getVariableLocal(taskId, variableName); variableScope = RestVariableScope.LOCAL; variableFound = true; } else { // Revert to execution-variable when not present local on the task Task task = ActivitiUtil.getTaskService().createTaskQuery().taskId(taskId).singleResult(); if (task.getExecutionId() != null && ActivitiUtil.getRuntimeService().hasVariable(task.getExecutionId(), variableName)) { value = ActivitiUtil.getRuntimeService().getVariable(task.getExecutionId(), variableName); variableScope = RestVariableScope.GLOBAL; variableFound = true; } } } else if (variableScope == RestVariableScope.GLOBAL) { Task task = ActivitiUtil.getTaskService().createTaskQuery().taskId(taskId).singleResult(); if (task.getExecutionId() != null && ActivitiUtil.getRuntimeService().hasVariable(task.getExecutionId(), variableName)) { value = ActivitiUtil.getRuntimeService().getVariable(task.getExecutionId(), variableName); variableFound = true; } } else if (variableScope == RestVariableScope.LOCAL) { if (ActivitiUtil.getTaskService().hasVariableLocal(taskId, variableName)) { value = ActivitiUtil.getTaskService().getVariableLocal(taskId, variableName); variableFound = true; } } if (!variableFound) { throw new ActivitiObjectNotFoundException( "Task '" + taskId + "' doesn't have a variable with name: '" + variableName + "'.", VariableInstanceEntity.class); } else { return getApplication(ActivitiRestServicesApplication.class) .getRestResponseFactory() .createRestVariable( this, variableName, value, variableScope, taskId, null, null, includeBinary); } }
@Post public ProcessInstanceResponse createProcessInstance(ProcessInstanceCreateRequest request) { if (request.getProcessDefinitionId() == null && request.getProcessDefinitionKey() == null && request.getMessage() == null) { throw new ActivitiIllegalArgumentException( "Either processDefinitionId, processDefinitionKey or message is required."); } int paramsSet = ((request.getProcessDefinitionId() != null) ? 1 : 0) + ((request.getProcessDefinitionKey() != null) ? 1 : 0) + ((request.getMessage() != null) ? 1 : 0); if (paramsSet > 1) { throw new ActivitiIllegalArgumentException( "Only one of processDefinitionId, processDefinitionKey or message should be set."); } RestResponseFactory factory = getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory(); Map<String, Object> startVariables = null; if (request.getVariables() != null) { startVariables = new HashMap<String, Object>(); for (RestVariable variable : request.getVariables()) { startVariables.put(variable.getName(), factory.getVariableValue(variable)); } } // Actually start the instance based on key or id try { ProcessInstance instance = null; if (request.getProcessDefinitionId() != null) { instance = ActivitiUtil.getRuntimeService() .startProcessInstanceById( request.getProcessDefinitionId(), request.getBusinessKey(), startVariables); } else if (request.getProcessDefinitionKey() != null) { instance = ActivitiUtil.getRuntimeService() .startProcessInstanceByKey( request.getProcessDefinitionKey(), request.getBusinessKey(), startVariables); } else { instance = ActivitiUtil.getRuntimeService() .startProcessInstanceByMessage( request.getMessage(), request.getBusinessKey(), startVariables); } setStatus(Status.SUCCESS_CREATED); return factory.createProcessInstanceResponse(this, instance); } catch (ActivitiObjectNotFoundException aonfe) { throw new ActivitiIllegalArgumentException(aonfe.getMessage(), aonfe); } }
@Put public AttachmentResponse addAttachment(Representation entity) { if (authenticate() == false) return null; String taskId = (String) getRequest().getAttributes().get("taskId"); try { RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory()); List<FileItem> items = upload.parseRepresentation(entity); FileItem uploadItem = null; for (FileItem fileItem : items) { if (fileItem.getName() != null) { uploadItem = fileItem; } } String fileName = uploadItem.getName(); Attachment attachment = ActivitiUtil.getTaskService() .createAttachment( uploadItem.getContentType(), taskId, null, fileName, fileName, uploadItem.getInputStream()); return new AttachmentResponse(attachment); } catch (Exception e) { throw new ActivitiException("Unable to add new attachment to task " + taskId); } }
protected boolean hasVariableOnScope(Task task, String variableName, RestVariableScope scope) { boolean variableFound = false; if (scope == RestVariableScope.GLOBAL) { if (task.getExecutionId() != null && ActivitiUtil.getRuntimeService().hasVariable(task.getExecutionId(), variableName)) { variableFound = true; } } else if (scope == RestVariableScope.LOCAL) { if (ActivitiUtil.getTaskService().hasVariableLocal(task.getId(), variableName)) { variableFound = true; } } return variableFound; }
@Get public DataResponse getTasks() { if (authenticate() == false) return null; String personalTaskUserId = getQuery().getValues("assignee"); String ownerTaskUserId = getQuery().getValues("owner"); String involvedTaskUserId = getQuery().getValues("involved"); String candidateTaskUserId = getQuery().getValues("candidate"); String candidateGroupId = getQuery().getValues("candidate-group"); String strPriority = getQuery().getValues("priority"); String strMinPriority = getQuery().getValues("minPriority"); String strMaxPriority = getQuery().getValues("maxPriority"); String strDueDate = getQuery().getValues("dueDate"); String strMinDueDate = getQuery().getValues("minDueDate"); String strMaxDueDate = getQuery().getValues("maxDueDate"); TaskQuery taskQuery = ActivitiUtil.getTaskService().createTaskQuery(); if (personalTaskUserId != null) { taskQuery.taskAssignee(personalTaskUserId); } else if (ownerTaskUserId != null) { taskQuery.taskOwner(ownerTaskUserId); } else if (involvedTaskUserId != null) { taskQuery.taskInvolvedUser(involvedTaskUserId); } else if (candidateTaskUserId != null) { taskQuery.taskCandidateUser(candidateTaskUserId); } else if (candidateGroupId != null) { taskQuery.taskCandidateGroup(candidateGroupId); } else { throw new ActivitiIllegalArgumentException( "Tasks must be filtered with 'assignee', 'owner', 'involved', 'candidate' or 'candidate-group'"); } if (strPriority != null) { taskQuery.taskPriority(RequestUtil.parseToInteger(strPriority)); } else if (strMinPriority != null) { taskQuery.taskMinPriority(RequestUtil.parseToInteger(strMinPriority)); } else if (strMaxPriority != null) { taskQuery.taskMaxPriority(RequestUtil.parseToInteger(strMaxPriority)); } if (strDueDate != null) { taskQuery.dueDate(RequestUtil.parseToDate(strDueDate)); } else if (strMinDueDate != null) { taskQuery.dueAfter(RequestUtil.parseToDate(strMinDueDate)); } else if (strMaxDueDate != null) { taskQuery.dueBefore(RequestUtil.parseToDate(strMaxDueDate)); } DataResponse dataResponse = new TasksPaginateList().paginateList(getQuery(), taskQuery, "id", properties); return dataResponse; }
protected void completeTask(Task task, TaskActionRequest actionRequest) { if (actionRequest.getVariables() != null) { Map<String, Object> variablesToSet = new HashMap<String, Object>(); for (RestVariable var : actionRequest.getVariables()) { if (var.getName() == null) { throw new ActivitiIllegalArgumentException("Variable name is required"); } Object actualVariableValue = getApplication(ActivitiRestServicesApplication.class) .getRestResponseFactory() .getVariableValue(var); variablesToSet.put(var.getName(), actualVariableValue); } ActivitiUtil.getTaskService().complete(task.getId(), variablesToSet); } else { ActivitiUtil.getTaskService().complete(task.getId()); } }
@Delete public void delegteAttachment() { if (!authenticate()) return; Task task = getTaskFromRequest(); String attachmentId = getAttribute("attachmentId"); if (attachmentId == null) { throw new ActivitiIllegalArgumentException("AttachmentId is required."); } Attachment attachment = ActivitiUtil.getTaskService().getAttachment(attachmentId); if (attachment == null || !task.getId().equals(attachment.getTaskId())) { throw new ActivitiObjectNotFoundException( "Task '" + task.getId() + "' doesn't have an attachment with id '" + attachmentId + "'.", Comment.class); } ActivitiUtil.getTaskService().deleteAttachment(attachmentId); setStatus(Status.SUCCESS_NO_CONTENT); }
protected void setVariable( Task task, String name, Object value, RestVariableScope scope, boolean isNew) { // Create can only be done on new variables. Existing variables should be updated using PUT boolean hasVariable = hasVariableOnScope(task, name, scope); if (isNew && hasVariable) { throw new ResourceException( new Status( Status.CLIENT_ERROR_CONFLICT.getCode(), "Variable '" + name + "' is already present on task '" + task.getId() + "'.", null, null)); } if (!isNew && !hasVariable) { throw new ResourceException( new Status( Status.CLIENT_ERROR_NOT_FOUND.getCode(), "Task '" + task.getId() + "' doesn't have a variable with name: '" + name + "'.", null, null)); } if (scope == RestVariableScope.LOCAL) { ActivitiUtil.getTaskService().setVariableLocal(task.getId(), name, value); } else { if (task.getExecutionId() != null) { // Explicitly set on execution, setting non-local variable on task will override // local-variable if exists ActivitiUtil.getRuntimeService().setVariable(task.getExecutionId(), name, value); } else { // Standalone task, no global variables possible throw new ActivitiIllegalArgumentException( "Cannot set global variable '" + name + "' on task '" + task.getId() + "', task is not part of process."); } } }
@Get public List<AttachmentResponse> getAttachments() { if (!authenticate()) return null; List<AttachmentResponse> result = new ArrayList<AttachmentResponse>(); RestResponseFactory responseFactory = getApplication(ActivitiRestServicesApplication.class).getRestResponseFactory(); Task task = getTaskFromRequest(); for (Attachment attachment : ActivitiUtil.getTaskService().getTaskAttachments(task.getId())) { result.add(responseFactory.createAttachmentResponse(this, attachment)); } return result; }
/** Get valid execution from request, in this case a {@link ProcessInstance}. */ protected Execution getExecutionFromRequest() { String processInstanceId = getAttribute("processInstanceId"); if (processInstanceId == null) { throw new ActivitiIllegalArgumentException("The processInstanceId cannot be null"); } Execution execution = ActivitiUtil.getRuntimeService() .createProcessInstanceQuery() .processInstanceId(processInstanceId) .singleResult(); if (execution == null) { throw new ActivitiObjectNotFoundException( "Could not find a process instance with id '" + processInstanceId + "'.", ProcessInstance.class); } return execution; }
protected AttachmentResponse createBinaryAttachment(Representation representation, Task task) throws FileUploadException, IOException { RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory()); List<FileItem> items = upload.parseRepresentation(representation); String name = null; String description = null; String type = null; FileItem uploadItem = null; for (FileItem fileItem : items) { if (fileItem.isFormField()) { if ("name".equals(fileItem.getFieldName())) { name = fileItem.getString("UTF-8"); } else if ("description".equals(fileItem.getFieldName())) { description = fileItem.getString("UTF-8"); } else if ("type".equals(fileItem.getFieldName())) { type = fileItem.getString("UTF-8"); } } else if (fileItem.getName() != null) { uploadItem = fileItem; } } if (name == null) { throw new ActivitiIllegalArgumentException("Attachment name is required."); } if (uploadItem == null) { throw new ActivitiIllegalArgumentException("Attachment content is required."); } Attachment createdAttachment = ActivitiUtil.getTaskService() .createAttachment( type, task.getId(), null, name, description, uploadItem.getInputStream()); setStatus(Status.SUCCESS_CREATED); return getApplication(ActivitiRestServicesApplication.class) .getRestResponseFactory() .createAttachmentResponse(this, createdAttachment); }
// For process instance there's only one scope. Using the local variables method for that protected void addLocalVariables(Execution execution, Map<String, RestVariable> variableMap) { Map<String, Object> rawVariables = ActivitiUtil.getRuntimeService().getVariables(execution.getId()); List<RestVariable> globalVariables = getApplication(ActivitiRestServicesApplication.class) .getRestResponseFactory() .createRestVariables( this, rawVariables, execution.getId(), RestResponseFactory.VARIABLE_PROCESS, RestVariableScope.LOCAL); // Overlay global variables over local ones. In case they are present the values are not // overridden, // since local variables get precedence over global ones at all times. for (RestVariable var : globalVariables) { if (!variableMap.containsKey(var.getName())) { variableMap.put(var.getName(), var); } } }
protected AttachmentResponse createSimpleAttachment(Representation representation, Task task) throws IOException { AttachmentRequest req = getConverterService().toObject(representation, AttachmentRequest.class, this); if (req.getName() == null) { throw new ActivitiIllegalArgumentException("Attachment name is required."); } Attachment createdAttachment = ActivitiUtil.getTaskService() .createAttachment( req.getType(), task.getId(), null, req.getName(), req.getDescription(), req.getExternalUrl()); return getApplication(ActivitiRestServicesApplication.class) .getRestResponseFactory() .createAttachmentResponse(this, createdAttachment); }
@Get public AttachmentResponse getAttachment() { if (!authenticate()) return null; Task task = getTaskFromRequest(); String attachmentId = getAttribute("attachmentId"); if (attachmentId == null) { throw new ActivitiIllegalArgumentException("AttachmentId is required."); } Attachment attachment = ActivitiUtil.getTaskService().getAttachment(attachmentId); if (attachment == null || !task.getId().equals(attachment.getTaskId())) { throw new ActivitiObjectNotFoundException( "Task '" + task.getId() + "' doesn't have an attachment with id '" + attachmentId + "'.", Comment.class); } return getApplication(ActivitiRestServicesApplication.class) .getRestResponseFactory() .createAttachmentResponse(this, attachment); }
protected void claimTask(Task task, TaskActionRequest actionRequest) { // In case the task is already claimed, a ActivitiTaskAlreadyClaimedException is thown and // converted to // a CONFLICT response by the StatusService ActivitiUtil.getTaskService().claim(task.getId(), actionRequest.getAssignee()); }
protected void delegateTask(Task task, TaskActionRequest actionRequest) { if (actionRequest.getAssignee() == null) { throw new ActivitiIllegalArgumentException("An assignee is required when delegating a task."); } ActivitiUtil.getTaskService().delegateTask(task.getId(), actionRequest.getAssignee()); }
protected void resolveTask(Task task, TaskActionRequest actionRequest) { ActivitiUtil.getTaskService().resolveTask(task.getId()); }