@Test
  public void testCompleteTaskMultipleSessionsSync() throws Exception {
    TestStatefulKnowledgeSession ksession = new TestStatefulKnowledgeSession();

    HornetQHTWorkItemHandler handler = new HornetQHTWorkItemHandler(ksession, true);

    client = handler.getClient();
    HornetQTestWorkItemManager manager = new HornetQTestWorkItemManager();
    ksession.setWorkItemManager(manager);
    WorkItemImpl workItem = new WorkItemImpl();
    workItem.setName("Human Task");
    workItem.setParameter("TaskName", "TaskName");
    workItem.setParameter("Comment", "Comment");
    workItem.setParameter("Priority", "10");
    workItem.setParameter("ActorId", "Darth Vader");
    workItem.setProcessInstanceId(10);
    handler.executeWorkItem(workItem, manager);

    TestStatefulKnowledgeSession ksession2 = new TestStatefulKnowledgeSession(10);
    HornetQHTWorkItemHandler handler2 =
        new HornetQHTWorkItemHandler("testConnector", null, ksession2, OnErrorAction.LOG);
    handler2.setOwningSessionOnly(true);

    HornetQTestWorkItemManager manager2 = new HornetQTestWorkItemManager();
    ksession2.setWorkItemManager(manager2);
    WorkItemImpl workItem2 = new WorkItemImpl();
    workItem2.setName("Human Task");
    workItem2.setParameter("TaskName", "TaskName");
    workItem2.setParameter("Comment", "Comment");
    workItem2.setParameter("Priority", "10");
    workItem2.setParameter("ActorId", "Darth Vader");
    workItem2.setProcessInstanceId(10);
    handler2.executeWorkItem(workItem2, manager2);

    List<TaskSummary> tasks = client.getTasksAssignedAsPotentialOwner("Darth Vader", "en-UK");
    assertEquals(2, tasks.size());
    TaskSummary task = tasks.get(0);
    // ensure we get first task
    if (task.getProcessSessionId() == 10) {
      task = tasks.get(1);
    }
    assertEquals("TaskName", task.getName());
    assertEquals(10, task.getPriority());
    assertEquals("Comment", task.getDescription());
    assertEquals(Status.Reserved, task.getStatus());
    assertEquals("Darth Vader", task.getActualOwner().getId());
    assertEquals(10, task.getProcessInstanceId());

    client.start(task.getId(), "Darth Vader");
    client.complete(task.getId(), "Darth Vader", null);

    Thread.sleep(1000);

    assertEquals(1, manager.getCompleteCounter());
    assertEquals(0, manager2.getCompleteCounter());

    handler.dispose();
    handler2.dispose();
  }
 private void registerTaskEvents() {
   TaskCompletedHandler eventResponseHandler = new TaskCompletedHandler();
   TaskEventKey key = new TaskEventKey(TaskCompletedEvent.class, -1);
   client.registerForEvent(key, false, eventResponseHandler);
   key = new TaskEventKey(TaskFailedEvent.class, -1);
   client.registerForEvent(key, false, eventResponseHandler);
   key = new TaskEventKey(TaskSkippedEvent.class, -1);
   client.registerForEvent(key, false, eventResponseHandler);
 }
 public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
   Task task = client.getTaskByWorkItemId(workItem.getId());
   if (task != null) {
     try {
       client.skip(task.getId(), "Administrator");
     } catch (PermissionDeniedException e) {
       logger.info(e.getMessage());
     }
   }
 }
  protected void tearDown() throws Exception {
    if (client != null) {
      client.disconnect();
    }

    if (clientAsync != null) {
      clientAsync.disconnect();
    }
    server.stop();
    super.tearDown();
  }
  public void testPotentialOwnerQueries() {
    Map<String, Object> vars = new HashMap();
    vars.put("users", users);
    vars.put("groups", groups);

    // Reader reader;
    Reader reader =
        new InputStreamReader(
            getClass().getResourceAsStream("../../QueryData_TasksPotentialOwner.mvel"));
    List<Task> tasks = (List<Task>) eval(reader, vars);
    for (Task task : tasks) {

      client.addTask(task, null);
    }

    // Test UK I18N

    List<TaskSummary> actual =
        client.getTasksAssignedAsPotentialOwner(users.get("bobba").getId(), "es_CL");
    assertEquals(2, actual.size());
  }
  public void testPeopleAssignmentQueries() {
    Map vars = new HashMap();
    vars.put("users", users);
    vars.put("groups", groups);

    Reader reader =
        new InputStreamReader(getClass().getResourceAsStream("../../QueryData_TasksOwned.mvel"));
    List<Task> tasks = (List<Task>) eval(reader, vars);
    for (Task task : tasks) {
      taskSession.addTask(task, null);
    }

    reader =
        new InputStreamReader(
            getClass().getResourceAsStream("../../QueryResults_PeopleAssignmentQuerries.mvel"));
    Map<String, List<TaskSummary>> expected = (Map<String, List<TaskSummary>>) eval(reader, vars);

    List<TaskSummary> actual =
        client.getTasksAssignedAsTaskInitiator(users.get("darth").getId(), "es_CL");
    assertEquals(1, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("darth"), actual));

    actual = client.getTasksAssignedAsBusinessAdministrator(users.get("steve").getId(), "es_CL");
    assertTrue(CollectionUtils.equals(expected.get("steve"), actual));

    actual = client.getTasksAssignedAsExcludedOwner(users.get("liz").getId(), "es_CL");
    assertEquals(2, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("liz"), actual));

    actual = client.getTasksAssignedAsPotentialOwner(users.get("bobba").getId(), "es_CL");
    assertEquals(3, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("bobba"), actual));

    actual = client.getTasksAssignedAsRecipient(users.get("sly").getId(), "es_CL");
    assertEquals(1, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("sly"), actual));
  }
 public void connect() {
   if (!initialized) {
     if (client == null) {
       throw new IllegalStateException("You must set the client to the work item to work");
     }
     if (client != null) {
       boolean connected = client.connect(ipAddress, port);
       if (!connected) {
         throw new IllegalArgumentException("Could not connect task client");
       }
     }
     registerTaskEvents();
     initialized = true;
   }
 }
  @SuppressWarnings("unchecked")
  public void testTasksOwnedQueryWithI18N() throws Exception {
    Map<String, Object> vars = new HashMap();
    vars.put("users", users);
    vars.put("groups", groups);

    // Reader reader;
    Reader reader =
        new InputStreamReader(getClass().getResourceAsStream("../../QueryData_TasksOwned.mvel"));
    List<Task> tasks = (List<Task>) eval(reader, vars);
    for (Task task : tasks) {
      client.addTask(task, null);
    }

    // Test UK I18N
    reader =
        new InputStreamReader(
            getClass().getResourceAsStream("../../QueryResults_TasksOwnedInEnglish.mvel"));
    Map<String, List<TaskSummary>> expected = (Map<String, List<TaskSummary>>) eval(reader, vars);

    List<TaskSummary> actual = client.getTasksOwned(users.get("peter").getId(), "es_CL");
    assertEquals(3, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("peter"), actual));

    actual = client.getTasksOwned(users.get("steve").getId(), "es_CL");
    assertEquals(2, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("steve"), actual));

    actual = client.getTasksOwned(users.get("darth").getId(), "es_CL");
    assertEquals(1, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("darth"), actual));

    // Test DK I18N
    reader =
        new InputStreamReader(
            getClass().getResourceAsStream("../../QueryResults_TasksOwnedInGerman.mvel"));
    expected = (Map<String, List<TaskSummary>>) eval(reader, vars);

    actual = client.getTasksOwned(users.get("peter").getId(), "en-DK");
    assertEquals(3, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("peter"), actual));

    actual = client.getTasksOwned(users.get("steve").getId(), "en-DK");
    assertEquals(2, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("steve"), actual));

    actual = client.getTasksOwned(users.get("darth").getId(), "en-DK");
    assertEquals(1, actual.size());
    assertTrue(CollectionUtils.equals(expected.get("darth"), actual));
  }
