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

  <descriptor>
    <name>com/emarson/lemonade/examples/mobank/moaccount/HistoryScreen</name>
    <version>1.0</version>
    <description>
      History screen.
      A user is shown an implicit list with all the dates any transfer 
      has been done. Selecting any element from this list results in
      showing a text message with the transfer data.
      
      A dataset of the following structure:
       + date of the transfer: String,
       + ammount of money transferred: int,
       + target account: String.
      is kept in the permanent memory in the node 
      'com/emarson/lemonade/examples/mobank/moaccount/HistoryData'.
      Every time a new transfer is being done a new row is being
      added to the top of this dataset.
    </description>
    <vendor>E-MARSON</vendor>
  </descriptor>

  <path>
  
    <!-- ASSOCIATIONS --->
    
    <!-- association to MainScreen --->
    <action name="action for 'Back'">
      <type>general/bluelemon/code</type>
      <definition resultTo="goToMainScreen">
        <blue:call node="com/emarson/lemonade/examples/mobank/moaccount/MainScreen"
                         location="@com/emarson/lemonade/examples/mobank/moaccount"/>

      </definition>
    </action>
    
    <!-- association to HistoryDetailsScreen --->
    <subprocess name="action for 'Select'">
      <source>com/emarson/lemonade/examples/mobank/moaccount/HistoryDetailsScreen</source>
      <resultTo>showHistoryForDate</resultTo>
    </subprocess>
    
    
    <!-- INSTRUCTION --->

    <action name="list with all the dates a transfer has been done">
      <type>general/bluelemon/code</type>
      <definition resultTo="screen">

        <!-- read the history dataset from the permanent location --->
        <blue:get node="com/emarson/lemonade/examples/mobank/moaccount/HistoryData" 
                location="permanent" toVariable="historyDataset"/>

                
        <!-- 
          prepare the descriptor which describes this dataset; we will 
          use it more than once, so it's good to keep it as a variable
        --->

        <blue:call manager="UtilsManager" method="this"
                   toVariable="historyDatasetDescriptor">

          <blue:argument type="dataset">
            <blue:descriptor>
              <blue:field type="byte"/>
              <blue:field type="byte"/>
              <blue:field type="byte"/>
            </blue:descriptor>
            <blue:row>
              <!-- date --->
              <blue:field value="STRING_TYPE"/>
              <!-- ammount of monety transferred --->
              <blue:field value="INT_TYPE"/>
              <!-- target account number --->
              <blue:field value="STRING_TYPE"/>
            </blue:row>
          </blue:argument>
        </blue:call> 

        <!-- 
          create a single column dataset by getting the first column 
          of the history dataset
        --->

        <blue:call manager="UtilsManager" method="getColumn"
                   toVariable="datasetWithDates">

          <blue:argument type="variable" name="historyDataset"/>
          <blue:argument type="variable" name="historyDatasetDescriptor"/>
          <blue:argument type="byte" value="0"/>
        </blue:call>

        <!-- count the number of rows in the history dataset --->
        <blue:call manager="UtilsManager" method="getRowsNumber"
                   toVariable="rowsNumber">

          <blue:argument type="variable" name="historyDataset"/>
          <blue:argument type="variable" name="historyDatasetDescriptor"/>
        </blue:call>     
        <!-- check if it is not zero --->
        <blue:call manager="UtilsManager" method="=="
                   toVariable="zeroRows">

          <blue:argument type="variable" name="rowsNumber"/>
          <blue:argument type="byte" value="0"/>
        </blue:call>
        
        <!-- 
          if it is, it doesn't make sense to show an empty list; 
          inform a user the list is empty
        --->

        <blue:if isTrue="zeroRows">

            <!-- prepare the information screen --->
            <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/Form"
                          toVariable="screen"/>


            <blue:call object="screen" method="initialize">
              <blue:argument type="String" value="History"/>
            </blue:call>

            <blue:call object="screen" method="appendString">
              <blue:argument type="String" value="No history so far."/>
            </blue:call>

        </blue:if>
        
        <!-- if we have some rows in the history dataset, use it --->
        <blue:else>
    
            <!-- create the instance of the list... --->
            <blue:newInstance module="com/emarson/lemonade/bluelemon/midp1/ChoiceFactory"
                              toVariable="choiceFactory"/>

            <!-- ... of type IMPLICIT --->
            <blue:call object="choiceFactory" method="createList"
                       toVariable="screen">

              <blue:argument type="byte" value="List.IMPLICIT"/>
            </blue:call>

            <!--  
              initialize it with the title, dataset with labels to
              show in the list and action to invoke when user selects
              any element from the list
            --->

            <blue:call object="screen" method="initialize">
              <blue:argument type="String" value="History"/>
              <blue:argument type="variable" name="datasetWithDates"/>
              <blue:argument type="byteArr" contextKey="showHistoryForDate"/>
            </blue:call>
            
        </blue:else>
        
        <!-- prepare the back command --->
        <blue:call manager="UtilsManager" method="getActionCommand"
                   toVariable="command">

          <blue:argument type="String" value="Back"/>
          <blue:argument type="byte" value="ActionCommand.BACK"/>
          <blue:argument type="byte" value="0"/>
          <blue:argument type="byteArr" contextKey="goToMainScreen"/>
        </blue:call>

        <!-- add it to the screen --->
        <blue:call object="screen" method="addCommand">
          <blue:argument type="variable" name="command"/>
        </blue:call>

        <!-- and show the screen on the device --->
        <blue:call manager="GeneralManager" method="setCurrent">
          <blue:argument type="variable" name="screen"/>
        </blue:call>

      </definition>
    </action>

    <finish name="done">
      <return>screen</return>
    </finish>

  </path>

</process>