SimpleAccess.jws Sample

This topic inludes the source code for the SimpleAccess.jws Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/xmlBeans/schema/

Sample Source Code


001 package xmlBeans.schema; 
002 
003 import org.openuri.easypo.PurchaseOrderDocument;
004 import org.openuri.easypo.PurchaseOrderDocument.PurchaseOrder;
005 import org.openuri.easypo.LineItem;
006 import java.math.BigDecimal;
007 
008 /**
009  * This web service illustrates basic use of XMLBeans compiled from
010  * XML schema. With XMLBeans, you can compile XML schemas to generate
011  * types through which you can access XML instances that conform to 
012  * the schema. With the generated types, you can use get and set methods
013  * in the way that you would with other JavaBeans.
014  
015  * In WebLogic Workshop, you can compile schema files simply by copying
016  * them to a Schemas project in the application. WebLogic Workshop compiles
017  * the schemas and places the resulting JAR file among the application's Libraries. 
018  * From there, types generated from the schema are available to other code in 
019  * the application.
020  
021  * The schema that supports the XML used by this web service is in EasyPO.xsd.
022  * The PurchaseOrderDocument type that is a parameter for each of this
023  * web service's methods is a type generated from the schema. It represents
024  * the XML document at the top level of the incoming purchase order XML. WebLogic
025  * Workshop automatically loads the incoming XML (here, the test XML) into 
026  * the type specified in the parameter.
027  
028  * @common:target-namespace namespace="http://workshop.bea.com/SimpleAccess"
029  
030  */
031 public class SimpleAccess implements com.bea.jws.WebService
032 
033 
034     /**
035      * This method uses simple XMLBean get methods to retrieve values 
036      * from a purchase order XML document. All of the retrieved values
037      * are put into a StringBuffer object, which is converted to a String
038      * for display in Test View.<br/><br/>
039      
040      * To test this method, copy the contents of the PurchaseOrderSimple.xml
041      * file in this project and paste them into the box below. Be sure to
042      * paste the test XML in place of the XML between the &lt;getSingleItemDetails&gt;
043      * element tags.
044      
045      * @common:operation
046      */
047     public String getSingleItemDetails(PurchaseOrderDocument poDoc)
048     {
049         // A buffer to hold the response as it's accumulated.
050         StringBuffer responseBuffer = new StringBuffer();
051 
052         /* 
053          * Use the top-level document to get the top-level element, 
054          * purchase-order.
055          */
056         PurchaseOrder po = poDoc.getPurchaseOrder();
057 
058         /* 
059          * A purchase-order element can have multiple line-item child 
060          * elements. The PurchaseOrder type that results from
061          * compiling the schema reflects this by providing get and set
062          * methods for handling its element children as an array.
063          */
064         LineItem[] lineItems = po.getLineItemArray();
065 
066         // Get the first line-item element in order to retrieve its values.
067         LineItem lineItem = lineItems[0];
068 
069         // Append the value of each line-item element to the string buffer.
070         responseBuffer.append("\n");
071         responseBuffer.append("Description: " + lineItem.getDescription() "\n");
072         responseBuffer.append("Quantity: " + lineItem.getQuantity() "\n");
073         responseBuffer.append("Price: " + lineItem.getPrice() "\n");
074 
075         // Return the buffer for display in Test View.
076         return responseBuffer.toString();
077     }
078 
079     /**
080      * This method illustrates how you can use XMLBeans to set values 
081      * in an XML instance document. As with the getSingleItemDetails
082      * method, you use JavaBeans accessors provided by the types 
083      * generated from schema.<br/><br/>
084      
085      * To test this method, copy the contents of PurchaseOrderSimple.xml
086      * into the box below. Be sure to paste the test XML in place of the 
087      * XML that begins and ends with &lt;eas:purchase-order&gt; tags. In
088      * other words, replace all of the elements whose namespace prefix is
089      * eas. After pasting in the test XML, enter new values for the 
090      * newDescription, newPerUnitOunce, newQuantity, and newPrice 
091      * elements at the bottom. Test View has provided these as placeholders.<br/><br/>
092      
093      * This method returns the same object that it accepts, but with the 
094      * new values you specify. Test View displays the returned object
095      * as its XML value.
096      
097      * @common:operation
098      */
099     public PurchaseOrderDocument setSingleItemDetails(PurchaseOrderDocument poDoc, 
100         String newDescription, double newPerUnitOunces,
101         int newQuantity, double newPrice)
102     {
103         /* 
104          * Get the purchase-order element contained in the incoming
105          * XML document.
106          */
107         PurchaseOrder po = poDoc.getPurchaseOrder();
108 
109         // Get the line-item element whose values will be changed.
110         LineItem lineItem = po.getLineItemArray(0);
111 
112         /*
113          * Set new values for each the line-item element's children.
114          */        
115         lineItem.setDescription(newDescription);
116         lineItem.setPerUnitOunces(new BigDecimal(newPerUnitOunces));
117         lineItem.setQuantity(newQuantity);
118         lineItem.setPrice(newPrice);
119         
120         // Return the XML with its new values.
121         return poDoc;
122     }
123 
124     /**
125      * This method is very similar to the getSingleItemDetails method,
126      * but differs in that it retrieves the values of ALL line-item elements
127      * in the incoming XML. It also counts the total quantity for each
128      * item order, then calculates a total price.<br/><br/>
129      
130      * To test this method, copy the contents of the PurchaseOrderSimple.xml
131      * file in this project and paste them into the box below. Be sure to 
132      * paste the test XML in place of the XML between the &lt;getAllItemDetails&gt;
133      * element tags.
134      
135      * @common:operation
136      */
137     public String getAllItemDetails(PurchaseOrderDocument poDoc)
138     {
139         StringBuffer responseBuffer = new StringBuffer();
140 
141         LineItem[] lineItems = poDoc.getPurchaseOrder().getLineItemArray();        
142         responseBuffer.append("\n Purchase order has " 
143             lineItems.length + " line items. \n");
144 
145         // Variables to hold count of item orders and total amount of purchase.
146         int numberOfItems = 0;
147         double totalAmount = 0.0;
148 
149         /* 
150          * Loop through the list of line-item elements, adding their
151          * details to the string buffer.
152          */
153         for (int j = 0; j < lineItems.length; j++) {
154             responseBuffer.append(" Line item: " + j + "\n");
155             responseBuffer.append("   Description: " + lineItems[j].getDescription() "\n");
156             responseBuffer.append("   Quantity: " + lineItems[j].getQuantity() "\n");
157             responseBuffer.append("   Price: " + lineItems[j].getPrice() "\n");
158 
159             // Increment item quantity and total purchase amount for each line-item.
160             numberOfItems += lineItems[j].getQuantity();
161             totalAmount += lineItems[j].getPrice() * lineItems[j].getQuantity();
162         }
163         responseBuffer.append("Total items: " + numberOfItems + "\n");
164         responseBuffer.append("Total amount: " + totalAmount + "\n");
165 
166         return responseBuffer.toString();
167     }
168 }