Skip to content

Commit

Permalink
working on task list
Browse files Browse the repository at this point in the history
  • Loading branch information
rushproject committed Jun 28, 2011
1 parent 661ba91 commit dcf19de
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 33 deletions.
4 changes: 2 additions & 2 deletions example/tasklist/src/META-INF/persistence.xml
Expand Up @@ -17,7 +17,7 @@

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

<persistence-unit name="helloWorldPU">
<persistence-unit name="taskListPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.worktoken.model.BusinessProcess</class>
<class>com.worktoken.model.CatchEventNode</class>
Expand All @@ -33,7 +33,7 @@
<class>com.worktoken.model.TimerTrigger</class>
<class>com.worktoken.model.UserTask</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:helloworld"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:tasklist"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
Expand Down
23 changes: 23 additions & 0 deletions example/tasklist/src/com/worktoken/tasklist/Start.java
@@ -0,0 +1,23 @@
package com.worktoken.tasklist;

import com.worktoken.annotation.FlowElement;
import com.worktoken.annotation.RefType;
import com.worktoken.model.CatchEventNode;
import com.worktoken.model.EventToken;
import com.worktoken.model.WorkToken;

import javax.persistence.Entity;

/**
* @author Alex Pavlov (alex@rushproject.com)
*/
@Entity
@FlowElement(nodeRef = "start", processId = "taskList")
public class Start extends CatchEventNode {
@Override
public void eventIn(EventToken event) {
WorkToken token = new WorkToken();
token.getData().put("subject", event.getData().get("subject"));
tokenOut(token);
}
}
107 changes: 84 additions & 23 deletions example/tasklist/src/com/worktoken/tasklist/TaskListApp.java
Expand Up @@ -3,6 +3,7 @@
import com.worktoken.engine.ClassListAnnotationDictionary;
import com.worktoken.engine.WorkSessionImpl;
import com.worktoken.model.EventToken;
import com.worktoken.model.UserTask;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
Expand All @@ -12,49 +13,57 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.LogManager;

/**
* @author Alex Pavlov (alex@rushproject.com)
*/
public class TaskListApp {

private static Scanner scanner;
private static WorkSessionImpl session;

public static void main(String[] args) throws SQLException, InterruptedException, IOException {

scanner = new Scanner(System.in);

LogManager.getLogManager().readConfiguration(LogManager.class.getResourceAsStream("/logging.properties"));
Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:helloworld", "sa", "");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloWorldPU");
Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:tasklist", "sa", "");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("taskListPU");

// Prepare and verify annotation library
List<Class> annotatedClasses = new ArrayList<Class>();
annotatedClasses.add(completeTask.class);
annotatedClasses.add(CompleteTask.class);
annotatedClasses.add(Start.class);
ClassListAnnotationDictionary dictionary = new ClassListAnnotationDictionary(annotatedClasses);
dictionary.build();

// Create work session and load process definition
WorkSessionImpl session = new WorkSessionImpl("com.worktoken.tasklist", emf, dictionary);
session = new WorkSessionImpl("com.worktoken.tasklist", emf, dictionary);
session.readDefinitions(TaskListApp.class.getResourceAsStream("tasklist.bpmn"));

// Create process
long processId = session.createProcess("taskList");

// Send message
EventToken message = new EventToken();
message.setDefinitionId("newTaskMessage");
session.sendEventToken(message, processId);
do {
String choice = mainMenu();
if ("1".equals(choice)) {
taskList();
} else if ("2".equals(choice)) {
addTask(getResponse("Short description: "));
} else if ("x".equalsIgnoreCase(choice)) {
break;
} else {
System.out.println("\nUnknown command: " + choice);
}
} while (true);


// Allow the process to reach User Task node (Say Hello)
Thread.sleep(1000);

// Fetch the task
completeTask completeTask = (completeTask) session.getUserTasks().get(0);

// Complete user task
completeTask.complete();

// Allow the process to finish
Thread.sleep(1000);
// // Fetch the task
// CompleteTask CompleteTask = (CompleteTask) session.getUserTasks().get(0);
//
// // Complete user task
// CompleteTask.complete();
//
// // Allow the process to finish
// Thread.sleep(1000);

// Clean up: close session, entity manager factory and shutdown database
session.close();
Expand All @@ -63,4 +72,56 @@ public static void main(String[] args) throws SQLException, InterruptedException
}
connection.createStatement().execute("SHUTDOWN");
}

private static void taskList() {
do {
List<UserTask> tasks = session.getUserTasks();
if (tasks.size() > 0) {
System.out.println("\nSelect task\n-----------");
for (int i = 0; i < tasks.size(); ++i) {
System.out.println((i + 1) + " " + tasks.get(i).getSubject());
}
} else {
System.out.println("\nNo tasks found");
}
int choice = getIntResponse("0 - Main Menu\n>");
if (choice == 0) {
return;
}
if (choice >= tasks.size()) {
System.out.println("\nNo such task: " + choice);
}
completeTask(tasks.get(choice - 1));
} while (true);
}

private static void completeTask(UserTask userTask) {
System.out.println("\nTask completed: " + userTask.getSubject() + "\n");

}

private static String mainMenu() {
return getResponse("\nMain Menu\n=========\n1 - Task List\n2 - New Task\nX - Exit\n>");
}

private static String getResponse(String prompt) {
System.out.print(prompt);
return scanner.nextLine();
}

private static int getIntResponse(String prompt) {
System.out.print(prompt);
return scanner.nextInt();
}

private static void addTask(String subject) {
// Create process
long processId = session.createProcess("taskList");

// Send message
EventToken message = new EventToken();
message.setDefinitionId("newTaskMessage");
message.getData().put("subject", subject);
session.sendEventToken(message, processId);
}
}
11 changes: 10 additions & 1 deletion example/tasklist/src/com/worktoken/tasklist/completeTask.java
Expand Up @@ -22,16 +22,25 @@
import com.worktoken.model.WorkToken;

import javax.persistence.Entity;
import javax.security.auth.Subject;

/**
* @author Alex Pavlov (alex@rushproject.com)
*/
@FlowElement(nodeRef = "completeTask", processId = "taskList")
@Entity
public class completeTask extends UserTask {
public class CompleteTask extends UserTask {

private String subject;

@Override
public void tokenIn(WorkToken token, Connector connector) {
subject = token.getData().get("subject").toString();
}

@Override
public String getSubject() {
return subject;
}

public void complete() {
Expand Down
12 changes: 5 additions & 7 deletions example/tasklist/src/com/worktoken/tasklist/tasklist.bpmn
Expand Up @@ -17,18 +17,16 @@
<outgoing>_2_9</outgoing>
</userTask>

<sequenceFlow id="_2_5" name="" sourceRef="start" targetRef="completeTask"/>

<endEvent id="end" name="End">
<incoming>_2_7</incoming>
<incoming>_2_10</incoming>
</endEvent>

<scriptTask id="archiveTask" name="Archive task">
<incoming>_2_9</incoming>
<outgoing>_2_10</outgoing>
</scriptTask>

<endEvent id="end" name="End">
<incoming>_2_10</incoming>
</endEvent>

<sequenceFlow id="_2_5" name="" sourceRef="start" targetRef="completeTask"/>
<sequenceFlow id="_2_9" name="" sourceRef="completeTask" targetRef="archiveTask" />
<sequenceFlow id="_2_10" name="" sourceRef="archiveTask" targetRef="end" />
</process>
Expand Down

0 comments on commit dcf19de

Please sign in to comment.