<?xml version="1.0"?>

<!-- 
The DOCTYPE declaration declares the location of database-specific parts of the
Ant build file. 
-->
<!DOCTYPE project [
	<!ENTITY properties SYSTEM "file:./properties.xml">
]>

<project name="lams_tool_twitter" basedir="." default="usage">

	<!-- import properties from the specified file -->
	<property file="build.properties"/>
	<property file="../lams_build/common.properties"/>
	<property file="../lams_build/${osPropertiesName}.properties" />

	<path id="all-libs">
	<fileset dir="${lib}">
			<include name="*.jar"/>
		</fileset>
		<fileset dir="${sharedlib}">
			<include name="**/*.jar"/>
		</fileset>
		<fileset dir="${j2eelibs}">
			<include name="**/*.jar"/>
		</fileset>
	</path>

	<path id="project.classpath">
		<path refid="all-libs"/>
		<!-- Java CLASSPATH should be added as the last item -->
		<!-- This property is supposed to be set in eclipse  -->
		<pathelement location="${java.class.path}" />
	</path>

	<target name="usage">
		<echo message=""/>
		<echo message="${project} build file"/>
		<echo message="------------------------------------------------------"/>
		<echo message=""/>
		<echo message="The available targets are:"/>
		<echo message=""/>
		<echo message="preparedirs  --> Create the build directories"/>
		<echo message="clean-build  --> Remove all the class files"/>
		<echo message="compile-java --> Compile the java source files"/>
		<echo message="compile-test --> Compile the java test source files"/>
		<echo message="clean-db     --> Drop the twitter tables"/>
		<echo message="build-db     --> Drop and Create the twitter tables"/>
		<echo message="insert-test-data     --> Builds the Db and inserts test data"/>
		<echo message="create-deploy-package --> Generates the deployment package"/>
		<echo message="deploy-tool  --> Creates the database and default content and then deploys the jar and war file to lams.ear "/>
		<echo message="test-report  --> Runs the junit testcases and generates the report "/>
		<echo message=""/>
		<echo message="------------------------------------------------------"/>
		<echo message="Notes on running NB junit tests:"/>
		<echo message="Run insert-test-data"/>
		<echo message="This will create the NB tables and sets up the test data needed for the test cases to run. Then you can run test-report"/>		
	</target>

	<target name="print-classpath">
		<echo message="java.class.path = ${java.class.path}"/>
		<property name="path.string" refid="project.classpath"/>
		<echo message="project.classpath = ${path.string}"/>
	</target>

	<!-- ================================================================ -->
	<!-- Preparations									                  -->
	<!-- ================================================================ -->
	<target name="preparedirs">
		<mkdir dir="${build}"/>
		<mkdir dir="${build.classes.java}"/>
		<mkdir dir="${build.classes.test}"/>
		<mkdir dir="${build.lib}"/>
	</target>


	<target name="clean-build" description="removes all class files">
		<delete dir="${build.classes.java}"/>
		<delete dir="${build.classes.test}"/>
		<delete dir="${build.lib}"/>
		<delete dir="${build.deploy}"/>
		<mkdir dir="${build.classes.java}"/>
		<mkdir dir="${build.classes.test}"/>
		<mkdir dir="${build.lib}"/>
		<mkdir dir="${build.deploy}"/>
	</target>

	<!-- ================================================================ -->
	<!-- Compilations								                  -->
	<!-- ================================================================ -->
	
	<target name="compile-java" depends="clean-build" description="compile java sources">
		<javac srcdir="${src.java.dir}" compiler="modern"
	         	 	destdir="${build.classes.java}" deprecation="on" debug="on">
			<classpath>
				<path refid="project.classpath"/>
			</classpath>
		</javac>
		<copy overwrite="yes" todir="${build.classes.java}/${package}">
			<fileset dir="${src.java.dir}/${package}">
					<include name="*.xml"/>
					<include name="*.properties"/>
					<!-- Autopatch -->
					<include name="dbupdates/*.sql"/>
					<include name="dbupdates/autopatchContext.xml"/>
			</fileset>
		</copy>
		<copy overwrite="yes" todir="${build.classes.java}/${package}/web">
			<fileset dir="${src.java.dir}/${package}/web">
					<include name="*.properties"/>
			</fileset>
		</copy>
	</target>

	<target name="compile-test" depends="compile-java">
		<javac destdir="${build.classes.test}" compiler="modern">
			<src path="${src.test.dir}"/>
			<classpath>
				<pathelement location="${build.classes.java}"/>
				<path refid="project.classpath"/>
			</classpath>
		</javac>
		<copy overwrite="yes" todir="${build.classes.test}">
			<fileset dir="${src.test.dir}">
				<include name="**/*.xml"/>
				<include name="**/*.zip"/>
			</fileset>
		</copy>
	</target>

	<!-- =================================================================== -->
	<!-- Database Tasks                                             -->
	<!-- =================================================================== -->
		<target name="clean-db"	description="Deletes the Twitter Tool Tables">
			<sql driver="${db.driver}" url="${db.url}" userid="${db.username}"
					password="${db.password}"  encoding="${db.encoding}">
				<classpath>
					<pathelement location="${db.driver.jar}"/>
				</classpath>
				<transaction src="${db.scripts}/drop_lams_tool_twitter.sql"/>
			</sql>
		</target>
		
		<target name="build-db" depends="" description="Deletes and Creates the Twitter Tool tables and inserts default content">
			<sql driver="${db.driver}" url="${db.url}" userid="${db.username}"
								password="${db.password}"  encoding="${db.encoding}">
							<classpath>
								<pathelement location="${db.driver.jar}"/>
							</classpath>
					<transaction src="${db.scripts}/init_lams_tool_twitter.sql"/>
			</sql>
		</target>	
	
	<!--	<target name="insert-test-data" depends="build-db" description="Inserts test data for twitter tool in order for test cases to run">
				<sql driver="${db.driver}" url="${db.url}" userid="${db.username}"
									password="${db.password}"  encoding="${db.encoding}">
								<classpath>
									<pathelement location="${db.driver.jar}"/>
								</classpath>
						<transaction src="${db.scripts}/insert_lams_tool_twitter_data.sql"/>
				</sql>
		</target>	 -->
	
	<target name="insert-test-data" 
			description="Insert some dummy test data. Removes any data that was in the tables first.">
			   <copy overwrite="yes" file="${db.scripts}/create_lams_tool_twitter.sql"
						   	tofile="${build.sql}/create_lams_tool_twitter_test.sql" >
				  <!-- replace ${default_content_id} sql file with a hardcoded value  -->
			        <filterset begintoken="$${" endtoken="}">
		                <filter token="default_content_id" value="${test.contentid}"/>
			        </filterset>
			    </copy>
				<sql driver="${db.driver}" url="${db.url}" userid="${db.username}"
					password="${db.password}"  encoding="${db.encoding}">
				<classpath>
					<pathelement location="${db.driver.jar}"/>
				</classpath>
				<transaction src="${db.scripts}/drop_lams_tool_twitter.sql"/>
				<transaction src="${build.sql}/create_lams_tool_twitter_test.sql"/>
				<transaction src="${db.scripts}/insert_test_data.sql"/>
			</sql>
	</target>

	
	
	<!-- ================================================================ -->
	<!-- Make jar archive for deployment        		    	          -->
	<!-- ================================================================ -->
	<target name="build-jar" depends="compile-java" description="creates jar file">
		<delete file="${build.lib}/${product}.jar"/>
		<jar jarfile="${build.lib}/${product}.jar" manifest="${conf.dir}/jar/META-INF/MANIFEST.MF">
			<fileset dir="${build.classes.java}">
			</fileset>
			<fileset dir="${conf.hibernate.mapping.dir}">
			</fileset>
		</jar>
	</target>

	<!-- = = = = = = = = = = = = = = = = = = = = = = = =  -->
	<!-- Make the war archive  for deployment             -->
	<!-- = = = = = = = = = = = = = = = = = = = = = = = =  -->
	<target name="build-war" depends="build-jar, webdoclet" description="creates war file">
		<delete file="${build.lib}/${product}.war"/>
		<war warfile="${build.lib}/${product}.war" 
  				webxml="${conf.webinf.dir}/web.xml" manifest="${conf.dir}/war/META-INF/MANIFEST.MF">
			<webinf dir="${conf.webinf.dir}">
				<include name="**/*.tld"/>
				<include name="*.xml"/>
				<include name="*.properties"/>
				<exclude name="web.xml"/>
				<include name="spring/*"/>
				<include name="tiles/*.xml"/>
				<include name="struts/*"/>
				<include name="tags/*.*"/>
			</webinf>
			<fileset dir="${conf.web.dir}">
				<include name="*"/>
				<include name="includes/*"/>
				<include name="images/*"/>
				<include name="template/*"/> <!-- used by tiles -->
				<include name="css/**"/>
				<include name="authoring/*.*"/>
				<include name="monitoring/*.*"/>
			</fileset>
		</war>
	</target>

	   <target name="copy-tags" depends="" description="copy the lams.tld and the lams tags">
	   		<copy todir="${conf.webinf.dir}" overwrite="yes" file="${tag.web.dir}/lams.tld" />	
			<copy todir="${conf.webinf.dir}/tags" overwrite="yes">
				<fileset dir="${tag.web.dir}/tags">
					<include name="*.tag" />
				</fileset>
			</copy>
	   </target>

	<!-- =================================================================== -->
	<!-- Deploy the example struts jar and war                               -->
	<!-- =================================================================== -->
	<target name="deploy-war" depends="build-war" description="Deploy the twitter tool war file and jar file">
		<delete quiet="true">
			<fileset dir="${jboss.deploy}/tmp"/>
			<fileset dir="${jboss.deploy}/work"/>
		</delete>
		<copy file="${build.lib}/${product}.jar"
				todir="${jboss.deploy}"/> 
		<copy file="${build.lib}/${product}.war"
				todir="${jboss.deploy}"/> 
	</target>


	<!-- =================================================================== -->
	<!-- Run Middlegen                                                     -->
	<!-- =================================================================== -->

	   <target 
	      name="middlegen" 
	      description="Run Middlegen" 
	      unless="middlegen.skip"
	      depends=""
	   >
	      <taskdef
	         name="middlegen"
	         classname="middlegen.MiddlegenTask"
	         classpathref="all-libs"
	      />

	      <middlegen
	         appname="${name}"
	         prefsdir="${middlegen}"
	         gui="${middlegen.gui}"
	         databaseurl="${db.url}"
	         initialContextFactory="${java.naming.factory.initial}"
	         providerURL="${java.naming.provider.url}"
	         datasourceJNDIName="${datasource.jndi.name}"
	         driver="${db.driver}"
	         username="${db.username}"
	         password="${db.password}"
	         schema="${db.schema}"
	         catalog="lams"
	         includeViews="false"
	      >

	         <hibernate 
	            destination="${conf.dir}/hibernate/mappings"
	            package="${package.name}"
	            genXDocletTags="true"
	            javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"
	         	
	         />

			<table name="tl_fptwit10_content" />
	      	<table name="tl_fptwit10_session" />
	      	<table name="tl_fptwit10_user" />
	      	<table name="tl_fptwit10_attachment" />
	      	<table name="tl_fptwit10_user_tweet" />
	      </middlegen>

	   </target>
	
	<!-- =================================================================== -->
	<!-- Generate hibernate xml files                             			 -->
	<!-- =================================================================== -->
	  <target name="generate-hbm-files" 
	   	description="Generate the hibernate hbm.xml files from the Java source (using XDoclet)">
	  
	  <taskdef name="hibernatedoclet"
	   classname="xdoclet.modules.hibernate.HibernateDocletTask"
	   classpathref="all-libs"/>
	  
	  <echo message="Building hbm.xml files using XDoclet"/>

	  <hibernatedoclet
	   destdir="${conf.hibernate.mapping.dir}"
	   excludedtags="@version,@author,@todo"
	   force="true">
	    <fileset dir="${src.java.dir}">
	    	<include name="**/*.java"/>
	    </fileset>
	   <hibernate version="3.0"/>
	  </hibernatedoclet>
	 </target>
	
	
		<!-- =================================================================== -->
		<!-- Run JUnit Tests                                                     -->
		<!-- =================================================================== -->
		<target name="test" depends="compile-test"> 
			<mkdir dir="${build.report}"/>
			<junit printsummary="yes" haltonerror="no" haltonfailure="no"
				   fork="yes">
				<jvmarg value="-Xms512M"/>
				<jvmarg value="-Xmx1024M"/>
				<formatter type="plain" usefile="false"/>
				<formatter type="xml"/>
				<batchtest todir="${build.report}">
					<fileset dir="${src.test.dir}">
						<include name="**/Test*.java"/>
						
					</fileset>
				</batchtest>
				<classpath>
					<pathelement location="${build.classes.java}"/>
					<pathelement location="${build.classes.test}"/>
					<pathelement location="${conf.hibernate.mapping.dir}"/>
					<pathelement location="${conf.dir}"/>
					<path refid="project.classpath"/>
				</classpath>
			</junit>
		</target>
		
		<target name="test-report" depends="test">
			<mkdir dir="${build.report}/html"/> 
			<junitreport todir="${build.report}"> 
				<fileset dir="${build.report}">
					<include name="TEST-*.xml"/>
				</fileset>
				<report todir="${build.report}/html"/> 
			</junitreport>
		</target>
	
		<!-- =================================================================== -->
		<!-- Generate struts xml files                             -->
		<!-- =================================================================== -->
		<target name="webdoclet" depends="preparedirs" description="Generate the struts xml files from the Java source (using XDoclet)">
			<taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask">
			    <classpath refid="all-libs"/>
			</taskdef>

			<webdoclet destdir="${generated.java}" force="${xdoclet.force}">
				
	         <fileset dir="${src.java.dir}">
	            <include name="**/*Action.java" />
	            <include name="**/*Form.java" />
	         </fileset>

	         <deploymentdescriptor servletspec="2.4" 
 				distributable="false" destdir="${conf.webinf.dir}"
				mergedir="${conf.xdoclet.dir}" sessiontimeout="${sessiontimeout}"/>
				
	         <strutsconfigxml destdir="${conf.webinf.dir}" mergedir="${conf.xdoclet.dir}" version="1.2"/>
	      </webdoclet>
		</target>
	
		<!-- =================================================================== -->
		<!-- Generate the deploy package                                         -->
		<!-- =================================================================== -->
		<target name="create-deploy-package" depends="build-war" 
				description="Generate the deployment package.">

				<path id="deploy.lib.classpath">
					<fileset dir="${deploy.tool.dir}">
						<include name="lib/*.jar"/>
					</fileset>
				</path> 
					  
				<mkdir dir="${build.deploy}"/>
				<mkdir dir="${build.deploy}/lib"/> 
				<mkdir dir="${build.deploy}/sql"/> 
				<mkdir dir="${build.deploy}/lang"/> 
				<mkdir dir="${build.deploy}/language"/>
				 
				<copy overwrite="yes" todir="${build.deploy}/sql">
					<fileset dir="${db.scripts}/">
						<include name="drop_lams_tool_twitter.sql"/>
						<include name="create_lams_tool_twitter.sql"/>
						<include name="activity_insert.sql"/>
						<include name="library_insert.sql"/>
					</fileset>
				</copy>
			
				<copy overwrite="yes" todir="${build.deploy}/sql">
						<fileset dir="${db.scripts}/">
							<include name="tool_insert.sql" />
						</fileset>	
						<filterset>
							<filter token="tool_version" value="${tool.version}" />
						</filterset>
				</copy>
			
				<copy overwrite="yes" todir="${build.deploy}/sql">
					<fileset dir="${db.scripts}/">
						<include name="db_version_insert.sql" />
					</fileset>	
					<filterset>
						<filter token="tool_version" value="${tool.version}" />
						<filter token="signature" value="${signature}" />
					</filterset>
				</copy>
			
				<copy overwrite="yes" todir="${build.deploy}/sql/updatescripts" failonerror="false">
					<fileset dir="${db.scripts}/updatescripts/">
						<include name="*.sql" />
					</fileset>
				</copy>

				<copy overwrite="yes" todir="${build.deploy}/">
					<fileset dir="${deploy.tool.dir}">
						<include name="*.*"/>
					</fileset>
				</copy>

				<copy overwrite="yes" todir="${build.deploy}/lib">
					<fileset dir="${deploy.tool.dir}/lib">
						<include name="*.jar"/>
					</fileset>
				</copy>

				<copy overwrite="yes" todir="${build.deploy}/language">
					<fileset dir="${conf.language.dir}">
						<include name="*.properties"/>
					</fileset>
				</copy>
			
			<copy overwrite="true" todir="${build.deploy}">
					<fileset dir="${build.lib}">
						<include name="*.jar"/>
						<include name="*.war"/>
					</fileset>
			</copy>

			<taskdef name="generateDeployProperties" 
					classname="org.lamsfoundation.lams.tool.deploy.CreateToolPackageTask">
				    <classpath refid="deploy.lib.classpath"/>
				</taskdef>

				<generateDeployProperties depends="compile" 
					mode="development" 
					outputPath="${build.deploy}"
					generateForInstallers="${generate.for.installers}"
					dbPassword="${db.password}"
					dbUsername="${db.username}"
					dbDriverUrl="${db.url}"
					dbDriverClass="com.mysql.jdbc.Driver"
					deployFiles="${build.deploy}/${product}.war,${build.deploy}/${product}.jar"
					toolJarFileName="${product}.jar"
					toolSignature="${signature}"
					toolVersion="${tool.version}"
					hideTool="${hideTool}"
					minServerVersionNumber="${min.server.version.number}"
					toolUpdateScriptPath="${build.deploy}/sql/updatescripts/updateTo${tool.version}.sql"
					toolTablesScriptPath="${build.deploy}/sql/create_lams_tool_twitter.sql" 
					toolTablesDeleteScriptPath="${build.deploy}/sql/drop_lams_tool_twitter.sql" 
					toolActivityInsertScriptPath="${build.deploy}/sql/activity_insert.sql" 
					toolLibraryInsertScriptPath="${build.deploy}/sql/library_insert.sql" 
					toolInsertScriptPath="${build.deploy}/sql/tool_insert.sql" 
					toolDBVersionScriptPath="${build.deploy}/sql/db_version_insert.sql"
					toolApplicationContextPath="/org/lamsfoundation/lams/tool/twitter/applicationContext.xml"
					lamsEarPath="${jboss.deploy}"
					toolContext="${toolContext}"
					toolWebUri="${product}.war"
					languageFilesPackage="org.lamsfoundation.lams.tool.twitter">

					<!-- language files go as a fileset. Not nice but can't work out how to embed them in a tag. -->
					<fileset dir="${build.deploy}/language">
						<include name="**/*.properties"/>
						<include name="**/*.txt"/>
					</fileset>
						
				</generateDeployProperties>
			</target>
		
	<target name="run-deploy-package" description="Run the deploy tool.">
				<path id="deploy.classpath">
					<fileset dir="${build.deploy}">
						<include name="lib/*.jar"/>
					</fileset>
				</path> 

				<echo>Deploying the Twitter Tool</echo>
		
				<java
					classname="org.lamsfoundation.lams.tool.deploy.Deploy"
					classpathref="deploy.classpath"
					fork="true">
						<arg file="${build.deploy}/deploy.xml"/>
						<arg value="False"/> 
				</java>
							
	</target>
	
	<target name="deploy-tool" depends="create-deploy-package, run-deploy-package" 
	   description="Build the war, jar and run the deploy tool. Deletes most old tool references from db, creates db tables, application.xml in ear, copies war and jar file to ear. deploy-tool is only designed to be run in a development environment, or on an empty db. Do not run on a production environment.">
	</target>
	
	<target name="delete-exploded-tool-folder" description="delete old tool exploded tool folder">
			<!-- delete old war folder tree -->
			<delete quiet="true" includeemptydirs="true">
				<fileset dir="${jboss.deploy}/${product}.war">
				    <include name="**"/>
				</fileset>
			</delete>
			<!-- if use explode tool target when jboss is running, explode may be failed and left the the temp folder. Here try to delete the temp folder -->
			<delete quiet="true" includeemptydirs="true">
				<fileset dir="${jboss.deploy}/temp-${product}.war">
				    <include name="**"/>
				</fileset>
			</delete>
		</target>
		
		<target name="explode-tool" depends="" description="explode tool jar and war package to a war folder">
			<!-- rename war file to avoid name conflict -->
			<move tofile="${jboss.deploy}/temp-${product}.war" file="${jboss.deploy}/${product}.war"/>
			<!-- explode the ear -->
			<!-- Don't explode jar file, becuase classes in jar will conflict with those same classes files in war -->
			<!--<unjar src="${jboss.deploy}/${product}.jar" dest="${jboss.deploy}/${product}.war" />-->
			<unjar src="${jboss.deploy}/temp-${product}.war" dest="${jboss.deploy}/${product}.war" />
			<!-- delete war and jar files -->
			<delete>
				<fileset dir="${jboss.deploy}">
				    <include name="temp-${product}.war"/>
				    <!--<include name="${product}.jar"/>-->
				</fileset>
			</delete>
		</target>
		
	    <!-- ================================= 
	          target: synchronize-jsp              
	         ================================= -->
	    <target name="synchronize-deploy" description="copy non-java file into deploy folder">
	        <copy verbose="yes" todir="${jboss.deploy}/${product}.war">
	        	<fileset dir="${conf.web.dir}">
	        		<include name="**/*.*"/>
	        	</fileset>
	        </copy>
	    </target>

	   <!-- =================================  
	         target: convert UTF-8 language files to Java properties files using native2ascii 
	        ================================= -->
	    <target name="native2ascii" 
	    	description="convert UTF-8 language files to Java properties files using native2ascii">
			<native2ascii encoding="UTF-8" src="${conf.language.dir}" dest="${conf.language.dir}"
			   includes="*.txt" ext=".properties"/>
	    </target>
	
</project>

