September 16, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Java Database Control in BEA Weblogic

  • December 12, 2002
  • By Sams Publishing
  • Send Email »
  • More Articles »

If you want to require customers to log in before they check their order status, you need your Web service to maintain a conversation. The login method initiates a conversation, and the logout method terminates the conversation. During the life of the conversation, you keep track of the customer's ID so you can use it for any status queries.

Listing 6.5 shows the main order status Web service. In addition to the login and logout methods, the Orders Web service includes methods to retrieve all available orders (using the customer ID determined during the login method), and also to get the status for a particular order. These data retrieval methods simply make use of the OrderStatusCtrl database control from Listing 6.1, and also the OrderStatus data object from Listing 6.4.

Listing 6.5 Source Code for Orders.jws

import weblogic.jws.control.JwsContext;

public class Orders
{ 
  public int customerId;
  
  /**
   * @jws:control
   */
  private OrderStatusCtrl orderStatus;
  /** @jws:context */ 
  JwsContext context; 

  /**
   * @jws:operation
   * @jws:conversation phase="start"
   */
  public String login(String userName, String password)
  {
    Customer cust = orderStatus.getCustomerByUserName(
      userName);
    
    if (cust != null)
    {
      if (!cust.password.equals(password))
      {
        customerId = -1;
        context.finishConversation();
        return "Invalid password";
      }
      else
      {
        customerId = cust.customerId;
        return "Login successful";
      }
    }
    else
    {
      customerId = -1;
      context.finishConversation();
      return "Invalid user-id";
    }
  }
  
  /**
   * @jws:operation
   * @jws:conversation phase="continue"
   * @jws:return-xml xml-map::
   *   <getAllOrdersResponse xmlns="http://openuri.org/">
   *   <order-statuses>
   *   <order xm:multiple="o in return" xmlns:xm="http://bea.com/jws/xmlmap"
   *     id="{o.orderId}">
   *     <status>{o.orderStatus}</status>
   *     <statusCode>{o.orderStatusCode}</statusCode>
   *   </order>
   *   </order-statuses>
   *   </getAllOrdersResponse>
   *   
   * ::
   */
  public OrderStatus[] getAllOrders()
  {
    if (customerId >= 0)
    {
      return orderStatus.getCustomerOrders(customerId);
    }
    else
    {
      return null;
    }
  }
  
  /**
   * @jws:operation
   * @jws:conversation phase="continue"
   * @jws:parameter-xml xml-map::
   *   <getOrderStatus xmlns="http://openuri.org/">
   *   <order-id>{orderId}</order-id>
   *   </getOrderStatus>
   *   
   * ::
   * @jws:return-xml xml-map::
   *   <getOrderStatusResponse xmlns="http://openuri.org/">
   *   <order id="{return.orderId}">
   *     <status>{return.orderStatus}</status>
   *     <statusCode>{return.orderStatusCode}</statusCode>
   *   </order>
   *   </getOrderStatusResponse>
   *   
   * ::
   */
  public OrderStatus getOrderStatus(int orderId)
  {
    if (customerId >= 0)
    {
      return orderStatus.getOrderStatus(customerId, orderId);
    }
    else
    {
      return null;
    }
  }

  /**
   * @jws:operation
   * @jws:conversation phase="finish"
   */
  public void logout()
  {
  }
} 

Although the database control might not solve all your database needs, it should certainly be sufficient for smaller applications. In larger, more complex applications, it becomes more difficult to manage your code. The reason for this is that there is often logic in your Java code that implements business rules on top of the data. For example, you might have a rule that says that a customer ID can never start with '9'. When you have several Web services that manipulate the customer data, you might find that you are enforcing these rules in several places.

To help manage the complexity of business rules, developers often make "business objects" that manage data in the database and also maintain business rules. In this kind of scenario, any Web service that needed to update the customer table would use methods in a special Customer business object that would perform any special processing or validation of the customer data.

Enterprise JavaBeans (EJBs) are a special case of business object. The EJB architecture provides a standard way to represent these business objects and also provides powerful mechanisms for storing these objects in a database and retrieving them. You will learn how to access EJBs in Chapter 10, "Including an EJB Control."

About the Authors

Joseph Weber is a Software Architect, Manager and consultant from Wisconsin. Mr. Weber has been an outspoken champion of Java and related technologies since their public birth in 1995. During his career he has provided senior leadership in software definition, research, development and implementation to numerous Fortune 200 and large government organizations. Currently Mr. Weber is a Senior Software Engineer and Project Manager for UltraVisual Medical Systems where he is helping to develop next generation medical imaging software (PACS). Mr. Weber is also the founder, and sole official member of the Green Sky Society dedicated to irradiating the social misunderstanding that the sky is blue and not green. BEA WebLogic Workshop Kick Start marks Joe's 11th book. He recently outlined and contributed to Sams' Java Web Services Unleashed (0-672-32363-X) and co-wrote Que's Special Edition Using Java 2 (2000 edition: 0-7897-2468-5).

Mark Wutka has been programming since the Carter administration and considers programming a relaxing pastime. He managed to get a computer science degree while designing and developing networking software at Delta Air Lines. Although he has been known to delve into areas of system and application architecture, he isn't happy unless he's writing code-usually in Java. As a consultant for Wutka Consulting, Mr. Wutka enjoys solving interesting technical problems and helping his coworkers explore new technologies. He has taught classes, written articles and books, and given lectures. His first book, Hacking Java, outsold Stephen King at the local technical bookstore. He's also known for having a warped sense of humor.

Most recently, Mr. Wutka contributed to Java Web Services Unleashed, and wrote Special Edition Using Java Server Pages and Servlets (ISBN: 0-7897-2441-3) and Special Edition Using Java 2 Enteprise Edition (ISBN: 0-7897-2503-7) He plays a mean game of Scrabble, a lousy game of chess, and is the bane of every greenskeeper east of Atlanta.

Source of this material

This is Chapter 6: The Database Control from the book BEA WebLogic Workshop Kick Start (ISBN:0-672-32417-2) written by Joseph Weber and Mark Wutka, published by Sams Publishing.

To access the full Table of Contents for the book


Other Chapters from Sams Publishing:

Web Services and Flows (WSFL)
Overview of JXTA
Introduction to EJBs
Processing Speech with Java





Page 3 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel