NewsQueueEJB.java - Web Service
ListenToNewsService.java - Web Service Client

/*
 * @author : Elangovan
 * @version 1.0
 *
 * Development Environment : Oracle9i JDeveloper 

 *
 * Name of the File        : PostToNewsService.java
 *
 * Creation / Modification History
 *
 *    Elangovan          16-May-2003        Created
 *

 */
package oracle.otnsamples.jmswebservice.client;

import NewsServiceProxy;

// XML imports
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import oracle.xml.parser.v2.XMLDocument;

/**
 *  This class is a Java Swing client to 'NewsService' Web Service. It accepts
 *  user inputs and generates an XML Element from the data and posts the
 *  News messages to the Newsservice. The proxy stub generated from the
 *  NewsService is used to post News to the Service.
 *
 *  The 'send()' operation is invoked on the Web Service to post News

 *  messages.
 *
 */
public class PostToNewsService  {

  // User interface to accept News data
  PostNewsFrame gui = null;

  /**
   * Constructor. Initializes UI.

   */
  public PostToNewsService() {
    gui = new PostNewsFrame(this);
  }

  /**
   *  Main method. This method is called when the application is run.
   */
  public static void main(String[] args) {
    new PostToNewsService();
  }


  /**
   *   Dispatches the appropriate methods to handle the events generated
   *   in the GUI. Those methods carry out the required operations.
   *
   *   @param event The string representation of the event generated by user
   */
  public void dispatchEvent(String event)   {
    if(event.equals("POSTNEWS"))
      this.postNews();
    else if(event.equals("EXIT"))
      this.exitApplication();

  }

  /**
   *  Generates an XML representation of the News data and posts the News to the
   *  'NewsService' Web Service using the proxy stub.
   */
  private void postNews()   {

    try {
      // Create an instance of the proxy stub
      NewsServiceProxy proxy = new NewsServiceProxy();

      // Get the XML representation and invoke the 'send()' operation

      proxy.send(this.getNewsElement());

      gui.putStatus( " News was posted successfully " );
      gui.resetForm();

    } catch (Exception ex)     {
      gui.putStatus(" Error sending News. Look at the console for a detailed error message ");
      System.out.println(" Error sending News :"+ex.toString());
    }

  }

  /**
   *  Generates an XML representation of the News data entered by the user.

   *
   *  A sample News element would be like
   *
   *   <News>
   *     <Title> news title <Title>
   *     <NewsType>S</NewsType>
   *     <Snippet> news snippet </Snippet>
   *     <DetailedNews> the detailed news </DetailedNews>
   *   </News>
   *
   *
   *  @return an XML representation of News.
   */
  private Element getNewsElement() throws java.io.IOException {


    Document  doc            = new XMLDocument();

    // Initialize XML elements
    Element elemNews         = doc.createElement( "News");
    Element elemTitle        = doc.createElement( "Title");
    Element elemNewsType     = doc.createElement("NewsType");
    Element elemSnippet      = doc.createElement( "Snippet");
    Element elemDetailedNews = doc.createElement( "DetailedNews");

    // Set the node value for each element
    elemTitle.appendChild(doc.createTextNode(gui.txtTitle.getText()));
    elemNewsType.appendChild(doc.createTextNode((String)gui.cmbNewsType.getSelectedItem()));
    elemSnippet.appendChild(doc.createTextNode(gui.txtSnippet.getText()));
    elemDetailedNews.appendChild(doc.createTextNode(gui.txtDetailedNews.getText()));

    // Append the elements to the root node

    elemNews.appendChild(elemTitle);
    elemNews.appendChild(elemNewsType);
    elemNews.appendChild(elemSnippet);
    elemNews.appendChild(elemDetailedNews);

    doc.appendChild(elemNews);

    return doc.getDocumentElement();

  }

  /**
   *  Closes the application.
   */
  private void exitApplication()   {
    System.exit(0);
  }


}

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy