/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 10:05:36*/
/*This class drives: Product*/

package MFP.core.black;

import java.util.*;
import MFP.*;
import MFP.core.green.*;
import MFP.core.red.*;
import MFP.core.blue.*;
import MFP.json.*;

public class ProductRun {
  //Class datatypes
  private String timePeriod;

  private String numUnits;

  //Class association variables
  private ProductType productType;

  private AssemblyLine assemblyLine;

  private List<Product> products;

  //Registry of our system.
  MFPRegistry registry = MFPRegistry.getInstance();

  //Constructor
  public ProductRun(String aTimePeriod, String aNumUnits,
    ProductType aProductType, AssemblyLine aAssemblyLine) {
    timePeriod = aTimePeriod;
    numUnits = aNumUnits;
    productType = aProductType;
    productType.addProductRun(this);

    assemblyLine = aAssemblyLine;
    assemblyLine.addProductRun(this);

    products = new ArrayList<Product>();
    registry.add(products);

  }

  public boolean setTimePeriod(String aTimePeriod) {
    timePeriod = aTimePeriod;
    return true;
  }

  public boolean setNumUnits(String aNumUnits) {
    numUnits = aNumUnits;
    return true;
  }

  public String getTimePeriod() {
    return timePeriod;
  }

  public String getNumUnits() {
    return numUnits;
  }

  public ProductType getProductType() {
    return productType;
  }

  public AssemblyLine getAssemblyLine() {
    return assemblyLine;
  }

  public List<Product> getProducts() {
    return products;
  }

  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;
  }

  /* This class does not drive ProductType and therefore sets the association unidirectionally.*/
  public void setProductType(ProductType aProductType) {
    productType = aProductType;
  }

  /* This class does not drive AssemblyLine and therefore sets the association unidirectionally.*/
  public void setAssemblyLine(AssemblyLine aAssemblyLine) {
    assemblyLine = aAssemblyLine;
  }

  public void delete() {
    //Delete all many ends first.
    for (Product aProduct : products) {
      aProduct.delete();
    }
    products.clear();

    //Delete all 1 ends.
    productType.deleteProductRun(this);
    assemblyLine.deleteProductRun(this);
  }

  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 (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("timePeriod", getTimePeriod());
    obj.put("numUnits", getNumUnits());
    return obj;
  }

}