######################################################################
##
## 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 CORBA::Container
## 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.

######################################################################
## Displays a list for the contents.
######################################################################

proc generate_contents_list(container, ac)
{
  ac.h2("Contained Objects")

  # obtains the contents.
if (sys.ORB == "VisiBroker3") {
  contains = container.contents(CORBA.DefinitionKind.dk_all, true)
} else {
  contains = container.describe_contents(
                 CORBA.DefinitionKind.dk_all, true, 1000)
}

  # iterates on contents.
  ac.println("<ul>")
  for c in contains {
    ac.print("<li> ")
    # generates an anchor to each contained object.
if (sys.ORB == "VisiBroker3") {
    ac.anchorToAction(c, "view", c.name, true)
} else {
    ac.anchorToAction(c.contained_object, "view", c.value.value.name, true)
}
    ac.p()
  }
  ac.println("</ul>")
}

######################################################################
## Generates the lookup form.
######################################################################

proc generate_lookup_form(container, ac)
{
  ac.action_form(container, "do_lookup")
    ac.submit_button("Lookup by an OMG IDL scoped name")
    ac.text_input("a_name", 50)
  ac.end_form()
}

######################################################################
## This action displays the contain of a CORBA::Container.
######################################################################

proc view(container, ac)
{
  ac.h1("Browsing a CORBA::Container")

  # generates a list of the contained objects.
  generate_contents_list(container, ac)

  # generates a form to apply the lookup method.
  generate_lookup_form(container, ac)
}

######################################################################
## This action lookups a contained object by its scoped name
## and views it.
######################################################################

proc do_lookup(container, ac)
{
  # obtains the name of the lookup contained object.
  a_name = ac.getString("a_name")

  # calls the lookup method.
  contained = container.lookup(a_name)

  if(CORBA.is_nil(contained)) {
    ac.println("The contained object named by `<b>" + a_name + 
               "</b>' doesn't exist!")
    ac.apply("view", container)
  } else {
    # applies the view action on this contained object.
    ac.apply("view", contained)
  }
}

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

declareAction("view", view,
              "Views the contain of a CORBA::Container object")

declareAction("do_lookup", do_lookup,
              "Lookups a CORBA::Contained object by its scoped name")

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