<?xml version="1.0" encoding="UTF-8"?>
<project name="juiseorg" basedir="." default="package-web">

    <!-- import properties (app settings, classpath, jar file locations) -->
    <import file="properties.xml"/>

    <target name="init" description="defines custom tasks">
        <!-- Taskdefs -->
        <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${ant-contrib.jar}"/>

        <!-- Ensure that Ant 1.6.2+ is being used -->
        <available classname="org.apache.tools.ant.DynamicAttribute" property="ant-1.6.2"/>
        <fail unless="ant-1.6.2" message="AppFuse requires Ant 1.6.2 for faster unit testing"/>

        <!-- Check that junit.jar is in $ANT_HOME/lib or junit task fails -->
        <available classname="junit.framework.TestCase" property="junit.present"/>
        <fail unless="junit.present" message="Please copy junit.jar into ${env.ANT_HOME}/lib"/>

        <taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="dao.test.classpath"/>
        
        <!-- Test for Tomcat 5.0.x -->
        <available classname="org.apache.catalina.ant.SessionsTask" property="tomcat5">
            <classpath>
                <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
            </classpath>
        </available>
        
        <!-- Test for Tomcat 5.5.x -->
        <available file="${tomcat.home}/common/lib/jasper-compiler-jdt.jar" property="tomcat5.5"/>
        
        <!-- Conditionally set property for converting to JSP 2.0 -->
        <condition property="convert.to.jsp2">
            <equals arg1="${jsp.2}" arg2="true"/>
        </condition>
    </target>
    
    <!-- Check timestamp on files -->
    <target name="prepare" depends="init" description="create target directories">
        <tstamp/>
        <tstamp><format property="copyright.year" pattern="yyyy"/></tstamp>
        <echo message="Preparing target directory '${webapp.target}'"/>
        <mkdir dir="${webapp.target}"/>
        <mkdir dir="${webapp.target}/WEB-INF"/>
        <mkdir dir="${webapp.dist}"/>
        
        <!-- Make sure hibernatedoclet is necessary -->
        <uptodate property="hibernatedoclet.unnecessary">
            <srcfiles dir="src/dao" includes="**/*.java"/>
            <srcfiles dir="${build.dir}/dao/gen" includes="**/*.java"/>
            <mapper type="glob" from="*.java" to="${build.dir}/dao/classes/*.class"/>
        </uptodate>
        
        <!-- Make sure webdoclet is necessary -->
        <uptodate property="webdoclet.uptodate">
            <srcfiles dir="src/web" includes="**/*.java"/>
            <mapper type="glob" from="*.java" to="${build.dir}/web/classes/*.class"/>
        </uptodate>

        <condition property="webdoclet.unnecessary">
            <and>
                <equals arg1="${webdoclet.uptodate}" arg2="true"/>
                <available file="${webapp.target}/WEB-INF/web.xml"/>
            </and>
        </condition>
        
    </target>

    <!-- List of variables in .properties files that will be replaced at
         build time -->
    <filterset id="variables.to.replace">
        <filter token="APPNAME" value="${webapp.name}"/>
        <filter token="ENCRYPT-ALGORITHM" value="${encrypt.algorithm}"/>
        <filter token="ERROR-MAILTO" value="${error.mailTo}"/>
        <filter token="ERROR-MAILHOST" value="${error.mailHost}"/>
        <filter token="ERROR-SERVER" value="${error.server}"/>
        <filter token="FORM-AUTH-ACTION" value="${form.auth.action}"/>
        <filter token="HTTP-PORT" value="${http.port}"/>
        <filter token="HTTPS-PORT" value="${https.port}"/>
        <filter token="TOMCAT-SERVER" value="${tomcat.server}"/>
        <filter token="SECURE-LOGIN" value="${secure.login}"/>
        <filter token="HIBERNATE-DIALECT" value="${hibernate.dialect}"/>
    </filterset>
            
    <!-- List of variables to replace when configuring Tomcat -->
    <filterset id="db.variables">
        <filter token="DB-DRIVERNAME" value="${database.driver_class}"/>
        <filter token="DB-URL" value="${database.url}"/>
        <filter token="DB-USERNAME" value="${database.username}"/>
        <filter token="DB-PASSWORD" value="${database.password}"/>
    </filterset>
    
    <!-- Copy any resource or configuration files -->
    <target name="copy-resources" depends="prepare"
        description="Copy .properties and .xml files from source directory">
        <copy todir="${build.dir}/web/classes" includeEmptyDirs="no">
            <fileset dir="web/WEB-INF/classes">   
                <exclude name="ApplicationResources_zh_CN.properties"/>
                <include name="*.properties"/>
                <include name="*.xml"/>
                <include name="*.vm"/>   
            </fileset>
            <filterset refid="variables.to.replace"/>
        </copy>            
        <native2ascii src="web/WEB-INF/classes" dest="${build.dir}/web/classes" 
            includes="ApplicationResources_zh_CN.properties" encoding="UTF-8"/>
        <generate-database-properties/>
        <copy todir="${build.dir}/web/classes" file="database.properties"/>
    </target>
    
    <!-- =================================================================== -->
    <!-- The "copy-web-files" target copies the static web resources portion -->
    <!-- of your web application source into the build target area           -->
    <!-- =================================================================== -->
    <target name="copy-web-files" depends="prepare" description="Copy static files">
        <echo message="Copying static files"/>
        <!-- Remove the copy block below if you're not displaying 
             version/copyright in the footer -->
        <copy todir="${webapp.target}">
            <fileset dir="web" includes="common/footer.jsp"/>
            <filterset>
                <filter token="APPVERSION" value="${webapp.version}"/>
                <filter token="COPYRIGHT-YEAR" value="${copyright.year}"/>
            </filterset>
        </copy>
        <copy todir="${webapp.target}" includeEmptyDirs="no">
            <fileset dir="${basedir}/web">
                <include name="**"/>
                <exclude name="pages/**"/>
                <exclude name="**/classes/**"/>
                <exclude name="**/*-resources.xml"/>
            </fileset>
        </copy>
        <!-- Copy JSP Pages under WEB-INF/pages -->
        <copy todir="${webapp.target}/WEB-INF">
            <fileset dir="${basedir}/web">
                <include name="pages/**/*.jsp"/>
            </fileset>
            <fileset dir="${spring.dir}" includes="*.xml"/>   
        </copy>
        <!-- Copy *.txt files so they can be included in release notes -->
        <copy todir="${webapp.target}">
            <fileset dir="${basedir}">
                <include name="*.txt"/>
            </fileset>
        </copy>
    </target>
    
    <!-- =================================================================== -->
    <!-- The "jsp-2" replaces JSTL's <c:out> tags with what's in "value" and -->
    <!-- removes JSTL's 1.0 JARS                                             -->
    <!-- =================================================================== -->
    <target name="jsp-2" depends="webdoclet" if="convert.to.jsp2"
        description="Convert JSP 1.2 tags (i.e. JSTL) to JSP 2.0">
        <echo level="info">Change JSTL Tags to Run-Time</echo>
        <replaceregexp flags="g">
            <regexp pattern='uri="http://java.sun.com/jstl/core"'/>
            <substitution expression='uri="http://java.sun.com/jstl/core_rt"'/>
            <fileset dir="${webapp.target}" includes="**/*.jsp"/>
        </replaceregexp>
        <replaceregexp flags="g">
            <regexp pattern='uri="http://java.sun.com/jstl/fmt"'/>
            <substitution expression='uri="http://java.sun.com/jstl/fmt_rt"'/>
            <fileset dir="${webapp.target}" includes="**/*.jsp"/>
        </replaceregexp>

        <echo level="info">Removing JSTL's c:out tags</echo>
        <replaceregexp flags="g">
            <regexp pattern='&lt;c:out value="([^"]*)"/&gt;'/>
            <substitution expression="\1"/>
            <fileset dir="${webapp.target}" includes="**/*.jsp"/>
        </replaceregexp>

        <echo level="info">Changing EL tags to non-EL since JSP 2.0 has EL built-in</echo>
        <replace file="${webapp.target}/common/taglibs.jsp">
            <replacetoken>http://displaytag.sf.net/el</replacetoken>
            <replacevalue>http://displaytag.sf.net</replacevalue>
        </replace>

        <echo level="info">Replacing 2.3 DTD with 2.4 XSD in web.xml</echo>
        <replaceregexp flags="g"
            file="${webapp.target}/WEB-INF/web.xml"
            match='&lt;!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"&gt;'
            replace=""/>

        <replaceregexp flags="g"
            file="${webapp.target}/WEB-INF/web.xml"
            match='&lt;web-app &gt;'
            replace='&lt;web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"&gt;'/>
    </target>
    
    <!-- =================================================================== -->
    <!-- The "stage-web" is used to gather all static web recourses in the   -->
    <!-- build directory.                                                    -->
    <!-- =================================================================== -->
    <target name="stage-web" depends="copy-resources,copy-web-files"
        description="Calls other targets to gather static resources"/>
        
    <!-- =================================================================== -->
    <!-- The "hibernatedoclet" target generates Hibernate mapping files      -->
    <!-- based on XDoclet marked-up Plain Old Java Object (POJO)             -->
    <!-- =================================================================== -->
    <target name="hibernatedoclet" depends="prepare" unless="hibernatedoclet.unnecessary"
        description="Generate Hibernate mapping files">

        <taskdef name="hibernatedoclet"
            classname="xdoclet.modules.hibernate.HibernateDocletTask"
            classpathref="xdoclet.classpath"/>
        
        <!-- generate hibernate files -->
        <hibernatedoclet destdir="${build.dir}/dao/gen" mergedir="metadata/dao"
            excludedtags="@version,@author" addedtags="@xdoclet-generated at ${TODAY}"
            force="${xdoclet.force}">
            <fileset dir="src/dao"/>
            <hibernate validatexml="true" version="3.0"/>
        </hibernatedoclet>
    </target>

    <target name="compile-dao" depends="hibernatedoclet" description="Compile dao module">
        <compile module="dao"/>
    </target>

    <target name="package-dao" depends="prepare,compile-dao" description="Package DAO JAR">
        <!-- Copy XML files from source -->
        <copy todir="${build.dir}/dao/gen">
            <fileset dir="src/dao" includes="**/*.xml" excludes="**/*-${dao.type}.xml"/>
            <filterset refid="variables.to.replace"/>
        </copy>
        <!-- Copy Spring configuration files -->
        <mkdir dir="${build.dir}/dao/gen/META-INF"/>
        <copy overwrite="true" tofile="${build.dir}/dao/gen/META-INF/applicationContext-${dao.type}.xml">
            <fileset dir="src/dao" includes="**/*-${dao.type}.xml"/>
            <filterset refid="variables.to.replace"/>
        </copy>
        <jar destfile="${dist.dir}/${webapp.name}-dao.jar">
            <manifest>
                <attribute name="Class-Path" value="${webapp.name}-dao.jar"/>
            </manifest>
            <fileset dir="${build.dir}/dao/classes"/>
            <fileset dir="${build.dir}/dao/gen" includes="**/*.xml" 
                excludes="**/applicationContext-*.xml"/>
            <metainf dir="${build.dir}/dao/gen/META-INF"/>
        </jar>
    </target>
   
    <target name="test-dao" depends="copy-resources,package-dao,db-load" description="Test dao module">
        <test-module module="dao" additional.src.dirs="${build.dir}/dao/gen"/>
    </target>
  
    <!-- Service -->
    <target name="compile-service" depends="package-dao" description="Compile service module">
        <compile module="service"/>
    </target>

    <target name="package-service" depends="compile-service">
        <mkdir dir="${build.dir}/service/classes/META-INF"/>
        <copy tofile="${build.dir}/service/classes/META-INF/applicationContext-service.xml">
            <fileset dir="src/service" includes="**/*-service.xml"/>
        </copy>
        <jar destfile="${dist.dir}/${webapp.name}-service.jar">
            <manifest>
                <attribute name="Class-Path"
                    value="${webapp.name}-dao.jar ${webapp.name}-service.jar"/>
            </manifest>
            <fileset dir="${build.dir}/service/classes" includes="**/*.class"/>
            <metainf dir="${build.dir}/service/classes/META-INF"/>
        </jar>
    </target>

    <target name="test-service" depends="copy-resources,compile-service" description="Test service module">
        <test-module module="service"/>
    </target>
