######################################################################
##
## 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 CosNaming::
## NamingContext 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.

import NSTools

######################################################################
# Recursive displaying
######################################################################

proc recursive_display (NC, depth, ac)
{
  hbl = Holder()
  hbi = Holder()

  NC.list (10000,hbl,hbi)

  ac.println("<UL>")
  for item in hbl.value {
    id = item.binding_name[0].id
    kind = item.binding_name[0].kind

    object = NC.resolve(item.binding_name)

    ac.print("<LI>")
    ac.anchorToAction(object, "view", id + " " + kind, true)

    ac.println("<BR>")

    if (depth != 0)
      if (item.binding_type == CosNaming.BindingType.ncontext)
        recursive_display(object,depth-1, ac)
  }
  ac.println("</UL>")
}

######################################################################
# Generates forms.
######################################################################

proc generate_forms(NC, ac)
{
  ac.h1("Basic Actions on this CosNaming::NamingContext")

  ac.action_form(NC,"bind_object")
    ac.submit_button("Bind an object")
    ac.print("path ")
    ac.text_input("a_path", 20)
    ac.print("object ")
    ac.text_input("an_object", 20)
  ac.end_form()

  ac.action_form(NC,"rebind_object")
    ac.submit_button("Rebind an object")
    ac.print("path ")
    ac.text_input("a_path", 20)
    ac.print("object ")
    ac.text_input("an_object", 20)
  ac.end_form()

  ac.action_form(NC,"bind_context")
    ac.submit_button("Bind a context")
    ac.print("path ")
    ac.text_input("a_path", 20)
    ac.println("context ")
    ac.text_input("a_context",20)
  ac.end_form()

  ac.action_form(NC,"rebind_context")
    ac.submit_button("Rebind a context")
    ac.print("path ")
    ac.text_input("a_path", 20)
    ac.println("context ")
    ac.text_input("a_context",20)
  ac.end_form()

  ac.action_form(NC,"unbind")
    ac.submit_button("Unbind")
    ac.print("path ")
    ac.text_input("a_path", 20)
  ac.end_form()

  ac.action_form(NC,"bind_new_context")
    ac.submit_button("Create a sub context")
    ac.print("path ")
    ac.text_input("a_path", 20)
  ac.end_form()

  ac.h1("Advanced Actions on this CosNaming::NamingContext")

  ac.action_form(NC,"destroy")
    ac.submit_button("Delete a sub context")
    ac.print("path ")
    ac.text_input("a_path", 20)
  ac.end_form()

  ac.action_form(NC,"view_tree")
    ac.submit_button("View the hierarchy")
    ac.println("depth ")
    ac.text_input("a_depth", 4, 2)
  ac.end_form()
}

######################################################################
## This action generates forms to execute CosNaming::NamingContext
## operations.
######################################################################

proc view(NC, ac)
{
  # generates <h1>...</h1>
  ac.h1("Contents of this CosNaming::NamingContext")

  ac.anchorToNameService()
  ac.p()

  # displays the content of the NamingContext
  recursive_display(NC,0,ac)

  # generates forms.
  ac.hr()
  generate_forms(NC, ac)
}

######################################################################
## This action executes the bind_object operation.
######################################################################

proc bind_object(NC, ac)
{
  # gets the form variables "a_path" and "an_object".
  a_path = ac.getString("a_path")
  an_object = ac.getObject("an_object")

  # executes the remote bind operation.
  try {
    NC.bind(NSTools.StringToName(a_path), an_object)
  } catch(e) {
    ac.println("Exception: bind_object(" + a_path + ", " +
               an_object._toString() + ")")
    ac.println(e)
  } 

  # generates forms.
  view(NC, ac)
}

######################################################################
## This action executes the rebind_object operation.
######################################################################

proc rebind_object(NC, ac)
{
  # gets the form variables "a_path" and "an_object".
  a_path = ac.getString("a_path")
  an_object = ac.getObject("an_object")

  # executes the remote rebind operation.
  try {
    NC.rebind(NSTools.StringToName(a_path), an_object)
  } catch(e) {
    ac.println("Exception: rebind_object(" + a_path + ", " +
               an_object._toString() + ")")
    ac.println(e)
  } 

  # generates forms.
  view(NC, ac)
}

######################################################################
## This action executes the bind_context operation.
######################################################################

proc bind_context(NC, ac)
{
  # gets the form variables "a_path" and "a_context".
  a_path = ac.getString("a_path")
  a_context = ac.getObject("a_context")

  # executes the remote bind_context operation.
  try {
    NC.bind_context(NSTools.StringToName(a_path), a_context)
  } catch(e) {
    ac.println("Exception: bind_context(" + a_path + ", " +
               a_context._toString() + ")")
    ac.println(e)
  } 

  # generates forms.
  view(NC, ac)
}

######################################################################
## This action executes the rebind_context operation.
######################################################################

proc rebind_context(NC, ac)
{
  # gets the form variables "a_path" and "a_context".
  a_path = ac.getString("a_path")
  a_context = ac.getObject("a_context")

  # executes the remote rebind_context operation.
  try {
    NC.rebind_context(NSTools.StringToName(a_path), a_context)
  } catch(e) {
    ac.println("Exception: rebind_context(" + a_path + ", " +
               a_context._toString() + ")")
    ac.println(e)
  } 

  # generates forms.
  view(NC, ac)
}

######################################################################
## This action executes the bind_new_context operation.
######################################################################

proc bind_new_context(NC, ac)
{
  # gets the form variable "a_path".
  a_path = ac.getString("a_path")

  # executes the remote bind_new_context operation.
  try {
    NC.bind_new_context(NSTools.StringToName(a_path))
  } catch(e) {
    ac.println("Exception: bind_new_context(" + a_path + ")")
    ac.println(e)
  } 

  # generates forms.
  view(NC, ac)
}

######################################################################
## This action executes the unbind operation.
######################################################################

proc unbind(NC, ac)
{
  # gets the form variable "a_path".
  a_path = ac.getString("a_path")

  # executes the remote unbind operation.
  try {
    NC.unbind(NSTools.StringToName(a_path))
  } catch(e) {
    ac.println("Exception: unbind(" + a_path + ")")
    ac.println(e)
  } 

  # generates forms.
  view(NC, ac)
}

######################################################################
## This action executes the destroy operation.
######################################################################

proc destroy(NC, ac)
{
  # gets the form variable "a_path".
  a_path = ac.getString("a_path")

  # executes the remote destroy operation.
  try {
    o = NC.resolve(NSTools.StringToName(a_path))
    o.destroy()
    NC.unbind(NSTools.StringToName(a_path))
  } catch(e) {
    ac.println("Exception: destroy(" + a_path + ")")
    ac.println(e)
  } 

  # generates forms.
  view(NC, ac)
}

######################################################################
## This action executes the list operation.
######################################################################

proc view_tree(NC, ac)
{
  a_depth = ac.getLong("a_depth")

  recursive_display(NC, a_depth, ac)
  
  # generates forms.
  ac.hr()
  generate_forms(NC, ac)
}

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

declareAction("view", view, "Views the CosNaming::NamingContext")
declareAction("bind_object", bind_object, "")
declareAction("rebind_object", rebind_object, "")
declareAction("bind_context", bind_context, "")
declareAction("rebind_context", rebind_context, "")
declareAction("bind_new_context", bind_new_context, "")
declareAction("unbind", unbind, "")
declareAction("destroy", destroy, "")
declareAction("view_tree", view_tree, "Views the CosNaming::NamingContext")

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