/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 10:05:36*/
/*This class drives: ProductType, Product*/

package MFP.core.green;

import java.util.*;
import MFP.*;
import MFP.core.red.*;
import MFP.core.black.*;
import MFP.core.blue.*;
import MFP.json.*;

public class Supplier {
  //Class datatypes
  private String name;

  //Class association variables
  private List<ProductType> productTypes;

  private List<Product> products;

  //Registry of our system.
  MFPRegistry registry = MFPRegistry.getInstance();

  //Constructor
  public Supplier(String aName) {
    name = aName;
    productTypes = new ArrayList<ProductType>();
    registry.add(productTypes);

    products = new ArrayList<Product>();
    registry.add(products);

  }

  public boolean setName(String aName) {
    name = aName;
    return true;
  }

  public String getName() {
    return name;
  }

  public List<ProductType> getProductTypes() {
    return productTypes;
  }

  public List<Product> getProducts() {
    return products;
  }

  public ProductType addProductType(String aDescription) {
    ProductType newProductType;
    newProductType = new ProductType(aDescription);
    if (!productTypes.contains(newProductType)) {
      registry.add(newProductType);
      productTypes.add(newProductType);
    }
    return newProductType;
  }

  public ProductType addProductType(ProductType aProductType) {
    if (!productTypes.contains(aProductType))
      productTypes.add(aProductType);
    return aProductType;
  }

  public Product addProduct(String aSerialNumber, Product aProduct,
    ProductType aProductType, OrderLineItem aOrderLineItem) {
    Product newProduct;
    newProduct =
      new Product(aSerialNumber, aProduct, aProductType, aOrderLineItem);
    if (!products.contains(newProduct)) {
      registry.add(newProduct);
      products.add(newProduct);
    }
    return newProduct;
  }

  public Product addProduct(Product aProduct) {
    if (!products.contains(aProduct))
      products.add(aProduct);
    return aProduct;
  }

  public void delete() {
    //Delete all many ends first.
    for (ProductType aProductType : productTypes) {
      aProductType.delete();
    }
    productTypes.clear();

    //Delete all many ends first.
    for (Product aProduct : products) {
      aProduct.delete();
    }
    products.clear();

  }

  public void deleteProductType(ProductType aProductType) {

    if (productTypes.contains(aProductType)) {
      productTypes.remove(aProductType);

      //registry.removeObj(registry.getKey(aProductType));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteProduct(Product aProduct) {

    if (products.contains(aProduct)) {
      products.remove(aProduct);

      //registry.removeObj(registry.getKey(aProduct));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (productTypes.size() == 0 && products.size() == 0) {
      return true;
    } else
      return false;
  }

  /***********************************
   * Returns the attribute list along with the  
   * class ID in JSON format.
   ***********************************/
  public JSONObject getAttributes() throws JSONException {
    JSONObject obj = new JSONObject();
    obj.put("CLASS_ID", registry.getKey(this));
    obj.put("name", getName());
    return obj;
  }

}