<target name="compile-web" depends="package-service,stage-web" description="Compile web module">
        <compile module="web"/>
    </target>

    <!-- Simple alias to compile-web -->
    <target name="compile" depends="compile-web" 
        description="alias for compile-web"/>

    <!-- =================================================================== -->
    <!-- The "webdoclet" target generates web deployment descriptors.           -->
    <!-- =================================================================== -->
    <target name="webdoclet" depends="compile-web" unless="webdoclet.unnecessary"
        description="Generate web deployment descriptors">
        <taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask">
            <classpath>
                <path refid="xdoclet.classpath"/>
                <path refid="web.compile.classpath"/>
            </classpath>
        </taskdef>
        <webdoclet destdir="${webapp.target}/WEB-INF"
            force="${xdoclet.force}"
            mergedir="metadata/web"
            excludedtags="@version,@author"
            verbose="true">
            <fileset dir="src/web"/>
            <fileset dir="src/dao"/>
            <deploymentdescriptor validateXML="true"
                servletspec="2.3" sessiontimeout="10"
                destdir="${webapp.target}/WEB-INF" distributable="false"
                displayname="${ant.project.name}">
                <configParam name="httpPort" value="${http.port}"/>
                <configParam name="httpsPort" value="${https.port}"/>
                <configParam name="daoType" value="${dao.type}"/>
            </deploymentdescriptor>
            <jsptaglib validateXML="true"
                description="Custom tag library for this application"
                shortName="${webapp.name}" filename="${webapp.name}.tld"/>
            <springvalidationxml/>
        </webdoclet>
    </target>

    <!-- Simple alias to package-web -->
    <target name="war" depends="package-web" 
        description="alias for package-web"/>

    <target name="package-web" depends="compile-web,jsp-2" description="Package WAR">
        <propertyfile comment="Build Information" file="${build.dir}/web/classes/build.properties">
            <entry key="build.date" type="date" pattern="EEEE MMM dd, yyyy" value="now"/>
            <entry key="build.time" type="date" pattern="hh:mm:ss a zz" value="now"/>
            <entry key="build.username" value="${user.name}"/>
            <entry key="build.computer" value="${env.COMPUTERNAME}"/>
            <entry key="build.dao.type" value="${dao.type}"/>
            <entry key="webapp.version" value="${webapp.version}"/>
        </propertyfile>
        <!-- If Tomcat 5, uncomment the <dispatcher> elements in filter-mappings -->
        <if>
            <or>
                <isset property="tomcat5"/>
                <isset property="tomcat5.5"/>
            </or>
            <then>
                <replaceregexp flags="g"
                    file="${webapp.target}/WEB-INF/web.xml"
                    match='&lt;!--dispatcher&gt;'
                    replace='&lt;dispatcher&gt;'/>
                <replaceregexp flags="g"
                    file="${webapp.target}/WEB-INF/web.xml"
                    match='&lt;/dispatcher--&gt;'
                    replace='&lt;/dispatcher&gt;'/>
            </then>
        </if>
        <echo>Preparing tomcat-context.xml for inclusion in war</echo>
        <!-- Copy tomcat-context.xml file to context.xml -->
        <if>
            <and>
                <isset property="tomcat5"/>
                <not><isset property="tomcat5.5"/></not>
            </and>
            <then>
                <copy tofile="${webapp.dist}/context.xml"
                    file="metadata/conf/tomcat-context.xml" overwrite="true">
                    <filterset refid="db.variables"/>
                </copy>
            </then>
            <elseif>
                <isset property="tomcat5.5"/>
                <then>
                    <copy tofile="${webapp.dist}/context.xml"
                        file="metadata/conf/tomcat-context-5.5.xml" overwrite="true">
                        <filterset refid="db.variables"/>
                    </copy>
                </then>
            </elseif>
        </if>
        <!-- Copy .properties files in src tree to build/web/classes -->
        <copy todir="${build.dir}/web/classes">
            <fileset dir="src/web">
                <include name="**/*.properties"/>
                <include name="**/*.xml"/>
            </fileset>
        </copy>
        <war destfile="${webapp.dist}/${webapp.war}"
            webxml="${webapp.target}/WEB-INF/web.xml" compress="true">
            <fileset dir="${webapp.target}" excludes="**/web.xml,**/*-resources.xml"/>
            <metainf dir="${webapp.dist}" includes="context.xml"/>

            <classes dir="${build.dir}/web/classes">
                <exclude name="**/database.properties"/>
            </classes>

            <lib file="${dist.dir}/${webapp.name}-dao.jar"/>
            <lib file="${dist.dir}/${webapp.name}-service.jar"/>

            <webinf dir="web/WEB-INF" includes="*-resources.xml"/>
            <webinf dir="${spring.dir}" includes="*.xml"/>
            
            <lib file="${clickstream.jar}"/>
            <lib dir="${struts.dir}" includes="*.jar"/>
            <lib dir="${jstl.dir}/lib">
                <include name="jstl.jar"/>
                <include name="standard.jar"/>
            </lib>
            <lib dir="${javamail.dir}" includes="*.jar"/>   
            <lib file="${log4j.jar}"/>
            <lib file="${strutsmenu.jar}"/>
            <lib dir="${displaytag.dir}" includes="*.jar"/>
            <lib file="${hibernate.jar}"/>
            <lib dir="${hibernate.dir}/lib" includes="*.jar" excludes="jta.jar"/>
            <lib dir="${spring.dir}" includes="*.jar" excludes="*mock.jar"/>
            <lib file="${commons-fileupload.jar}"/>
            <lib file="${requesttag.jar}"/>
            <lib file="${sitemesh.jar}"/>
            <lib dir="${velocity.dir}" includes="*.jar"/>   
            <lib file="${urlrewrite.jar}"/>
        </war>
    </target>

    <target name="test-web" depends="webdoclet,db-load" description="Test web module">
        <copy todir="${webapp.target}/WEB-INF" file="${spring.dir}/validator-rules.xml"/>
        <test-module module="web"/>
    </target>

    <macrodef name="compile">
        <attribute name="module"/>
        <attribute name="additional.src.dirs" default=""/>
        <element name="options" optional="yes"/>

        <sequential>
            <echo>Compiling @{module}...</echo>
            <mkdir dir="${build.dir}/@{module}/classes"/>
            <mkdir dir="${test.dir}/@{module}/classes"/>
            <javac srcdir="${src}/@{module};@{additional.src.dirs}"
                destdir="${build.dir}/@{module}/classes"
                debug="${compile.debug}"
                deprecation="${compile.deprecation}"
                optimize="${compile.optimize}"
                classpathref="@{module}.compile.classpath">
                <options/>
            </javac>
            <javac srcdir="test/@{module}"
                destdir="${test.dir}/@{module}/classes"
                debug="true">
                <classpath>
                    <path refid="@{module}.test.classpath"/>
                    <path location="${build.dir}/@{module}/classes"/>
                </classpath>
                <options/>
            </javac>
        </sequential>
    </macrodef>

    <macrodef name="test-module">
        <attribute name="module"/>
        <attribute name="additional.src.dirs" default=""/>
        <element name="options" optional="yes"/>

        <sequential>
            <echo level="info">Testing @{module}...</echo>
            <mkdir dir="${test.dir}/data"/>
            <propertycopy name="testcase" from="@{module}-testcase" silent="true"/>
            <!-- Replace tokens in test properties files -->
            <copy todir="${test.dir}/@{module}/classes">
                <fileset dir="test/@{module}" excludes="**/*.java"/>
                <filterset refid="variables.to.replace"/>
            </copy>
            <junit printsummary="no" forkmode="once" errorProperty="test.failed"
                failureProperty="test.failed" fork="${junit.fork}">
                <classpath>
                    <pathelement path="@{additional.src.dirs}"/>
                    <path refid="@{module}.test.classpath"/>
                    <pathelement location="${build.dir}/@{module}/classes"/>
                    <pathelement location="${test.dir}/@{module}/classes"/>
                    <pathelement path="${java.class.path}"/>
                    <pathelement path="${webapp.target}"/>
                    <pathelement path="${build.dir}/dao/gen"/>
                    <!-- For .properties and .xml files -->
                    <pathelement path="${build.dir}/web/classes"/>
                    <pathelement path="src/@{module}"/>
                </classpath>
                <formatter type="xml"/>
                <formatter type="brief" usefile="false"/>
                <batchtest todir="${test.dir}/data" if="testcase">
                    <fileset dir="${test.dir}/@{module}/classes">
                        <include name="**/*${testcase}*"/>
                        <exclude name="**/*TestCase.class"/>
                    </fileset>
                </batchtest>
                <batchtest todir="${test.dir}/data" unless="testcase">
                    <fileset dir="${test.dir}/@{module}/classes">
                        <include name="**/*Test.class*"/>
                    </fileset>
                </batchtest>
            </junit>

            <fail if="test.failed">
              Unit tests failed. For error messages, check the log files in
              ${test.dir}/data or run "ant test-reports"
              to generate reports at ${test.dir}/reports.</fail>
        </sequential>
    </macrodef>

    <target name="test-reports" description="Generate test reports">
        <mkdir dir="${test.dir}/reports"/>
        <junitreport todir="${test.dir}">
            <fileset dir="${test.dir}/data">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${test.dir}/reports"/>
        </junitreport>
        <echo level="info">
        Test reports generated at ${test.dir}/reports
        </echo>
    </target>

    <target name="deploy-test-reports" depends="test-reports"
        description="Deploys Generated JUnit test reports to server">
        <mkdir dir="${test.dir}/reports"/>
        <copy todir="${tomcat.home}/webapps/${webapp.name}/test-reports">
            <fileset dir="${test.dir}/reports"/>
        </copy>
    </target>

    <!-- =================================================================== -->
    <!--            Compile JSPs using jspc                                  -->
    <!-- =================================================================== -->
    <target name="compile-jsp" depends="deploy-web">
        <!-- For more info see http://cvs.apache.org/~fhanik/precompile.html -->
        <ant antfile="compile-jsp.xml"/>
    </target>

    <!-- =================================================================== -->
    <!--            Run Canoo WebTests in Tomcat                             -->
    <!-- =================================================================== -->
    <target name="test-canoo" if="tomcat.home" depends="ping-tomcat,db-load"
        description="Runs Canoo WebTests in Tomcat to test JSPs">

        <taskdef file="${webtest.dir}/webtestTaskdefs.properties">
            <classpath>
                <path refid="web.test.classpath"/>
                <!-- for log4j.properties -->
                <path location="${build.dir}/web/classes"/>
            </classpath>
        </taskdef>

        <mkdir dir="${test.dir}/data"/>
        
        <!-- Delete old results file if it exists -->
        <delete file="${test.dir}/data/web-tests-result.xml"/>
        
        <!-- This is so the default will be used if no test case is specified -->
        <property name="testcase" value="run-all-tests"/>
        <echo level="info">Testing with locale '${user.language}'</echo>
        <ant antfile="test/web/web-tests.xml" target="${testcase}"/>
    </target>

    <!-- =================================================================== -->
    <!--            Run Canoo WebTests using Cargo                           -->
    <!-- =================================================================== -->
    <target name="test-jsp" if="tomcat.home" depends="war"
        description="Runs Canoo WebTests using Cargo">
         
        <taskdef resource="cargo.tasks" classpath="${cargo.jar}"/>   

        <echo>running canoo tests at:
              http://${tomcat.server}:${http.port}/${webapp.name}</echo>

        <if>
            <isset property="tomcat5"/>
            <then>
                <echo message="Starting Tomcat 5x..."/>
                <cargo-tomcat5x homeDir="${tomcat.home}" id="cargo.tomcat5"
                    output="${test.dir}/cargo.log" action="start">
                    <configuration dir="${test.dir}/tomcat5x">
                        <property name="cargo.logging" value="high"/>
                        <property name="cargo.servlet.port" value="${http.port}"/>
                        <war warFile="${webapp.dist}/${webapp.war}"/>
                    </configuration>   
                </cargo-tomcat5x>
        
                <antcall target="test-canoo"/>
        
                <cargo-tomcat5x refid="cargo.tomcat5" action="stop"/>
            </then>
            <else>
                <echo message="Starting Tomcat 4x..."/>
                <cargo-tomcat4x homeDir="${tomcat.home}" id="cargo.tomcat4"
                    output="${test.dir}/cargo.log" action="start">
                    <configuration dir="${test.dir}/tomcat4x">
                        <property name="cargo.logging" value="high"/>
                        <property name="cargo.servlet.port" value="${http.port}"/>
                        <war warFile="${webapp.dist}/${webapp.war}"/>
                    </configuration>   
                </cargo-tomcat4x>
        
                <antcall target="test-canoo"/>
        
                <cargo-tomcat4x refid="cargo.tomcat4" action="stop"/>
            </else>
        </if>
    </target>

    <!-- =================================================================== -->
    <!--            Run all tests (tomcat not running)                       -->
    <!-- =================================================================== -->
    <target name="test-all" depends="test-dao,test-service,test-web,test-jsp"
        description="runs all tests for dao, service, web and jsp">
        <antcall target="db-load"/>
    </target>

    <!-- =================================================================== -->
    <!--  Run all tests (tomcat IS running). The deploy-test-war task        -->
    <!--  should be run before this one.                                     -->
    <!-- =================================================================== -->
    <target name="test-all-running"
        description="runs all tests for DAOs, Services, Controllers and JSPs">
        <antcall target="test-dao"/>
        <antcall target="test-service"/>
        <antcall target="test-web"/>
        <antcall target="test-canoo"/>
        <antcall target="db-load"/>
    </target>
        
    <!-- =================================================================== -->
    <!--            Simple task to ping tomcat and make sure it's running    -->
    <!-- =================================================================== -->
    <target name="ping-tomcat" description="pings tomcat to make sure it's running">
        <echo level="info">Verifying that tomcat is running...</echo>
        <get src="http://${tomcat.server}:${http.port}/${webapp.name}"
            dest="../ping-${tomcat.server}.html"/>
    </target>
    
    <!-- =================================================================== -->
    <!--            Export the data                                          -->
    <!-- =================================================================== -->
    <target name="db-export" depends="prepare"
        description="Exports the current data in the database">
        <dbunit driver="${database.driver_class}"
            url="${database.url}"
            userid="${database.username}"
            password="${database.password}">
            <export dest="db-export.xml" format="xml"/>
        </dbunit>
    </target>

    <target name="db-load" depends="prepare"
        description="Loads the database with sample data">
        <property name="operation" value="CLEAN_INSERT"/>
        <property name="file" value="metadata/sql/sample-data.xml"/>
        <dbunit driver="${database.driver_class}"
            supportBatchStatement="false"
            url="${database.url}"
            userid="${database.username}"
            password="${database.password}">
            <operation type="${operation}" src="${file}" format="xml"/>
        </dbunit>
    </target>
    
    <!-- =================================================================== -->
    <!-- The "db-create" target creates a database based on properties       --> 
    <!-- from the database.properties file                                   -->
    <!-- =================================================================== -->
    <target name="db-create" depends="init"
        description="create database for ${database.type}">
        <echo level="debug">${database.jar}</echo>
        <if>
            <equals arg1="${database.type}" arg2="mysql"/>
            <then>
                <echo message="Detected MySQL, creating database..."/>
                <echo>Creating database with: ${database.type}-create.sql</echo>
                <echo>URL: ${database.admin.url}</echo>
                <sql
                    driver="${database.driver_class}"
                    url="${database.admin.url}"
                    userid="${database.admin.username}"
                    password="${database.admin.password}">

                    <classpath>
                        <pathelement location="${database.jar}"/>
                    </classpath>

                    <fileset dir="metadata/sql">
                        <include name="${database.type}-create.sql"/>
                    </fileset>
                </sql>
            </then>
            <else>
                <echo level="warning">WARNING:</echo>
                <echo>You selected a '${database.type}' database which </echo>
                <echo>does NOT support creating a database via JDBC.  </echo>
                <echo>Please create a '${database.name}' database manually</echo>
                <echo>and then run "ant db-prepare" to create the </echo>
                <echo>database tables.  You might want to check the </echo>
                <echo>metadata/sql directory to see if the file</echo>
                <echo>'${database.type}-create.sql' exists.</echo>
            </else>
        </if>
    </target>
    
    <!-- =================================================================== -->
    <!-- The "db-prepare" target generates the database schema and creates   --> 
    <!-- tables based on the mapping files                                   -->
    <!-- =================================================================== -->
    <target name="db-prepare" depends="clean,package-dao"
        description="creates database tables">
        <taskdef name="schemaexport"
            classname="org.hibernate.tool.hbm2ddl.SchemaExportTask">
            <classpath>
                <path refid="xdoclet.classpath"/>
                <path refid="hibernate.classpath"/>
            </classpath>
        </taskdef>

        <generate-database-properties/>
        <schemaexport quiet="no" text="no" drop="no" delimiter=";" 
            properties="database.properties" output="create-tables.sql">
            <fileset dir="${build.dir}/dao/gen" includes="**/*.hbm.xml"/>
        </schemaexport>
    </target>

    <target name="db-drop" depends="hibernatedoclet"
        description="drops database tables">
        <taskdef name="schemaexport"
            classname="org.hibernate.tool.hbm2ddl.SchemaExportTask">
            <classpath>
                <path refid="xdoclet.classpath"/>
                <path refid="hibernate.classpath"/>
            </classpath>
        </taskdef>

        <generate-database-properties/>
        <schemaexport quiet="no" text="no" drop="yes" delimiter=";"
            properties="database.properties" output="drop-tables.sql">
            <fileset dir="${build.dir}/dao/gen" includes="**/*.hbm.xml"/>
        </schemaexport>
    </target>

    <!-- =================================================================== -->
    <!--            Setup Database                                           -->
    <!-- =================================================================== -->
    <target name="setup-db" depends="db-create,db-prepare,db-load"
        description="creates database and populates by calling other tasks"/>
        
    <!-- Remove classes directory for clean build -->
    <target name="clean" description="Removes build artifacts">
        <echo level="info">Cleaning build and distribution directories</echo>
        <delete dir="${build.dir}"/>
        <delete dir="${dist.dir}"/>
        <delete file="database.properties"/>
        <delete file="create-tables.sql"/>
    </target>

    <!-- =================================================================== -->
    <!-- Create JavaDoc Documentation                                        -->
    <!-- =================================================================== -->
    <target name="javadoc" depends="java2html"
        description="Generate JavaDoc API docs">
        <javadoc
            destdir="${build.dir}/docs/api"
            packagenames="org.*"
            author="true"
            version="true"
            windowtitle="${webapp.name} api"
            doctitle="&lt;h1&gt;${webapp.name} javadocs for
                version ${webapp.version}&lt;/h1&gt;"
            bottom="Copyright &#169; 2002-${copyright.year}"
            stylesheetfile="${build.dir}/docs/api/stylesheet.css">

            <fileset dir="${src}">
                <patternset refid="java.files.pattern"/>
            </fileset>
            <fileset dir="test">
                <patternset refid="java.files.pattern"/>
            </fileset>

            <classpath refid="web.test.classpath"/>
            <classpath>
                <pathelement location="${jmock.jar}"/>
                <pathelement location="${build.dir}/web/gen"/>
            </classpath>
            
            <!-- XDoclet Tags -->
            <tag name="hibernate.class" scope="all" description="Class:"/>
            <tag name="hibernate.id" scope="all" description="Id:"/>
            <tag name="hibernate.property" scope="all" description="Property:"/>
            <tag name="jsp.tag" scope="all" description="JSP Tag:"/>
            <tag name="jsp.attribute" scope="all" description="JSP Attribute:"/>
            <tag name="struts.action" scope="all" description="Mapping:"/>
            <tag name="struts.action-forward" scope="all" description="ActionForward:"/>
            <tag name="struts.form" scope="all" description="Form Name:"/>
            <tag name="struts.validator" scope="all" description="Validator:"/>
            <tag name="struts.validator-args" scope="all" description="Validator Args:"/>
            <tag name="struts.validator-var" scope="all" description="Validator Var:"/>
            <tag name="web.listener" scope="all" description="Listener:"/>
            <tag name="web.filter" scope="all" description="Filter:"/>
            <tag name="web.filter-prepare-param" scope="all" description="prepare-Param:"/>
            <tag name="web.filter-mapping" scope="all" description="Mapping:"/>
            <tag name="web.servlet" scope="all" description="Servlet:"/>
            <tag name="web.servlet-prepare-param" scope="all" description="prepare-Param:"/>
            <tag name="web.servlet-mapping" scope="all" description="Mapping:"/>
            
            <!-- Application frameworks -->
            <link href="http://jakarta.apache.org/commons/lang/api"/>
            <link href="http://jakarta.apache.org/struts/api"/>
            <link href="http://www.springframework.org/docs/api"/>
            <link href="http://www.hibernate.org/hib_docs/api"/>
            
            <!-- Testing frameworks -->
            <link href="http://www.junit.org/junit/javadoc/${junit.version}"/>
            <link href="http://strutstestcase.sf.net/api/"/>
            
            <!-- SDKs -->
            <link href="http://java.sun.com/j2se/1.4/docs/api/"/>
            <link href="http://java.sun.com/j2ee/sdk_1.3/techdocs/api/"/>
        </javadoc>
    </target>

    <!-- =================================================================== -->
    <!-- Create HTML versions of the Java code in this project               -->
    <!-- =================================================================== -->
    <target name="java2html" depends="hibernatedoclet"
        description="creates HTML pages of code for online viewing">
        <taskdef name="java2html" classname="com.java2html.Java2HTMLTask"
            classpath="${j2h.jar}"/>

        <java2html
            destination="${build.dir}/docs/api">
            <fileset dir="${src}">
                <patternset refid="java.files.pattern"/>
            </fileset>
            <fileset dir="test">
                <patternset refid="java.files.pattern"/>
            </fileset>
        </java2html>
    </target>

    <!-- =================================================================== -->
    <!-- The "deploy-web" target deploys the web application in the in the   -->
    <!-- non-war format.                                                     -->
    <!-- =================================================================== -->
    <target name="deploy-web" depends="stage-web" if="tomcat.home"
        description="deploy only web classes to servlet container's deploy directory">

        <echo message="Deploying web application to ${tomcat.home}/webapps"/>
        <copy todir="${tomcat.home}/webapps/${webapp.name}">
            <fileset dir="${webapp.target}"
                excludes="**/web-test.xml,**/web.xml,**/*-resources.xml"/>
        </copy>

    </target>
    
    <!-- =================================================================== -->
    <!--            Refresh - Stops Tomcat, and refreshes everything         -->
    <!-- =================================================================== -->
    <target name="refresh" depends="undeploy,clean,deploy"
        description="Undeploys, cleans, then re-deploys"/>
    
    <!-- =================================================================== -->
    <!-- The "deploy" target deploys the web application by packaging it as  -->
    <!-- a WAR and then unpackaging it into Tomcat's webapps directory       -->
    <!-- =================================================================== -->
    <target name="deploy" depends="package-web" if="tomcat.home"
        description="unwar into the servlet container's deployment directory">

        <unwar src="${webapp.dist}/${webapp.war}"
            dest="${tomcat.home}/webapps/${webapp.name}"/>

    </target>
    
    <!-- =================================================================== -->
    <!-- The "undeploy" target deletes the web application archive and the   -->
    <!-- web application directory in the compatible servlet container.      -->
    <!-- =================================================================== -->
    <target name="undeploy" if="tomcat.home"
        description="undeploy war file to servlet container's deployment dir">
        <echo message="Undeploying webapp from Tomcat"/>
        <delete file="${tomcat.home}/webapps/${webapp.war}"/>
        <delete dir="${tomcat.home}/webapps/${webapp.name}"/>
    </target>

    <!-- =================================================================== -->
    <!-- The "deploy-war" deploys the web application in the in a war format -->
    <!-- =================================================================== -->
    <target name="deploy-war" depends="package-web" if="tomcat.home"
        description="Deploy '${webapp.name}.war' to local Tomcat">
        <copy file="${webapp.dist}/${webapp.war}"
            todir="${tomcat.home}/webapps"/>
    </target>
    
    <!-- Utilities -->
    <patternset id="java.files.pattern" includes="**/*.java"/>

    <target name="checkstyle" depends="prepare"
        description="Check code style for compliance with coding standards">
        <property name="checkstyle.data.dir"
            location="${build.dir}/docs/checkstyle"/>
        <property name="checkstyle.data.file"
            location="${checkstyle.data.dir}/checkstyle.xml"/>
        <property name="checkstyle.report.file"
            location="${checkstyle.data.dir}/checkstyle.html"/>
        <property name="checkstyle.xsl.file"
            location="${checkstyle.dir}/contrib/checkstyle-noframes.xsl"/>
        <mkdir dir="${checkstyle.data.dir}"/>
        <taskdef resource="checkstyletask.properties" classpath="${checkstyle.jar}"/>
        <checkstyle config="${checkstyle.dir}/sun_checks.xml"
            failOnViolation="false" failureProperty="checkstyle.failure">
            <fileset dir="src">
                <patternset refid="java.files.pattern"/>
            </fileset>
            <!-- uncomment to print to console as well -->
            <!--formatter type="plain"/-->
            <formatter type="xml" toFile="${checkstyle.data.file}"/>
        </checkstyle>
        <xslt in="${checkstyle.data.file}" out="${checkstyle.report.file}"
            style="${checkstyle.xsl.file}"/>
    </target>

    <target name="pmd" if="pmd.jar" depends="prepare"
        description="Locates unused imports, unused variables, etc.">
        <mkdir dir="${build.dir}/docs/pmd"/>
        <property name="pmd.data.file"  value="${build.dir}/docs/pmd/pmdreport.html"/>
        <taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask">
            <classpath location="${pmd.jar}"/>
            <classpath location="${pmd.dir}/lib/jaxen-core-1.0-fcs.jar"/>
            <classpath location="${pmd.dir}/lib/saxpath-1.0-fcs.jar"/>
        </taskdef>

        <pmd rulesetfiles="rulesets/unusedcode.xml,rulesets/imports.xml"
            failonerror="no" shortFilenames="true">
            <formatter type="html" toFile="${pmd.data.file}"/>
            <fileset dir="src">
                <patternset refid="java.files.pattern"/>
            </fileset>
        </pmd>
    </target>

    <target name="todo" description="creates report of todos in code">

        <taskdef name="todo"
            classname="xdoclet.modules.doc.DocumentDocletTask"
            classpathref="xdoclet.classpath"/>
            
        <mkdir dir="${build.dir}/docs/todo"/>
        <todo destdir="${build.dir}/docs/todo">
            <fileset dir="${src}">
                <patternset refid="java.files.pattern"/>
            </fileset>
        </todo>
    </target>

    <target name="docs" depends="javadoc, todo, checkstyle, pmd"
        description="runs javadoc, todo, checkstyle and pmd tasks">
        <echo level="info">API and Reports generated successfully...</echo>
    </target>
        
    <!-- =================================================================== -->
    <!--            Setup Tomcat                                             -->
    <!-- =================================================================== -->
    <target name="setup-tomcat" depends="init" if="tomcat.home"
        description="copies jdbc driver and context.xml to tomcat">
        <if>
            <and>
                <isset property="tomcat5"/>
                <not><isset property="tomcat5.5"/></not>
            </and>
            <then>
                <echo message="Detected Tomcat 5..."/>
                <echo level="info">Copying ${webapp.name}.xml...</echo>
                <copy tofile="${tomcat.home}/conf/Catalina/localhost/${webapp.name}.xml"
                    file="metadata/conf/tomcat-context.xml" overwrite="true">
                    <filterset refid="db.variables"/>
                </copy>
            </then>
            <elseif>
                <isset property="tomcat5.5"/>
                <then>
                    <echo message="Detected Tomcat 5.5..."/>
                    <echo level="info">Copying ${webapp.name}.xml...</echo>
                    <copy tofile="${tomcat.home}/conf/Catalina/localhost/${webapp.name}.xml"
                        file="metadata/conf/tomcat-context-5.5.xml" overwrite="true">
                        <filterset refid="db.variables"/>
                    </copy>
                </then>
            </elseif>
            <else>
                <echo message="Detected Tomcat 4..."/>
                <echo level="info">Copying ${webapp.name}.xml...</echo>
                <copy tofile="${tomcat.home}/webapps/${webapp.name}.xml"
                    file="metadata/conf/tomcat-context.xml" overwrite="true">
                    <filterset refid="db.variables"/>
                </copy>
            </else>
        </if>

        <echo level="info">Copying ${database.type} JDBC Driver...</echo>
        <copy todir="${tomcat.home}/common/lib" file="${database.jar}" overwrite="true"/>
        <echo level="info">Copying jta.jar...</echo>
        <copy todir="${tomcat.home}/common/lib">
            <fileset dir="${hibernate.dir}/lib" includes="jta.jar"/>
        </copy>
    </target>
    
    <!-- =================================================================== -->
    <!--            Start Tomcat                                             -->
    <!-- =================================================================== -->
    <target name="start.tomcat"
        description="starts tomcat in the current console window">
        <java classname="org.apache.catalina.startup.Bootstrap" fork="yes">
            <jvmarg value="-Dcatalina.home=${tomcat.home}"/>
            <arg value="start"/>
            <classpath>
                <pathelement path="${java.home}/../lib/tools.jar"/>
                <fileset dir="${tomcat.home}">
                    <include name="bin/bootstrap.jar"/>
                </fileset>
            </classpath>
        </java>
    </target>
    
    <!-- ============================================================== -->
    <!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
    <!-- ============================================================== -->
    <taskdef file="${ant-contrib.dir}/tomcatTasks.properties">
        <classpath>
            <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
        </classpath>
    </taskdef>
    <target name="install" description="Install application in Tomcat"
        depends="package-web">
        <deploy url="${tomcat.manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"
            path="/${webapp.name}"
            war="file:${webapp.dist}/${webapp.war}"/>
    </target>

    <target name="remove" description="Remove application in Tomcat">
        <undeploy url="${tomcat.manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"
            path="/${webapp.name}"/>
    </target>

    <target name="reload" description="Reload application in Tomcat">
        <reload url="${tomcat.manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"
            path="/${webapp.name}"/>
    </target>

    <target name="start" description="Start Tomcat application">
        <start url="${tomcat.manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"
            path="/${webapp.name}"/>
    </target>

    <target name="stop" description="Stop Tomcat application">
        <stop url="${tomcat.manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"
            path="/${webapp.name}"/>
    </target>

    <target name="list" description="List Tomcat applications">
        <list url="${tomcat.manager.url}"
            username="${tomcat.username}"
            password="${tomcat.password}"/>
    </target>

    <!-- =================================================================== -->
    <!--            Creates documentation from wiki                          -->
    <!-- =================================================================== -->
    <target name="wiki" description="downloads files from wiki for doco">
        <!-- Get latest stylesheet -->
        <get src="http://raibledesigns.com/wiki/templates/redman/jspwiki.css"
            dest="docs/style.css"/>
        <!-- Get wiki pages -->
        <get src="http://raibledesigns.com/wiki/printwikishow.jsp?${wiki.pages}"
            dest="docs/wiki.html"/>
        <!-- Add some style -->
        <replaceregexp flags="g"
            file="docs/wiki.html"
            match='&lt;/HEAD&gt;'
            replace='&lt;/HEAD&gt; &lt;link rel="stylesheet" type="text/css" href="style.css" /&gt; &lt;style type="text/css"&gt;body {margin: 20px}&lt;/style&gt;'/>
    </target>
    
    <!-- =================================================================== -->
    <!--            Creates release (source) distribution                    -->
    <!-- =================================================================== -->
    <target name="dist" depends="clean,wiki"
        description="create zip and tar.gz of entire directory for distribution">
        <property name="archive.prefix" value="${webapp.name}"/>
        <property name="archive.name" value="${archive.prefix}-${webapp.version}-src"/>
        <property name="archive.target" value="../${archive.name}"/>
        <zip zipfile="${archive.target}.zip">
            <zipfileset prefix="${webapp.name}" dir="${basedir}">
                <patternset id="srcfiles">
                    <include name="**"/>
                    <exclude name="*.log"/>
                    <exclude name="junit*.properties"/>
                    <exclude name="**/*~"/>
                    <exclude name="**/*.bak"/>
                    <exclude name="*/**.java.txt"/>
                    <exclude name="${dist.dir}"/>
                    <exclude name="${build.dir}"/>
                    <exclude name="bin/**"/>
                    <exclude name=".#**"/>
                    <exclude name="anthill.version"/>
                    <exclude name="www/**"/>
                    <exclude name="*.sh"/>
                    <exclude name="*.sql"/>   
                </patternset>
            </zipfileset>
        </zip>
        <tar tarfile="${archive.target}.tar">
            <tarfileset dir="${basedir}" mode="${tar.mode}" prefix="${webapp.name}"
                username="${tar.user}" group="${tar.group}">
                <patternset refid="srcfiles"/>
            </tarfileset>
        </tar>
        <gzip zipfile="${archive.target}.tar.gz" src="${archive.target}.tar"/>
        <delete file="${archive.target}.tar"/>
    </target>
    
    <!-- =================================================================== -->
    <!--            Creates release (binary) distribution                    -->
    <!-- =================================================================== -->
    <target name="bin" depends="clean,package-web"
        description="creates zip file with war, jdbc drivers, and XML file">
        
        <!-- Copy tomcat-context.xml file to webapp.name.xml so it's friendlier -->
        <copy tofile="${webapp.dist}/${webapp.name}.xml"
            file="metadata/conf/tomcat-context.xml">
            <filterset refid="db.variables"/>
        </copy>
        
        <!-- Remove version number from WAR file -->
        <move file="${webapp.dist}/${webapp.war}"
            tofile="${webapp.dist}/${webapp.name}.war"/>
        
        <!-- Copy JDBC Driver -->
        <copy todir="${webapp.dist}" file="${database.jar}"/>

        <!-- Create the zip and tar.gz files -->
        <property name="archive.name" value="${webapp.name}-${webapp.version}-bin"/>
        <property name="archive.target" value="${dist.dir}/${archive.name}"/>
        <zip zipfile="${archive.target}.zip">
            <zipfileset prefix="${webapp.name}-${webapp.version}"
                dir="${webapp.dist}">
                <patternset id="binfiles">
                    <include name="**"/>
                </patternset>
            </zipfileset>
        </zip>
        <tar tarfile="${archive.target}.tar">
            <tarfileset dir="${webapp.dist}" mode="${tar.mode}"
                prefix="${webapp.name}-${webapp.version}"
                username="${tar.user}" group="${tar.group}">
                <patternset refid="binfiles"/>
            </tarfileset>
        </tar>
        <gzip zipfile="${archive.target}.tgz" src="${archive.target}.tar"/>
        <delete file="${archive.target}.tar"/>
    </target>
    
    <!-- =================================================================== -->
    <!--            Fix CRLF for Unix Machines                               -->
    <!-- =================================================================== -->
    <target name="fixcrlf" depends="init">
        <condition property="notWindows">
            <not><os family="windows"/></not>
        </condition>
        <if>
            <isset property="notWindows"/>
            <then>
                <echo>Running fixcrlf....</echo>
                <fixcrlf eol="lf" srcdir="${basedir}" 
                    includes="**/*.txt,
                              **/*.properties,
                              **/*.sql,
                              **/*.java,
                              **/*.jsp,
                              **/*.xdt,
                              **/*.xml,
                              **/*.page,
                              **/*.html,
                              **/*.js,
                              **/*.css, 
                              **/*.reference,
                              **/*.ldif,
                              **/*.conf"/>
            </then>
            <else>
                <echo>You shouldn't need to run this task on Windows</echo>
            </else>
        </if>
    </target>
    
    <!-- =================================================================== -->
    <!--            Convenience target to setup everything                   -->
    <!-- =================================================================== -->
    <target name="setup" depends="setup-db,setup-tomcat,deploy"
        description="Sets up database, tomcat and deploys expanded war"/>
        
    <!-- =================================================================== -->
    <!--            Targets to install optional packages for testing         -->
    <!--            NOTE: These will screw up code, so only use with Anthill -->
    <!-- =================================================================== -->
    <property name="extras.dir" value="extras"/>
    <target name="install-ibatis" depends="clean" description="Installs iBATIS">
        <ant dir="${extras.dir}/ibatis" target="install"/>
    </target>
    
    <target name="install-springmvc" depends="clean" 
        description="Installs Spring's MVC, removes Struts">
        <ant dir="${extras.dir}/spring" target="install"/>
    </target>
    
    <target name="install-webwork" depends="clean" 
        description="Installs WebWork, removes Struts">
        <ant dir="${extras.dir}/webwork" target="install"/>
    </target>

    <target name="install-jsf" depends="clean" 
        description="Installs JSF, removes Struts">
        <ant dir="${extras.dir}/jsf" target="install"/>
    </target>

    <target name="install-tapestry" depends="clean" 
        description="Installs Tapestry, removes Struts">
        <ant dir="${extras.dir}/tapestry" target="install"/>
    </target>
    
    <!--target name="remove-options" 
        description="Removes unneeded files in 'extras' folder">
        <delete dir="${extras.dir}/ibatis"/>
        <delete dir="${extras.dir}/jsf"/>
        <delete dir="${extras.dir}/spring"/>
        <delete dir="${extras.dir}/tapestry"/>
        <delete dir="${extras.dir}/webwork"/>
    </target-->

    <!-- =================================================================== -->
    <!-- The "new" target is used to create a new project by duplicating     -->
    <!-- this project and replacing "juiseorg" instances with the new name.   -->
    <!-- =================================================================== -->
    <target name="new" depends="clean,init"
        description="creates a new project with the specified name">
<echo level="info">
+-------------------------------------------------------------+
|    -- Welcome to the AppFuse New Application Wizard! --     |
|                                                             |
| To create a new application, please answer the following    |
| questions.                                                  |
+-------------------------------------------------------------+
</echo>
        <echo/>

        <!-- Prompt user for input -->
        <input message="What would you like to name your application [myapp]?" addproperty="app.name"
            defaultvalue="myapp"/>
        <input message="And your database [mydb]?" addproperty="db.name" defaultvalue="mydb"/>
        <input message="What about a package name [org.juise]?" addproperty="new.pkg.name"
            defaultvalue="org.juise"/>

        <echo level="info">Creating new application named '${app.name}'...</echo>
        <copy todir="../${app.name}">
            <fileset dir="${basedir}">
                <exclude name="target/**"/>
                <exclude name="sandbox/**"/>
                <exclude name="www/**"/>
                <exclude name="${webapp.name}*"/>
                <exclude name="*.log"/>
                <exclude name="*.sh"/>
                <include name="**"/>
                <exclude name=".#**"/>
            </fileset>
        </copy>
        <!-- replace app name -->
        <replaceregexp flags="g">
            <regexp pattern="juiseorg"/>
            <substitution expression="${app.name}"/>
            <fileset dir="../${app.name}">
                <include name="**"/>
                <exclude name="src/**/*.java"/>
                <exclude name="src/**/*.xml"/>
                <exclude name="test/**/*.java"/>
                <exclude name="test/**/*.xml"/>   
                <exclude name="extras/**/*.java"/>
                <exclude name="extras/**/*.xml"/>
                <exclude name="metadata/web/global-exceptions.xml"/>
                <exclude name="web/WEB-INF/*.xml"/>
                <exclude name="web/WEB-INF/classes/log4j.properties"/>
                <exclude name="web/WEB-INF/classes/mail.properties"/>
                <exclude name="**/*.page"/>
                <exclude name="**/*.application"/>
                <exclude name="**/*_page.xdt"/>
                <exclude name="**/managed-beans.xdt"/>
                <exclude name="docs/**"/>
                <exclude name="**/loginMenu.jsp"/>
                <exclude name="**/README.txt"/>
                <exclude name=".springBeansProject"/>
            </fileset>
        </replaceregexp>

        <!-- Copy IDEA project files -->
        <copy file="juiseorg.iml" tofile="../${app.name}/${app.name}.iml"/>
        <copy file="juiseorg.ipr" tofile="../${app.name}/${app.name}.ipr"/>
        <replace dir="../${app.name}" value="${app.name}" token="juiseorg">
            <include name="${app.name}.i*"/>
        </replace>

        <!-- Fix path for packages in build.xml -->
        <replaceregexp flags="g" match="org.${app.name}" replace="org.juise">
            <fileset dir="../${app.name}">
                <include name="build.xml"/>
                
                <include name="metadata/web/*.java"/>
            </fileset>
        </replaceregexp>

        <!-- Replace JNDI Name for database -->
        <replaceregexp flags="g">
            <regexp pattern="jdbc(.*)${app.name}"/>
            <substitution expression="jdbc\1${db.name}"/>
            <fileset dir="../${app.name}/metadata/web" includes="*web-resource-env-refs.xml"/>
            <fileset dir="../${app.name}/metadata/conf" includes="**"/>
            <fileset dir="../${app.name}/web" includes="**/*.jsp"/>
            <fileset dir="../${app.name}/extras" includes="**/demos/users.jsp"/>
            <fileset dir="../${app.name}" includes="database.properties.reference"/>
        </replaceregexp>

        <!-- Replace JNDI Name in Spring config files -->
        <replaceregexp flags="g" match="jdbc/juiseorg" replace="jdbc/${db.name}"
            file="../${app.name}/web/WEB-INF/applicationContext-resources.xml"/>

        <!-- Replace database name in properties.xml and -->
        <!-- in database creation script -->
        <replaceregexp flags="g">
            <regexp pattern="${app.name}"/>
            <substitution expression="${db.name}"/>
            <fileset dir="../${app.name}/metadata/sql" includes="*.sql"/>
            <fileset dir="../${app.name}" includes="properties.xml"/>
        </replaceregexp>

        <if>
            <not><equals arg1="${new.pkg.name}" arg2="org.juise"/></not>
            <then><rename-packages newPkgName="${new.pkg.name}"/></then>
        </if>

