<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	REVISIT (KP): We can have a script Id here to be able to identify the script externaly. Not sure.
-->
<script version="1.0" id="customer.registration">
	<!--
		REVISIT (KP): Add a 'note' element to script - can be used in proposed 
		script builder.
	-->
	<note>
		This script defines the process of customer account registration
	</note>
	<!-- ============== Variables declarations =============== -->
	<variables>
		<!-- 
		REVISIT (KP):
		We can have 'usage' attribute with values (input | output | both | internal), so 
		illegal usage can be prevented during interpretation. 
		By default it's value can be 'both' - input and output.
		
		Add initialization with default values. Can be done for simple types:
		String, Integer, Long, Boolean and maybe Date.		
		-->
		<!-- ============== Internal variables =============== -->
		<variable name="customerId" type="java.lang.String" usage="internal"/>
		<variable name="posCode" type="java.lang.String" usage="internal">SC</variable>
		<!-- ============== Value holders =============== -->
		<variable name="customer" type="com.protek.bccs.bc.customer.PartyCustomerProfile"/>
		<!-- ============== Processing variables =============== -->
		<variable name="errors" type="screenflow.helper.ErrorList"/>
	</variables>
	<!-- ============== Operations declarations =============== -->
	<external-operations>
		<!-- 
			REVISIT (KP): 
			Remove parameter 'value' attribute from declaration or use it as default.
			Remove 'result' from operation declaration.
			
			Parameter values and result should be assigned on usage.
		-->
		<!-- No argument operation -->
		<external-operation id="createCustomer" src="com.protek.bccs.screenflow.operations.CreateCustomerEntity"/>
		<!-- One argument operation -->
		<external-operation id="validatePersonalInfo" src="com.protek.bccs.screenflow.operations.ValidatePersonalInfo">
			<parameter name="customer" type="com.protek.bccs.bc.customer.PartyCustomerProfile"/>
		</external-operation>
	</external-operations>
	<!-- ============== Screenflow definition =============== -->
	<!-- 
		REVISIT (KP): State definition
			- make 'note' an element.
			- change element name to 'state'
			- redesign structure 
			- make 'form' attribute not required
	-->
	<start-state id="customer.registration.start">
		<note>
			Creation and initialization of customer entity value bean
		</note>
		<!--
			This part must be executed just after the control moved to this state
			<on-enter>
				{operations | conditions | transitions}
			</on-enter>
		-->
		<on-enter>
			<exec operation="createCustomer">
				<!-- Set result to the 'customer' variable -->
				<result variable="$customer"/>
			</exec>
			<!-- Do not send any response to user - just switch to a new state -->
			<goto state="customer.personal.edit"/>
		</on-enter>
		<!--
		<on-event name="event-name1">
			{operations | conditions | transitions}
		</on-event>
			...
		<on-event name="event-nameN">
			{operations | conditions | transitions}
		</on-event>
		-->
		<!--
		REVISIT (KP): This part should be performed just before leaving this state.
		Can we have a transition here?
		-->
		<!--
		<on-exit>
			{operations | conditions}
		</on-exit>
		-->
	</start-state>
	<state id="customer.personal.edit" form="customer.personal.edit">
		<note>
			This state performs customer personal details editing 
		</note>
		<on-event name="next">
			<exec operation="validatePersonalInfo">
				<result variable="$errors"/>
			</exec>
			<!-- Removed 'then' element-->
			<if test="$errors">
				<!-- REVISIT (KP):
					A set of internal variables is needed: _self, _parent, _initiator, etc. 
				-->
				<goto state="_self"/>
				<!-- REVISIT (KP):
					We should have a possibility to specify when send response to the user
					and with what data in parameters.
				-->
			</if>
			<goto state="customer.registration.save"/>
		</on-event>
		<on-event name="cancel">
			<goto state="customer.registration.final"/>
		</on-event>
	</state>
	<state id="customer.registration.save">
		<note>
			Save customer registration info
		</note>
		<on-enter>
			<exec operation="registerCustomer">
				<!-- Set result to the 'customer' variable -->
				<result variable="$errors"/>
			</exec>
			<if test="$errors">
				<goto state="customer.registration.error"/>
			</if>
			<!-- Do not send any response to user - just switch to a new state -->
			<goto state="customer.personal.edit"/>
		</on-enter>
	</state>
	<!--
		REVISIT (KP): Can we have many final-states? 
		Example: registration success or error states
	-->
	<final-state id="customer.registration.error" form="customer.registration.error"/>
	<final-state id="customer.registration.final" form="customer.registration.final">
		<!-- REVISIT (KP): Switch to the previous active script or show default screen -->
	</final-state>
</script>
