Skip to main content

Java_Admin_Principal_Reviewer_Remove.java

/*****************************************************************************************
*
* Java_Admin_Principal_Reviewer_Remove.java
*
******************************************************************************************/

import com.eruces.teagent.TEAgentConnection;
import com.eruces.teagent.TEAgentException;
import com.eruces.teagent.TEAgentConnectionFactory;
import com.eruces.teadmin.Administration;
import com.eruces.teadmin.AdminException;
import com.eruces.teadmin.UserAndPasswordInfo;
import com.eruces.teagent.PAny;
import java.util.Iterator;
import com.eruces.teagent.PList;
import java.util.TreeSet;

public class Java_Admin_Principal_Reviewer_Remove {

private String m_strServerName;
private int m_nPort;
private String m_strUsername;
private String m_strPasswd;
private TEAgentConnection m_conn;

public void removeReviewer() throws TEAgentException, AdminException, Exception {
Administration admin = new Administration();
admin.attachConnection(m_conn);

// Get two native users for testing.
UserAndPasswordInfo principal = new UserAndPasswordInfo();
principal.setUserName("testuser");
principal = (UserAndPasswordInfo) admin.getPrincipal(principal);
if (principal == null) {
throw new Exception("Principal testuser was not found !");
}

UserAndPasswordInfo principal2 = new UserAndPasswordInfo();
principal2.setUserName("testuser2");
principal2 = (UserAndPasswordInfo) admin.getPrincipal(principal2);
if (principal2 == null) {
throw new Exception("Principal testuser2 was not found !");
}

System.out.println("");
System.out.println("Principal got:");
System.out.println(" " + principal.toString());

System.out.println("");
System.out.println("Principal got:");
System.out.println(" " + principal2.toString());

// Display reviewers after adding reviewer
principal = (UserAndPasswordInfo) admin.getPrincipal(principal);
PList reviewerIDs = principal.getReviewerIDs();
TreeSet reviewerSet = reviewerIDs.getAllElements();
System.out.println("Before removing reviewer, the principal have " + reviewerSet.size() + " reviewers.");
Iterator iter = reviewerSet.iterator();
while (iter.hasNext()) {
PAny panyID = (PAny) iter.next();
int id = panyID.getInt();
System.out.println("ReviewerID : " + id);
}

// make testuser2 a reviewer of testuser
principal.removeReviewer(principal2.getPrincipalID());
admin.updatePrincipal(principal);
System.out.println();
System.out.println("Reviewer was removed.");

// Display reviewers after removing reviewer
principal = (UserAndPasswordInfo) admin.getPrincipal(principal);
reviewerIDs = principal.getReviewerIDs();
reviewerSet = reviewerIDs.getAllElements();
System.out.println();
System.out.println("After removing reviewer, the principal have " + reviewerSet.size() + " reviewers.");
iter = reviewerSet.iterator();
while (iter.hasNext()) {
PAny panyID = (PAny) iter.next();
int id = panyID.getInt();
System.out.println("ReviewerID : " + id);
}

admin.detachConnection();
}

public void connect(String strServerName, int nPort, String strUsername, String strPasswd) throws TEAgentException {
m_strServerName = strServerName;
m_nPort = nPort;
m_strUsername = strUsername;
m_strPasswd = strPasswd;

m_conn = TEAgentConnectionFactory.getInstance(TEAgentConnectionFactory.SECURE_USER_PASSWORD);
// Connect to server.
m_conn.open(m_strServerName, m_nPort, m_strUsername, m_strPasswd);
System.out.println("Connected to " + m_strServerName + " at " + m_nPort + " as " + m_strUsername);
}

public void disconnect() throws TEAgentException {
m_conn.close(); // disconnect from the server.
System.out.println("Disconnected from " + m_strServerName);
}

public static void main(String[] args) {
try {
Java_Admin_Principal_Reviewer_Remove test = new Java_Admin_Principal_Reviewer_Remove();
test.connect("sampleserver", 8888, "admin", "password1A");
test.removeReviewer();
test.disconnect();
}
catch (TEAgentException te) {
System.out.println("TEAgentException encountered: ");
te.printStackTrace();
}
catch (AdminException te) {
System.out.println("AdminException encountered: ");
te.printStackTrace();
}
catch (Exception ex) {
System.out.println("Exception encountered: ");
ex.printStackTrace();
}
}
}


/*
Connected to sampleserver at 8888 as admin

Principal got:
UserAndPasswordInfo:
UpdatableObject: <user,>
<System ID: 1 >
<Principal ID: 3004 >
<Role ID List: PList: {[<9,2>]} >
<Group ID List: PList: {[]} >
<Default Group ID: 0 >
<Reviewer ID List: PList: {[<9,3007>]} >
<User Name: testuser >


Principal got:
UserAndPasswordInfo:
UpdatableObject: <user,>
<System ID: 1 >
<Principal ID: 3007 >
<Role ID List: PList: {[<9,2>]} >
<Group ID List: PList: {[]} >
<Default Group ID: 0 >
<Reviewer ID List: PList: {[]} >
<User Name: testuser2 >

Before removing reviewer, the principal have 1 reviewers.
ReviewerID : 3007

Reviewer was removed.

After removing reviewer, the principal have 0 reviewers.
Disconnected from sampleserver

*/