cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Check if Creater has added himself has a Promotion Approver?

SOLVED
Highlighted
Newbie

How to Check if Creater has added himself has a Promotion Approver?

Hi Community,

I have a requirement in a workflow where a user (Creator) receives a task to select the Promotion Approvers. I am writing validation where i want to check if the user has not selected himself as an promotion approver?

Anyone has the code snippet for this? Looks like this:-

public ArrayList getRoles() throws WTException {

wt.project.Role[] mandatoryRoles = {wt.project.Role.toRole("PROMOTION APPROVERS")};

for (Role role: mandatoryRoles){

EnumeratedType[] displays = role.getValueSet();

for (int i = 0; i < displays.length; i++) {

EnumeratedType enumeratedType = displays[i];

Tags (1)
1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to Check if Creater has added himself has a Promotion Approver?

Hi Ishaan,

I have used something like below before to remove people from the Promotion Approver role if they are in the "Owner" role - i.e. the person who made the request. It retrieves the people in each role, then loops through them to determine if there is someone in both roles. I wrote the below code a long time ago and there is definitely a neater approach. To use it in this format you need to put it in a class file. To use it in an expression robot, remove the imports, replace the class references in the code with their fully qualified names and merge the two functions into one block of code.

import java.util.Enumeration;

import java.util.Random;

import java.util.Vector;

import wt.fc.WTObject;

import wt.org.StandardOrganizationServicesManager;

import wt.org.WTGroup;

import wt.org.WTOrganization;

import wt.org.WTPrincipal;

import wt.org.WTPrincipalReference;

import wt.project.Role;

import wt.team.Team;

import wt.team.TeamManaged;

/**

* This method removes a Principal argument from an argument Role in an agument Team.

*

* @param team the Team which the target role belongs to.

* @param role the role in the target team from which the principal is to be removed.

* @param participant the Principal to be removed infromto the role in the target team.

*/

public static void removeParticipant(Team team, Role role, WTPrincipal participant) throws

WTException {

team.deletePrincipalTarget(role, participant);

}

/**

* This method compares the sets of participants in two Role areguments in the same team.<br />

* It will then remove matches or mismatches - depending on the boolean arguments provided.

*

* @param obj the Object containing the Team

* @param RoleAStr a String containing the name of the comparative role.

* @param RoleBStr a String containing the name of the target role - the role from which principals may be removed.

* @param removeMatches boolean determining whether matches should be removed.

* @param removeMisMatches boolean determining whether mismatches should be removed.

*/

public static void compareParticipants(WTObject obj, String roleAStr, String roleBStr,

boolean removeMatches, boolean removeMisMatches) throws WTException {

// Only want to apply if object has a team.

if (obj instanceof TeamManaged) {

Team team = wt.team.TeamHelper.service.getTeam((TeamManaged)obj);

Role roleA = Role.toRole(roleAStr); // Benchmark role - participants are unaffected.

Role roleB = Role.toRole(roleBStr); // Role where matches / mis-matches are removed from.

// Determine whether to remove matches or mis-matches.

if (removeMatches && removeMisMatches) {

// Remove every participant from Role "B".

removeAllParticipants(obj, roleBStr);

} else if (removeMatches || removeMisMatches) {

// Remove matches OR mis-matches - not both.

Enumeration<?> principalsA = team.getPrincipalTarget(roleA);

Enumeration<?> principalsB = team.getPrincipalTarget(roleB);

// Cycle through each participant in role "A".

while (principalsA.hasMoreElements()) {

WTPrincipalReference participantARef = (WTPrincipalReference)principalsA.nextElement();

WTPrincipal participantA = participantARef.getPrincipal();

System.out.println("COMPARE PARTICIPANTS >> Principal A: " +

participantA.getPrincipalDisplayIdentifier());

// Cycle through each participant in role "B".

while (principalsB.hasMoreElements()) {

WTPrincipalReference participantBRef =

(WTPrincipalReference)principalsB.nextElement();

WTPrincipal participantB = participantBRef.getPrincipal();

System.out.println("COMPARE PARTICIPANTS >> Principal B: " +

participantB.getPrincipalDisplayIdentifier());

// Compare particpant in role "A" and "B".

if (participantA.equals(participantB)) {

System.out.println("COMPARE PARTICIPANTS >> Snap! Participant Match");

if (removeMatches) {

// Match found and match to be removed.

System.out.println("COMPARE PARTICIPANTS >> Match Removed");

removeParticipant(team, roleB, participantB);

}

} else {

System.out.println("COMPARE PARTICIPANTS >> Participant Mis-Match");

if (removeMisMatches) {

// Mis-match found and mis-match to be removed.

System.out.println("COMPARE PARTICIPANTS >> Mis-Match Removed");

removeParticipant(team, roleB, participantB);

}

}

}

}

} else {

// Do not remove matches or mis-matches - Do nothing!

System.out.println("COMPARE PARTICIPANTS >> " +

"Neither matches or mis-matches to be removed!");

}

}

}

(Sorry the indentation got lost in the pasting).

You may want to count if you have any matches, in which case replace the highlighted with a counter.

Regards,

Toby

1 REPLY 1

Re: How to Check if Creater has added himself has a Promotion Approver?

Hi Ishaan,

I have used something like below before to remove people from the Promotion Approver role if they are in the "Owner" role - i.e. the person who made the request. It retrieves the people in each role, then loops through them to determine if there is someone in both roles. I wrote the below code a long time ago and there is definitely a neater approach. To use it in this format you need to put it in a class file. To use it in an expression robot, remove the imports, replace the class references in the code with their fully qualified names and merge the two functions into one block of code.

import java.util.Enumeration;

import java.util.Random;

import java.util.Vector;

import wt.fc.WTObject;

import wt.org.StandardOrganizationServicesManager;

import wt.org.WTGroup;

import wt.org.WTOrganization;

import wt.org.WTPrincipal;

import wt.org.WTPrincipalReference;

import wt.project.Role;

import wt.team.Team;

import wt.team.TeamManaged;

/**

* This method removes a Principal argument from an argument Role in an agument Team.

*

* @param team the Team which the target role belongs to.

* @param role the role in the target team from which the principal is to be removed.

* @param participant the Principal to be removed infromto the role in the target team.

*/

public static void removeParticipant(Team team, Role role, WTPrincipal participant) throws

WTException {

team.deletePrincipalTarget(role, participant);

}

/**

* This method compares the sets of participants in two Role areguments in the same team.<br />

* It will then remove matches or mismatches - depending on the boolean arguments provided.

*

* @param obj the Object containing the Team

* @param RoleAStr a String containing the name of the comparative role.

* @param RoleBStr a String containing the name of the target role - the role from which principals may be removed.

* @param removeMatches boolean determining whether matches should be removed.

* @param removeMisMatches boolean determining whether mismatches should be removed.

*/

public static void compareParticipants(WTObject obj, String roleAStr, String roleBStr,

boolean removeMatches, boolean removeMisMatches) throws WTException {

// Only want to apply if object has a team.

if (obj instanceof TeamManaged) {

Team team = wt.team.TeamHelper.service.getTeam((TeamManaged)obj);

Role roleA = Role.toRole(roleAStr); // Benchmark role - participants are unaffected.

Role roleB = Role.toRole(roleBStr); // Role where matches / mis-matches are removed from.

// Determine whether to remove matches or mis-matches.

if (removeMatches && removeMisMatches) {

// Remove every participant from Role "B".

removeAllParticipants(obj, roleBStr);

} else if (removeMatches || removeMisMatches) {

// Remove matches OR mis-matches - not both.

Enumeration<?> principalsA = team.getPrincipalTarget(roleA);

Enumeration<?> principalsB = team.getPrincipalTarget(roleB);

// Cycle through each participant in role "A".

while (principalsA.hasMoreElements()) {

WTPrincipalReference participantARef = (WTPrincipalReference)principalsA.nextElement();

WTPrincipal participantA = participantARef.getPrincipal();

System.out.println("COMPARE PARTICIPANTS >> Principal A: " +

participantA.getPrincipalDisplayIdentifier());

// Cycle through each participant in role "B".

while (principalsB.hasMoreElements()) {

WTPrincipalReference participantBRef =

(WTPrincipalReference)principalsB.nextElement();

WTPrincipal participantB = participantBRef.getPrincipal();

System.out.println("COMPARE PARTICIPANTS >> Principal B: " +

participantB.getPrincipalDisplayIdentifier());

// Compare particpant in role "A" and "B".

if (participantA.equals(participantB)) {

System.out.println("COMPARE PARTICIPANTS >> Snap! Participant Match");

if (removeMatches) {

// Match found and match to be removed.

System.out.println("COMPARE PARTICIPANTS >> Match Removed");

removeParticipant(team, roleB, participantB);

}

} else {

System.out.println("COMPARE PARTICIPANTS >> Participant Mis-Match");

if (removeMisMatches) {

// Mis-match found and mis-match to be removed.

System.out.println("COMPARE PARTICIPANTS >> Mis-Match Removed");

removeParticipant(team, roleB, participantB);

}

}

}

}

} else {

// Do not remove matches or mis-matches - Do nothing!

System.out.println("COMPARE PARTICIPANTS >> " +

"Neither matches or mis-matches to be removed!");

}

}

}

(Sorry the indentation got lost in the pasting).

You may want to count if you have any matches, in which case replace the highlighted with a counter.

Regards,

Toby