<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
<section id="JCR.OrderByDescendant">
  <title>Ordering by Descendant Nodes Property (XPath only)</title>

  <para>Find all nodes with the primary type 'nt:unstructured' and sort them
  by the property value of descendant nodes with the relative path
  '/a/b'.</para>

  <note>
    <para>This ORDER BY construction only works in XPath!</para>
  </note>

  <section id="JCR.OrderByDescendant.RepositoryStructure">
    <title>Repository structure:</title>

    <itemizedlist>
      <listitem>
        <para>root</para>

        <itemizedlist>
          <listitem>
            <para>node1 (nt:unstructured)</para>

            <itemizedlist>
              <listitem>
                <para>a (nt:unstructured)</para>

                <itemizedlist>
                  <listitem>
                    <para>b (nt:unstructured)</para>
                  </listitem>
                </itemizedlist>
              </listitem>
            </itemizedlist>
          </listitem>

          <listitem>
            <para>node2 (nt:unstructured)</para>

            <itemizedlist>
              <listitem>
                <para>a (nt:unstructured)</para>

                <itemizedlist>
                  <listitem>
                    <para>b (nt:unstructured)</para>

                    <itemizedlist>
                      <listitem>
                        <para>c (nt:unstructured) prop = "a"</para>
                      </listitem>
                    </itemizedlist>
                  </listitem>
                </itemizedlist>
              </listitem>
            </itemizedlist>
          </listitem>

          <listitem>
            <para>node3 (nt:unstructured)</para>

            <itemizedlist>
              <listitem>
                <para>a (nt:unstructured)</para>

                <itemizedlist>
                  <listitem>
                    <para>b (nt:unstructured)</para>

                    <itemizedlist>
                      <listitem>
                        <para>c (nt:unstructured) prop = "b"</para>
                      </listitem>
                    </itemizedlist>
                  </listitem>
                </itemizedlist>
              </listitem>
            </itemizedlist>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

  <section id="JCR.OrderByDescendant.QueryExecution">
    <title>Query Execution</title>

    <para><emphasis role="bold">XPath</emphasis></para>

    <programlisting language="java">// make XPath query
QueryManager queryManager = workspace.getQueryManager();
// create query
String xpathStatement = "/jcr:root/* order by a/b/c/@prop descending;
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>
  </section>

  <section id="JCR.OrderByDescendant.FetchingResult">
    <title>Fetching the Result</title>

    <para>Let's get nodes:</para>

    <programlisting language="java">NodeIterator it = result.getNodes();

if(it.hasNext())
{
   Node findedNode = it.nextNode();
}</programlisting>

    <para>NodeIterator will return nodes in the following order -
    "node3","node2" and "node1".</para>

    <para>We can also get a table:</para>

    <programlisting language="java">String[] columnNames = result.getColumnNames();
RowIterator rit = result.getRows();
while (rit.hasNext())
{
   Row row = rit.nextRow();
   // get values of the row
   Value[] values = row.getValues();
}</programlisting>

    <para>Table content is:</para>

    <table>
      <title>Table content</title>

      <tgroup cols="3">
        <thead>
          <row>
            <entry>jcr:primaryType</entry>

            <entry>jcr:path</entry>

            <entry>jcr:score</entry>
          </row>
        </thead>

        <tbody>
          <row>
            <entry>nt:unstructured</entry>

            <entry>/testroot/node3</entry>

            <entry>1000</entry>
          </row>

          <row>
            <entry>nt:unstructured</entry>

            <entry>/testroot/node2</entry>

            <entry>1000</entry>
          </row>

          <row>
            <entry>nt:unstructured</entry>

            <entry>/testroot/node1</entry>

            <entry>1000</entry>
          </row>
        </tbody>
      </tgroup>
    </table>
  </section>
</section>
