Performing a sorted search
Use the search(DataObject) method and SortControl DataObject to specify the order of items returned during a search.
About this task
The virtual member manager search function returns a set
of entities which match the search expression. Use the SortControl
function to specify whether the results are returned in ascending
or descending order. In this example the sort key cn is passed in
from the SortControl. The sorting order is set to descending (the
default setting is ascending). The locale is set to English, en.
Search results are sorted by the cn attribute of a person.
Note: The
sort key cn is included in the properties list of the SearchControl.
This is required for sorted search. Sort Keys have to be a part
of the return attributes for the search operation.
To
use the sorted search function in a search, add the following sample
code to your application code and replace the variables with
the actual values that you want to use for this search:
DataObject root = SDOHelper.createRootDataObject();
DataObject searchCtrl = root.createDataObject(DO_CONTROLS, WIM_NS_URI, "SearchControl");
searchCtrl.getList(PROP_PROPERTIES).add("uid");
searchCtrl.getList(PROP_PROPERTIES).add("sn");
searchCtrl.getList(PROP_PROPERTIES).add("cn");
searchCtrl.setString(PROP_SEARCH_EXPRESSION, "//entities[@xsi:type='PersonAccount' and
uid='SalesPerson*']");
searchCtrl.getList(PROP_SEARCH_BASES).add(uNames.get("sales"));
DataObject sortCtrl = root.createDataObject(DO_CONTROLS, WIM_NS_URI, DO_SORT_CONTROL);
sortCtrl.setString(PROP_SORT_LOCALE, "en-US");
DataObject sortKey = sortCtrl.createDataObject(DO_SORT_KEYS, WIM_NS_URI, DO_SORT_KEY_TYPE);
sortKey.setString(PROP_PROPERTY_NAME,"cn");
sortKey.setBoolean(PROP_ASCENDING_ORDER, false);
DataObject returnDO = service.search(root);
Sample input and output data graphs
For
this particular example the input data graph is:
<?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:properties>cn</wim:properties>
<wim:searchBases>o=Sales,cn=users,dc=yourco,dc=com</wim:searchBases>
</wim:controls>
<wim:controls xsi:type="wim:SortControl">
<wim:sortKeys>
<wim:propertyName>cn</wim:propertyName>
<wim:ascendingOrder>false</wim:ascendingOrder>
</wim:sortKeys>
<wim:locale>en-US</wim:locale>
</wim:controls>
</wim:Root>
</sdo:datagraph>
A sample output data graph for this example is:
<?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=SalesPerson4,ou=US,o=Sales,cn=users,dc=yourco,dc=com"
repositoryId="LDAP1"
uniqueId="a2d1be42-212e-44f0-8396-0bfc2d7d346d"
uniqueName="uid=SalesPerson4,ou=US,o=Sales,cn=users,dc=yourco,dc=com"/>
<wim:uid>SalesPerson4</wim:uid>
<wim:cn>Sales Person4</wim:cn>
<wim:sn>SalesPerson4Sn</wim:sn>
</wim:entities>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson3,ou=US,o=Sales,cn=users,dc=yourco,dc=com"
repositoryId="LDAP1"
uniqueId="13ae346f-1e4c-40cd-8896-1c7f0b2b3079"
uniqueName="uid=SalesPerson3,ou=US,o=Sales,cn=users,dc=yourco,dc=com"/>
<wim:uid>SalesPerson3</wim:uid>
<wim:cn>Sales Person3</wim:cn>
<wim:sn>SalesPerson3Sn</wim:sn>
</wim:entities>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson2,ou=CA,o=Sales,cn=users,dc=yourco,dc=com"
repositoryId="LDAP1"
uniqueId="a3f9e8a1-117a-4252-840f-6d0cb079a7fb"
uniqueName="uid=SalesPerson2,ou=CA,o=Sales,cn=users,dc=yourco,dc=com"/>
<wim:uid>SalesPerson2</wim:uid>
<wim:cn>Sales Person2</wim:cn>
<wim:sn>SalesPerson2Sn</wim:sn>
</wim:entities>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson1,ou=CA,o=Sales,cn=users,dc=yourco,dc=com"
repositoryId="LDAP1"
uniqueId="e7db8c0e-d2b3-43fe-be14-1475125606f4"
uniqueName="uid=SalesPerson1,ou=CA,o=Sales,cn=users,dc=yourco,dc=com"/>
<wim:uid>SalesPerson1</wim:uid>
<wim:cn>Sales Person1</wim:cn>
<wim:sn>SalesPerson1Sn</wim:sn>
</wim:entities>
</wim:Root>
</sdo:datagraph>
Also see the topic, Sample code for performing a paged search with sorted results.