Mustercode für die Ausführung eines Suchvorgangs mit seitenweise abgerufenen und sortierten Suchergebnissen

Mit dem Mustercode und den Datengraphen kann unter Verwendung der Datenobjekte "SortControl" und "PageControl" ein Suchvorgang mit seitenweise abgerufenen und sortierten Suchergebnissen ausgeführt werden.

Dieses Mustercode-Snippet deckt die folgenden Schritte ab:

  1. Die Merkmale im Datenobjekt "SearchControl" und die Suchbasis für die Suche werden festgelegt.
  2. Das Datenobjekt "SortControl" wird so definiert, dass das Merkmal angegeben ist, nach dem die Ergebnisse sortiert werden sollen. Sie müssen sicherstellen, dass der vom Datenobjekt "SortControl" verwendete Sortierschlüssel im Datenobjekt "SearchControl" hinzugefügt wurde. Im folgenden Mustercode wird die Sortierung anhand des Schlüssels "sn" vorgenommen.
  3. Das Datenobjekt "PageControl" wird definiert, damit die Suchergebnisse seitenweise abgerufen werden. Sie können die Größe der Seiten für diesen Vorgang definieren. Im folgenden Mustercode ist der entsprechende Wert auf "2" gesetzt.
Anmerkung: Den Mustercode, mit dem Sie nur eine Suche mit seitenweise abgerufenen Suchergebnissen oder nur eine Suche mit sortierten Ergebnissen ausführen, finden Sie im Thema Suchvorgang mit seitenweise abgerufenen Suchergebnissen ausführen bzw. Suchvorgang mit sortierten Suchergebnissen ausführen.

Voraussetzungen

Stellen Sie sicher, dass Sie die Informationen gelesen und die Schritte ausgeführt haben, die im Thema Voraussetzungen für die Programmierung beschrieben sind.

Mustercode

Fügen Sie den folgenden Mustercode zu Ihrem Anwendungscode hinzu und ersetzen Sie die Variablen durch die tatsächlichen Werte, die Sie verwenden wollen.

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

Eingabe- und Ausgabedatengraphen

Eingabedatengraph für Suchvorgang mit seitenweise abgerufenen und sortierten Suchergebnissen (erster Aufruf):
<?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>
Ausgabedatengraph für Suchvorgang mit seitenweise abgerufenen und sortierten Suchergebnissen:
<?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>
Eingabedatengraph für Suchvorgang mit seitenweise abgerufenen und sortierten Suchergebnissen (zweiter Aufruf):
<?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>
Ausgabedatengraph für Suchvorgang mit seitenweise abgerufenen und sortierten Suchergebnissen:
<?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>


Rechtliche Hinweise | Feedback