/*PLEASE DO NOT EDIT THIS CODE*/
/*This code was generated using the UMPLE @UMPLE_VERSION@ modeling language!*/

package ecommerceWS;
import java.util.*;
import java.io.Serializable;
import javax.xml.namespace.QName;
import java.net.URL;
import javax.xml.ws.Service;
import javax.xml.ws.Endpoint;
import javax.jws.WebService;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.*;

// line 63 "../ecommerceWS.ump"
@WebService(endpointInterface = "ecommerceWS.IVendorImpl")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(factoryMethod="umplenewInstance", propOrder={})
public class Vendor extends Agent implements java.io.Serializable, IVendorImpl
{

  //------------------------
  // MEMBER VARIABLES
  //------------------------

  //Vendor Associations
  private transient  List<Warehouse> warehouses;
  private transient  List<Order> orders;
  private transient  List<Customer> customers;

  //------------------------
  // CONSTRUCTOR
  //------------------------

  public Vendor(String aName, UmpleRuntime.UmpleComponent umpleComponent)
  {
    super(aName,umpleComponent);
    if(umpleComponent.getNode().getId()!=UmpleRuntime.getThisNodeId())
    {
      if(this.getClass()== Vendor.class)
        UmpleRuntime.getInstance().newVendor(aName, umpleComponent, this);

      return;
    }

    warehouses = new ArrayList<Warehouse>();
    orders = new ArrayList<Order>();
    customers = new ArrayList<Customer>();
  }
  //------------------------
  // Returning the Hashcode
  //------------------------
  public int getHashCodeImpl()
  {
    return hashCode();
  }

  //------------------------
  // INTERFACE
  //------------------------

  public Warehouse getWarehousImpl(int index)
  {
    Warehouse aWarehous = warehouses.get(index);
    return aWarehous;
  }

  public List<Warehouse> getWarehouses()
  {
    List<Warehouse> newWarehouses = Collections.unmodifiableList(warehouses);
    return newWarehouses;
  }

  public int numberOfWarehousesImpl()
  {
    int number = warehouses.size();
    return number;
  }

  public boolean hasWarehousesImpl()
  {
    boolean has = warehouses.size() > 0;
    return has;
  }

  public int indexOfWarehousImpl(Warehouse aWarehous)
  {
    int index = warehouses.indexOf(aWarehous);
    return index;
  }

  public Order getOrderImpl(int index)
  {
    Order aOrder = orders.get(index);
    return aOrder;
  }

  public List<Order> getOrders()
  {
    List<Order> newOrders = Collections.unmodifiableList(orders);
    return newOrders;
  }

  public int numberOfOrdersImpl()
  {
    int number = orders.size();
    return number;
  }

  public boolean hasOrdersImpl()
  {
    boolean has = orders.size() > 0;
    return has;
  }

  public int indexOfOrderImpl(Order aOrder)
  {
    int index = orders.indexOf(aOrder);
    return index;
  }

  public Customer getCustomerImpl(int index)
  {
    Customer aCustomer = customers.get(index);
    return aCustomer;
  }

  public List<Customer> getCustomers()
  {
    List<Customer> newCustomers = Collections.unmodifiableList(customers);
    return newCustomers;
  }

  public int numberOfCustomersImpl()
  {
    int number = customers.size();
    return number;
  }

  public boolean hasCustomersImpl()
  {
    boolean has = customers.size() > 0;
    return has;
  }

  public int indexOfCustomerImpl(Customer aCustomer)
  {
    int index = customers.indexOf(aCustomer);
    return index;
  }

  public static int minimumNumberOfWarehouses()
  {
    return 0;
  }

  public boolean addWarehousImpl(Warehouse aWarehous)
  {
    boolean wasAdded = false;
    if (warehouses.contains(aWarehous)) { return false; }
    warehouses.add(aWarehous);
    if (aWarehous.indexOfVendor(this) != -1)
    {
      wasAdded = true;
    }
    else
    {
      wasAdded = aWarehous.addVendor(this);
      if (!wasAdded)
      {
        warehouses.remove(aWarehous);
      }
    }
    return wasAdded;
  }

