Express (Distributed operating systems), v8.0 > Secure applications and their environment > Authenticate users > Select a registry or repository > Manage realms in a federated repository > Virtual member manager > Develop with virtual member manager > Integrate virtual member manager into the application > Sample code


Sample code for creating an entity in a hierarchy and getting descendants and ancestors

Use the sample code snippet and data graphs to create an entity in a hierarchy and to get the descendants and ancestors of an entity by using the get() method.

The sample code snippet and data graphs cover the following steps:

  1. Create a hierarchy with entities of the OrgContainer entity type in the default realm.

  2. Create a child entities under the OrgContainer hierarchy.
  3. Get descendants of parent entity by using the get() method and DescendantControl.
  4. Get ancestors of child entity by using the get() method and AscendantControl.


Prerequisites

Ensure that we have read the information and completed the steps described in the topic, Program prerequisites.


Sample code

Add the following code snippet to the application code and replace the variables with the actual values to use.

/**
 *  testDescendantsAndAncestorsSnippet does the following:
 *  Creates an OrgContainer hierarchy  in default realm
 *  Creates child users of this OrgContainer hierarchy
 *  Gets the descendants of parent entity
 *  Gets the ancestor of child entity
 */
public static void testDescendantsAndAncestorsSnippet()
{
    //Create entity of OrgContainer type
    DataObject rt = addOrgContainer("cn", "users", null);
    // Create sub-entity under cn=users
    DataObject rt1 = addOrgContainer("o", "SWG", rt);
    // Create sub-entity under o=SWG,cn=users
    DataObject rt2 = addOrgContainer("ou", "India", rt1);
    //Retrieve the parent entity for the users that are created below
    DataObject parentEntity = (DataObject) rt2.getList(SchemaConstants.DO_ENTITIES).get(0);
    //Add two users user1 and user2
    DataObject usr1 = addPersonAccountWithNonDefaultParent("user1","user1cn","user1sn",parentEntity);
    DataObject usr2 = addPersonAccountWithNonDefaultParent("user2","user2cn","user2sn",parentEntity);
    //Get the descendants
    getDescendant();
    //Get the ancestors of user1
    getAncestor(usr1);
}

/**
 * addOrgContainer adds an entry in the given parent
 * @param   prefix is the rdnProperty
 * @param   value is the values of rdnProperty
 * @param   obj is the dataObject from which parent is identified
 * @return  DataObject
 */
