/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.integration.console;
import org.jboss.bpm.console.client.model.*;
import org.jbpm.api.Deployment;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.job.Job;
import org.jbpm.api.task.Participation;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.ProcessDefinitionImpl;
import org.jbpm.pvm.internal.model.Transition;
import org.jbpm.pvm.internal.repository.DeploymentImpl;
import org.jbpm.pvm.internal.task.TaskImpl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
/**
* @author Heiko.Braun <heiko.braun@jboss.com>
*/
public class ModelAdaptor
{
public static ProcessDefinitionRef adoptDefinition(ProcessDefinition processDefinition)
{
ProcessDefinitionRef def = new ProcessDefinitionRef();
def.setId( processDefinition.getId() );
def.setName(processDefinition.getName());
def.setVersion(processDefinition.getVersion());
def.setKey( processDefinition.getKey() );
// TODO: why is that not part of the API and requires cast?
ProcessDefinitionImpl cast = (ProcessDefinitionImpl)processDefinition;
//def.setDescription(processDefinition.getDescription());
def.setPackageName(cast.getPackageName());
def.setDeploymentId(String.valueOf(cast.getDeploymentId()));
return def;
}
public static ProcessInstanceRef adoptExecution(ExecutionImpl e0)
{
ProcessInstanceRef ref = new ProcessInstanceRef();
ref.setId( e0.getId() );
ref.setKey(e0.getKey());
ref.setDefinitionId(e0.getProcessDefinition().getId() );
ref.setStartDate( new Date() ); // TODO: FIXME
ExecutionImpl topLevelExecution = e0.getProcessInstance();
TokenReference tok = execution2TokenReference(topLevelExecution);
Collection<ExecutionImpl> childExecutions = (Collection)topLevelExecution.getExecutions();
if(childExecutions!=null)
{
for(ExecutionImpl childExecution : childExecutions)
{
TokenReference childTok = execution2TokenReference(childExecution);
tok.getChildren().add(childTok);
}
}
ref.setRootToken(tok);
return ref;
}
private static TokenReference execution2TokenReference(ExecutionImpl topLevelExecution)
{
TokenReference tok = new TokenReference();
tok.setName(topLevelExecution.getName());
tok.setId(topLevelExecution.getId());
tok.setCurrentNodeName( topLevelExecution.getActivityName() );
// transitions
List<String> availableSignals = new ArrayList<String>();
ExecutionImpl openTopLevelExecution = (ExecutionImpl) topLevelExecution;
List<Transition> outTransitions = openTopLevelExecution.getActivity().getOutgoingTransitions();
if(outTransitions!=null) // crappy jBPM API
{
for(Transition t : outTransitions)
{
// TODO: Fix when https://jira.jboss.org/jira/browse/JBPM-2220 is done
String transitionName = t.getName()!=null ? t.getName() : "to_"+t.getDestination().getName();
availableSignals.add(transitionName);
}
}
tok.setAvailableSignals(availableSignals);
return tok;
}
public static TaskRef adoptTask(Task t0)
{
TaskRef task = new TaskRef();
task.setId( ((TaskImpl)t0).getDbid() );
task.setName( t0.getName());
task.setAssignee( t0.getAssignee() );
TaskImpl cast = ((TaskImpl) t0);
task.setSignalling( cast.isSignalling());
ExecutionImpl execution = cast.getProcessInstance();
task.setProcessInstanceId( cast.getProcessInstance().getId() );
// TODO: weird API
task.setProcessId( execution.getProcessInstance().getProcessDefinition().getId() );
// participations
for(Participation p0 : cast.getParticipations())
{
if(p0.getType().equals(Participation.CANDIDATE))
{
if(p0.getGroupId()!=null)
{
ParticipantRef participant = new ParticipantRef("candidate", p0.getGroupId());
participant.setGroup(true);
task.getParticipantGroups().add(participant);
}
else if(p0.getUserId()!=null)
{
ParticipantRef participant = new ParticipantRef("candidate", p0.getUserId());
task.getParticipantUsers().add(participant);
}
else
{
throw new IllegalArgumentException("Participation doesn't have user or group: " + p0);
}
}
else
{
throw new IllegalArgumentException("Unknown participation type: " +p0.getType());
}
}
// prio and duedate
task.setPriority(t0.getPriority());
task.setDueDate(t0.getDuedate());
task.setCreateDate(t0.getCreateTime());
// task formResourceName url
String url = t0.getFormResourceName()!=null ? t0.getFormResourceName() : "";
task.setUrl( url );
return task;
}
public static DeploymentRef adoptDeployment(Deployment dpl)
{
DeploymentImpl d0 = (DeploymentImpl)dpl;
DeploymentRef dRef = new DeploymentRef(
dpl.getId(), d0.isSuspended()
);
String name = d0.getName();
dRef.getResourceNames().addAll(d0.getResourceNames());
// strip path info
if(name.indexOf("/")!=-1)
{
name = name.substring(name.lastIndexOf("/")+1, name.length());
}
dRef.setName(name);
dRef.setTimestamp(d0.getTimestamp());
return dRef;
}
public static JobRef adoptJob(Job j)
{
JobRef job = new JobRef();
job.setId(String.valueOf(j.getId()));
if(j.getDueDate()!=null)
{
job.setTimestamp(j.getDueDate().getTime());
}
if(j.getException()!=null)
{
job.setErrMsg(j.getException());
}
return job;
}
}