<project name="Cairngen 2.1" default="main" >
	
	<!-- 
	User specific configuration properties. Typically this is the only file you need to modify
	-->
	<property file="project.properties" />
	
	<!-- 
    Cairngen configuration properties. This file contains the properties which are specific to 
    all Cairngen projects. Typically you need not modify this file. 
    -->
	<property file="build/config/cairngen.properties" />
	
	<!-- 
    Cairngen logging configuration properties. This file contains the messages which are displayed
    in the console output and optionally appended to the cairngen-log.txt file
    -->
	<property file="build/config/log.properties" />

	<!-- 
	The Antcontrib library adds the ability to implement conditional expression as well as many 
	additional logical evaluations and expresssions
	-->
	<taskdef name="foreach" 
	         classpath="build/libs/ant-contrib-1.0b3.jar" 
	         classname="net.sf.antcontrib.logic.ForEach" />

	<taskdef name="if"
	         classpath="build/libs/ant-contrib-1.0b3.jar" 
	         classname="net.sf.antcontrib.logic.IfTask" />
    <!-- 
    contains the applications copyright information which is appended
    to the beginning of the file (before the package declaration).
    This file can be located under build/templates/file-header-copy.tpl
    --> 
    <loadresource property="copy">
         <file file="build/templates/file-header-copy.tpl" />
    </loadresource>
	
	<!-- 
    Default Cairngen target which generates the typical cairgorm project structure which consists of the 
    following packaes: business, control, commands, events, model, views, vo 
    Additionally, this target generates the applications ModelLocator, FrontController and services.mxml
    -->
	<target name="main" depends="log, clean, create-cairngorm-project" />

	<!-- 
    Logging target, writes build output to log file, cairngen-log.txt 
    -->
    <target name="log" >
		<if><equals arg1="${log.output}" arg2="true" />
			<then>
				<record name="${root.dir}/${log.file.name}.txt" action="start" append="${log.append.to.file}" />
		    </then>
		</if>
    </target>
	    	
    <!-- delete previously created directories and create default cairngorm directories -->
	<target name="clean" depends="log, delete-cairngorm-directories, create-cairngorm-directories" />

    <!-- create required cairngorm classes -->
	<target name="create-cairngorm-project" 
		    depends="log, create-model-locator, create-front-controller, create-service-locator" />
	
	<!-- create default cairngorm project directories -->
	<target name="create-cairngorm-directories" >
		<echo>${log.create.cairngorm.directories.message}</echo>
        <mkdir dir="${project-uri}/${business.dir}" />
        <mkdir dir="${project-uri}/${commands.dir}" />  
		<mkdir dir="${project-uri}/${control.dir}"  />
        <mkdir dir="${project-uri}/${events.dir}"   />
		<mkdir dir="${project-uri}/${model.dir}"    />
        <mkdir dir="${project-uri}/${view.dir}"     /> 
		<mkdir dir="${project-uri}/${vo.dir}"       />
	</target>
	
	<!-- delete all previously generated directories -->
	<target name="delete-all-directories" depends="log" >
		<if><equals arg1="${prompt.on.delete}" arg2="true" />
			<then>
				<input message="All files and directories will be deleted. Do you wish to continue" 
					   validargs="yes,no" 
					   defaultvalue="yes" 
					   addproperty="verifyDelete" />
				       <condition property="abort" >
				           <equals arg1="no" arg2="${verifyDelete}" />
				       </condition>
				<fail if="abort">target delete-all-directories aborted by user</fail>
		    </then>
		</if>
		<echo>${log.delete.all.directories.message}</echo>
		<delete dir="${root.dir}/${com.dir}" />
	</target>
		
	<!-- delete previously generated cairngorm directories -->
	<target name="delete-cairngorm-directories" depends="log" >
		<if><equals arg1="${prompt.on.delete}" arg2="true" />
			<then>
				<input message="All Cairngorm files and directories will be deleted. Do you wish to continue" 
					   validargs="yes,no" 
					   defaultvalue="yes" 
					   addproperty="verifyDelete" />
				       <condition property="abort" >
				          <equals arg1="no" arg2="${verifyDelete}"/>
				       </condition>
				<fail if="abort">target delete-cairngorm-directories aborted by user</fail>
		    </then>
		</if>
		<echo>${log.delete.cairngorm.directories.message}</echo>
		<delete dir="${project-uri}/${business.dir}" />
        <delete dir="${project-uri}/${commands.dir}" />  
		<delete dir="${project-uri}/${control.dir}"  />
        <delete dir="${project-uri}/${events.dir}"   />
		<delete dir="${project-uri}/${model.dir}"    />
        <delete dir="${project-uri}/${view.dir}"     /> 
		<delete dir="${project-uri}/${vo.dir}"       />
	</target>
	
	<!-- create default cairngorm project ModelLocator -->
	<target name="create-model-locator" >
		<echo>${log.model.locator.message}</echo>
		<copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/ModelLocator.tpl" 
			  tofile="${project-uri}/${model.dir}/ModelLocator.as"  
			  overwrite="${overwrite.files}" >
	        <filterchain>
	           <replacetokens>
	                <token key="projectname" value="${project.name}" />
	                <token key="namespace"   value="${namespace}"    />
	                <token key="model"       value="${model.dir}"    />
		           	<token key="copy"        value="${copy}"         />
	            </replacetokens>
	        </filterchain>
	    </copy>
	</target>
    
    <!-- create default cairngorm project FrontController -->
	<target name="create-front-controller" >
	    <echo>${log.front.controller.message}</echo>
	    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/FrontController.tpl" 
	    	  tofile="${project-uri}/${control.dir}/${project.name}Controller.as" 
	    	  overwrite="${overwrite.files}" >
	        <filterchain>
	            <replacetokens>
	                <token key="projectname" value="${project.name}" />
	                <token key="namespace"   value="${namespace}"    />
	                <token key="control"     value="${control.dir}"  />
	                <token key="events"      value="${events.dir}"   />
	                <token key="commands"    value="${commands.dir}" />
	           		<token key="copy"        value="${copy}"         />
	            </replacetokens>
	        </filterchain>
	    </copy>
	</target>
	
	<!-- create default cairngorm project ServiceLocator -->
	<target name="create-service-locator" >
		<echo>${log.service.locator.message}</echo>
	    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/Services.tpl" 
	    	  tofile="${project-uri}/${business.dir}/Services.mxml" 
	    	  overwrite="${overwrite.files}" >
	        <filterchain>
	            <replacetokens>
	                <token key="namespace" value="${namespace}" />
	            </replacetokens>
	        </filterchain>
	    </copy>
	</target>
			
	<!-- 
	Cairngen create-sequence-include-delegate; generates Event, Command
	and Business Delegete. Use this target to generate a sequence which 
	requires a Business Delegates to be generated
	-->
	<target name="create-sequence-include-delegate" 
		    depends="log, add-command-to-controller, add-command-to-controller-cleanup" >
		<echo>${log.sequence.include.delegate.message}</echo>
		
		<replaceSpaces      property="optomizedSequence" value="${sequence.name}"     />
		<convertToUpperCase property="uppercaseSequence" value="${optomizedSequence}" />
		
		<!-- Create the Event class -->
		<copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/Event.tpl"
			  tofile="${project-uri}/${events.dir}/${optomizedSequence}Event.as" 
			  overwrite="${overwrite.files}" >
			<filterchain>
	        	 <replacetokens>
	        		<token key="sequenceToUpperCase"  value="${uppercaseSequence}" />
	           	    <token key="sequence"             value="${optomizedSequence}" />
	                <token key="namespace"   		  value="${namespace}"         />
	                <token key="events"      		  value="${events.dir}"        />
	        		<token key="model"       		  value="${model.dir}"         />
	        		<token key="copy"        		  value="${copy}"              />
	            </replacetokens>
	        </filterchain>
	    </copy>
		
		<!-- Create the Command class -->
	    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/CommandIncludeDelegate.tpl" 
	    	  tofile="${project-uri}/${commands.dir}/${optomizedSequence}Command.as" 
	    	  overwrite="${overwrite.files}" >
	        <filterchain>
	           <replacetokens>
	                <token key="sequence"   value="${optomizedSequence}" />
	                <token key="namespace"  value="${namespace}"         />
	                <token key="events"     value="${events.dir}"        />
	                <token key="commands"   value="${commands.dir}"      />
	                <token key="business"   value="${business.dir}"      />
	           	    <token key="model"      value="${model.dir}"         />
	           		<token key="copy"       value="${copy}"              />
	            </replacetokens>
	        </filterchain>
	    </copy>
		
		<!-- create the BusinessDelegate class -->
	    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/BusinessDelegate.tpl" 
	    	  tofile="${project-uri}/${business.dir}/${optomizedSequence}Delegate.as" 
	    	  overwrite="${overwrite.files}" >
	        <filterchain>
	           <replacetokens>
	                <token key="sequence"   value="${optomizedSequence}" />
	                <token key="namespace"  value="${namespace}"         />
	                <token key="business"   value="${business.dir}"      />
           			<token key="copy"       value="${copy}"  		     />
	            </replacetokens>
	        </filterchain>
	    </copy>
	</target>
	
	<!-- 
	Cairngen create-sequence-exclude-delegate, generates: Event and Command classes
	Use this target to generate a sequence which does not require Business Delegates 
	to be generated
	-->
	<target name="create-sequence-exclude-delegate" 
		    depends="log, add-command-to-controller, add-command-to-controller-cleanup" >
		
		<echo>${log.sequence.exclude.delegate.message}</echo>
		
		<replaceSpaces      property="optomizedSequence" value="${sequence.name}"     />
		<convertToUpperCase property="uppercaseSequence" value="${optomizedSequence}" />
		
		<!-- Generate Cairngorm Event class -->
	    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/Event.tpl" 
	    	  tofile="${project-uri}/${events.dir}/${optomizedSequence}Event.as" 
	    	  overwrite="${overwrite.files}" >
	        <filterchain>
	           <replacetokens>
	           		<token key="sequenceToUpperCase"  value="${uppercaseSequence}" />
	           		<token key="sequence"             value="${optomizedSequence}" />
	           		<token key="namespace"            value="${namespace}"         />
	           		<token key="events"               value="${events.dir}"        /> 
           			<token key="copy"                 value="${copy}"              />
	            </replacetokens>
	        </filterchain>
	    </copy>
		<!-- Generate Cairngorm Command class -->
	    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/CommandExcludeDelegate.tpl" 
	    	  tofile="${project-uri}/${commands.dir}/${optomizedSequence}Command.as" 
	    	  overwrite="${overwrite.files}" >
	        <filterchain>
	           <replacetokens>
	                <token key="sequence"    value="${optomizedSequence}" />
	                <token key="namespace"   value="${namespace}"         />
	                <token key="events"      value="${events.dir}"        />
	                <token key="commands"    value="${commands.dir}"      /> 
	           	    <token key="model"       value="${model.dir}"         />
   					<token key="copy"        value="${copy}"              />
	            </replacetokens>
	        </filterchain>
	    </copy>
	</target>

	<!-- 
	create multiple sequences: allows mulitple Event, Command and Delegate 
	classes to be generated together. Use a comma (,) to seperate the names
	of each Sequence (e.g. Login,Logout)
	
	Use this target to generate multiple sequence which require Business
	Delegates to be generated
	-->
	<target name="create-multiple-sequences-include-delegates">
		
		<echo>${log.create.mulitple.sequences}</echo>
		<foreach list="${sequence.name}" target="create-sequence-include-delegate" 
			                             param="sequence.name" 
			                             delimiter="," />
	</target>
	
	<!-- 
	create multiple sequences: generates mulitple Event, Command and Business
	Delegate classes. Use a comma (,) to seperate the names of each Sequence 
    (e.g. Login, Logout, etc)
	
	Use this target to generate multiple sequence which do not require Business
	Delegates to be generated
	-->
	<target name="create-multiple-sequences-exclude-delegates" >

		<echo>${log.create.mulitple.sequences}</echo>
		<foreach list="${sequence.name}" target="create-sequence-exclude-delegate"  
			                             param="sequence.name" 
			                             delimiter="," />
	</target>

	<!-- 
    Insert FrontController.addCommand to map Event and Command classes 
    *** IMPORTANT: Do not modify the comment "//todo: add commands" ***
    This comment must be available in the Controller in order for addCommand();
    invocations to be generated in the FrontController by Cairngen
    -->
	<target name="add-command-to-controller" depends="log" >
		
		<echo>${log.addcommmand.message}</echo>
		
		<replaceSpaces      property="optomizedSequence" value="${sequence.name}" />
		<convertToUpperCase property="uppercaseSequence" value="${optomizedSequence}" />
		
		<replace file="${project-uri}/${control.dir}/${project.name}Controller.as" 
			     token="//todo: add commands" 
			     value="this.addCommand( ${optomizedSequence}Event.${uppercaseSequence}_EVENT, ${optomizedSequence}Command );//todo: add commands"
			     />
	</target>
	
	<target name="add-command-to-controller-cleanup" >
		<replace file="${project-uri}/${control.dir}/${project.name}Controller.as" >
		    <replacetoken>//todo: add commands</replacetoken>
		    <replacevalue><![CDATA[
		    //todo: add commands]]></replacevalue>
		</replace>
    </target>
	
	<!-- create caringorm ValueObject class -->
	<target name="create-value-object" >
		<echo>${log.value.object.message}</echo>
		
		<replaceSpaces property="optomizedVO" value="${vo.name}" />
		
		<if><equals arg1="${vo.remoteClass}" arg2="true" />
			<then>
			   <property name="voSufix" value="${vo.registerSuffix}" />
			</then>
			<else>
				<property name="voSufix" value="" />
			</else>
		</if>
	    <copy file="${cairngorm.templates.dir}/${cairngorm.framework.name}${cairngorm.version}/ValueObject${voSufix}.tpl" 
	    	tofile="${project-uri}/${vo.dir}/${optomizedVO}VO.as" overwrite="${overwrite.files}" >
	        <filterchain>
	           <replacetokens>
	                <token key="vo"          value="${optomizedVO}"   />
	                <token key="namespace"   value="${namespace}" />
                    <token key="vo.dir"      value="${vo.dir}"    />
	           	    <token key="copy"        value="${copy}"      />
	            </replacetokens>
	        </filterchain>
	    </copy>
	</target>
	
	<!-- 
	create multiple sequences: generates mulitple Event, Command and Business
	Delegate classes. Use a comma (,) to seperate the names of each Sequence 
    (e.g. Login, Logout, etc)
	
	Use this target to generate multiple sequence which do not require Business
	Delegates to be generated
	-->
	<target name="create-multiple-value-objects">
		<echo>${log.create.mulitple.value.objects}</echo>
		<foreach list="${vo.name}" target="create-value-object" param="vo.name" delimiter="," />
	</target>
	
    <!-- private -->
	<scriptdef name="convertToUpperCase" language="javascript" >
		<attribute name="property" />
		<attribute name="value"    />
		project.setNewProperty( attributes.get("property"), attributes.get("value").toUpperCase() );
	</scriptdef>
	<scriptdef name="replaceUnderscores" language="javascript" >
		<attribute name="property" />
		<attribute name="value"    />
		project.setNewProperty( attributes.get("property"), attributes.get("value").replace(" ", "_") );
	</scriptdef>
	<scriptdef name="replaceSpaces" language="javascript" >
		<attribute name="property" />
		<attribute name="value"    />
		project.setNewProperty( attributes.get("property"), attributes.get("value").replace(" ", "") );
	</scriptdef>
	
</project>

