######################################################################
##
## Copyright (c) LIFL 1999
## The GOODE team of the LIFL
## University of Lille, France
## All rights reserved
##
## The CorbaWeb script associated to the OMG IDL ChatService::Server interface.
##
######################################################################

# Each action must have two arguments: the first one refers to the
# current CORBA object and the second one refers to a CorbaWeb
# ActionContext instance.

######################################################################
## This action generates forms to execute ChatService::Server operations.
######################################################################

proc view(server, ac)
{
  # generates <h1>...</h1>
  ac.h1("List of created chats")

  # gets chat descriptions
  descriptions = server.list()

  # displays the description of existing chats.
  ac.println("<UL>")
  for d in descriptions {
    ac.println("<LI>")
    ac.anchorToAction(d.objref, "view", d.name, false, "_blank")
    ac.println(": " + d.description)
  }
  ac.println("</UL>")
   
  # generates <h1>...</h1>
  ac.h1("Actions on this ChatService::Server object")

  # generates a HTML form referring the do_create action.
  ac.action_form(server,"do_create")
    # generates the submit button.
    ac.submit_button("Creates")
    ac.println(" the ")
    # generates the text field input for the variable "a_name".
    ac.text_input("a_name", 20)
    ac.println(" chat with the following description:")
    # generates the text field input for the variable "a_description".
    ac.text_input("a_description", 40)
  ac.end_form()

  # generates a HTML form referring the do_find action.
  ac.action_form(server,"do_find")
    # generates the submit button.
    ac.submit_button("Finds")
    ac.println(" the chat ")
    # generates the text field input for the variable "a_name".
    ac.text_input("a_name", 20)
  ac.end_form()

  # generates a HTML form referring the do_destroy action.
  ac.action_form(server,"do_destroy")
    # generates the submit button.
    ac.submit_button("Destroys")
    ac.println(" the chat ")
    # generates the text field input for the variable "a_name".
    ac.text_input("a_name", 20)
  ac.end_form()
}

######################################################################
## This action executes the create_chat operation on a ChatService::Server.
######################################################################

proc do_create(server, ac)
{
  # gets the form variable "a_name".
  a_name = ac.getString("a_name")
  # gets the form variable "a_description".
  a_description = ac.getString("a_description")

  # executes the remote create_chat operation.
  try {
    server.create_chat(a_name, a_description)
  } catch(ChatService.AlreadyCreated e) {
    ac.println("The <b>" + a_name + "</b> chat has already created.")
    ac.hr()
  }

  # generates the forms.
  view(server,ac)
}

######################################################################
## This action executes the find_chat operation on a ChatService::Server.
######################################################################

proc do_find(server, ac)
{
  # gets the form variable "a_name".
  a_name = ac.getString("a_name")

  # executes the remote find_chat operation.
  try {
    chat = server.find_chat(a_name)
    ac.println("The requested chat is ")
    ac.anchorToAction(chat, "view", a_name, false, "_blank")
  } catch(ChatService.Unknown e) {
    ac.println("The <b>" + a_name + "</b> chat is unknown.")
  }
  ac.hr()

  # generates the forms.
  view(server,ac)
}

######################################################################
## This action executes the destroy_chat operation on a ChatService::Server.
######################################################################

proc do_destroy(server, ac)
{
  # gets the form variable "a_name".
  a_name = ac.getString("a_name")

  # executes the remote destroy_chat operation.
  try {
    server.destroy_chat(a_name)
    ac.println("The <b>" + a_name + "</b> chat is destroyed.")
  } catch(ChatService.Unknown e) {
    ac.println("The <b>" + a_name + "</b> chat is unknown.")
  }
  ac.hr()

  # generates the forms.
  view(server,ac)
}

######################################################################
## Declaration of these actions into the CorbaWeb ActionRepository.
######################################################################

declareAction("view", view, "Views forms to act on the ChatService::Server object")
declareAction("do_create", do_create, "Creates a chat")
declareAction("do_find", do_find, "Finds a chat")
declareAction("do_destroy", do_destroy, "Destroys a chat")

######################################################################
# end of file 'CorbaWeb/actions/ChatService_Server'
######################################################################
