IntroductionUseful classes: ProcessService processService = ServiceFactory.getProcessService();
ActivityService activityService = ServiceFactory.getActivityService();
ProcessFinder processFinder =FinderFactory.getProcessFinder();
ActivityFinder activityFinder=FinderFactory.getActivityFinder(); |
Processes ProcessBuilderDefinition definition = new ProcessBuilderDefinition();
definition.setPackageId( "suncode" );
definition.setCreator( "admin" );
definition.setCreatorPassword( "xxxxxx" );
definition.setProcessDefId( "proces1" );
Map<String, Object> variables = new HashMap<String, Object>();
variables.put( "zmienna1", "abc" );
definition.setVariables( variables );
String processId = processService.createProcess( definition );
// Pobieranie informacji o procesie
Process process = processService.getProcess( processId );
process = processService.getProcess( processId, Process.JOIN_PROCESS_DEFINITION );// if we want to access
// to the process definition
String name = process.getName();
String desc = process.getDescription();
// Definicja procesu
ProcessDefinition processDef = process.getProcessDefinition();
String packageId = processDef.getPackageId();// without Process.JOIN_PROCESS_DEFINITION an exception will occur
String processDefId = processDef.getProcessDefinitionId();
// Zmiana wartości zmiennych procesu
Map<String, Object> ctx = processService.getProcessContext( processId );
ctx.put( "zmienna", "abc" );
processService.setProcessContext( processId, ctx );
//Usunięcie procesu
processService.deleteProcess( processId ); |
Use the ProcessFinder class to search for processes //define the values of the variables of the process we want to find
Map<String, Object> indexes = new HashMap<String, Object>();
indexes.put( "textcol", "text5" );
indexes.put( "datecol", DateUtils.parseDate( "2014-02-12", "yyyy-MM-dd" ) );
indexes.put( "doublecol", 4.3);
indexes.put( "intcol", 5 );
List<Process> processes = finder.findByIndexes( processDefId, indexes );
Process p = processes.get( 0 );//found process
//odczyt indeksów
String textcol = (String) p.getIndexValue( "textcol" );
Date datecol = (Date) p.getIndexValue( "datecol" );
Double doublecol = (Double) p.getIndexValue( "doublecol" );
Integer intcol = (Integer) p.getIndexValue( "intcol" ); |
The method presented above searches for processes with indices equal (operator '=' ) to the given values and combines all conditions with the logical operator AND. If we want to create more advanced search conditions, we can use the following method: List<IndexFilter> filters = new ArrayList<IndexFilter>();
filters.add( new SimpleIndexFilter( "datecol", date, FilterOperator.GT ) );// Greater then '>'
filters.add( new SimpleIndexFilter( "textcol", "%text%", FilterOperator.LIKE ) );
List<Process> result = finder.findByIndexes( processDefId, filters ); |
In the above example, we have defined search operators, but the conditions are still connected by a logical AND operator. To change this, we can use filter grouping: List<IndexFilter> filters = new ArrayList<IndexFilter>();
// create a group of conditions connected by OR operator
GroupIndexFilter gif = new GroupIndexFilter( LogicOperator.OR );
// create the first subgroup of conditions
GroupIndexFilter subGroup1 = new GroupIndexFilter( LogicOperator.AND );
subGroup1.addFilter( new SimpleIndexFilter( "datecol", date, FilterOperator.GT ) );
subGroup1.addFilter( new SimpleIndexFilter( "textcol", "%text%", FilterOperator.LIKE ) );
// create a second group of conditions
GroupIndexFilter subGroup2 = new GroupIndexFilter( LogicOperator.AND );// create another group of conditions
subGroup2.addFilter( new SimpleIndexFilter( "textcol", "text3" ) );
subGroup2.addFilter( new SimpleIndexFilter( "intcol", 3 ) );
gif.addFilter( subGroup1 );// to a group of conditions we can add another group creating a tree of conditions
gif.addFilter( subGroup2 );
filters.add( gif );
List<Process> result = finder.findByIndexes( processDefId, filters );
|
The above example will create the following condition: ( ( datecol > '2014-02-11' and textcol like '%text%' ) or ( textcol = 'text3' and intcol = 3 ) ) Tasks
// Downloading task information
Activity activity=activityService.getActivity( processId, activityId );
String activityDefId=activity.getActivityDefinitionId();
String desc=activity.getDescription();
String name=activity.getName();
ActivityState state=activity.getState();//RUNNING, NOT_STARTED, SUSPENDED, COMPLETED, TERMINATED, ABORTED
Date created=activity.getCreatedTime();//task date creation
Date started=activity.getStartedTime();//date of running
// Changing the value of task variables
Map<String,Object> activityContext=activityService.getActivityContext( processId, activityId );
activityContext.put( "zmienna", "wartość" );
activityService.setActivityContext( processId, activityId, activityContext );
//Open task by the specified user. If the task has a status of 'Pending launch'
//and is assigned to the specified user then the following function will assign the task
//to the specified user and change the status to 'Running'.
activityService.openActivity( userName, userPassword, processId, activityId );
//Task acceptance
String executor="jkowalski";//user accepting
String actionName="akceptacja";//map action name
Map<String,Object> map=new HashMap<String,Object>();
map.put( "zmienna", "wartosc" );//map will not be completely replaced by the specified variable
//only the specified values will be overwritten
AcceptationDefinition acceptation=new AcceptationDefinition( processId, activityId, executor, actionName );
acceptation.setContextMap( map );//optional
acceptation.setIgnoreValidators( false ); //optional force acceptance validation
//Task acceptance, on any user (ownerLogin), if user is the owner of the task (i.e.it was opened by him/her in PWFL)
String ownerLogin = "jkowalski";
AcceptationDefinition acceptation = new AcceptationDefinition( procId, activId, ownerLogin, actionName );
activityService.acceptActivity( acceptation );
//Accept the task, on any user (approverLogin), even if he is not the owner of the task
String approverLogin = "test";
assignmentService.assignActivityToUser( procId, activId, approverLogin );
AcceptationDefinition acceptation = new AcceptationDefinition( procId, activId, approverLogin, actionName );
acceptation.setIgnoreOwnerShip( true );
activityService.acceptActivity( acceptation );
|
The above-mentioned task acceptance cannot be started in the same transaction as the creation of the process in which the accepted task is involved. Then the acceptance will be successful, but in the logs there will be exceptions concerning system notifications, which will not be executed correctly. If you want the acceptance of a task to be executed immediately after the creation of the process for this task (e.g. in one automatic/scheduled task, etc.), then you need to force the creation of the process in a separate transaction. Which can be done according to the instructions Description of using transactions. |
Only component validators are considered when validating a task through the API. |
// Downloading open tasks
List<Activity> activities = activityFinder.findOpenedActivities( processId );
for ( Activity activity : activities )
{
activityId = activity.getActivityId();
}
// Downloading tasks assigned to a user
activities = activityFinder.findActivitiesAssignedToUser( userName, Activity.JOIN_PROCESS );
for ( Activity activity : activities )
{
activityId = activity.getActivityId();
Process process = activity.getProcess();// tylko gdy użyliśmy Activity.JOIN_PROCESS
}
// Downloading tasks from the process
activities = activityFinder.findByProcessId( processId, Activity.JOIN_PROCESS );
for ( Activity activity : activities )
{
activityId = activity.getActivityId();
Process process = activity.getProcess();// tylko gdy użyliśmy Activity.JOIN_PROCESS
} |
|