ページ検索を実行して検索をソートするためのサンプル・コード

このサンプル・コードおよびデータ・グラフは、SortControl データ・オブジェクトと PageControl データ・オブジェクトの両方を使用して、ページ検索を実行して結果をソートするために使用します。

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

  1. SearchControl 内のプロパティーと、検索を実行するための検索ベースを設定します
  2. 結果をソートする際に使用するプロパティーを指定するには、SortControl を設定します。SortControl が使用するソート・キーが、SearchControl 内に追加されていることを確認してください。以下のサンプル・コードでは、sn というキーでソートが実行されます。
  3. 検索結果をページングするには、PageControl を設定します。ページングのサイズを定義することができます。以下のサンプル・コードでは、2 に設定されています。
注: ページ検索のみ、またはソート検索のみを実行するサンプル・コードについては、「ページ検索の実行」トピックおよび「ソート検索の実行」トピックを参照してください。

前提条件

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

サンプル・コード

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

    /**
     * Test Scenario for searching with page and sort control 
     * This tests sorts the search results based on the sort key provided.
     * Sorted results can be paged by applying page control, and page size can be set 
     * on page control to return desired output.
     */
    @SuppressWarnings("unchecked")
    public static void testPageSortControl() throws Exception
    {
        DataObject root = SDOHelper.createRootDataObject();
        
        DataObject searchCtrl = root.createDataObject(SchemaConstants.DO_CONTROLS, 
                SchemaConstants.WIM_NS_URI, SchemaConstants.DO_SEARCH_CONTROL);
        // Set the properties that need to be retrieved for entities
        // as part of search operation
        searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
        searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn"); 
        // Set the search filter to look for entities which have 
        // uid starting with SalesPerson
        searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "//entities[@xsi:type='PersonAccount' 
                and uid='SalesPerson*']");
        // Add the search base in which search will be performed
        searchCtrl.getList(SchemaConstants.PROP_SEARCH_BASES).add("o=SalesPerson,dc=yourco,dc=com");
        // Add the sort control to sort the results
        DataObject sortCtrl = root.createDataObject(SchemaConstants.DO_CONTROLS, SchemaConstants.WIM_NS_URI, 
                SchemaConstants.DO_SORT_CONTROL);
        // Specify the sort key as “sn” on which sorting will be performed
        sortCtrl.setString(SchemaConstants.PROP_SORT_LOCALE, "en-US");
        DataObject sortKey  = sortCtrl.createDataObject(SchemaConstants.DO_SORT_KEYS, SchemaConstants.WIM_NS_URI, 
                SchemaConstants.DO_SORT_KEY_TYPE);
        sortKey.setString(SchemaConstants.PROP_PROPERTY_NAME,"sn");
        // Set the order as ascending so that returned results are
        // sorted in ascending order
        sortKey.setBoolean(SchemaConstants.PROP_ASCENDING_ORDER, true);
        // Add the page control to page the sorted results
        DataObject pageCtrl = root.createDataObject(SchemaConstants.DO_CONTROLS, SchemaConstants.WIM_NS_URI, 
                SchemaConstants.DO_PAGE_CONTROL);
        // Set the page size to two, so that not more than 2 results are returned per page size
        pageCtrl.setInt(SchemaConstants.PROP_SIZE, 2);
        boolean first = true;
        byte[] cookie = null;
        DataObject returnDO = null;
        int pageCount = 0;
        while (first || cookie != null) {
            first = false;
            pageCount++;
            if (cookie != null) {
                // Set the cookie retrieved from the response control, on the PageControl of input dataobject
                pageCtrl.setBytes(SchemaConstants.PROP_COOKIE, cookie);
                //Delete the search control from the input data object for the next call
                searchCtrl.delete();
            }
            System.out.println("Input datagraph" + printDO(root));
            returnDO = service.search(root);
            System.out.println("Output datagraph" + printDO(returnDO));
            List entities = returnDO.getList(SchemaConstants.DO_ENTITIES );
            Map ctrlMap = getControlMap(returnDO);
            // From the control map get the Page response control
            // and set the cookie using that
            DataObject responseCtrl = (DataObject)ctrlMap.get(SchemaConstants.DO_PAGE_RESPONSE_CONTROL);
            if (responseCtrl != null) {
                cookie = responseCtrl.getBytes(SchemaConstants.PROP_COOKIE);
            } else {
                cookie = null;
            }
        }    

    /**
     * Get the ControlMap
     * Utility method to create a Map of all controls
     * available on the DataObject. 
     */
    public static Map getControlMap(DataObject root)
    {
        Map ctrlMap = new HashMap();
        // Get all the controls and create a map of control types
        List controls = root.getList(SchemaConstants.DO_CONTROLS);
        if (controls != null) {
            for (int i = 0; i < controls.size(); i++) {
                DataObject control = (DataObject) controls.get(i);
                String type = control.getType().getName();
                if (ctrlMap.get(type) == null) {
                    ctrlMap.put(type, control);
                }
            }
        }
        return ctrlMap;
    }

入出力データ・グラフ

結果をソートするページ検索の入力データ・グラフ (最初の呼び出し):
<?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="//entities[@xsi:type='PersonAccount' 
        and uid='SalesPerson*']">
      <wim:properties>uid</wim:properties>
      <wim:properties>sn</wim:properties>
      <wim:searchBases>o=SalesPerson,dc=yourco,dc=com</wim:searchBases>
    </wim:controls>
    <wim:controls xsi:type="wim:SortControl">
      <wim:sortKeys>
        <wim:propertyName>sn</wim:propertyName>
        <wim:ascendingOrder>true</wim:ascendingOrder>
      </wim:sortKeys>
      <wim:locale>en-US</wim:locale>
    </wim:controls>
    <wim:controls xsi:type="wim:PageControl" size="2"/>
  </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=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:sn>SalesPerson1sn</wim:sn>
    </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:sn>SalesPerson2sn</wim:sn>
    </wim:entities>
    <wim:controls xsi:type="wim:PageResponseControl" 
        cookie="57494D53656172636843616368654E616D6574696D653A31323938333535393336343933"
        totalSize="3"/>
  </wim:Root>
</sdo:datagraph>
結果をソートするページ検索の入力データ・グラフ (2 回目の呼び出し):
<?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:SortControl">
      <wim:sortKeys>
        <wim:propertyName>sn</wim:propertyName>
        <wim:ascendingOrder>true</wim:ascendingOrder>
      </wim:sortKeys>
      <wim:locale>en-US</wim:locale>
    </wim:controls>
    <wim:controls xsi:type="wim:PageControl" 
        cookie="57494D53656172636843616368654E616D6574696D653A31323938333535393336343933"
        size="2"/>
  </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:sn>SalesPerson3sn</wim:sn>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>


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