検査結果を検索および解析するためのサンプル・コード

このサンプル・コードおよびデータ・グラフは、search() メソッドを使用して検索し、検索フィルターを作成し、検索結果を解析するために使用します。

このサンプル・コード・スニペットでは、以下の手順を実行します。

  1. 検索フィルターを作成します。
  2. search() メソッドを呼び出し、検索結果を解析します。

前提条件

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

サンプル・コード

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

/**
 *  This test builds a search filter, calls a search API,
 *  and parses the search results
 */
@SuppressWarnings("unchecked")
public static void testSearch() throws Exception
{
    DataObject root = SDOHelper.createRootDataObject();
    DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, SchemaConstants.DO_SEARCH_CONTROL);
    // Set the properties that need to be retrieved in search results
    searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
    searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn"); 
    searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn"); 
    searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("telephoneNumber");  
    // Set the search base in which search will be performed
    searchCtrl.getList(SchemaConstants.PROP_SEARCH_BASES).add("o=SalesPerson,dc=yourco,dc=com");
    // Set the search filter as uid, here the expression will search for all uids
    searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccount' and uid='*'");
    System.out.println("Input datagraph before searching for users in the searchbase"+ printDO(root));  
    DataObject result = service.search(root);
    System.out.println("Output datagraph after searching for users in the searchbase"+ printDO(result));
    // Loop through the search results and print the uniqueName
    printIdentifiers(result);
}

入出力データ・グラフ

検索ベースでユーザーを検索するための入力データ・グラフ:
<?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:SearchControl" expression="@xsi:type='PersonAccount' and uid='*'">
      <wim:properties>uid</wim:properties>
      <wim:properties>cn</wim:properties>
      <wim:properties>sn</wim:properties>
      <wim:properties>telephoneNumber</wim:properties>
      <wim:searchBases>o=SalesPerson,dc=yourco,dc=com</wim:searchBases>
    </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=SalesPerson3,o=SalesPerson,dc=yourco,dc=com" 
          repositoryId="ldapRepo" uniqueId="f3fe184d-e51a-4d02-a5c2-4e2e79e79fea" 
          uniqueName="uid=SalesPerson3,o=SalesPerson,dc=yourco,dc=com"/>
      <wim:uid>SalesPerson3</wim:uid>
      <wim:cn>SalesPerson3cn</wim:cn>
      <wim:sn>SalesPerson3sn</wim:sn>
    </wim:entities>
    <wim:entities xsi:type="wim:PersonAccount">
      <wim:identifier externalName="uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com" 
          repositoryId="ldapRepo" uniqueId="e0db3534-a0ec-4c8b-bfff-a399895a7ca3" 
          uniqueName="uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com"/>
      <wim:uid>SalesPerson1</wim:uid>
      <wim:cn>SalesPerson1cn</wim:cn>
      <wim:sn>SalesPerson1sn</wim:sn>
      <wim:telephoneNumber>11111111</wim:telephoneNumber>
    </wim:entities>
    <wim:entities xsi:type="wim:PersonAccount">
      <wim:identifier externalName="uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com" 
          repositoryId="ldapRepo" uniqueId="7130a46b-d896-4bdd-8f2c-04d9374908c6" 
          uniqueName="uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com"/>
      <wim:uid>SalesPerson2</wim:uid>
      <wim:cn>SalesPerson2cn</wim:cn>
      <wim:sn>SalesPerson2sn</wim:sn>
      <wim:telephoneNumber>22222222</wim:telephoneNumber>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>
検索結果のループ処理および固有名の出力:
UniqueName is  -> uid=SalesPerson3,o=SalesPerson,dc=yourco,dc=com
UniqueName is  -> uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com
UniqueName is  -> uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com


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