<?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.ChildNodeConstraint">
  <title>Child Node Constraint</title>

  <para>Find all nodes with the primary type 'nt:folder' that are children of
  node by path "/root1/root2". Only find children, do not find further
  descendants.</para>

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

    <para>The repository is filled by "nt:folder" nodes. The nodes are placed
    in a multilayer tree.</para>

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

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

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

                <itemizedlist>
                  <listitem>
                    <para>folder3 (nt:folder) // This node we want to
                    find</para>

                    <itemizedlist>
                      <listitem>
                        <para>folder4 (nt:folder) // This node is not child
                        but a descendant of '/folder1/folder2/'.</para>
                      </listitem>
                    </itemizedlist>
                  </listitem>

                  <listitem>
                    <para>folder5 (nt:folder) // This node we want to
                    find</para>
                  </listitem>
                </itemizedlist>
              </listitem>
            </itemizedlist>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>
  </section>

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

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

    <para>The use of "%" in the LIKE statement includes any string, therefore
    there is a second LIKE statement that excludes that the string contains
    "/". This way child nodes are included but descendant nodes are
    excluded.</para>

    <programlisting language="java">// make SQL query
QueryManager queryManager = workspace.getQueryManager();
// create query
String sqlStatement = "SELECT * FROM nt:folder WHERE jcr:path LIKE '/folder1/folder2/%' AND NOT jcr:path LIKE '/folder1/folder2/%/%'";
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 XPath query
QueryManager queryManager = workspace.getQueryManager();
// create query
String xpathStatement = "/jcr:root/folder1[1]/folder2[1]/element(*,nt:folder)";
Query query = queryManager.createQuery(xpathStatement, Query.XPATH);
// execute query and fetch result
QueryResult result = query.execute();</programlisting>
  </section>

  <section id="JCR.ChildNodeConstraint.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>The NodeIterator will return "folder3" and "folder5".</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>The 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/folder3</entry>

              <entry>1707</entry>
            </row>

            <row>
              <entry>/folder1/folder2/folder5</entry>

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