<?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.ExactPathConstraint">
  <title>Exact Path Constraint</title>

  <para>Find a node with the primary type 'nt:file' that is located on the
  exact path "/folder1/folder2/document1".</para>

  <section id="JCR.ExactPathConstraint.RepositoryStructure">
    <title>Repository Structure</title>

    <para>Repository filled by different nodes. There are several folders
    which contain other folders and files.</para>

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

        <itemizedlist>
          <listitem>
            <para>folder1 (nt:folder)</para>

            <itemizedlist>
              <listitem>
                <para>folder2 (nt:folder)</para>

                <itemizedlist>
                  <listitem>
                    <para>document1 (nt:file) // This document we want to
                    find</para>
                  </listitem>

                  <listitem>
                    <para>folder3 (nt:folder)</para>

                    <itemizedlist>
                      <listitem>
                        <para>document1 (nt:file)</para>
                      </listitem>
                    </itemizedlist>
                  </listitem>
                </itemizedlist>
              </listitem>
            </itemizedlist>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

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

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

    <programlisting language="java">// make SQL query
QueryManager queryManager = workspace.getQueryManager();
// we want find 'document1'
String sqlStatement = "SELECT * FROM nt:file WHERE jcr:path = '/folder1/folder2/document1'";
// create query
Query query = queryManager.createQuery(sqlStatement, Query.SQL);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>

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

    <programlisting language="java">// make SQL query
QueryManager queryManager = workspace.getQueryManager();
// we want to find 'document1'
String xpathStatement = "/jcr:root/folder1[1]/folder2[1]/element(document1,nt:file)[1]";
// create query
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>

    <para>Remark: The indexes [1] are used in order to get the same result as
    the SQL statement. SQL by default only returns the first node, whereas
    XPath fetches by default all nodes.</para>
  </section>

  <section id="JCR.ExactPathConstraint.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 expected "document1".</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: <table>
        <title>Table content</title>

        <tgroup cols="2">
          <thead>
            <row>
              <entry>jcr:path</entry>

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

          <tbody>
            <row>
              <entry>/folder1/folder2/document1</entry>

              <entry>1030</entry>
            </row>
          </tbody>
        </tgroup>
      </table></para>
  </section>
</section>
