Package client.ws.milanas.cart

Source Code of client.ws.milanas.cart.AmazonCartWrapper

/*
* File: AmazonCartWrapper.java
* Created on 21 / desembre / 2007, 12:48
*
* Copyright (C) 2008 M.Àngels Cerveró Abelló
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package client.ws.milanas.cart;

import client.ws.milanas.cart.CartWrapper;
import client.ws.milanas.exceptions.SOAPEnvelopeCreationException;
import client.ws.milanas.helpers.SOAPEnvelopeCreator;
import client.ws.milanas.helpers.beans.Cart;
import client.ws.milanas.helpers.beans.CartItem;
import client.ws.milanas.helpers.dom.DOMCartHandler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import org.w3c.dom.Document;

/**
*
* @author milana
*/
public class AmazonCartWrapper implements CartWrapper
{   
    static private SOAPConnection connection;
    static private Properties properties = new Properties();
    private Cart cart;
   
   
    /**
     * Creates a new instance of AmazonCartWrapper
     */
    public AmazonCartWrapper()
    {
        cart = null;
        ClassLoader cl = this.getClass().getClassLoader();
        try
        {
            properties.load(cl.getResourceAsStream("MilanasWSClient.properties"));
        }
        catch(IOException ioe){}
    }
   
    /**
     * Creates a new SOAP connection for the WebService client
     */
    public void createConnection()
    {
        try
        {
            connection = SOAPConnectionFactory.newInstance().createConnection();
        }
        catch(SOAPException se){}
    }
   
    /**
     * Closes the SOAP connection of the client
     */   
    public void closeConnection()
    {
        try
        {
            connection.close();
        }
        catch(SOAPException se){}
    }
   
    /**
     * Puts the Amazon item with the id "offeredId" into the Amazon's cart by
     * calling the web service CartCreate or CartAdd depending on whether Amazon's
     * Cart exists or it has to be created.
     * The parameter quantity means the units of the product you want to insert
     * into the Amazon's Cart.
     * Once received the Amazon's response, this method saves the state of the cart
     * that is sent with the shop message.
     * This method returns true if the operation has succeded amb false otherwise.
     */
    public boolean addItem(String offerId, int quantity) throws SOAPEnvelopeCreationException
    {
        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;
                }
                else
                {
                    return false;
                }
            }
            catch(SOAPException se){return false;}
        }
    }
   
    /**
     * Modifies the items in the ArrayList in the Amazon's cart by
     * calling the web service CartModify.
     * Once received the Amazon's response, this method saves the cart that
     * is sent with the shop message.
     */
    public void modifyItems(ArrayList<CartItem> items) throws SOAPEnvelopeCreationException
    {
        HashMap<String, String> parameters = new HashMap<String, String>();
        SOAPMessage message, response = null;
        DOMCartHandler dch = null;
        Document doc = null;
        Cart auxCart = null;
        if(cart != null)
        {
            parameters.put("CartId", cart.getCartId());
            parameters.put("HMAC", cart.getCartHmac());
            message = SOAPEnvelopeCreator.createAmazonCartSOAPEnvelope("CartModify", 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());
            }
            catch(SOAPException se){}
        }
    }
   
    /**
     * Cleans the Amazon's Cart by calling the web service CartClear.
     * Once received the Amazon's response, this method saves the cart that
     * is sent with the shop message.
     */
    public void resetCart() throws SOAPEnvelopeCreationException
    {
        HashMap<String, String> parameters = new HashMap<String, String>();
        SOAPMessage message, response = null;
        DOMCartHandler dch = null;
        Document doc = null;
        Cart auxCart = null;
        if(cart != null)
        {
            parameters.put("CartId", cart.getCartId());
            parameters.put("HMAC", cart.getCartHmac());
            parameters.put("AssociateTag", properties.getProperty("AssociateTag"));
            message = SOAPEnvelopeCreator.createAmazonCartSOAPEnvelope("CartClear", parameters,  new ArrayList<CartItem>(), 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());
            }
            catch(SOAPException se){}
        }
    }
   
    /**
     * Returns the state of the Amazon's Cart saved by this wrapper.
     */
    public Cart getCart()
    {
        if(cart != null)
        { 
            return cart;
        }
        return new Cart("", "");
    }
}
TOP

Related Classes of client.ws.milanas.cart.AmazonCartWrapper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.