######################################################################
##
## 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::
## OBNamingContext interface (ORBacus specific).
##
######################################################################

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

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

  ac.anchorToDo(OBNC, "view_frames",
                     "Creates a new window to observe updates.",
                     false, "_blank")
  ac.p()

  ac.anchorToNameService()
  ac.p()

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

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

proc view_frames(OBNC, ac)
{
  ac._reply.setContentType("text/html")

  ac.println("<html>")
  ac.println("<frameset rows=\"50%,50%\">")
  ac.println("<frame src=\"" + ac.getDoURL(OBNC, "frame_listener") + "\">")
  ac.println("<frame src=\"" + ac.getActionURL(OBNC, "frame_forms") + "\">")
  ac.println("</frameset>")
  ac.println("</html>")
}

proc frame_forms(OBNC, ac)
{
  generate_forms(OBNC, ac)
}

######################################################################
## This action adds a listener to a CosNaming::OBNamingContext.
######################################################################

import CorbaWeb

# Inheritance to be a CORBA callback object which pushes Web documents.
class ListenerImpl(CorbaWeb.PushCallbackObject)
{
  # The constructor.

  proc __ListenerImpl__(self, OBNC, ac)
  {
    # Initializes the PushCallbackObject part.
    self.__PushCallbackObject__(ac, CosNaming.BindingListener)

    # Adds this listener object to the observed OBNamingContext.
    OBNC.add_listener(self)

    # Stores the observer CosNaming::OBNamingContext.
    self._OBNC = OBNC

    # Obtains the contains.
    contents = {}
    for eb in OBNC.list_extended() {
      bn = eb.binding_name[0]
if (sys.ORB_RELEASE < 400) {
      if (eb.ncOrObj._d == CosNaming.BindingType.nobject)
        o = eb.ncOrObj.obj
      else
        o = eb.ncOrObj.nc
} else {
      if (eb.bound_obj._d == CosNaming.BindingType.nobject)
        o = eb.bound_obj.obj
      else
        o = eb.bound_obj.nc
}
      contents[bn.id + ' ' + bn.kind] = o
    }
    self._contents = contents
  }

  # When the navigator quits this page, then removes the listener.

  proc stop_callback(self)
  {
    println("ListenerImpl::stop_callback")

    self._OBNC.remove_listener(self)
  }

  # Pushes the contents to the navigator.

  proc push_contents(self, message_screen, name="")
  {
    println("ListenerImpl::push_contents ", message_screen)

    ac = self._ac

    # Begins the pushed document.
    self.begin_push("text/html")

    ac.h1("Contents of this CosNaming::OBNamingContext")
    ac.println("<ul>")
    for n in self._contents.keys {
      ac.print("<li>")
      o = self._contents[n]
      if (n == name) {
        ac.println("<blink>")
        ac.anchorToAction(o, "view", n, true, "_blank")
        ac.println(" LAST UPDATE </blink>")
      } else
        ac.anchorToAction(o, "view", n, true, "_blank")
      ac.println("<br>")
    }
    ac.println("</ul>")

    # End of the pushed document.
    self.end_push()
  }

  proc context_added (self, parent_ctx, child_ctx, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!parent_ctx._is_equivalent(self._OBNC)) return;

    # Updates the contents.
    n = name.id+' '+name.kind
    self._contents[n] = child_ctx

    # Pushes it.
    self.push_contents("context_added", n)
  }

  proc context_removed (self, parent_ctx, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!parent_ctx._is_equivalent(self._OBNC)) return;

    # Updates the contents.
    n = name.id+' '+name.kind
    self._contents.remove(n)

    # Pushes it.
    self.push_contents("context_removed", n)
  }

  proc context_changed (self, parent_ctx, child_ctx, old_name, new_name)
  {
    # Returns if it is not the observed NamingContext?
    if(!parent_ctx._is_equivalent(self._OBNC)) return;

    # Updates the contents.
    self._contents.remove(old_name.id+' '+old_name.kind)
    n = new_name.id+' '+new_name.kind
    self._contents[n] = child_ctx

    # Pushes it.
    self.push_contents("context_changed", n)
  }

  proc object_added (self, parent_ctx, child_obj, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!parent_ctx._is_equivalent(self._OBNC)) return;

    # Updates the contents.
    n = name.id+' '+name.kind
    self._contents[n] = child_obj

    # Pushes it.
    self.push_contents("object_added", n)
  }

  proc object_removed (self, parent_ctx, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!parent_ctx._is_equivalent(self._OBNC)) return;

    # Updates the contents.
    n = name.id+' '+name.kind
    self._contents.remove(n)

    # Pushes it.
    self.push_contents("object_removed", n)
  }

  proc object_changed (self, parent_ctx, child, old_name, new_name)
  {
    # Returns if it is not the observed NamingContext?
    if(!parent_ctx._is_equivalent(self._OBNC)) return;

    # Updates the contents.
    self._contents.remove(old_name.id+' '+old_name.kind)
    n = new_name.id+' '+new_name.kind
    self._contents[n] = child

    # Pushes it.
    self.push_contents("object_changed", n)
  }
}

proc frame_listener(OBNC, ac)
{
  # Creates a listener for this OBNamingContext.
  listener = ListenerImpl(OBNC, ac)

  # Pushes the MIME header.
  listener.send_push_header()

  # Pushes the contents.
  listener.push_contents("first time")
}

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

declareAction("view", view, "Views the CosNaming::OBNamingContext")
declareAction("view_frames", view_frames,
                      "Frames for CosNaming::OBNamingContext")
declareAction("frame_forms", frame_forms, "Forms Frame")
declareAction("frame_listener", frame_listener, "Listener Frame")

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