Building the Scenario Using Microsoft.NET


This scenario simulates using Microsoft.NET by building the Warehouse as a Web service and the Retailer as a Web application that consumes this service. In the full SCM example, the Retailer is also a Web service, but as it isn't being consumed in this simulation, it is just built as a Web application that exposes a Graphical User Interface (GUI) to make testing easier.

WSDL for the Warehouse Service

The WSI published a WSDL document [WSI2] that outlines the interface that they would expect to see for the Warehouse service. A snippet of this document can be seen in Listing 16-1.

Listing 16-1. Warehouse Service WSDL

<wsdl:message name="ShipGoodsRequest">   <wsdl:part name="ItemList" type="wh:ItemList"/>   <wsdl:part name="Customer"     type="wh:CustomerReferenceType"/>   <wsdl:part name="ConfigurationHeader"     element="ct:Configuration"/>  </wsdl:message>  <wsdl:message name="ShipGoodsResponse">    <wsdl:documentation>      A response of true indicates the goods have been      shipped. A response of false indicates the warehouse      either does not carry that part or does not have enough      stock to fill the requested quantity.    </wsdl:documentation>    <wsdl:part name="Response"      type="wh:ItemShippingStatusList"/>  </wsdl:message> 

This snippet defines the contract for submitting an order asynchronously and how the ShipGoods request and response should be handled.

SOAP for the Warehouse Service

A snippet of an example SOAP document, also published by the WSI [WSI2] that conforms to this contract for a request, is shown in Listing 16-2:

Listing 16-2. Warehouse Service SOAP

<s:Body>  <ns1:ShipGoods xmlns:ns1="<path to>/Warehouse.wsdl"                 xmlns:ns2="<path to>/Warehouse.xsd">    <ItemList>      <ns2:Item>        <ns2:ProductNumber>605006</ns2:ProductNumber>        <ns2:Quantity>23</ns2:Quantity>      </ns2:Item>      <ns2:Item>        <ns2:ProductNumber>605007</ns2:ProductNumber>        <ns2:Quantity>22</ns2:Quantity>      </ns2:Item>     </ItemList>    <Customer>D22845-W8N349Y-tky</Customer>  </ns1:ShipGoods> </s:Body> And the SOAP document for the response is below: <Response>   <ns2:ItemStatus>     <ns2:ProductNumber>605006</ns2:ProductNumber>     <ns2:Status>false</ns2:Status>   </ns2:ItemStatus>   <ns2:ItemStatus>     <ns2:ProductNumber>605007</ns2:ProductNumber>     <ns2:Status>true</ns2:Status>   </ns2:ItemStatus> </Response> 

The important thing to note in building a service that implements this is the data types that are being passed to and from the service. The request message contains a number of items of type Item that is a structure containing a product number and a quantity, and the response message contains a number of items of type ItemStatus, which contain a product number and a status. The Request message is passed to the service containing the list of requested items based on their product number and the desired quantity. Taking a look at the preceding snippets, the request is for 23 items of type 605006, and the response to that is false, as sufficient quantity isn't available.

Defining the Data Classes in C#

The first step in implementing this service is to define classes in C# to handle these data structures.

Listing 16-3 presents the class representing the Item used for the request:

Listing 16-3. C# Item Class

public class Item {   private string _productNumber;   private int _quantity;   public string ProductNumber   {     get     {       return this._productNumber;     }     set     {       this._productNumber = value;     }   }   public int Quantity   {     get     {       return this._quantity;     }     set     {       this._quantity = value;     }   } } 

The ItemStatus class is very similar to this, except that it has a property for Status instead of quantity. The Web service sets this property to true if there is sufficient quantity of the desired product available. The full source code is available in the download.

The C# Web Service

The Warehouse Web service has to expose a method called ShipGoods, which, upon receiving a request containing an array of type Item, checks through the list to see if there is sufficient quantity on hand of the desired item. It creates an array of ItemStatus objects that it uses to return the status of the ordered items. Should there be sufficient quantity of the desired item, it flags the shipping status for that product as TRue; otherwise, it flags it as false. The current quantities are stored in an SQL Server database. Listing 16-4 shows this WebMethod:

Listing 16-4. C# Web Service

[WebMethod] public ItemStatus[] ShipGoods(Item[] ItemList) {   // Intialize Data Connection   string connectionString = "Data Source=(local);"   connectionString+="uid=sa;pwd=welcome;   connectionString+="database=Warehouse";   SqlConnection con =      new SqlConnection(connectionString);   String sql = "";   // Create return structure   ItemStatus[] collRtn =      new ItemStatus[ItemList.Length];   // Open the Database connection   con.Open();   // Loop through the incoming list   for(int lp=0;lp<ItemList.Length;lp++)   {     // Create a new ItemStatus for this item     collRtn[lp] = new ItemStatus();     // And give it the product number for the item     collRtn[lp].ProductNumber =          ItemList[lp].ProductNumber;     // Look in the DB for this product     sql = "Select QuantityOnHand from WarehouseStock"     sql+="where ProductID='";     sql+=ItemList[lp].ProductNumber;     sql+="'";     SqlCommand com = new SqlCommand(sql,con);     SqlDataReader r = com.ExecuteReader();     r.Read();     // If the query returned something, check if there     //is sufficient quantity and set the status     if (r.HasRows)     {       int nQuantityonHand = (int) r.GetValue(0);       if(nQuantityonHand>ItemList[lp].Quantity)         collRtn[lp].Status=true;       else         collRtn[lp].Status=false;     }       else       {         // If the DB has nothing for this product, then         // set the status as false         collRtn[lp].Status=false;       }     }     // Close the connection and free it up     con.Close();     // Return the Status array     return collRtn;   }  } } 

Consuming the Web Service Using C#

Finally, following is some example C# code that consumes this Web service. This code requires a Web reference, called Warehouse, to be added to the project in Visual Studio.NET.

To call this service, asynchronously, using callbacks, the AsyncCallback class is used like this

AsyncCallback cb = new AsyncCallback(ServiceCallback); theService.BeginShipGoods(itemsList, cb, theService); 


This creates a callback, cb, which maps to a function called ServiceCallback. This should be implemented as a public void that takes an IAsyncResult as a parameter as shown below in Listing 16-5.

Listing 16-5. C# Web Service Callback

public void ServiceCallback(IAsyncResult ar) {   Warehouse.WarehouseService theService = new                 Warehouse.WarehouseService();   Warehouse.ItemStatus[] itemsOut = theService.EndShipGoods(ar); } 

When the Web service completes processing, it calls back to this function, where the EndShipGoods function is called to complete the transaction.




Java EE and. Net Interoperability(c) Integration Strategies, Patterns, and Best Practices
Java EE and .NET Interoperability: Integration Strategies, Patterns, and Best Practices
ISBN: 0131472232
EAN: 2147483647
Year: N/A
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net