SchemaEnum.jws Sample

This topic inludes the source code for the SchemaEnum.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.priceSummary.PriceSummaryDocument;
006 import org.openuri.easypo.priceSummary.PriceSummaryDocument.PriceSummary;
007 import org.openuri.easypo.priceSummary.PriceType;
008 import org.openuri.easypo.priceSummary.ItemType;
009 import org.openuri.easypo.LineItem;
010 
011 /**
012  * This web service illustrates how you can access XML values that are
013  * defined in schema as enumerations. When a schema containing 
014  * enumerations is compiled, the generated Java types represent the 
015  * enumerations with enumerations of their own. You can access these through 
016  * their constants and corresponding int values.
017  
018  * The schemas used by this service are defined in PriceSummary.xsd and 
019  * EasyPO.xsd.
020  
021  * @common:target-namespace namespace="http://workshop.bea.com/SchemaEnum"
022  */
023 public class SchemaEnum implements com.bea.jws.WebService
024 
025 
026     /**
027      * This method uses values in the incoming XML to construct
028      * a new XML document of a different schema. PriceSummary.xsd, the schema 
029      * for the new document, defines XML enumerations for a price 
030      * threshold attribute. Items whose price is between $10 and $20 receive 
031      * a threshold value of 10.00; items above 20 get a threshold value of 
032      * 20.00.<br/><br/>
033      
034      * This method loops through the purchase order items, creating a summary
035      * document that specifies their threshold value.<br/><br/>
036      
037      * Test this method by pasting the contents of PurchaseOrder.xml into
038      * the following box. Be sure to paste the test XML in place of the 
039      * XML between the &lt;summarizeItems&gt; element tags. Verify
040      * the test by comparing the resulting XML with the XML in PriceSummary.xml.<br/><br/>
041      
042      * You can use this test's return value in Test View to test 
043      * the sortByThreshold method.
044      
045      * @common:operation
046      */
047     public PriceSummaryDocument summarizeItems(PurchaseOrderDocument poDoc)
048     {
049         PurchaseOrder po = poDoc.getPurchaseOrder();
050 
051         /* 
052          * Create a new instance of the PriceSummary schema. This is the document
053          * the code creates, extracting values from the purchase order. 
054          */        
055         PriceSummaryDocument summaryDoc = PriceSummaryDocument.Factory.newInstance();
056         PriceSummary summary = summaryDoc.addNewPriceSummary();
057 
058         /* 
059          * Create price elements to hold item elements according to their
060          * price threshold.
061          */
062         PriceType priceZero = summary.addNewPrice();
063         PriceType priceTen = summary.addNewPrice();
064         PriceType priceTwenty = summary.addNewPrice();
065 
066         /*
067          * Set the threshold attribute value for the two new elements.
068          */
069         priceZero.setThreshold(PriceType.Threshold.BELOW_10_DOLLARS);
070         priceTen.setThreshold(PriceType.Threshold.BETWEEN_10_AND_20_DOLLARS);
071         priceTwenty.setThreshold(PriceType.Threshold.ABOVE_20_DOLLARS);        
072 
073         /*
074          * Loop through the purchase order line-item elements. If their
075          * price child element is between 10.00 and 20.00, add the line-item
076          * to the price element whose threshold is 10.00. For those over 20.00,
077          * add them to the price element whose threshold is 20.00.
078          */
079         for (int i = 0; i < po.getLineItemArray().length; i++)
080         {
081             LineItem item = po.getLineItemArray(i);
082 
083             if (item.getPrice() 10.00)
084             {
085                 
086                 ItemType newItem = priceZero.addNewItem();                
087                 newItem.setTitle(item.getDescription());
088                 newItem.xsetQuantity(item.xgetQuantity());
089                 newItem.setAmount(item.getPrice());
090 
091             else if (item.getPrice() >= 10.00 && item.getPrice() 20.00)
092             {
093                 
094                 ItemType newItem = priceTen.addNewItem();                
095                 newItem.setTitle(item.getDescription());
096                 newItem.xsetQuantity(item.xgetQuantity());
097                 newItem.setAmount(item.getPrice());
098 
099             else if (item.getPrice() >= 20.00)
100             {
101             
102                 ItemType newItem = priceTwenty.addNewItem();
103                 newItem.setTitle(item.getDescription());
104                 newItem.xsetQuantity(item.xgetQuantity());
105                 newItem.setAmount(item.getPrice());                
106             }
107         }
108 
109         /* Return the newly created summary document. */
110         return summaryDoc;
111     }
112 
113     /**
114      * The sortByThreshold method loops through a price summary to  
115      * create a string that lists the items grouped by threshold.
116      * Unlike the summarizeItems method, with creates a new XML
117      * document that contains an attribute whose value is enumerated,
118      * this method retrieves values from an enumeration.<br/><br/>
119      
120      * This method illustrates how you can use the int value corresponding
121      * to enumerations to specify them in Java switch statements.<br/><br/>
122      
123      * To test this method, copy the contents of PriceSummary.xml into the 
124      * box in Test View. Be sure to paste the test XML in place of the XML
125      * between the &lt;sortByThreshold&gt; element tags.
126      
127      * @common:operation
128      */
129     public String sortByThreshold(PriceSummaryDocument summaryDoc)
130     {
131         System.out.println(summaryDoc.xmlText());
132         /*
133          * Extract the summary element from the incoming XML, then use it
134          * to extract an array of the price elements.
135          */
136         PriceSummary summary = summaryDoc.getPriceSummary();
137         StringBuffer responseBuffer = new StringBuffer();
138         PriceType[] priceElements = summary.getPriceArray();
139 
140         /*
141          * Create string buffers to hold the sorted results of the values
142          * retrieved.
143          */
144         StringBuffer zeroBuffer = new StringBuffer("\n Items under 10 dollars: \n");
145         StringBuffer tenBuffer = new StringBuffer("\n Items between 10 and 20 dollars: \n");
146         StringBuffer twentyBuffer = new StringBuffer("\n Items more than 20 dollars: \n");
147 
148         /* 
149          * Loop through the price elements, extracting the array of item child 
150          * elements in each.
151          */
152         for (int i = 0; i < priceElements.length; i++)
153         {           
154             ItemType[] itemElements = priceElements[i].getItemArray();
155             
156             /* 
157              * Loop through the item elements, binding a variable to each 
158              * item element in turn.
159              */
160             for (int j = 0; j < itemElements.length; j++)
161             {
162                 ItemType item = itemElements[j];
163 
164                 /* 
165                  * For each item element, find out the int value of its price parent 
166                  * element's threshold attribute value. Append the item's title to 
167                  * the appropriate string buffer.
168                  */                
169                 switch(priceElements[i].getThreshold().intValue())
170                 {
171                     
172                     case PriceType.Threshold.INT_BELOW_10_DOLLARS:
173                         zeroBuffer.append(item.getTitle() "\n");
174                         break;
175 
176                     case PriceType.Threshold.INT_BETWEEN_10_AND_20_DOLLARS:
177                         tenBuffer.append(item.getTitle() "\n");
178                         break;
179     
180                     case PriceType.Threshold.INT_ABOVE_20_DOLLARS:
181                         twentyBuffer.append(item.getTitle() "\n");
182                         break;
183 
184                     default:
185                         System.out.println("Yo! Something unexpected happened!");
186                         break;
187                 }
188             }
189         }
190 
191         /* 
192          * Combine the two result buffers into one, then return a string from the 
193          * combined result. 
194          */
195         responseBuffer.append(tenBuffer);
196         responseBuffer.append(twentyBuffer);
197         return responseBuffer.toString();
198     }
199 }