public static DataObject addOrgContainer (String prefix, String value, DataObject obj)
{
    DataObject parentEntity;
    boolean first = false;
    try
    {
        if (obj == null) {
            obj = SDOHelper.createRootDataObject();
            first = true;
        }
        DataObject entity = SDOHelper.createEntityDataObject(obj, null, SchemaConstants.DO_ORGCONTAINER);
        if (!first) {
            // Set the parent of the OrgContainer entity
            parentEntity = (DataObject) obj.getList(SchemaConstants.DO_ENTITIES).get(0);
            entity.set("parent",parentEntity);
        }
        // Set the OrgContainer property
entity.set(prefix, value);
        System.out.println("Input datagraph before creating OrgContainer"+ printDO(obj));
        // Create the OrgContainer entity
        obj = service.create(obj);
        System.out.println("Output datagraph after creating OrgContainer"+ printDO(obj));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return obj;
}

/**
 *  addPersonAccountWithNonDefaultParent
 *  Adds an entity of PersonAccount entity type under the specified parent
 *  @param uid value to be set
  *   @param cn value to be set
  *   @param sn value to be set
  *   @param parent value to be set
  *   @return   DataObject
 */
public static DataObject addPersonAccountWithNonDefaultParent(String uid, String cn, String sn, DataObject parent)
{
    DataObject root = null;
    try
    {
        root = SDOHelper.createRootDataObject();
        DataObject entity = SDOHelper.createEntityDataObject(root, null, SchemaConstants.DO_PERSON_ACCOUNT);
        // Set the properties of the entity
        entity.set("uid", uid);
        entity.set("cn", cn);
        entity.set("sn", sn);
        entity.set("parent", parent);
        System.out.println("Input datagraph before creating user"+ printDO(root));
        // Create the PersonAccount entity
        root = service.create(root);
        System.out.println("Output datagraph after creating user"+ printDO(root));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return root;
}
/**
 *  getDescendant retrieves the descendants entities of an entity
 */
public static void getDescendant()
{
    try
    {
        DataObject root = SDOHelper.createRootDataObject();
        DataObject entity = SDOHelper.createEntityDataObject(root, null, SchemaConstants.DO_ENTITY);
        //Set the entity unique name whose descendants need to be found
        entity.createDataObject(SchemaConstants.DO_IDENTIFIER).setString(SchemaConstants.PROP_UNIQUE_NAME,
                "cn=users,o=defaultWIMFileBasedRealm");
        //Create the descendant control object
DataObject descCtrl = SDOHelper.createControlDataObject(root, null,
                SchemaConstants.DO_DESCENDANT_CONTROL);
        // Set the property level to retrieve all nested descendants
        descCtrl.setInt(SchemaConstants.PROP_LEVEL, SchemaConstants.PROP_LEVEL_NESTED);
        // Set the properties that need to be retrieved of the descendants
        descCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
        descCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("o");
        descCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("ou");
        System.out.println("Input datagraph before getting descendants"+ printDO(root));
        // Get the entities
        root = service.get(root);
        System.out.println("Output datagraph after getting descendants"+ printDO(root));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

/**
 * getAncestor retrieves the ancestor entities of an entity
 * @param entity    Name of the entity whose ancestor need to be retrieved  */
public static void getAncestor(DataObject entity)
{
    try
    {
        // Set the ancestor control object
DataObject ancesCtrl = SDOHelper.createControlDataObject(entity, null,
                SchemaConstants.DO_ANCESTOR_CONTROL);
        // Set the property level to retrieve all nested ancestors
        ancesCtrl.setInt(SchemaConstants.PROP_LEVEL, SchemaConstants.PROP_LEVEL_NESTED);
        // Set the properties that need to be retrieved of the ancestors
        ancesCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
        ancesCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("o");
        ancesCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("ou");
        System.out.println("Input datagraph before getting ancestors"+ printDO(entity));
        // Get the entities
        entity = service.get(entity);
        System.out.println("Output datagraph after getting ancestors"+ printDO(entity));
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }


Input and output data graphs

Input data graph for creating a parent entity, OrgContainer, cn:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:OrgContainer">
<wim:cn>users
</wim:cn>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph after creating a parent entity, OrgContainer, cn:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:OrgContainer">
<wim:identifier externalName="cn=users,o=defaultWIMFileBasedRealm" repositoryId="InternalFileRepository"
          uniqueId="ecf1d16b-9ab8-4de6-a2a5-7eb48fd2fe7c" uniqueName="cn=users,o=defaultWIMFileBasedRealm"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for creating a parent entity, OrgContainer, o:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:OrgContainer">
<wim:parent xsi:type="wim:OrgContainer">  
<wim:identifier externalName="cn=users,o=defaultWIMFileBasedRealm" repositoryId="InternalFileRepository"
            uniqueId="ecf1d16b-9ab8-4de6-a2a5-7eb48fd2fe7c" uniqueName="cn=users,o=defaultWIMFileBasedRealm"/>
</wim:parent>
<wim:o>SWG
</wim:o>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph after creating a parent entity, OrgContainer, o:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:OrgContainer">
<wim:identifier externalName="o=SWG,cn=users,o=defaultWIMFileBasedRealm" repositoryId="InternalFileRepository"
          uniqueId="e6fc7e62-7492-44f9-8db6-4bccbc40429e" uniqueName="o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for creating a parent entity, OrgContainer, ou:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:OrgContainer">
<wim:parent xsi:type="wim:OrgContainer">  
<wim:identifier externalName="o=SWG,cn=users,o=defaultWIMFileBasedRealm" repositoryId="InternalFileRepository"
            uniqueId="e6fc7e62-7492-44f9-8db6-4bccbc40429e" uniqueName="o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
</wim:parent>
<wim:ou>India
</wim:ou>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph after creating a parent entity, OrgContainer, ou:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:OrgContainer">
<wim:identifier externalName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
          repositoryId="InternalFileRepository" uniqueId="2c281163-3daf-4223-bc63-6cf3f6502c4d"
          uniqueName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for creating a child entity, user1:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:PersonAccount">
<wim:parent xsi:type="wim:OrgContainer">  
<wim:identifier externalName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
            repositoryId="InternalFileRepository" uniqueId="2c281163-3daf-4223-bc63-6cf3f6502c4d"
            uniqueName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
</wim:parent>
<wim:uid>user1
</wim:uid>
<wim:cn>user1cn
</wim:cn>
<wim:sn>user1sn
</wim:sn>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph after creating a child entity, user1:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
          repositoryId="InternalFileRepository" uniqueId="664e4422-d9b6-4b0e-a9e8-62620c6c710f"
          uniqueName="uid=user1,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for creating a child entity, user2:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:PersonAccount">
<wim:parent xsi:type="wim:OrgContainer">  
<wim:identifier externalName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
            repositoryId="InternalFileRepository" uniqueId="2c281163-3daf-4223-bc63-6cf3f6502c4d"
            uniqueName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
</wim:parent>
<wim:uid>user2
</wim:uid>
<wim:cn>user2cn
</wim:cn>
<wim:sn>user2sn
</wim:sn>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Output data graph after creating a child entity, user2:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user2,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
          repositoryId="InternalFileRepository" uniqueId="664e4422-d9b6-4b0e-a9e8-62620c6c710f"
          uniqueName="uid=user2,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for getting descendants of the parent entity:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities>
<wim:identifier uniqueName="cn=users,o=defaultWIMFileBasedRealm"/>
</wim:entities>
<wim:controls xsi:type="wim:DescendantControl" level="0">
<wim:properties>uid
</wim:properties>
<wim:properties>ou
</wim:properties>
<wim:properties>o
</wim:properties>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Output data graph after getting descendants of the parent entity:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:OrgContainer">
<wim:identifier externalName="cn=users,o=defaultWIMFileBasedRealm" repositoryId="InternalFileRepository"
          uniqueId="c9218bab-2444-4c56-8697-88a83a1d82f9" uniqueName="cn=users,o=defaultWIMFileBasedRealm"/>
<wim:children xsi:type="wim:OrgContainer">  
<wim:identifier externalId="14126738-5bbe-4d3f-8589-ef71b8724696"
            externalName="o=SWG,cn=users,o=defaultWIMFileBasedRealm"
            repositoryId="InternalFileRepository" uniqueId="14126738-5bbe-4d3f-8589-ef71b8724696"
            uniqueName="o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>  
<wim:o>SWG
</wim:o>
</wim:children>
<wim:children xsi:type="wim:OrgContainer">  
<wim:identifier externalId="22200d23-5fec-4d5f-9033-5c8c925e7a82"
            externalName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
            repositoryId="InternalFileRepository" uniqueId="22200d23-5fec-4d5f-9033-5c8c925e7a82"
            uniqueName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>  
<wim:ou>India
</wim:ou>
</wim:children>
<wim:children xsi:type="wim:PersonAccount">  
<wim:identifier externalId="63cbb4ca-a629-4a64-9681-98154ec877b6"
            externalName="uid=user2,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
            repositoryId="InternalFileRepository" uniqueId="63cbb4ca-a629-4a64-9681-98154ec877b6"
            uniqueName="uid=user2,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>  
<wim:uid>user2
</wim:uid>
</wim:children>
<wim:children xsi:type="wim:PersonAccount">  
<wim:identifier externalId="7f421b09-e22a-4311-9e2f-7b59f77b6d21"
            externalName="uid=user1,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
            repositoryId="InternalFileRepository" uniqueId="7f421b09-e22a-4311-9e2f-7b59f77b6d21"
            uniqueName="uid=user1,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>  
<wim:uid>user1
</wim:uid>
</wim:children>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Input data graph for getting ancestors of the child entity:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
          repositoryId="InternalFileRepository" uniqueId="9f57af27-573b-4924-a405-7e35e7be012a"
          uniqueName="uid=user1,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
</wim:entities>
<wim:controls xsi:type="wim:AncestorControl" level="0">
<wim:properties>cn
</wim:properties>
<wim:properties>ou
</wim:properties>
<wim:properties>o
</wim:properties>
</wim:controls>
</wim:Root>
</sdo:datagraph> 

Output data graph after getting ancestors of the child entity:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=user1,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
          repositoryId="InternalFileRepository" uniqueId="9f57af27-573b-4924-a405-7e35e7be012a"
          uniqueName="uid=user1,ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>
<wim:parent xsi:type="wim:OrgContainer">  
<wim:identifier externalId="127bac7a-50da-4616-907b-f7345873c087"
            externalName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"
            repositoryId="InternalFileRepository" uniqueId="127bac7a-50da-4616-907b-f7345873c087"
            uniqueName="ou=India,o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>  
<wim:parent xsi:type="wim:OrgContainer">    
<wim:identifier externalId="7e1c0b12-e45b-4595-bfb6-5d39e5741642"
              externalName="o=SWG,cn=users,o=defaultWIMFileBasedRealm"
              repositoryId="InternalFileRepository" uniqueId="7e1c0b12-e45b-4595-bfb6-5d39e5741642"
              uniqueName="o=SWG,cn=users,o=defaultWIMFileBasedRealm"/>    
<wim:parent xsi:type="wim:OrgContainer">      
<wim:identifier externalId="ef168b8d-baed-4cd1-9f10-999866ea4c29"
                externalName="cn=users,o=defaultWIMFileBasedRealm"
                repositoryId="InternalFileRepository" uniqueId="ef168b8d-baed-4cd1-9f10-999866ea4c29"
                uniqueName="cn=users,o=defaultWIMFileBasedRealm"/>      
<wim:cn>users
</wim:cn>    
</wim:parent>    
<wim:o>SWG
</wim:o>  
</wim:parent>  
<wim:ou>India
</wim:ou>
</wim:parent>
</wim:entities>
</wim:Root>
</sdo:datagraph> 

Parent topic: Sample code



+

Search Tips   |   Advanced Search