用于在层次结构中创建实体以及获取后代和祖代的样本代码

使用样本代码片段和数据图在层次结构中创建实体,并通过使用 get() 方法来获取实体的后代和祖代。

样本代码片段和数据图涵盖以下步骤:

  1. 在缺省领域中使用类型为 OrgContainer 的实体来创建层次结构。
  2. 在 OrgContainer 层次结构下创建子实体。
  3. 通过使用 get() 方法和 DescendantControl 来获取父实体的后代。
  4. 通过使用 get() 方法和 AscendantControl 来获取子实体的祖代。

必备条件

确保您已阅读了“编程先决条件”主题中的信息并完成了其中描述的步骤。

样本代码

请将以下代码片段添加到您的应用程序代码,并将变量替换为您要使用的实际值。

/**
 *  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 next
    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();
    }
} 

输入和输出数据图

用于创建父实体 OrgContainer, cn 的输入数据图:

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

创建父实体 OrgContainer, cn 之后的输出数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

用于创建父实体 OrgContainer, o 的输入数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

创建父实体 OrgContainer, o 之后的输出数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

用于父实体 OrgContainer, ou 的输入数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

创建父实体 OrgContainer, ou 之后的输出数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

用于创建子实体 user1 的输入数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

创建子实体 user1 之后的输出数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

用于创建子实体 user2 的输入数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

创建子实体 user2 之后的输出数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

用于获取父实体的后代的输入数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

获取父实体的后代之后的输出数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

用于获取子实体的祖代的输入数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>

获取子实体的祖代之后的输出数据图:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://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>


使用条款 | 反馈