変更済みエンティティーの検索のためのサンプル・コード

このエンドツーエンド・サンプル・コードおよびデータ・グラフは、変更済みエンティティーでの検索操作のために使用します。

このサンプル・シナリオでは、以下の手順を実行します。

  1. 2 つのエンティティーを作成します。
  2. ChangeResponseControl データ・オブジェクトを使用して、リポジトリーの現行チェックポイントを取得します。このチェックポイント以降に変更されたエンティティーのみが返されるようにするには、ChangeControl データ・オブジェクトを使用して変更済みエンティティーを検索するとき (ステップ 5) にこのチェックポイントを渡さなければならないため、このチェックポイントが必要です。
    重要: ChangeControl データ・オブジェクトおよび ChangeResponseControl データ・オブジェクトを使用する場合、2 つの前提条件があります。つまり、リポジトリーの supportChangeLog プロパティーの値を native に設定することと、基礎となるリポジトリー内の変更トラッキング機能を使用可能にすることが必要になります。詳しくは、「変更済みエンティティーの検索」トピックを参照してください。
  3. エンティティーのいずれかを更新します。
  4. 更新されたエンティティーのプロパティーを取得します。
  5. ChangeControl データ・オブジェクトおよび ChangeResponseControl データ・オブジェクトを使用して、変更済みエンティティーを検索します。

前提条件

プログラミングの前提条件」トピックで説明されている情報を参照済みであること、またトピックの手順を実行済みであることを確認してください。

サンプル・コード

次のエンドツーエンド・サンプル・コードをアプリケーション・コードに追加し、変数 を実際に使用する値に置き換えます。

public class SearchSample extends BaseApp
{
    private static String ejbJndiName = "ejb/com/ibm/websphere/wim/ejb/WIMServiceHome";

    /**
     *  testScenario 
     *  This test scenario does the following:
     *  Creates two entities
     *  Gets the current checkpoint for the repository
     *  Updates one of the entities
     *  Gets the properties of the updated entity
     *  Searches for changed entities
     */
    public static void main(String[] args) throws Exception
    {
        // Locate the service
        service = locateService(ejbJndiName);
        // Create two users with uid user1 and user2 respectively
        DataObject root1 = addPersonAccount("user1","Admin","AdminSn");
        DataObject root2 = addPersonAccount("user2","Operator","OperatorSn");
        // Get the current checkpoint for the repository
        // This is required before we do any updates on the entities
        // so that it can be passed back when we do a search
        // to retrieve entities changed since this checkpoint
        DataObject prevRoot = getCurrentCheckPoint();
        // Update entity user1
        DataObject updatedRoot = updatePersonAccount(root1, "Manager");
        // Get the properties of the updated entity
        DataObject getRoot = getPersonAccount(updatedRoot);
        // Search for the changed entity
        DataObject searchRoot = searchChangedEntities(prevRoot);
    }
    
    /**
     *  addPersonAccount 
     *  Adds an entity of PersonAccount entity type
     *  @param uid value to be set
     *  @param cn value to be set
     *  @param sn value to be set
     *  @return DataObject
     *  @throws Exception
     */
    public static DataObject addPersonAccount(String uid, String cn, String sn) throws Exception
    {
        DataObject root = SDOHelper.createRootDataObject();
        DataObject entity = SDOHelper.createEntityDataObject(root, null, 
                SchemaConstants.DO_PERSON_ACCOUNT);
        // Set the properties that the newly created entity will have
        entity.set("uid", uid);
        entity.set("cn", cn);
        entity.set("sn", sn);
        System.out.println("Input datagraph before creating user"+ printDO(root));
        root = service.create(root);
        System.out.println("Output datagraph after creating user"+ printDO(root));
        return root;
    }
    
    /**
     * getCurrentCheckPoint Retrieves the current checkpoint from the repository
     * @return DataObject
     * @throws Exception
     */
    public static DataObject getCurrentCheckPoint() throws Exception 
    { 
        DataObject root = SDOHelper.createRootDataObject();
        DataObject ctrl = SDOHelper.createControlDataObject(root, SchemaConstants.WIM_NS_URI, 
                SchemaConstants.DO_CHANGE_CONTROL);          
        DataObject result = service.search(root);
        System.out.println("Checkpoint: "+ printDO(result));
        return result;
    }
    
    /**
     *  updatePersonAccount 
     *  Updates an already existing entity of PersonAccount entity type
     *  @param entity Input DataObject that needs to be modified
     *  @param newCn value of property that needs to be updated
     *  @return Updated DataObject
     *  @throws Exception
     */
    public static DataObject updatePersonAccount(DataObject entity, String newCn) throws Exception
    {
        DataObject user = (DataObject) entity.getList(SchemaConstants.DO_ENTITIES).get(0);
        // Set the new value for cn to update the entity
        user.set("cn", newCn);
        System.out.println("Input datagraph before updating user"+ printDO(entity));
        DataObject root = service.update(entity);
        System.out.println("Output datagraph after updating user"+ printDO(root));   
        return root;
    }
    
