Stardust/Knowledge Base/API/ExamplesofAPIUsage/GeneratingRoleBasedAPI

From Eclipsepedia

Jump to: navigation, search

Purpose

This Example describes how to filter activity instances for every user role (grant) he is assigned to.

Solution

//Create user query (use attribute account which is equal to your CARNOT login name)
UserQuery userQuery = new UserQuery();
userQuery.getFilter().add(UserQuery.ACCOUNT.isEqual("torben"));
 
//Get user
Users users = queryService.getAllUsers(userQuery);
User user = (User) users.get(0);
 
//Get user grants
List grants = user.getAllGrants();
 
//Create activity query
ActivityInstanceQuery activityQuery = new ActivityInstanceQuery();
 
//Add OR filter to activity query
FilterOrTerm orFilter = activityQuery.getFilter().addOrTerm();
 
//Extract single roles and build STARDUST query
for (int x = 0; x < grants.size(); x++) {
	String grant = ((Grant) grants.get(x)).getId();
	PerformingParticipantFilter performerFilter = PerformingParticipantFilter.forModelParticipant(grant);
	orFilter.or(performerFilter);
}
 
//Find activities for user roles
ActivityInstances activeInstances = queryService.getAllActivityInstances(activityQuery);
 
//Get single activity informations
for (int y = 0; y < activeInstances.getSize(); y++) {
	ActivityInstance activity = (ActivityInstance) activeInstances.get(y);
	System.out.println(y +") Activity OID: "+ activity.getOID() +" / Performer: "+ activity.getPerformedByName());
}

OR

//Create query service
ServiceFactory serviceFactory = ServiceFactoryLocator.get("motu", "motu");
QueryService queryService = serviceFactory.getQueryService();
 
//Create activity query
ActivityInstanceQuery activityQuery = new ActivityInstanceQuery();
 
//Add OR filter to activity query
FilterOrTerm orFilter = activityQuery.getFilter().addOrTerm();
orFilter.add(PerformingParticipantFilter.ANY_FOR_USER);
orFilter.add(PerformingUserFilter.CURRENT_USER);
 
//Find activities for user roles
ActivityInstances activeInstances = queryService.getAllActivityInstances(activityQuery);
 
//Get single activity informations
for (int y = 0; y < activeInstances.getSize(); y++) {
	ActivityInstance activity = (ActivityInstance) activeInstances.get(y);
	System.out.println(y +") Activity OID: "+ activity.getOID() +" / Performer: "+ activity.getPerformedByName());
}