Mustercode für die Erstellung einer Entität in einer Hierarchie und den Abruf von untergeordneten und übergeordneten Elementen

Mit dem Mustercode-Snippet und den Datengraphen können Sie eine Entität in einer Hierarchie erstellen und die untergeordneten Elemente (descendants) sowie die übergeordneten Elemente (ancestors) einer Entität unter Verwendung der Methode get() abrufen.

Das Mustercode-Snippet und die Datengraphen decken die folgenden Schritte ab:

  1. Es wird eine Hierarchie mit Entitäten des Entitätstyps "OrgContainer" im Standardrealm erstellt.
  2. Es werden untergeordnete Entitäten unter der Hierarchie "OrgContainer" erstellt.
  3. Es werden die untergeordneten Elemente der übergeordneten Entität unter Verwendung der Methode get() und des Datenobjekts "DescendantControl" abgerufen.
  4. Es werden die übergeordneten Elemente der untergeordneten Entität unter Verwendung der Methode get() und des Datenobjekts "AscendantControl" abgerufen.

Voraussetzungen

Stellen Sie sicher, dass Sie die Informationen gelesen und die Schritte ausgeführt haben, die im Thema Voraussetzungen für die Programmierung beschrieben sind.

Mustercode

Fügen Sie das folgende Code-Snippet zu Ihrem Anwendungscode hinzu und ersetzen Sie die Variablen durch die tatsächlichen Werte, die Sie verwenden wollen.

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

Eingabe- und Ausgabedatengraphen

Eingabedatengraph für die Erstellung einer übergeordneten Entität "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>

Ausgabedatengraph nach der Erstellung einer übergeordneten Entität "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>

Eingabedatengraph für die Erstellung einer übergeordneten Entität "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>

Ausgabedatengraph nach der Erstellung einer übergeordneten Entität "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>

Eingabedatengraph für die Erstellung einer übergeordneten Entität "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>

Ausgabedatengraph nach der Erstellung einer übergeordneten Entität "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>

Eingabedatengraph für die Erstellung einer untergeordneten Entität (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>

Ausgabedatengraph nach der Erstellung einer untergeordneten Entität (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>

Eingabedatengraph für die Erstellung einer untergeordneten Entität (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>

Ausgabedatengraph nach der Erstellung einer untergeordneten Entität (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>

Eingabedatengraph für den Abruf von untergeordneten Elementen der übergeordneten Entität:

<?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>

Ausgabedatengraph nach dem Abruf der untergeordneten Elemente der übergeordneten Entität:

<?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>

Eingabedatengraph für den Abruf von übergeordneten Elementen der untergeordneten Entität:

<?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>

Ausgabedatengraph nach dem Abruf von übergeordneten Elementen der untergeordneten Entität:

<?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>


Rechtliche Hinweise | Feedback