/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 13:12:55*/
/*This class drives: Slot*/

package Warehouse.core;

import java.util.*;
import Warehouse.*;
import Warehouse.json.*;

public class Level {
  //Class datatypes
  private int height;

  private int number;

  //Class association variables
  private List<Slot> slots;

  private Row row;

  //Registry of our system.
  WarehouseRegistry registry = WarehouseRegistry.getInstance();

  //Constructor
  public Level(int aHeight, int aNumber, Row aRow) {
    height = aHeight;
    number = aNumber;
    slots = new ArrayList<Slot>();
    registry.add(slots);

    row = aRow;
    row.addLevel(this);

  }

  public boolean setHeight(int aHeight) {
    height = aHeight;
    return true;
  }

  public boolean setNumber(int aNumber) {
    number = aNumber;
    return true;
  }

  public int getHeight() {
    return height;
  }

  public int getNumber() {
    return number;
  }

  public List<Slot> getSlots() {
    return slots;
  }

  public Row getRow() {
    return row;
  }

  public Slot addSlot(int aNumber, double aWidth, SlotSet aSlotSet, Level aLevel) {
    Slot newSlot;
    newSlot = new Slot(aNumber, aWidth, aSlotSet, this);
    if (!slots.contains(newSlot)) {
      registry.add(newSlot);
      slots.add(newSlot);
    }
    return newSlot;
  }

  public Slot addSlot(Slot aSlot) {
    if (!slots.contains(aSlot))
      slots.add(aSlot);
    return aSlot;
  }

  /* This class does not drive Row and therefore sets the association unidirectionally.*/
  public void setRow(Row aRow) {
    row = aRow;
  }

  public void delete() {
    //Delete all many ends first.
    for (Slot aSlot : slots) {
      aSlot.delete();
    }
    slots.clear();

    //Delete all 1 ends.
    row.deleteLevel(this);
  }

  public void deleteSlot(Slot aSlot) {

    if (slots.contains(aSlot)) {
      slots.remove(aSlot);

      //registry.removeObj(registry.getKey(aSlot));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (slots.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("height", getHeight());
    obj.put("number", getNumber());
    return obj;
  }

}