Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Stardust/Knowledge Base/API/JavaAPICookbook/QueryingWorklist Utility

Introduction

The following program retrieves and prints a list of WorkItems (Activity Instances) for a given combination of user, partition, role and department. The search is therefore restricted to roles defined in active models (those with active process instances) where the user is assigned the specified role and is limited by the given department scope.

Utility

package com.test;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
import org.eclipse.stardust.engine.api.dto.ActivityInstanceDetails;
import org.eclipse.stardust.engine.api.dto.OrganizationDetails;
import org.eclipse.stardust.engine.api.model.Participant;
import org.eclipse.stardust.engine.api.model.Role;
import org.eclipse.stardust.engine.api.query.DataFilter;
import org.eclipse.stardust.engine.api.query.DeployedModelQuery;
import org.eclipse.stardust.engine.api.query.DeployedModelQuery.DeployedModelState;
import org.eclipse.stardust.engine.api.query.PerformingParticipantFilter;
import org.eclipse.stardust.engine.api.query.UserQuery;
import org.eclipse.stardust.engine.api.query.Worklist;
import org.eclipse.stardust.engine.api.query.WorklistQuery;
import org.eclipse.stardust.engine.api.runtime.DeployedModel;
import org.eclipse.stardust.engine.api.runtime.DeployedModelDescription;
import org.eclipse.stardust.engine.api.runtime.Grant;
import org.eclipse.stardust.engine.api.runtime.Models;
import org.eclipse.stardust.engine.api.runtime.QueryService;
import org.eclipse.stardust.engine.api.runtime.ServiceFactory;
import org.eclipse.stardust.engine.api.runtime.ServiceFactoryLocator;
import org.eclipse.stardust.engine.api.runtime.User;
import org.eclipse.stardust.engine.api.runtime.WorkflowService;
import org.eclipse.stardust.engine.core.runtime.beans.removethis.SecurityProperties;
 
public class QueryClient {
 
	public static void main(String args[]) throws Exception {
		if(args.length < 5) throw new Exception("Usage: java QueryClient <username> <password> 
                <partition> <role> <department>");
		Map<String,String> properties = new HashMap<String,String>();
		properties.put(SecurityProperties.PARTITION, args[2]);
		ServiceFactory sf = ServiceFactoryLocator.get(args[0], args[1], properties);
		QueryService qs = sf.getQueryService();
		WorkflowService ws = sf.getWorkflowService();
		UserQuery uquery = new UserQuery();
		uquery.getFilter().add(UserQuery.ACCOUNT.isEqual(args[0]));
		User user = qs.findFirstUser(uquery);
 
		List<Grant> grants = user.getAllGrants();
		Grant reqGrant = null;
		for(Grant grant : grants){
			if(grant.getId().equals(args[3]) && grant.getDepartment() != null &&
                grant.getDepartment().getId().equals(args[4])) { 
				reqGrant = grant;
				break;
			}
		}
		if(reqGrant == null) throw new Exception("Combination of role/department for specified
                user is invalid.");
 
		DeployedModelQuery dmquery = DeployedModelQuery.findInState(DeployedModelState.ALIVE);
		Models models = qs.getModels(dmquery);
		List<Integer> modelOIDList = new ArrayList<Integer>();
		for(DeployedModelDescription model : models) {
			modelOIDList.add(model.getModelOID());
		}
 
		List<Participant> participants = null;
		Participant queryParticipant = null;
		List<ActivityInstanceDetails> workListItems = new ArrayList();
		String dataId  = null;
		String dataPath = null;
		for(long modelOID : modelOIDList) {
			participants = qs.getAllParticipants(modelOID);
			DeployedModel model = qs.getModel(modelOID);
			List<OrganizationDetails> orgDetails = model.getAllOrganizations();
			for(OrganizationDetails orgDetail : orgDetails) {
				Map<String, Object> orgAttributes = orgDetail.getAllAttributes();
				if(orgAttributes.containsKey("carnot:engine:bound") &&
                                orgAttributes.containsKey("carnot:engine:dataId")
				&& (Boolean)orgAttributes.get("carnot:engine:bound")==true) {
					dataId = (String) orgAttributes.get("carnot:engine:dataId");
					if(orgAttributes.containsKey("carnot:engine:dataPath"))
						dataPath = (String) 
                                           orgAttributes.get("carnot:engine:dataPath");
 					   break;
				}
			}
			for(Participant modelParticipant : participants) {
				if(modelParticipant instanceof Role) {
					if(((Role)modelParticipant).getId().equals(reqGrant.getId())) {
						queryParticipant = modelParticipant;
						break;
					}
				}
			}
		}
		if(dataId == null) throw new Exception("Unable to find bind data for department in any live model");
		if(queryParticipant == null) throw new Exception("Unable to resolve Participant for any live model");
		WorklistQuery wlsq = WorklistQuery.findCompleteWorklist();
		wlsq.setUserContribution(false);
		wlsq.setParticipantContribution(PerformingParticipantFilter.forParticipant(queryParticipant));
		if(dataPath == null)
			wlsq.getFilter().add(DataFilter.isEqual(dataId, reqGrant.getDepartment().getId()));
		else
			wlsq.getFilter().add(DataFilter.isEqual(dataId, dataPath, reqGrant.getDepartment().getId()));
		Worklist worklist = ws.getWorklist(wlsq);
		Iterator worklistIter = worklist.getCumulatedItems().iterator();
		while(worklistIter.hasNext())
			workListItems.add((ActivityInstanceDetails)(worklistIter.next()));
 
		for(ActivityInstanceDetails details : workListItems)
			System.out.println(details.getOID());
	}
}

Back to the top