用于执行分页搜索并对结果进行排序的样本代码

使用样本代码和数据图,通过使用 SortControl 以及 PageControl 数据对象来执行分页搜索并对结果进行排序。

此样本代码片段中涵盖以下步骤:

  1. 设置 SearchControl 中的属性以及用于执行搜索的搜索条件。
  2. 设置 SortControl 以指定要用于对结果进行排序的属性。请确保 SearchControl 中已添加了 SortControl 使用的排序键。在以下样本代码中,是针对 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>
对结果进行排序的分页搜索的输入数据图(第二次调用):
<?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>


使用条款 | 反馈