Esempio n. 1
0
  @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);
  }
Esempio n. 2
0
  @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 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);
    }
  }
Esempio n. 4
0
  @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;
  }
Esempio n. 5
0
  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);
  }
  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);
    }
  }
  @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;
  }
  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;
  }
  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);
  }
  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.");
      }
    }
  }
  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);
  }
Esempio n. 13
0
  @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);
  }
Esempio n. 14
0
 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());
 }
Esempio n. 15
0
 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());
 }
Esempio n. 16
0
 protected void resolveTask(Task task, TaskActionRequest actionRequest) {
   ActivitiUtil.getTaskService().resolveTask(task.getId());
 }