ArrayList<CartItem> items = new ArrayList<CartItem>();
CartItem item = null;
Cart auxCart = null;
HashMap<String, String> parameters = new HashMap<String, String>();
SOAPMessage message, response = null;
DOMCartHandler dch = null;
Document doc = null;
if(quantity <= 0)
{
return true;
}
item = new CartItem();
item.setOfferId(offerId);
item.setQuantity(quantity);
items.add(item);
if(cart != null)
{
parameters.put("CartId", cart.getCartId());
parameters.put("HMAC", cart.getCartHmac());
message = SOAPEnvelopeCreator.createAmazonCartSOAPEnvelope("CartAdd", parameters, items, properties);
try
{
response = connection.call(message, properties.getProperty("endpoint"));
dch = new DOMCartHandler();
doc = response.getSOAPBody().extractContentAsDocument();
auxCart = dch.getCart(doc);
cart.setItems(auxCart.getItems());
cart.setTotal(auxCart.getTotal());
cart.setUrl(auxCart.getUrl());
if(dch.isItemAdded(doc))
{
return true;
}
else
{
return false;
}
}
catch(SOAPException se){return false;}
}
else
{
parameters.put("AssociateTag", properties.getProperty("AssociateTag"));
message = SOAPEnvelopeCreator.createAmazonCartSOAPEnvelope("CartCreate", parameters, items, properties);
try
{
response = connection.call(message, properties.getProperty("endpoint"));
dch = new DOMCartHandler();
doc = response.getSOAPBody().extractContentAsDocument();
if(dch.isItemAdded(doc))
{
cart = new Cart(dch.getCartId(doc), dch.getCartHmac(doc));
auxCart = dch.getCart(doc);
cart.setItems(auxCart.getItems());
cart.setTotal(auxCart.getTotal());
cart.setUrl(auxCart.getUrl());
return true;
}