Skip to end of metadata
Go to start of metadata

First you have to implement a help class:

package getAllPortalRoles;

import java.io.Serializable;
public class roleModel implements Serializable {


	private String Role;
	private String RoleID;
	private String Description;


	/**
	 * @return
	 */
	public String getRole() {
		return Role;
	}

	/**
	 * @return
	 */
	public String getRoleID() {
		return RoleID;
	}



	/**
	 * @param string
	 */
	public void setRole(String string) {
		Role = string;
	}

	/**
	 * @param string
	 */
	public void setRoleID(String string) {
		RoleID = string;
	}

	/**
	 * @return
	 */
	public String getDescription() {
		return Description;
	}

	/**
	 * @param string
	 */
	public void setDescription(String string) {
		Description = string;
	}

}

Then implement the Business method for getting all portal roles:

package getAllPortalRoles;
import java.util.ArrayList;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import com.sap.security.api.IRole;
import com.sap.security.api.IRoleFactory;
import com.sap.security.api.IRoleSearchFilter;
import com.sap.security.api.ISearchAttribute;
import com.sap.security.api.ISearchResult;
import com.sap.security.api.UMFactory;

/**
 * @ejbHome <{getAllPortalRoles.GetAllPortalRolesHome}>
 * @ejbLocal <{getAllPortalRoles.GetAllPortalRolesLocal}>
 * @ejbLocalHome <{getAllPortalRoles.GetAllPortalRolesLocalHome}>
 * @ejbRemote <{getAllPortalRoles.GetAllPortalRoles}>
 * @stateless
 * @transactionType Container
 */


public class GetAllPortalRolesBean implements SessionBean {

	public void ejbRemove() {
	}

	public void ejbActivate() {
	}

	public void ejbPassivate() {
	}

	public void setSessionContext(SessionContext context) {
		myContext = context;
	}

	private SessionContext myContext;
	/**
	 * Create Method.
	 */
	public void ejbCreate() throws CreateException {
		// TODO : Implement
	}



	/**
	 * Business Method.
	 */
	public roleModel[] getRoles() throws Exception{
			// Create new array list for roles
			ArrayList RoleList = new ArrayList();
			try {
				IRoleFactory rfact = UMFactory.getRoleFactory();
				// define filter for portal roles; using * for all portal Roles
				IRoleSearchFilter roleFilter = rfact.getRoleSearchFilter();
				roleFilter.setUniqueName("*", ISearchAttribute.EQUALS_OPERATOR, false);
				ISearchResult resultList = rfact.searchRoles(roleFilter);
				// Passing the result list to the array list
				if (resultList.getState() == ISearchResult.SEARCH_RESULT_OK) {
					while (resultList.hasNext()) {
						// read roleUid
						String roleUid = (String) resultList.next();
						// create roleObject for roleUid
						IRole roleObject = rfact.getRole(roleUid);
						// instantiate new role for array list
						roleModel rolle = new roleModel();
						// read DisplayName and UniqeID of the the portal role and set it to the portal role object
						rolle.setRole(roleObject.getDisplayName());
						rolle.setRoleID(roleObject.getUniqueID());
						rolle.setDescription(roleObject.getDescription());
						// add the role object to the array list
						RoleList.add(rolle);
					}
				}
				// create new array of the data type roleModel with the corresponding size
				roleModel[] returnList = new roleModel[RoleList.size()];
				// move ArrayList into the created array
				RoleList.toArray(returnList);
				// retrun all portal roles
				return returnList;
			// throw an exception if an error occurs
			} catch (Exception e) {
				throw new Exception(e);
			}
	}
}