placeWidgetOrder
uses two complex types containing the substitution group. This
operation demonstrates to use such a structure in a Java implementation. Both the consumer and the service must get and set
members of a substitution group.
To invoke placeWidgetOrder()
the consumer must construct a widget order containing
one element of the widget substitution group. When adding the widget to the order, the consumer should use the object factory
methods generated for each element of the substitution group. This ensures that the runtime and the service can correctly process
the order. For example, if an order is being placed for a plastic widget,
the ObjectFactory.createPlasticWidget()
method is used to create the element before adding it
to the order.
Example 15.16 shows consumer code for setting the
widget property of the WidgetOrderInfo
object.
Example 15.16. Setting a Substitution Group Member
ObjectFactory of = new ObjectFactory(); WidgetOrderInfo order = new of.createWidgetOrderInfo(); ... System.out.println(); System.out.println("What color widgets do you want to order?"); String color = reader.readLine(); System.out.println(); System.out.println("What shape widgets do you want to order?"); String shape = reader.readLine(); System.out.println(); System.out.println("What type of widgets do you want to order?"); System.out.println("1 - Normal"); System.out.println("2 - Wood"); System.out.println("3 - Plastic"); System.out.println("Selection [1-3]"); String selection = reader.readLine(); String trimmed = selection.trim(); char widgetType = trimmed.charAt(0); switch (widgetType) { case '1': { WidgetType widget = of.createWidgetType(); widget.setColor(color); widget.setShape(shape); JAXB<WidgetType> widgetElement = of.createWidget(widget); order.setWidget(widgetElement); break; } case '2': { WoodWidgetType woodWidget = of.createWoodWidgetType(); woodWidget.setColor(color); woodWidget.setShape(shape); System.out.println(); System.out.println("What type of wood are your widgets?"); String wood = reader.readLine(); woodWidget.setWoodType(wood); JAXB<WoodWidgetType> widgetElement = of.createWoodWidget(woodWidget); order.setWoodWidget(widgetElement); break; } case '3': { PlasticWidgetType plasticWidget = of.createPlasticWidgetType(); plasticWidget.setColor(color); plasticWidget.setShape(shape); System.out.println(); System.out.println("What type of mold to use for your widgets?"); String mold = reader.readLine(); plasticWidget.setMoldProcess(mold); JAXB<WidgetType> widgetElement = of.createPlasticWidget(plasticWidget); order.setPlasticWidget(widgetElement); break; } default : System.out.println("Invaid Widget Selection!!"); }
The placeWidgetOrder()
method receives an order in the form of a
WidgetOrderInfo
object, processes the order, and returns a bill to the consumer in the form of a
WidgetOrderBillInfo
object. The orders can be for a plain widget, a plastic widget, or a wooden
widget. The type of widget ordered is determined by what type of object is stored in
widgetOrderForm
object’s widget property. The
widget property is a substitution group and can contain a widget
element,
a woodWidget
element, or a plasticWidget
element.
The implementation must determine which of the possible elements is stored in the order. This can be accomplished using
the JAXBElement<? extends T>
object's getName()
method
to determine the element's QName. The QName can then be used to determine which element in the substitution group is in the
order. Once the element included in the bill is known, you can extract its value into the proper type of object.
Example 15.17 shows a possible implementation.
Example 15.17. Implementation of placeWidgetOrder()
public com.widgetvendor.types.widgettypes.WidgetOrderBillInfo placeWidgetOrder(WidgetOrderInfo widgetOrderForm) { ObjectFactory of = new ObjectFactory();WidgetOrderBillInfo bill = new WidgetOrderBillInfo()
// Copy the shipping address and the number of widgets // ordered from widgetOrderForm to bill ... int numOrdered = widgetOrderForm.getAmount();
String elementName = widgetOrderForm.getWidget().getName().getLocalPart();
if (elementName.equals("woodWidget")
{ WoodWidgetType widget=order.getWidget().getValue();
buildWoodWidget(widget, numOrdered); // Add the widget info to bill JAXBElement<WoodWidgetType> widgetElement = of.createWoodWidget(widget);
bill.setWidget(widgetElement);
float amtDue = numOrdered * 0.75; bill.setAmountDue(amtDue);
} else if (elementName.equals("plasticWidget") { PlasticWidgetType widget=order.getWidget().getValue(); buildPlasticWidget(widget, numOrdered); // Add the widget info to bill JAXBElement<PlasticWidgetType> widgetElement = of.createPlasticWidget(widget); bill.setWidget(widgetElement); float amtDue = numOrdered * 0.90; bill.setAmountDue(amtDue); } else { WidgetType widget=order.getWidget().getValue(); buildWidget(widget, numOrdered); // Add the widget info to bill JAXBElement<WidgetType> widgetElement = of.createWidget(widget); bill.setWidget(widgetElement); float amtDue = numOrdered * 0.30; bill.setAmountDue(amtDue); } return(bill); }
The code in Example 15.17 does the following:
Instantiates an object factory to create elements. | |
Instantiates a | |
Gets the number of widgets ordered. | |
Gets the local name of the element stored in the order. | |
Checks to see if the element is a | |
Extracts the value of the element from the order to the proper type of object. | |
Creates a | |
Sets the bill object's widget property. | |
Sets the bill object's amountDue property. |