<project
  default="site"
  xmlns:j="jelly:core"
  xmlns:maven="jelly:maven"
  xmlns:velocity="jelly:org.apache.commons.jelly.tags.velocity.VelocityTagLibrary">

  <!--
   |
   | S I T E
   |
   -->
  
  <preGoal name="site">
    <attainGoal name="db-site"/>
  </preGoal>
  
  <goal name="db-site">
  
    <!--
     |
     | Walk over all the DB subprojects and collect its information.
     |
     -->
    <maven:reactor
      basedir="${basedir}/projects"
      includes="**/project.xml"
      postProcessing="true"
      ignoreFailures="true"
      collectOnly="true"
      collectionVar="reactorResult"
    />

    <!-- Will contain a sorted list of all Projects in the db-site -->
    <j:new var="projList" className="java.util.TreeMap"/>

    <!-- Will contain a sorted list of all Developers in the subprojects -->
    <j:new var="devList" className="java.util.TreeMap"/>

    <j:forEach var="project" items="${reactorResult.iterator()}">
      <j:set var="dummy" value="${projList.put(project.getName(), project)}"/>

      <j:forEach var="developer" items="${project.getDevelopers()}">
        <j:set var="dummy" value="${devList.put(developer.getId(), developer)}"/>
      </j:forEach>
    </j:forEach>

    <j:set var="reactorProjects" value="${projList.values()}"/>

    <!--
     |
     | Now we take each of our template in the ${basedir}/templates
     | directory and process them against the information collected
     | by the reactor.
     |
     | For each template name Y.xml a corresponding Y.html will be
     | generated in the top-level directory of the site. So for example
     | if we have a template named 'mail.xml', a page named 'mail.html'
     | will be generated in the top-level directory.
     | 
     -->

    <fileScanner var="templates">
      <fileset dir="${basedir}/templates">
        <include name="*.xml"/>
      </fileset>
    </fileScanner>

    <j:forEach var="template" items="${templates.iterator()}">
      
      <j:set var="templateName" value="${template.name}"/>
      <echo>Processing ${templateName} template ...</echo> 
    
      <velocity:merge 
        name="${basedir}/xdocs/${templateName}"
        basedir="${basedir}/templates"
        template="${templateName}"/>

    </j:forEach>
  </goal>

</project>
