/**
* Examples for getting Perforce Groups.
*/
package com.perforce.jbrown.examples;
import java.util.List;
import com.perforce.jbrown.utils.LameServerFactory;
import com.perforce.jbrown.utils.LogCallBack;
import com.perforce.jbrown.workarounds.GetGroupsMapCmd;
import com.perforce.p4java.core.IUserGroup;
import com.perforce.p4java.option.server.GetUserGroupsOptions;
import com.perforce.p4java.server.IOptionsServer;
public class GetGroups {
public static void main(String[] args) {
com.perforce.p4java.Log.setLogCallback(new LogCallBack());
try {
IOptionsServer server = LameServerFactory.getServer();
// client not required for getting groups.
GetUserGroupsOptions groupOpts = null;
List<IUserGroup> groups = null;
/**
* Get all the groups and print them. This is the normal way
* to retrieve all groups.
*/
groupOpts = new GetUserGroupsOptions();
groups = server.getUserGroups(null, groupOpts) ;
printGroups("getUserGroups() returns:", groups);
/**
* Get all the groups that user 'p4dtguser' belongs to, directly or indirectly.
*/
groupOpts = new GetUserGroupsOptions().setUserName(true).setIndirect(true);
groups = server.getUserGroups("p4dtguser", groupOpts) ;
printGroups("getUserGroups() for user p4dtguser:", groups);
/**
* Workaround for P4Java job087545 (Subgroups not correct)
*/
groupOpts = new GetUserGroupsOptions();
groups = GetGroupsMapCmd.getUserGroups(server, null, groupOpts);
printGroups("workaround for getUserGroups() returns:", groups);
/**
* Also job087545 workaround for get the groups that user 'p4dtguser'
*/
groupOpts = new GetUserGroupsOptions().setUserName(true).setIndirect(true);
groups = GetGroupsMapCmd.getUserGroups(server, "p4dtguser", groupOpts) ;
printGroups("getUserGroups() for user p4dtguser:", groups);
} catch (Throwable e) {
System.out.println("Exeception: " + e.getMessage());
e.printStackTrace(System.out);
}
}
/***
* print simple info about a group - users, subgroups, and ticket timeout
* @param title
* @param groups
*/
public static void printGroups(String title, List<IUserGroup> groups) {
System.out.println("\n---------------- ");
System.out.println(title);
for (IUserGroup group : groups) {
System.out.println("\n" + group.getName());
System.out.println(" Timeout: " + group.getTimeout());
StringBuilder sb = new StringBuilder();
for (String user : group.getUsers()) {
sb.append(user).append(" ");
}
System.out.println( " Users: " + sb);
sb = new StringBuilder();
for (String subgroup : group.getSubgroups()) {
sb.append(subgroup).append(" ");
}
System.out.println(" Subgroups: " + sb);
}
}
}