<echo level="info">
+-------------------------------------------------------------+
|           -- Application created successfully! --           |
|                                                             |
| Now you should be able to cd to your application and run:   |
| > ant setup test-all                                        |
+-------------------------------------------------------------+
</echo>
    </target>

    <macrodef name="rename-packages">
        <attribute name="src.base.dir" default="../${app.name}"/>
        <attribute name="newPkgName"/>

        <sequential>
            <input message="Please enter the first part of your new package name (i.e. com, org, net) [org]"
                addproperty="new.base.pkg.name" defaultvalue="org"/>

            <typedef resource="org.juise/ant/juiseorg-contrib.xml"
                classpath="${rename-packages.jar}" uri="juiseorg:/org.juise.ant"/>

            <juiseorg:renamepackages xmlns:juiseorg="juiseorg:/org.juise.ant"
                newPkgName="@{newPkgName}" baseDir="@{src.base.dir}">
                                                                                                                                             
                <!-- The renamepackages task will search through this file list
                     and replace any paths or packages it finds in these files -->
                                                                                                                                             
                <fileset includes="**/*.xml" excludes="build.xml"/>
                <fileset includes="metadata/web/*.java" />
                <fileset includes="**/*.xdt" />
                <fileset includes="**/*.properties" />
                <fileset includes="**/*.xml" />
                <fileset includes="**/*.page" />
                <fileset includes="**/*.application" />
                <fileset includes=".springBeansProject" />
            </juiseorg:renamepackages>

            <replaceregexp flags="g" match='packagenames="org.*"'
                replace='packagenames="${new.base.pkg.name}.*"'>
                <fileset dir="@{src.base.dir}">
                    <include name="build.xml"/>
                </fileset>
            </replaceregexp>

            <replaceregexp flags="g" match="org.juise" replace="@{newPkgName}">
                <fileset dir="@{src.base.dir}">
                    <include name="build.xml"/>
                </fileset>
            </replaceregexp>

            <echo message="Repackaging info written to rename.log"/>
        </sequential>
    </macrodef>

</project>