Esempio n. 9
0
  public static final void main(String[] args) {
    try {
      // load up the knowledge base
      KnowledgeBase kbase = readKnowledgeBase();
      StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
      KnowledgeRuntimeLogger logger =
          KnowledgeRuntimeLoggerFactory.newThreadedFileLogger(ksession, "test", 1000);
      HornetQHTWorkItemHandler hornetQHTWorkItemHandler = new HornetQHTWorkItemHandler(ksession);
      ksession.getWorkItemManager().registerWorkItemHandler("Human Task", hornetQHTWorkItemHandler);
      // start a new process instance
      Map<String, Object> params = new HashMap<String, Object>();
      params.put("employee", "krisv");
      params.put("reason", "Yearly performance evaluation");
      ksession.startProcess("com.sample.evaluation", params);

      SystemEventListenerFactory.setSystemEventListener(new SystemEventListener());
      TaskService taskClient =
          new SyncTaskServiceWrapper(new AsyncHornetQTaskClient("EvaluationExample2-testClient"));
      taskClient.connect("127.0.0.1", 5153);

      Thread.sleep(1000);
      // "krisv" executes his own performance evaluation
      TaskSummary task1 = taskClient.getTasksAssignedAsPotentialOwner("krisv", "en-UK").get(0);
      System.out.println(
          "Krisv executing task "
              + task1.getName()
              + "("
              + task1.getId()
              + ": "
              + task1.getDescription()
              + ")");

      taskClient.start(task1.getId(), "krisv");

      taskClient.complete(task1.getId(), "krisv", null);

      Thread.sleep(1000);
      // "john", part of the "PM" group, executes a performance evaluation
      List<String> groups = new ArrayList<String>();
      groups.add("PM");

      TaskSummary task2 =
          taskClient.getTasksAssignedAsPotentialOwner("john", groups, "en-UK").get(0);
      System.out.println(
          "John executing task "
              + task2.getName()
              + "("
              + task2.getId()
              + ": "
              + task2.getDescription()
              + ")");

      taskClient.claim(task2.getId(), "john", groups);

      taskClient.start(task2.getId(), "john");

      taskClient.complete(task2.getId(), "john", null);

      Thread.sleep(1000);
      // "mary", part of the "HR" group, delegates a performance evaluation
      groups = new ArrayList<String>();
      groups.add("HR");

      TaskSummary task3 =
          taskClient.getTasksAssignedAsPotentialOwner("mary", groups, "en-UK").get(0);
      System.out.println(
          "Mary delegating task "
              + task3.getName()
              + "("
              + task3.getId()
              + ": "
              + task3.getDescription()
              + ") to krisv");

      taskClient.claim(task3.getId(), "mary", groups);

      taskClient.delegate(task3.getId(), "mary", "krisv");

      // "administrator" delegates the task back to mary
      System.out.println("Administrator delegating task back to mary");

      taskClient.delegate(task3.getId(), "Administrator", "mary");

      Thread.sleep(1000);
      // mary executing the task
      TaskSummary task3b = taskClient.getTasksAssignedAsPotentialOwner("mary", "en-UK").get(0);
      System.out.println(
          "Mary executing task "
              + task3b.getName()
              + "("
              + task3b.getId()
              + ": "
              + task3b.getDescription()
              + ")");

      taskClient.start(task3b.getId(), "mary");

      taskClient.complete(task3b.getId(), "mary", null);

      taskClient.disconnect();
      hornetQHTWorkItemHandler.dispose();
      Thread.sleep(1000);

      logger.close();
      System.exit(0);
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
  public void testAddRemoveComment() {
    Map vars = fillVariables(users, groups);

    String str =
        "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { createdOn = now, activationTime = now}), ";
    str += "deadlines = new Deadlines(),";
    str += "delegation = new Delegation(),";
    str += "peopleAssignments = new PeopleAssignments(),";
    str += "names = [ new I18NText( 'en-UK', 'This is my task name')] })";

    Task task = (Task) eval(new StringReader(str), vars);
    client.addTask(task, null);

    long taskId = task.getId();

    Comment comment = new Comment();
    Date addedAt = new Date(System.currentTimeMillis());
    comment.setAddedAt(addedAt);
    comment.setAddedBy(users.get("luke"));
    comment.setText("This is my comment1!!!!!");

    client.addComment(taskId, comment);

    long commentId = comment.getId();

    Task task1 = client.getTask(taskId);
    // We are reusing this for local clients where the object is the same
    // assertNotSame(task, task1);
    // assertFalse(task.equals(task1));

    List<Comment> comments1 = task1.getTaskData().getComments();
    assertEquals(1, comments1.size());
    Comment returnedComment = comments1.get(0);
    assertEquals("This is my comment1!!!!!", returnedComment.getText());
    assertEquals(addedAt, returnedComment.getAddedAt());
    assertEquals(users.get("luke"), returnedComment.getAddedBy());

    assertEquals(commentId, (long) returnedComment.getId());

    // Make the same as the returned tasks, so we can test equals
    task.getTaskData().setComments(comments1);
    task.getTaskData().setStatus(Status.Created);
    assertEquals(task, task1);

    // test we can have multiple comments
    comment = new Comment();
    addedAt = new Date(System.currentTimeMillis());
    comment.setAddedAt(addedAt);
    comment.setAddedBy(users.get("tony"));
    comment.setText("This is my comment2!!!!!");

    client.addComment(taskId, comment);
    long commentId2 = comment.getId();

    task1 = client.getTask(taskId);
    List<Comment> comments2 = task1.getTaskData().getComments();
    assertEquals(2, comments2.size());

    // make two collections the same and compare
    comments1.add(comment);
    assertTrue(CollectionUtils.equals(comments1, comments2));

    client.deleteComment(taskId, commentId2);

    task1 = client.getTask(taskId);
    comments2 = task1.getTaskData().getComments();
    assertEquals(1, comments2.size());

    assertEquals("This is my comment1!!!!!", comments2.get(0).getText());
  }
 protected void tearDown() throws Exception {
   client.disconnect();
   server.stop();
   super.tearDown();
 }
  public void testAddRemoveAttachment() throws Exception {
    Map vars = new HashMap();
    vars.put("users", users);
    vars.put("groups", groups);
    vars.put("now", new Date());

    String str =
        "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { createdOn = now, activationTime = now}), ";
    str += "deadlines = new Deadlines(),";
    str += "delegation = new Delegation(),";
    str += "peopleAssignments = new PeopleAssignments(),";
    str += "names = [ new I18NText( 'en-UK', 'This is my task name')] })";

    Task task = (Task) eval(new StringReader(str), vars);
    client.addTask(task, null);

    long taskId = task.getId();

    Attachment attachment = new Attachment();
    Date attachedAt = new Date(System.currentTimeMillis());
    attachment.setAttachedAt(attachedAt);
    attachment.setAttachedBy(users.get("luke"));
    attachment.setName("file1.txt");
    attachment.setAccessType(AccessType.Inline);
    attachment.setContentType("txt");

    byte[] bytes = "Ths is my attachment text1".getBytes();
    Content content = new Content();
    content.setContent(bytes);

    client.addAttachment(taskId, attachment, content);

    Task task1 = client.getTask(taskId);
    // For local clients this will be the same.. that's why is commented
    // assertNotSame(task, task1);
    // assertFalse(task.equals(task1));

    List<Attachment> attachments1 = task1.getTaskData().getAttachments();
    assertEquals(1, attachments1.size());
    Attachment returnedAttachment = attachments1.get(0);
    assertEquals(attachedAt, returnedAttachment.getAttachedAt());
    assertEquals(users.get("luke"), returnedAttachment.getAttachedBy());
    assertEquals(AccessType.Inline, returnedAttachment.getAccessType());
    assertEquals("txt", returnedAttachment.getContentType());
    assertEquals("file1.txt", returnedAttachment.getName());
    assertEquals(bytes.length, returnedAttachment.getSize());

    assertEquals((long) attachment.getId(), (long) returnedAttachment.getId());
    assertEquals((long) content.getId(), (long) returnedAttachment.getAttachmentContentId());

    // Make the same as the returned tasks, so we can test equals
    task.getTaskData().setAttachments(attachments1);
    task.getTaskData().setStatus(Status.Created);
    assertEquals(task, task1);

    content = client.getContent(returnedAttachment.getAttachmentContentId());
    assertEquals("Ths is my attachment text1", new String(content.getContent()));

    // test we can have multiple attachments

    attachment = new Attachment();
    attachedAt = new Date(System.currentTimeMillis());
    attachment.setAttachedAt(attachedAt);
    attachment.setAttachedBy(users.get("tony"));
    attachment.setName("file2.txt");
    attachment.setAccessType(AccessType.Inline);
    attachment.setContentType("txt");

    bytes = "Ths is my attachment text2".getBytes();
    content = new Content();
    content.setContent(bytes);

    client.addAttachment(taskId, attachment, content);

    task1 = client.getTask(taskId);
    // In local clients this will be the same object and we are reusing the tests
    // assertNotSame(task, task1);
    // assertFalse(task.equals(task1));

    List<Attachment> attachments2 = task1.getTaskData().getAttachments();
    assertEquals(2, attachments2.size());

    content = client.getContent(content.getId());
    assertEquals("Ths is my attachment text2", new String(content.getContent()));

    // make two collections the same and compare
    attachment.setSize(26);
    attachment.setAttachmentContentId(content.getId());
    attachments1.add(attachment);
    assertTrue(CollectionUtils.equals(attachments2, attachments1));

    client.deleteAttachment(taskId, attachment.getId(), content.getId());

    task1 = client.getTask(taskId);
    attachments2 = task1.getTaskData().getAttachments();
    assertEquals(1, attachments2.size());

    assertEquals("file1.txt", attachments2.get(0).getName());
  }
Esempio n. 13
0
 public void dispose() throws Exception {
   if (client != null) {
     client.disconnect();
   }
 }
Esempio n. 14
0
  public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    if (this.session == null) {
      if (this.manager == null) {
        this.manager = manager;
      } else {
        if (this.manager != manager) {
          throw new IllegalArgumentException(
              "This WSHumanTaskHandler can only be used for one WorkItemManager");
        }
      }
    }
    connect();
    Task task = new Task();
    String taskName = (String) workItem.getParameter("TaskName");
    if (taskName != null) {
      List<I18NText> names = new ArrayList<I18NText>();
      names.add(new I18NText("en-UK", taskName));
      task.setNames(names);
    }
    String comment = (String) workItem.getParameter("Comment");
    if (comment != null) {
      List<I18NText> descriptions = new ArrayList<I18NText>();
      descriptions.add(new I18NText("en-UK", comment));
      task.setDescriptions(descriptions);
      List<I18NText> subjects = new ArrayList<I18NText>();
      subjects.add(new I18NText("en-UK", comment));
      task.setSubjects(subjects);
    }
    String priorityString = (String) workItem.getParameter("Priority");
    int priority = 0;
    if (priorityString != null) {
      try {
        priority = new Integer(priorityString);
      } catch (NumberFormatException e) {
        // do nothing
      }
    }
    task.setPriority(priority);

    TaskData taskData = new TaskData();
    taskData.setWorkItemId(workItem.getId());
    taskData.setProcessInstanceId(workItem.getProcessInstanceId());
    if (session != null && session.getProcessInstance(workItem.getProcessInstanceId()) != null) {
      taskData.setProcessId(
          session.getProcessInstance(workItem.getProcessInstanceId()).getProcess().getId());
    }
    if (session != null && (session instanceof StatefulKnowledgeSession)) {
      taskData.setProcessSessionId(((StatefulKnowledgeSession) session).getId());
    }
    taskData.setSkipable(!"false".equals(workItem.getParameter("Skippable")));
    // Sub Task Data
    Long parentId = (Long) workItem.getParameter("ParentId");
    if (parentId != null) {
      taskData.setParentId(parentId);
    }

    String subTaskStrategiesCommaSeparated = (String) workItem.getParameter("SubTaskStrategies");
    if (subTaskStrategiesCommaSeparated != null && !subTaskStrategiesCommaSeparated.equals("")) {
      String[] subTaskStrategies = subTaskStrategiesCommaSeparated.split(",");
      List<SubTasksStrategy> strategies = new ArrayList<SubTasksStrategy>();
      for (String subTaskStrategyString : subTaskStrategies) {
        SubTasksStrategy subTaskStrategy =
            SubTasksStrategyFactory.newStrategy(subTaskStrategyString);
        strategies.add(subTaskStrategy);
      }
      task.setSubTaskStrategies(strategies);
    }

    PeopleAssignments assignments = new PeopleAssignments();
    List<OrganizationalEntity> potentialOwners = new ArrayList<OrganizationalEntity>();

    String actorId = (String) workItem.getParameter("ActorId");
    if (actorId != null && actorId.trim().length() > 0) {
      String[] actorIds = actorId.split(",");
      for (String id : actorIds) {
        potentialOwners.add(new User(id.trim()));
      }
      // Set the first user as creator ID??? hmmm might be wrong
      if (potentialOwners.size() > 0) {
        taskData.setCreatedBy((User) potentialOwners.get(0));
      }
    }

    String groupId = (String) workItem.getParameter("GroupId");
    if (groupId != null && groupId.trim().length() > 0) {
      String[] groupIds = groupId.split(",");
      for (String id : groupIds) {
        potentialOwners.add(new Group(id.trim()));
      }
    }

    assignments.setPotentialOwners(potentialOwners);
    List<OrganizationalEntity> businessAdministrators = new ArrayList<OrganizationalEntity>();
    businessAdministrators.add(new User("Administrator"));
    assignments.setBusinessAdministrators(businessAdministrators);
    task.setPeopleAssignments(assignments);

    task.setTaskData(taskData);

    ContentData content = null;
    Object contentObject = workItem.getParameter("Content");
    if (contentObject == null) {
      contentObject = workItem.getParameters();
    }
    if (contentObject != null) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      ObjectOutputStream out;
      try {
        out = new ObjectOutputStream(bos);
        out.writeObject(contentObject);
        out.close();
        content = new ContentData();
        content.setContent(bos.toByteArray());
        content.setAccessType(AccessType.Inline);
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    client.addTask(task, content);
  }