{ try { //get id and quantity from request object String id = orderStockRequest.findElement("ID").getText(); int quantity = Integer.parseInt(orderStockRequest.findElement("Quantity").getText()); //set the total quantity that is on order for the item specified //this quantity will be increased if there are already orders for this item int onOrderTotal = quantity; //get orders variable and put all the elements from it called 'item' into a list //updating this list will update the orders variable IXml orders = webAppAccess.getVariables().getXml("orders"); List ordersList = orders.getChildren("Item"); //flag that denotes whether a stock item already exists boolean stockAlreadyExists = false; //check if an order for the specified stock item already exists in the list for (Iterator it = ordersList.iterator(); it.hasNext();) { //get order from orders list IXml order = (IXml)it.next(); //if there is already an order for the id specified... if (order.findElement("ID").getText().equals(id)) { //the order already exists, increment quantity in orders variable IXml quantityItem = order.findElement("Quantity"); onOrderTotal = quantity+Integer.parseInt(quantityItem.getText()); quantityItem.setText(Integer.toString(onOrderTotal)); //flag that the item already exists stockAlreadyExists = true; //exit for loop break; } } //if the stock item doesn't already exist in the orders variable if (! stockAlreadyExists) { //order doesn't exist, create new order IXml newElement = orders.addChildElement("Item"); newElement.addChildWithText("ID", id); newElement.addChildWithText("Quantity", Integer.toString(quantity)); } //place order into orders variable webAppAccess.getVariables().setXml("orders", orders); //assemble response object String resultString = "An order was successfully made for " + quantity + " items of " + id + ".
" + onOrderTotal + " items of " + id + " are currently on order."; IXml responseXml = XmlUtil.create("OrderStockResponse"); responseXml.addChildWithText("Result", resultString); responseXml.addChildWithText("Fault", ""); //return response object return responseXml; } catch (NumberFormatException e) { //an exception was raised, return fault information IXml errorXml = XmlUtil.create("OrderStockResponse"); errorXml.addChildWithText("Result", "No result [md] exception raised."); errorXml.addChildWithText("Fault", "An invalid quantity was specified."); return errorXml; } catch (Exception e) { //an exception was raised, return fault information IXml errorXml = XmlUtil.create("OrderStockResponse"); errorXml.addChildWithText("Result", "No result [md] exception raised."); errorXml.addChildWithText("Fault", e.getMessage()); return errorXml; } }