    /**
     *  getPersonAccount 
     *  Gets the properties of an existing entity of PersonAccount entity type
     *  @param entity Input DataObject that needs to be retrieved with the properties
     *  @return DataObject with the properties
     *  @throws Exception
     */
    public static DataObject getPersonAccount(DataObject entity) throws Exception
    {   
        DataObject propCtrl = SDOHelper.createControlDataObject(entity, null, 
                SchemaConstants.DO_PROPERTY_CONTROL);           
        // Set the properties that have to be retrieved for the entity
        propCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
        propCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
        propCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
        System.out.println("Input datagraph before getting user properties"+ printDO(entity));
        // Perform get operation
        DataObject root = service.get(entity);
        System.out.println("Output datagraph after getting user properties"+ printDO(root));
        return root;
    }
    
    /**
     * searchChangedEntities Searches for the entities that have changed 
     * @param prevRoot DataObject the represents the previous checkpoint
     * @return DataObject with search results
     * @throws Exception
     */
    public static DataObject searchChangedEntities(DataObject prevRoot) throws Exception
    {
        DataObject root = SDOHelper.createRootDataObject();
        DataObject ctrl = SDOHelper.createControlDataObject(root, null, 
                SchemaConstants.DO_CHANGE_CONTROL);
        // Set the search expression
        ctrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccount'");                
        ctrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
        ctrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
        ctrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
        // Set the CHANGETYPE_ALL to search changed entities
        ctrl.getList(SchemaConstants.PROP_CHANGETYPES).add(SchemaConstants.CHANGETYPE_ALL);
        SDOHelper.createChangeCtrlFromChangeRespCtrl(root, prevRoot);
        System.out.println("Input datagraph before searching for changed entities"+ printDO(root));
        DataObject results = service.search(root);
        System.out.println("output datagraph after searching changed entities"+ printDO(results));
        return results;
    }
}

入出力データ・グラフ

この例のステップごとの入力データ・グラフおよび生成される出力データ・グラフを、次に示します。

ユーザー (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:uid>user1</wim:uid>
      <wim:cn>Admin</wim:cn>
      <wim:sn>AdminSn</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,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
    </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:uid>user2</wim:uid>
      <wim:cn>Operator</wim:cn>
      <wim:sn>OperatorSn</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,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="6ca02580-d6d1-4131-91ed-2329ad6599eb" uniqueName="uid=user2,dc=yourco,dc=com"/>
    </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:controls xsi:type="wim:ChangeResponseControl">
      <wim:checkPoint>
        <wim:repositoryId>ldapRepo</wim:repositoryId>
        <wim:repositoryCheckPoint>28</wim:repositoryCheckPoint>
      </wim:checkPoint>
    </wim:controls>
  </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,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
      <wim:cn>Manager</wim:cn>
    </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,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
    </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,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
      <wim:cn>Manager</wim:cn>
    </wim:entities>
    <wim:controls xsi:type="wim:PropertyControl">
      <wim:properties>sn</wim:properties>
      <wim:properties>uid</wim:properties>
      <wim:properties>cn</wim:properties>
    </wim:controls>
  </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,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
      <wim:uid>user1</wim:uid>
      <wim:cn>Manager</wim:cn>
      <wim:sn>AdminSn</wim:sn>
    </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:controls xsi:type="wim:ChangeControl" expression="@xsi:type='PersonAccount'">
      <wim:properties>uid</wim:properties>
      <wim:properties>cn</wim:properties>
      <wim:properties>sn</wim:properties>
      <wim:checkPoint>
        <wim:repositoryId>ldapRepo</wim:repositoryId>
        <wim:repositoryCheckPoint>28</wim:repositoryCheckPoint>
      </wim:checkPoint>
      <wim:changeTypes>*</wim:changeTypes>
    </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,dc=yourco,dc=com" repositoryId="ldapRepo"
          uniqueId="45258d76-82b4-4a44-9c3a-077f5a82f15e" uniqueName="uid=user1,dc=yourco,dc=com"/>
      <wim:changeType>modify</wim:changeType>
      <wim:uid>user1</wim:uid>
      <wim:cn>Manager</wim:cn>
      <wim:sn>AdminSn</wim:sn>
    </wim:entities>
    <wim:controls xsi:type="wim:ChangeResponseControl">
      <wim:checkPoint>
        <wim:repositoryId>ldapRepo</wim:repositoryId>
        <wim:repositoryCheckPoint>29</wim:repositoryCheckPoint>
      </wim:checkPoint>
    </wim:controls>
  </wim:Root>
</sdo:datagraph>


利用条件 | フィードバック