<?xml version="1.0" encoding="UTF-8"?>
<process xmlns:blue="http://www.e-marson.com/bluelemon">

  <descriptor>
    <name>com/emarson/lemonade/examples/mobank/Validate</name>
    <version>1.0</version>
    <description>
      This is the action invoked when user is trying to continue
      after filling the AuthenticationScreen text fields:
      userId and password.
      
      This action:
       - reads the values of the text fields,
       - if any of them is empty, shows the warning alert and returns,
       - forwards the processing to the Authenticate node in the 
         "network" location.
    </description>
    <vendor>E-MARSON</vendor>
  </descriptor>

  <path>
  
    <!-- ASSOCIATIONS --->

    <!-- association to AuthenticationScreen --->
    <action name="go to the authentication screen screen">
      <type>general/bluelemon/code</type>
      <definition resultTo="actionForCancel">

        <blue:call node="com/emarson/lemonade/examples/mobank/AuthenticationScreen"
                   location="@com/emarson/lemonade/examples/mobank"/>


      </definition>
    </action>
    
    
    <!-- INSTRUCTION --->
    
    <action name="read data, verify and continue">
      <type>general/bluelemon/code</type>
      <definition resultTo="result">

        <!-- get the instance of the screen which is currently being shown --->
        <blue:call manager="GeneralManager" method="getCurrent"
                   toVariable="form"
                   castTo="com/emarson/lemonade/bluelemon/midp1/Form"/>


        <!-- get its first item... --->
        <blue:call object="form" method="getItem"
                   toVariable="userIdField"
                   castTo="com/emarson/lemonade/bluelemon/midp1/TextField">

          <blue:argument type="byte" value="0"/>
        </blue:call>
        <!-- ...and read its value --->
        <blue:call object="userIdField" method="getString"
                   toVariable="userId"/>

        
        <!-- same with the other item --->
        <blue:call object="form" method="getItem"
                   toVariable="passwordField"
                   castTo="com/emarson/lemonade/bluelemon/midp1/TextField">

          <blue:argument type="byte" value="1"/>
        </blue:call>
        <blue:call object="passwordField" method="getString"
                   toVariable="password"/>

                   
        <!-- let's make sure both values are not empty --->
        <blue:call manager="UtilsManager" method="length"
                   toVariable="userIdLength">

          <blue:argument type="variable" name="userId"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="length"
                   toVariable="passwordLength">

          <blue:argument type="variable" name="password"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="=="
                   toVariable="userIdIsEmpty">

          <blue:argument type="variable" name="userIdLength"/>
          <blue:argument type="byte" value="0"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="=="
                   toVariable="passwordIsEmpty">

          <blue:argument type="variable" name="passwordLength"/>
          <blue:argument type="byte" value="0"/>
        </blue:call>
        <blue:call manager="UtilsManager" method="OR"
                   toVariable="anyStringIsEmpty">

          <blue:argument type="variable" name="userIdIsEmpty"/>
          <blue:argument type="variable" name="passwordIsEmpty"/>
        </blue:call>
        
        <!-- if any of them is empty, show the warning alert --->
        <blue:if isTrue="anyStringIsEmpty">
        
          <!-- create the instance of the alert --->
             <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/Alert"
                            toVariable="alert"/>


          <!-- 
            set its title and text, mark this is a warning alert
           --->

          <blue:call object="alert" method="initialize">
            <blue:argument type="String" value="Incorrect data"/>
            <blue:argument type="String" value="The user id and password cannot be empty."/>
            <blue:argument type="byte" value="AlertType.WARNING"/>
            <blue:argument type="short" value="1500"/>
          </blue:call>

          <!-- show it to the screen and return --->
          <blue:call manager="GeneralManager" method="setCurrent">
            <blue:argument type="variable" name="alert"/>
          </blue:call>
          
          <blue:return/>
          
        </blue:if>
        
        <!-- initialize the networking screen --->
        <blue:call node="com/emarson/lemonade/examples/mobank/InitializeDownloading"
                   location="@com/emarson/lemonade/examples/mobank">

          <blue:argument type="String" value="Logging in"/>
          <blue:argument type="String" value="You are being authenticated..."/>
          <blue:argument type="String" value="Cancel"/>
          <blue:argument type="byteArr" contextKey="actionForCancel"/>          
        </blue:call>
        
        <!-- process the networking action --->
        <blue:try>
        
            <!-- invoke the downloading screen --->
            <blue:call node="com/emarson/lemonade/examples/mobank/Authenticate"
                       location="network">

              <blue:argument type="variable" name="userId"/>
              <blue:argument type="variable" name="password"/>
            </blue:call>
          
        </blue:try>
        
        <!-- catch the IOException --->
        <blue:catch exception="com/emarson/midp/IOException">
        
          <!-- go to authentication screen --->
          <blue:call node="com/emarson/lemonade/examples/mobank/AuthenticationScreen"
                   location="@com/emarson/lemonade/examples/mobank"/>

        
          <!-- go to the main screen --->
          <blue:call node="com/emarson/lemonade/examples/mobank/authentication/Screen"
                         location="@com/emarson/lemonade/examples/mobank"/>


          <!-- create the instance of the alert --->
          <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/Alert"
                            toVariable="alert"/>


          <!-- 
            set its title and text, mark this is a warning alert
           --->

          <blue:call object="alert" method="initialize">
            <blue:argument type="String" value="Connection problem"/>
            <blue:argument type="String" value="Can't connect to the Internet."/>
            <blue:argument type="byte" value="AlertType.ERROR"/>
            <blue:argument type="short" value="Alert.FOREVER"/>
          </blue:call>

          <!-- show it to the screen and return --->
          <blue:call manager="GeneralManager" method="setCurrent">
            <blue:argument type="variable" name="alert"/>
          </blue:call>

        </blue:catch>
        
        <!-- finalize the networking screen --->
        <blue:call node="com/emarson/lemonade/examples/mobank/FinalizeDownloading"
                   location="@com/emarson/lemonade/examples/mobank"/>


      </definition>
    </action>
    
    <finish name="done">
      <return>result</return>
    </finish>

  </path>

</process>