/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 10:05:36*/
/*This class drives: BillOfMaterialsLineItem, RobotAllocation*/

package MFP.core.red;

import java.util.*;
import java.sql.Time;
import MFP.*;
import MFP.core.green.*;
import MFP.core.black.*;
import MFP.core.blue.*;
import MFP.json.*;

public class AssemblyStep {
  //Class datatypes
  private String description;

  //Class association variables
  private List<BillOfMaterialsLineItem> billOfMaterialsLineItems;

  private List<RobotAllocation> robotAllocations;

  private ProductType productType;

  //Registry of our system.
  MFPRegistry registry = MFPRegistry.getInstance();

  //Constructor
  public AssemblyStep(String aDescription, ProductType aProductType) {
    description = aDescription;
    billOfMaterialsLineItems = new ArrayList<BillOfMaterialsLineItem>();
    registry.add(billOfMaterialsLineItems);

    robotAllocations = new ArrayList<RobotAllocation>();
    registry.add(robotAllocations);

    productType = aProductType;
    productType.addAssemblyStep(this);

  }

  public boolean setDescription(String aDescription) {
    description = aDescription;
    return true;
  }

  public String getDescription() {
    return description;
  }

  public List<BillOfMaterialsLineItem> getBillOfMaterialsLineItems() {
    return billOfMaterialsLineItems;
  }

  public List<RobotAllocation> getRobotAllocations() {
    return robotAllocations;
  }

  public ProductType getProductType() {
    return productType;
  }

  public BillOfMaterialsLineItem addBillOfMaterialsLineItem(
    String aNumberRequired, ProductType aProductType, ProductType aConsistsOf,
    AssemblyStep aAssemblyStep) {
    BillOfMaterialsLineItem newBillOfMaterialsLineItem;
    newBillOfMaterialsLineItem =
      new BillOfMaterialsLineItem(aNumberRequired, aProductType, aConsistsOf,
        this);
    if (!billOfMaterialsLineItems.contains(newBillOfMaterialsLineItem)) {
      registry.add(newBillOfMaterialsLineItem);
      billOfMaterialsLineItems.add(newBillOfMaterialsLineItem);
    }
    return newBillOfMaterialsLineItem;
  }

  public BillOfMaterialsLineItem addBillOfMaterialsLineItem(
    BillOfMaterialsLineItem aBillOfMaterialsLineItem) {
    if (!billOfMaterialsLineItems.contains(aBillOfMaterialsLineItem))
      billOfMaterialsLineItems.add(aBillOfMaterialsLineItem);
    return aBillOfMaterialsLineItem;
  }

  public RobotAllocation addRobotAllocation(String aTimePeriod,
    AssemblyStep aAssemblyStep, Robot aRobot) {
    RobotAllocation newRobotAllocation;
    newRobotAllocation = new RobotAllocation(aTimePeriod, this, aRobot);
    if (!robotAllocations.contains(newRobotAllocation)) {
      registry.add(newRobotAllocation);
      robotAllocations.add(newRobotAllocation);
    }
    return newRobotAllocation;
  }

  public RobotAllocation addRobotAllocation(RobotAllocation aRobotAllocation) {
    if (!robotAllocations.contains(aRobotAllocation))
      robotAllocations.add(aRobotAllocation);
    return aRobotAllocation;
  }

  /* This class does not drive ProductType and therefore sets the association unidirectionally.*/
  public void setProductType(ProductType aProductType) {
    productType = aProductType;
  }

  public void delete() {
    //Delete all many ends first.
    for (BillOfMaterialsLineItem aBillOfMaterialsLineItem : billOfMaterialsLineItems) {
      aBillOfMaterialsLineItem.delete();
    }
    billOfMaterialsLineItems.clear();

    //Delete all many ends first.
    for (RobotAllocation aRobotAllocation : robotAllocations) {
      aRobotAllocation.delete();
    }
    robotAllocations.clear();

    //Delete all 1 ends.
    productType.deleteAssemblyStep(this);
  }

  public void deleteBillOfMaterialsLineItem(
    BillOfMaterialsLineItem aBillOfMaterialsLineItem) {

    if (billOfMaterialsLineItems.contains(aBillOfMaterialsLineItem)) {
      billOfMaterialsLineItems.remove(aBillOfMaterialsLineItem);

      //registry.removeObj(registry.getKey(aBillOfMaterialsLineItem));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteRobotAllocation(RobotAllocation aRobotAllocation) {

    if (robotAllocations.contains(aRobotAllocation)) {
      robotAllocations.remove(aRobotAllocation);

      //registry.removeObj(registry.getKey(aRobotAllocation));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (billOfMaterialsLineItems.size() == 0 && robotAllocations.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("description", getDescription());
    return obj;
  }

}