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

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

  ac.anchorToDo(ENC, "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(ENC,0,ac)

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

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

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

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

######################################################################
## This action adds a listener to an ExtNamingContext.
######################################################################

import CorbaWeb

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

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

    # Adds this listener object to the observed ExtNamingContext.
    ENC.add_client(self)

    # Stores the observer ExtNamingContext.
    self._ENC = ENC

    # Obtains the contains and resolve all bindings.
    contents = {}
    for eb in ENC.list_all() {
      bn = eb.binding_name[0]
      contents[bn.id + ' ' + bn.kind] = ENC.resolve(eb.binding_name)
    }
    self._contents = contents
  }

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

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

    self._ENC.remove_client(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::ExtNamingContext")
    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, ctx, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!ctx._is_equivalent(self._ENC)) return;

    # Updates the contents.
    n = name.id+' '+name.kind
    self._contents[n] = self._ENC.resolve([name])

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

  proc context_removed (self, ctx, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!ctx._is_equivalent(self._ENC)) 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, ctx, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!ctx._is_equivalent(self._ENC)) return;

    # Updates the contents.
    n = name.id+' '+name.kind
    self._contents[n] = self._ENC.resolve([name])

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

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

    # Updates the contents.
    n = name.id+' '+name.kind
    self._contents[n] = self._ENC.resolve([name])

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

  proc object_removed (self, ctx, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!ctx._is_equivalent(self._ENC)) 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, ctx, name)
  {
    # Returns if it is not the observed NamingContext?
    if(!ctx._is_equivalent(self._ENC)) return;

    # Updates the contents.
    n = name.id+' '+name.kind
    self._contents[n] = self._ENC.resolve([name])

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

proc frame_listener(ENC, ac)
{
  # Creates a listener for this ExtNamingContext.
  listener = ListenerImpl(ENC, 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::ExtNamingContext")
declareAction("view_frames", view_frames,
                      "Frames for CosNaming::ExtNamingContext")
declareAction("frame_forms", frame_forms, "Forms Frame")
declareAction("frame_listener", frame_listener, "Listener Frame")

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