/**
* Copyright 2010 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.integration.console;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.drools.SystemEventListenerFactory;
import org.drools.process.workitem.wsht.BlockingGetTaskResponseHandler;
import org.drools.task.AccessType;
import org.drools.task.Status;
import org.drools.task.Task;
import org.drools.task.query.TaskSummary;
import org.drools.task.service.ContentData;
import org.drools.task.service.TaskClient;
import org.drools.task.service.mina.MinaTaskClientConnector;
import org.drools.task.service.mina.MinaTaskClientHandler;
import org.drools.task.service.responsehandlers.BlockingTaskOperationResponseHandler;
import org.drools.task.service.responsehandlers.BlockingTaskSummaryResponseHandler;
import org.jboss.bpm.console.client.model.TaskRef;
import org.jboss.bpm.console.server.integration.TaskManagement;
public class DroolsFlowTaskManagement implements TaskManagement {
// TODO: make this configurable
private String ipAddress = "127.0.0.1";
private int port = 9123;
private TaskClient client;
public void setConnection(String ipAddress, int port) {
this.ipAddress = ipAddress;
this.port = port;
}
public void connect() {
if (client == null) {
client = new TaskClient(new MinaTaskClientConnector("org.drools.process.workitem.wsht.WSHumanTaskHandler",
new MinaTaskClientHandler(SystemEventListenerFactory.getSystemEventListener())));
boolean connected = client.connect(ipAddress, port);
if (!connected) {
throw new IllegalArgumentException(
"Could not connect task client");
}
}
}
public TaskRef getTaskById(long taskId) {
connect();
BlockingGetTaskResponseHandler responseHandler = new BlockingGetTaskResponseHandler();
client.getTask(taskId, responseHandler);
Task task = responseHandler.getTask();
return DroolsFlowTransform.task(task);
}
public void assignTask(long taskId, String idRef, String userId) {
connect();
BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler();
if (idRef == null) {
client.release(taskId, userId, responseHandler);
} else if (idRef.equals(userId)) {
client.claim(taskId, idRef, responseHandler);
} else {
client.delegate(taskId, userId, idRef, responseHandler);
}
responseHandler.waitTillDone(5000);
}
@SuppressWarnings("unchecked")
public void completeTask(long taskId, Map data, String userId) {
connect();
BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler();
client.start(taskId, userId, responseHandler);
responseHandler.waitTillDone(5000);
responseHandler = new BlockingTaskOperationResponseHandler();
ContentData contentData = null;
if (data != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out;
try {
out = new ObjectOutputStream(bos);
out.writeObject(data);
out.close();
contentData = new ContentData();
contentData.setContent(bos.toByteArray());
contentData.setAccessType(AccessType.Inline);
} catch (IOException e) {
e.printStackTrace();
}
}
client.complete(taskId, userId, contentData, responseHandler);
responseHandler.waitTillDone(5000);
}
@SuppressWarnings("unchecked")
public void completeTask(long taskId, String outcome, Map data, String userId) {
data.put("outcome", outcome);
completeTask(taskId, data, userId);
}
public void releaseTask(long taskId, String userId) {
// TODO: this method is not being invoked, it's using
// assignTask with null parameter instead
connect();
BlockingTaskOperationResponseHandler responseHandler = new BlockingTaskOperationResponseHandler();
client.release(taskId, userId, responseHandler);
responseHandler.waitTillDone(5000);
}
public List<TaskRef> getAssignedTasks(String idRef) {
connect();
List<TaskRef> result = new ArrayList<TaskRef>();
try {
BlockingTaskSummaryResponseHandler responseHandler = new BlockingTaskSummaryResponseHandler();
client.getTasksOwned(idRef, "en-UK", responseHandler);
List<TaskSummary> tasks = responseHandler.getResults();
for (TaskSummary task: tasks) {
if (task.getStatus() == Status.Reserved) {
result.add(DroolsFlowTransform.task(task));
}
}
} catch (Throwable t) {
t.printStackTrace();
}
return result;
}
public List<TaskRef> getUnassignedTasks(String idRef, String participationType) {
// TODO participationType ?
connect();
List<TaskRef> result = new ArrayList<TaskRef>();
try {
BlockingTaskSummaryResponseHandler responseHandler = new BlockingTaskSummaryResponseHandler();
client.getTasksAssignedAsPotentialOwner(idRef, "en-UK", responseHandler);
List<TaskSummary> tasks = responseHandler.getResults();
for (TaskSummary task: tasks) {
result.add(DroolsFlowTransform.task(task));
}
} catch (Throwable t) {
t.printStackTrace();
}
return result;
}
}