  public boolean removeWarehousImpl(Warehouse aWarehous)
  {
    boolean wasRemoved = false;
    if (!warehouses.contains(aWarehous))
    {
      return wasRemoved;
    }

    int oldIndex = warehouses.indexOf(aWarehous);
    warehouses.remove(oldIndex);
    if (aWarehous.indexOfVendor(this) == -1)
    {
      wasRemoved = true;
    }
    else
    {
      wasRemoved = aWarehous.removeVendor(this);
      if (!wasRemoved)
      {
        warehouses.add(oldIndex,aWarehous);
      }
    }
    return wasRemoved;
  }

  public boolean addWarehousAtImpl(Warehouse aWarehous, int index)
  {  
    boolean wasAdded = false;
    if(addWarehous(aWarehous))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfWarehouses()) { index = numberOfWarehouses() - 1; }
      warehouses.remove(aWarehous);
      warehouses.add(index, aWarehous);
      wasAdded = true;
    }
    return wasAdded;
  }

  public boolean addOrMoveWarehousAtImpl(Warehouse aWarehous, int index)
  {
    boolean wasAdded = false;
    if(warehouses.contains(aWarehous))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfWarehouses()) { index = numberOfWarehouses() - 1; }
      warehouses.remove(aWarehous);
      warehouses.add(index, aWarehous);
      wasAdded = true;
    } 
    else 
    {
      wasAdded = addWarehousAt(aWarehous, index);
    }
    return wasAdded;
  }

  public static int minimumNumberOfOrders()
  {
    return 0;
  }

  public boolean addOrderImpl(Order aOrder)
  {
    boolean wasAdded = false;
    if (orders.contains(aOrder)) { return false; }
    Vendor existingVendor = aOrder.getVendor();
    if (existingVendor == null)
    {
      aOrder.setVendor(this);
    }
    else if (!this.equals(existingVendor))
    {
      existingVendor.removeOrder(aOrder);
      addOrder(aOrder);
    }
    else
    {
      orders.add(aOrder);
    }
    wasAdded = true;
    return wasAdded;
  }

  public boolean removeOrderImpl(Order aOrder)
  {
    boolean wasRemoved = false;
    if (orders.contains(aOrder))
    {
      orders.remove(aOrder);
      aOrder.setVendor(null);
      wasRemoved = true;
    }
    return wasRemoved;
  }

  public boolean addOrderAtImpl(Order aOrder, int index)
  {  
    boolean wasAdded = false;
    if(addOrder(aOrder))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfOrders()) { index = numberOfOrders() - 1; }
      orders.remove(aOrder);
      orders.add(index, aOrder);
      wasAdded = true;
    }
    return wasAdded;
  }

  public boolean addOrMoveOrderAtImpl(Order aOrder, int index)
  {
    boolean wasAdded = false;
    if(orders.contains(aOrder))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfOrders()) { index = numberOfOrders() - 1; }
      orders.remove(aOrder);
      orders.add(index, aOrder);
      wasAdded = true;
    } 
    else 
    {
      wasAdded = addOrderAt(aOrder, index);
    }
    return wasAdded;
  }

  public static int minimumNumberOfCustomers()
  {
    return 0;
  }

  public boolean addCustomerImpl(Customer aCustomer)
  {
    boolean wasAdded = false;
    if (customers.contains(aCustomer)) { return false; }
    customers.add(aCustomer);
    if (aCustomer.indexOfVendor(this) != -1)
    {
      wasAdded = true;
    }
    else
    {
      wasAdded = aCustomer.addVendor(this);
      if (!wasAdded)
      {
        customers.remove(aCustomer);
      }
    }
    return wasAdded;
  }

  public boolean removeCustomerImpl(Customer aCustomer)
  {
    boolean wasRemoved = false;
    if (!customers.contains(aCustomer))
    {
      return wasRemoved;
    }

    int oldIndex = customers.indexOf(aCustomer);
    customers.remove(oldIndex);
    if (aCustomer.indexOfVendor(this) == -1)
    {
      wasRemoved = true;
    }
    else
    {
      wasRemoved = aCustomer.removeVendor(this);
      if (!wasRemoved)
      {
        customers.add(oldIndex,aCustomer);
      }
    }
    return wasRemoved;
  }

  public boolean addCustomerAtImpl(Customer aCustomer, int index)
  {  
    boolean wasAdded = false;
    if(addCustomer(aCustomer))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfCustomers()) { index = numberOfCustomers() - 1; }
      customers.remove(aCustomer);
      customers.add(index, aCustomer);
      wasAdded = true;
    }
    return wasAdded;
  }

  public boolean addOrMoveCustomerAtImpl(Customer aCustomer, int index)
  {
    boolean wasAdded = false;
    if(customers.contains(aCustomer))
    {
      if(index < 0 ) { index = 0; }
      if(index > numberOfCustomers()) { index = numberOfCustomers() - 1; }
      customers.remove(aCustomer);
      customers.add(index, aCustomer);
      wasAdded = true;
    } 
    else 
    {
      wasAdded = addCustomerAt(aCustomer, index);
    }
    return wasAdded;
  }

  public void deleteImpl()
  {
    ArrayList<Warehouse> copyOfWarehouses = new ArrayList<Warehouse>(warehouses);
    warehouses.clear();
    for(Warehouse aWarehous : copyOfWarehouses)
    {
      aWarehous.removeVendor(this);
    }
    while( !orders.isEmpty() )
    {
      orders.get(0).setVendor(null);
    }
    ArrayList<Customer> copyOfCustomers = new ArrayList<Customer>(customers);
    customers.clear();
    for(Customer aCustomer : copyOfCustomers)
    {
      aCustomer.removeVendor(this);
    }
    super.delete();
  }

  // line 68 "../ecommerceWS.ump"
   public Product findProductImpl(ProductType productType){
    for(Warehouse w:getWarehouses())
		{
			Product p= w.findProduct(productType);
			if(p!=null)
				return p;		
		}
		return null;
  }

  // line 77 "../ecommerceWS.ump"
   public Order makeOrderImpl(Customer aCustomer, Product aProduct){
    if(aProduct==null)
			return null;
		Order aOrder= new Order(aProduct);
		aOrder.setCustomer(aCustomer);
		aOrder.setVendor(this);
		return aOrder;
  }


  transient IVendorImpl realObject=this;
  public Vendor(String aName)
  {

this(aName,UmpleRuntime.getComponent("Vendor"));
  }
  public int getHashCode()
  {
    while(true)
      try{
        return realObject.getHashCodeImpl();
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public Warehouse getWarehous(int index)
  {
    while(true)
      try{
        return realObject.getWarehousImpl(index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public int numberOfWarehouses()
  {
    while(true)
      try{
        return realObject.numberOfWarehousesImpl();
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean hasWarehouses()
  {
    while(true)
      try{
        return realObject.hasWarehousesImpl();
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public int indexOfWarehous(Warehouse aWarehous)
  {
    while(true)
      try{
        return realObject.indexOfWarehousImpl(aWarehous);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public Order getOrder(int index)
  {
    while(true)
      try{
        return realObject.getOrderImpl(index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public int numberOfOrders()
  {
    while(true)
      try{
        return realObject.numberOfOrdersImpl();
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean hasOrders()
  {
    while(true)
      try{
        return realObject.hasOrdersImpl();
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public int indexOfOrder(Order aOrder)
  {
    while(true)
      try{
        return realObject.indexOfOrderImpl(aOrder);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public Customer getCustomer(int index)
  {
    while(true)
      try{
        return realObject.getCustomerImpl(index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public int numberOfCustomers()
  {
    while(true)
      try{
        return realObject.numberOfCustomersImpl();
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean hasCustomers()
  {
    while(true)
      try{
        return realObject.hasCustomersImpl();
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public int indexOfCustomer(Customer aCustomer)
  {
    while(true)
      try{
        return realObject.indexOfCustomerImpl(aCustomer);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addWarehous(Warehouse aWarehous)
  {
    while(true)
      try{
        return realObject.addWarehousImpl(aWarehous);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean removeWarehous(Warehouse aWarehous)
  {
    while(true)
      try{
        return realObject.removeWarehousImpl(aWarehous);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addWarehousAt(Warehouse aWarehous, int index)
  {
    while(true)
      try{
        return realObject.addWarehousAtImpl(aWarehous,index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addOrMoveWarehousAt(Warehouse aWarehous, int index)
  {
    while(true)
      try{
        return realObject.addOrMoveWarehousAtImpl(aWarehous,index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addOrder(Order aOrder)
  {
    while(true)
      try{
        return realObject.addOrderImpl(aOrder);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean removeOrder(Order aOrder)
  {
    while(true)
      try{
        return realObject.removeOrderImpl(aOrder);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addOrderAt(Order aOrder, int index)
  {
    while(true)
      try{
        return realObject.addOrderAtImpl(aOrder,index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addOrMoveOrderAt(Order aOrder, int index)
  {
    while(true)
      try{
        return realObject.addOrMoveOrderAtImpl(aOrder,index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addCustomer(Customer aCustomer)
  {
    while(true)
      try{
        return realObject.addCustomerImpl(aCustomer);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean removeCustomer(Customer aCustomer)
  {
    while(true)
      try{
        return realObject.removeCustomerImpl(aCustomer);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addCustomerAt(Customer aCustomer, int index)
  {
    while(true)
      try{
        return realObject.addCustomerAtImpl(aCustomer,index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public boolean addOrMoveCustomerAt(Customer aCustomer, int index)
  {
    while(true)
      try{
        return realObject.addOrMoveCustomerAtImpl(aCustomer,index);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
  public void delete()
  {
    while(true)
      try{
        realObject.deleteImpl();
        break;
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
   public Product findProduct(ProductType productType)
  {
    while(true)
      try{
        return realObject.findProductImpl(productType);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }
   public Order makeOrder(Customer aCustomer, Product aProduct)
  {
    while(true)
      try{
        return realObject.makeOrderImpl(aCustomer,aProduct);
        }
    catch(Exception e) {System.err.println(e.toString());}
  }

  private static Vendor umplenewInstance()
  {
    return new Vendor((UmpleRuntime.UmpleNode)null);
  }   

  protected Vendor (UmpleRuntime.UmpleNode node)
  {
    super(node);
  }

  public void setRealObject(IVendorImpl aObject)
  {
    super.setRealObject(aObject);
    realObject=aObject;
  }
  void afterUnmarshal(Unmarshaller u, Object parent)
  {
    initializeConnection();
  }
  private void initializeConnection()
  {
    if(objectId.contains(Vendor.class.getName()))
    {  
      boolean success = false;
      while (!success) {        
        try 
        {
          URL url = new URL(remoteUrl+":"+remotePort+"/Vendor"+objectId+"?wsdl");
          QName qname = new QName("https://ecommerceWS/","VendorService");
          Service service = Service.create(url, qname);
          setRealObject(service.getPort(IVendorImpl.class));
          success = true;
        } 
        catch (Exception e) 
        {
          System.err.println("Client exception: " + e.toString());
          e.printStackTrace();
          try { Thread.sleep(1000); } catch (InterruptedException interruptedException) {};
        }
      }
    }
  }
 
  public boolean equals(Object obj)
  {    if(obj.getClass()!=this.getClass())
      return false;
    return (getHashCode()==((Vendor)obj).getHashCode());
  }
}