      ************************************************************************
      * Copyright (C) 1999 Micro Focus International Ltd.
      * All rights reserved.
      *  
      * This demonstration program is provided for use by users of 
      * Micro Focus products (the "Software") and may be used, 
      * modified and distributed as part of your application provided 
      * that you properly acknowledge the copyright of Micro Focus in this
      * material.
      *     
      * All conditions, warranties and undertakings, express or implied, 
      * statutory or otherwise, on the part of Micro Focus are excluded, 
      * including without limitation those of merchantability or fitness  
      * for purpose. This shall not, however, purport to exclude or 
      * restrict the liability of Micro Focus to any extent not 
      * permitted by law.
      *
      * IN NO EVENT WILL MICRO FOCUS BE LIABLE FOR ANY INDIRECT, 
      * INCIDENTAL, SPECIAL, CONSEQUENTIAL OR OTHER DAMAGES ARISING OUT 
      * OF THE USE OF THE DEMONSTRATION PROGRAM DISTRIBUTED WITH THE 
      * SOFTWARE, WHETHER OR NOT INFORMED OF THE POSSIBILITIES OF 
      * DAMAGES IN ADVANCE.  THESE LIMITATIONS APPLY TO ALL CAUSES OF 
      * ACTION, INCLUDING BREACH OF CONTRACT, BREACH OF WARRANTY, STRICT
      * LIABILITY, AND ANY ACTIONS IN TORTS.
      ************************************************************************
      * If creating pizza.[dll or so] for ISAPI please uncomment 
      * the two following $set lines. 
      * Note you will have to modify the 78 level item CGI-BIN to point to 
      * the directory where the html files will be located.
      *
      * This is because these web servers don't see the current working
      * directory as the same one the shared object(UNIX) or dll(PC) is
      * loaded from.
      *
      *> $set webserver(isapi) 
      *> $set case reentrant(1) constant as-thread(1)
      *
      * pizza.cbl
      *
      * This program uses three ways to transfer information between
      * html pages.
      *
      * 1) cookies
      *    The ACCEPT external-form-name now reads all the cookie
      *    name=value pairs and merges them with the html form set.
      * 2) Hidden field
      *    Each page has a hidden field
      *    <input type=hidden name=app-state=xxxx> so we know which
      *    page the customer has just used.
      * 3) Server side persistence calls
      *    Calls provided to easily and quickly save client or CGI
      *    state information on the server. Such information can be
      *    stored for recovery weeks later when the same user visits
      *    again.
      *
       identification division.
       program-id. pizza.
       working-storage section.
       78 max-items value 16.
      $IF as-thread DEFINED
       78 cgi-bin   value "<location of pizza.so or pizza.dll>".
      $END
       01 expiry-days pic xxxx comp-x value 10.
       01 server-name pic x(255) value "clpizza.dat".
       thread-local-storage section.

      * ACCEPT external-form-name now reads the Cookie information too.
       01 client-cookie is external-form.              *> get-cookie
          03 client-id          pic x(10) identified by "client-id".

      * input form generic for all pages.
       01 input-form is external-form.
          03 app-state          pic x(10) identified by "app-state".
          03 submit-type        pic x(30) identified by "submit".
          03 client-details.
             05 client-name     pic x(20) identified by "client-name".
             05 client-addr     pic x(120) identified by "client-addr".
             05 client-email    pic x(20) identified by "client-email".
          03 customer-list      pic x(320).
          03 customer-selection pic x(20) redefines customer-list
                                          identified by "item"
                                          count in item-count
                                          occurs max-items.

      * Used to set the Cookie value. The file new.html contains
      * <meta http-equiv="Set-Cookie" content=
      * "client-id=%%!s client-id%%; expires=%%!s client-expiry%%">
       01 new-customer is external-form identified by customer-htm.
          03 client-id        pic x(10) identified by "client-id".
          03 client-expiry    pic x(50) identified by "client-expiry".
          03 client-details.
             05 client-name   pic x(20) identified by "client-name".
             05 client-addr   pic x(120) identified by "client-addr".
             05 client-email  pic x(20) identified by "client-email".
        
      * The main selection screen. shop.htm
       01 do-customer is external-form identified by customer-htm.
          03 client-name     pic x(20) identified by "client-name".
          03 item1           pic x(7)  identified by "item01".
          03 item2           pic x(7)  identified by "item02".
          03 item3           pic x(7)  identified by "item03".
          03 item4           pic x(7)  identified by "item04".
          03 item5           pic x(7)  identified by "item05".
          03 item6           pic x(7)  identified by "item06".
          03 item7           pic x(7)  identified by "item07".
          03 item8           pic x(7)  identified by "item08".
          03 item9           pic x(7)  identified by "item09".
          03 item10          pic x(7)  identified by "item10".
          03 item11          pic x(7)  identified by "item11".
          03 item12          pic x(7)  identified by "item12".
          03 item13          pic x(7)  identified by "item13".
          03 item14          pic x(7)  identified by "item14".
          03 item15          pic x(7)  identified by "item15".
          03 item16          pic x(7)  identified by "item16".

      * Generic output form for all other pages.
       01 show-details-customer is external-form identified by
                                                         customer-htm.
          03 client-details.
             05 client-name      pic x(20) identified by "client-name".
             05 client-addr      pic x(120) identified by "client-addr".
             05 client-email     pic x(20) identified by "client-email".
          03 message-text        pic x(30) identified by "message".
          03 customer-list       pic x(320).
          03 customer-selection  pic x(20) redefines customer-list
                                           identified by "item"
                                        *> count in item-count
                                           occurs max-items.

       01 client-id-x   pic x(30).
       01 server-status pic x comp-x.
       01 item-count    pic x comp-x.
       01 customer-htm  pic x(255).
       01 html-file     pic x(255).
       01 temp-01       pic x comp-x.
       01 data-len      pic xxxx comp-x.
      
      * This information will be stored on the server via CALL statements
       01 client-state.
          03 client-details.
             05 client-name     pic x(20).
             05 client-addr     pic x(120).
             05 client-email    pic x(20).
          03 message-text       pic x(30).
          03 customer-list      pic x(320).
          03 customer-selection pic x(20) redefines customer-list
                                          occurs max-items.
       procedure division.
        *> CALL "CBL_DEBUGBREAK"          *> Debug ISAPI applications
      * Needs to be initialized in multi threaded application
           move space to client-cookie
           move space to show-details-customer
           move space to do-customer
           move space to input-form

           accept client-cookie                   *> get cookie

      * Set the file name used to store the client or the CGI
      * application information.
           call "MF_CLIENT_STATE_FILE" using server-name
                                             server-status

           evaluate client-id of client-cookie
           when space                             *> new customer

      * Allocate a record and recieve a key.
               call "MF_CLIENT_STATE_ALLOCATE" using client-id-x
                                               length of client-state
                                               server-status
      * Might want to check the server-status.

               move client-id-x to client-id of new-customer

      * Recieve a expiry date to use when setting the cookie.
               call "MF_CLIENT_STATE_EXPIRY" using expiry-days
                                                   client-expiry

               move "new.htm" to html-file
               perform add-cgi-bin
               display new-customer               *> set cookie

           when other                             *> customer

               move client-id of client-cookie to client-id-x
               perform restore-client
               accept input-form

                      *> update input-form into client-state
                      *> then client-state to show-details-customer

               evaluate app-state                 *> Hidden input field
               when "entry"                       *> previous customer
                   move corr client-state to show-details-customer
                   move "previous.htm" to html-file
                   perform do-show-details-customer

               when "new"                         *> new customer
                   move corr input-form to client-state
                   move "did not select anything" 
                     to message-text of client-state
                   move client-name of client-state 
                     to client-name of do-customer
                   move "shop.htm" to html-file
                   perform add-cgi-bin
                   display do-customer

               when "previous"                    *> update details.
               when "change"
                   move corr client-details of input-form 
                          to client-details of client-state
                   perform do-customer-checked

               when "shop"                        *> after selection
                   move customer-list of input-form 
                     to customer-list of client-state
                   move corr client-state to show-details-customer
                   evaluate submit-type
                   when "Buy selection"
                       move "confirm.htm" to html-file
                       perform do-show-details-customer
                   when "Save selection"
                       move "saved" to message-text of client-state
                       move "save.htm" to html-file
                       perform do-show-details-customer
                   when "Change your details"
                       move "change.htm" to html-file
                       perform do-show-details-customer
                   end-evaluate

               when "confirm"                     *> after buy
                   evaluate submit-type
                   when "Change your details"
                       move corr client-details of input-form
                              to client-details of client-state
                       move corr client-state to show-details-customer
                       move "confirm.htm" to html-file
                       perform do-show-details-customer
                   when "Back to selection page"
                       perform do-customer-checked
                   when "Confirm purchase"
                       move "bought" to message-text of client-state
                       move corr client-state to show-details-customer
                       move "buy.htm" to html-file
                       perform do-show-details-customer
                   end-evaluate
               end-evaluate
               *> client-state will have been updated
               perform save-client
           end-evaluate

           exit program
           stop run
           .

       do-show-details-customer section.
           move 0 to temp-01
           perform max-items times
               add 1 to temp-01
               evaluate customer-selection of client-state(temp-01)
               when "item01"
                   move "7 inch base" to
                   customer-selection of show-details-customer(temp-01)
               when "item02"
                   move "10 inch base" to
                   customer-selection of show-details-customer(temp-01)
               when "item03"
                   move "14 inch base" to 
                   customer-selection of show-details-customer(temp-01)
               when "item04"
                   move "pepperoni" to
                   customer-selection of show-details-customer(temp-01)
               when "item05"
                   move "onion" to
                   customer-selection of show-details-customer(temp-01)
               when "item06"
                   move "tomato" to
                   customer-selection of show-details-customer(temp-01)
               when "item07"
                   move "green pepper" to
                   customer-selection of show-details-customer(temp-01)
               when "item08"
                   move "red pepper" to
                   customer-selection of show-details-customer(temp-01)
               when "item09"
                   move "ham" to
                   customer-selection of show-details-customer(temp-01)
               when "item10"
                   move "beef" to
                   customer-selection of show-details-customer(temp-01)
               when "item11"
                   move "pineapple" to
                   customer-selection of show-details-customer(temp-01)
               when "item12"
                   move "chilli" to
                   customer-selection of show-details-customer(temp-01)
               when "item13"
                   move "cheese" to
                   customer-selection of show-details-customer(temp-01)
               when "item14"
                   move "anchovy" to
                   customer-selection of show-details-customer(temp-01)
               when "item15"
                   move "olive" to
                   customer-selection of show-details-customer(temp-01)
               when "item16"
                   move "mushroom" to
                   customer-selection of show-details-customer(temp-01)
               end-evaluate
           end-perform
           perform add-cgi-bin
           display show-details-customer
           .

       do-customer-checked section.
           move client-name of client-state 
             to client-name of do-customer
           move 0 to temp-01
           perform max-items times
               add 1 to temp-01
               evaluate customer-selection of client-state
                                               (temp-01)
               when "item01"
                   move "checked" to item1
               when "item02"
                   move "checked" to item2
               when "item03"
                   move "checked" to item3
               when "item04"
                   move "checked" to item4
               when "item05"
                   move "checked" to item5
               when "item06"
                   move "checked" to item6
               when "item07"
                   move "checked" to item7
               when "item08"
                   move "checked" to item8
               when "item09"
                   move "checked" to item9
               when "item10"
                   move "checked" to item10
               when "item11"
                   move "checked" to item11
               when "item12"
                   move "checked" to item12
               when "item13"
                   move "checked" to item13
               when "item14"
                   move "checked" to item14
               when "item15"
                   move "checked" to item15
               when "item16"
                   move "checked" to item16
               end-evaluate
           end-perform
           move "shop.htm" to html-file
           perform add-cgi-bin
           display do-customer
           .

       add-cgi-bin section.
      $IF as-thread DEFINED
           move cgi-bin to customer-htm
           move html-file to customer-htm(length of cgi-bin + 1:)
      $ELSE
           move html-file to customer-htm
      $END
           .

      * Store client and CGI application state in server file
       save-client section.
           call "MF_CLIENT_STATE_SAVE" using client-id-x
                                             client-state
                                             length of client-state
                                             server-status
           .

      * Recover client and CGI application state from server file
       restore-client section.
           move length of client-state to data-len
           call "MF_CLIENT_STATE_RESTORE" using client-id-x
                                                client-state
                                                data-len
                                                server-status
           .


      * This program will remove all client information older than the
      * number of days specified.
       identification division.
       program-id. pizzatidy.
       working-storage section.
       01 server-status pic x comp-x.
       01 expire-after-days pic xxxx comp-x.
       01 server-name pic x(255) value "clpizza.dat".
       procedure division.
           call "MF_CLIENT_STATE_FILE" using server-name server-status
           move 10 to expire-after-days
           call "MF_CLIENT_STATE_PURGE" using expire-after-days
                                              server-status
           stop run
       end program pizzatidy.
       end program pizza.

