/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 10:46:15*/
/*This class drives: RentableSpace*/

package hotel.core;

import java.util.*;
import hotel.*;
import hotel.json.*;

public class Hotel {
  //Class datatypes
  private String name;

  private String address;

  //Class association variables
  private HotelCompany hotelCompany;

  private List<RentableSpace> rentableSpaces;

  //Registry of our system.
  HotelRegistry registry = HotelRegistry.getInstance();

  //Constructor
  public Hotel(String aName, String aAddress, HotelCompany aHotelCompany) {
    name = aName;
    address = aAddress;
    hotelCompany = aHotelCompany;
    hotelCompany.addOwns(this);

    rentableSpaces = new ArrayList<RentableSpace>();
    registry.add(rentableSpaces);

  }

  public boolean setName(String aName) {
    name = aName;
    return true;
  }

  public boolean setAddress(String aAddress) {
    address = aAddress;
    return true;
  }

  public String getName() {
    return name;
  }

  public String getAddress() {
    return address;
  }

  public HotelCompany getHotelCompany() {
    return hotelCompany;
  }

  public List<RentableSpace> getRentableSpaces() {
    return rentableSpaces;
  }

  public RentableSpace addRentableSpace(String aCostPerDay, String aFloorArea,
    Hotel aHotel, Suite aSuite) {
    RentableSpace newRentableSpace;
    newRentableSpace = new RentableSpace(aCostPerDay, aFloorArea, this, aSuite);
    if (!rentableSpaces.contains(newRentableSpace)) {
      registry.add(newRentableSpace);
      rentableSpaces.add(newRentableSpace);
    }
    return newRentableSpace;
  }

  public RentableSpace addRentableSpace(RentableSpace aRentableSpace) {
    if (!rentableSpaces.contains(aRentableSpace))
      rentableSpaces.add(aRentableSpace);
    return aRentableSpace;
  }

  /* This class does not drive HotelCompany and therefore sets the association unidirectionally.*/
  public void setHotelCompany(HotelCompany aHotelCompany) {
    hotelCompany = aHotelCompany;
  }

  public void delete() {
    //Delete all many ends first.
    for (RentableSpace aRentableSpace : rentableSpaces) {
      aRentableSpace.delete();
    }
    rentableSpaces.clear();

    //Delete all 1 ends.
    hotelCompany.deleteOwns(this);
  }

  public void deleteRentableSpace(RentableSpace aRentableSpace) {

    if (rentableSpaces.contains(aRentableSpace)) {
      rentableSpaces.remove(aRentableSpace);

      //registry.removeObj(registry.getKey(aRentableSpace));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (rentableSpaces.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());
    obj.put("address", getAddress());
    return obj;
  }

}