/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/29 14:29:19*/
/*This class drives: EmployeeRole, Booking*/

package Airline.flights;

import java.sql.Date;
import java.sql.Time;
import java.util.*;
import Airline.*;
import Airline.humanResources.*;
import Airline.json.*;

public class SpecificFlight {
  //Class datatypes
  private Date date;

  //Class association variables
  private RegularFlight regularFlight;

  private List<EmployeeRole> employeeRoles;

  private List<Booking> bookings;

  //Registry of our system.
  AirlineRegistry registry = AirlineRegistry.getInstance();

  //Constructor
  public SpecificFlight(Date aDate, RegularFlight aRegularFlight) {
    date = aDate;
    regularFlight = aRegularFlight;
    regularFlight.addSpecificFlight(this);

    employeeRoles = new ArrayList<EmployeeRole>();
    registry.add(employeeRoles);

    bookings = new ArrayList<Booking>();
    registry.add(bookings);

  }

  public boolean setDate(Date aDate) {
    if (regularFlight.getSpecificFlight(aDate) == null) {
      date = aDate;
      return true;
    }

    //Another posibility would be to throw an exception
    //describing which of the 1 ends already has this element ID.
    return false;
  }

  public Date getDate() {
    return date;
  }

  public RegularFlight getRegularFlight() {
    return regularFlight;
  }

  public List<EmployeeRole> getEmployeeRoles() {
    return employeeRoles;
  }

  public List<Booking> getBookings() {
    return bookings;
  }

  public EmployeeRole addEmployeeRole(Person aPerson, String aJobFunction,
    EmployeeRole aSupervisor) throws Exception {
    EmployeeRole newEmployeeRole;
    newEmployeeRole = new EmployeeRole(aPerson, aJobFunction, aSupervisor);
    if (!employeeRoles.contains(newEmployeeRole)) {
      registry.add(newEmployeeRole);
      employeeRoles.add(newEmployeeRole);
    }
    return newEmployeeRole;
  }

  public EmployeeRole addEmployeeRole(EmployeeRole aEmployeeRole) {
    if (!employeeRoles.contains(aEmployeeRole))
      employeeRoles.add(aEmployeeRole);
    return aEmployeeRole;
  }

  public Booking addBooking(String aSeatNumber, PassangerRole aPassangerRole) {
    Booking newBooking;
    newBooking = new Booking(aSeatNumber, aPassangerRole, this);
    if (!bookings.contains(newBooking)) {
      registry.add(newBooking);
      bookings.add(newBooking);
    }
    return newBooking;
  }

  public Booking addBooking(Booking aBooking) {
    if (!bookings.contains(aBooking))
      bookings.add(aBooking);
    return aBooking;
  }

  /* This class does not drive RegularFlight and therefore sets the association unidirectionally.*/
  public void setRegularFlight(RegularFlight aRegularFlight) {
    regularFlight = aRegularFlight;
  }

  public void delete() {
    //Delete all many ends first.
    for (EmployeeRole aEmployeeRole : employeeRoles) {
      aEmployeeRole.delete();
    }
    employeeRoles.clear();

    //Delete all many ends first.
    for (Booking aBooking : bookings) {
      aBooking.delete();
    }
    bookings.clear();

    //Delete all 1 ends.
    regularFlight.deleteSpecificFlight(this);
  }

  public void deleteEmployeeRole(EmployeeRole aEmployeeRole) {

    if (employeeRoles.contains(aEmployeeRole)) {
      employeeRoles.remove(aEmployeeRole);

      //registry.removeObj(registry.getKey(aEmployeeRole));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteBooking(Booking aBooking) {

    if (bookings.contains(aBooking)) {
      bookings.remove(aBooking);

      //registry.removeObj(registry.getKey(aBooking));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (employeeRoles.size() == 0 && bookings.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("date", getDate());
    return obj;
  }

}