diff --git a/example/tasklist/src/META-INF/persistence.xml b/example/tasklist/src/META-INF/persistence.xml index 28c8cb3..7d7b1a8 100644 --- a/example/tasklist/src/META-INF/persistence.xml +++ b/example/tasklist/src/META-INF/persistence.xml @@ -17,7 +17,7 @@ - + org.hibernate.ejb.HibernatePersistence com.worktoken.model.BusinessProcess com.worktoken.model.CatchEventNode @@ -33,7 +33,7 @@ com.worktoken.model.TimerTrigger com.worktoken.model.UserTask - + diff --git a/example/tasklist/src/com/worktoken/tasklist/Start.java b/example/tasklist/src/com/worktoken/tasklist/Start.java new file mode 100644 index 0000000..78d6e0e --- /dev/null +++ b/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); + } +} diff --git a/example/tasklist/src/com/worktoken/tasklist/TaskListApp.java b/example/tasklist/src/com/worktoken/tasklist/TaskListApp.java index cc84df2..2c42b08 100644 --- a/example/tasklist/src/com/worktoken/tasklist/TaskListApp.java +++ b/example/tasklist/src/com/worktoken/tasklist/TaskListApp.java @@ -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; @@ -12,6 +13,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.Scanner; import java.util.logging.LogManager; /** @@ -19,42 +21,49 @@ */ 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 annotatedClasses = new ArrayList(); - 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(); @@ -63,4 +72,56 @@ public static void main(String[] args) throws SQLException, InterruptedException } connection.createStatement().execute("SHUTDOWN"); } + + private static void taskList() { + do { + List 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); + } } diff --git a/example/tasklist/src/com/worktoken/tasklist/completeTask.java b/example/tasklist/src/com/worktoken/tasklist/completeTask.java index e7694d5..5f73277 100644 --- a/example/tasklist/src/com/worktoken/tasklist/completeTask.java +++ b/example/tasklist/src/com/worktoken/tasklist/completeTask.java @@ -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() { diff --git a/example/tasklist/src/com/worktoken/tasklist/tasklist.bpmn b/example/tasklist/src/com/worktoken/tasklist/tasklist.bpmn index c435b49..99e7946 100644 --- a/example/tasklist/src/com/worktoken/tasklist/tasklist.bpmn +++ b/example/tasklist/src/com/worktoken/tasklist/tasklist.bpmn @@ -17,18 +17,16 @@ _2_9 - - - - _2_7 - _2_10 - - _2_9 _2_10 + + _2_10 + + +