Express (Distributed operating systems), v8.0 > Secure applications and their environment > Authenticate users > Select a registry or repository > Manage realms in a federated repository > Virtual member manager > Develop with virtual member manager > Integrate virtual member manager into the application > Sample code


Sample code for performing a paged search with sorted results

Use the sample code and data graphs for performing paged search with sorted results by using both the SortControl and PageControl data objects.

The following steps are covered in this sample code snippet:

  1. Set the properties in the SearchControl and the search base for performing the search.

  2. Set the SortControl to specify the property with which you want to sort the results. Ensure that the sort key used by the SortControl has been added in the SearchControl. In the following sample code, the sorting is done on the key, sn.

  3. Set the PageControl to page the search results. We can define the paging size. In the following sample code, it is set to 2.

Note: For sample code to perform only a paged search or only a sorted search, see the topics Performing a paged search and Performing a sorted search.


Prerequisites

Ensure that we have read the information and completed the steps described in the topic, Program prerequisites.


Sample code

Add the following sample code to the application code and replace the variables with the actual values to use.

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


Input and output data graphs

Input data graph for paged search with sorted results (first call):

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//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> 

Output data graph for paged search with sorted results:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//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> 

Input data graph for paged search with sorted results (second call):

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//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> 

Output data graph for paged search with sorted results:

<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="//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> 

Parent topic: Sample code



+

Search Tips   |   Advanced Search