/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/29 14:29:19*/
/*This class drives: EmployeeRole*/

package Airline.humanResources;

import java.util.*;
import java.sql.Date;
import Airline.*;
import Airline.flights.*;
import Airline.json.*;

public class EmployeeRole extends PersonRole {
  //Class datatypes
  private String jobFunction;

  //Class association variables
  private EmployeeRole supervisor;

  private List<EmployeeRole> employeeRoles;

  private List<SpecificFlight> specificFlights;

  //Registry of our system.
  AirlineRegistry registry = AirlineRegistry.getInstance();

  //Constructor
  public EmployeeRole(Person aPerson, String aJobFunction,
    EmployeeRole aSupervisor) throws Exception {
    super(aPerson);
    jobFunction = aJobFunction;
    supervisor = aSupervisor;
    supervisor.addEmployeeRole(this);

    employeeRoles = new ArrayList<EmployeeRole>();
    registry.add(employeeRoles);

    specificFlights = new ArrayList<SpecificFlight>();
    registry.add(specificFlights);

  }

  public boolean setJobFunction(String aJobFunction) {
    jobFunction = aJobFunction;
    return true;
  }

  public String getJobFunction() {
    return jobFunction;
  }

  public EmployeeRole getSupervisor() {
    return supervisor;
  }

  public List<EmployeeRole> getEmployeeRoles() {
    return employeeRoles;
  }

  public List<SpecificFlight> getSpecificFlights() {
    return specificFlights;
  }

  /*SpecificFlight is unique with respect to EmployeeRole*/
  public SpecificFlight getSpecificFlight(Date aDate) {
    for (SpecificFlight aSpecificFlight : getSpecificFlights()) {
      if (aSpecificFlight.getDate().equals(aDate))
        return aSpecificFlight;
    }

    //If the value was found, it would have been returned.
    return null;
  }

  public EmployeeRole addEmployeeRole(Person aPerson, String aJobFunction)
    throws Exception {
    EmployeeRole newEmployeeRole;
    newEmployeeRole = new EmployeeRole(aPerson, aJobFunction, this);
    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 SpecificFlight addSpecificFlight(Date aDate,
    RegularFlight aRegularFlight) {
    SpecificFlight newSpecificFlight;
    newSpecificFlight = new SpecificFlight(aDate, aRegularFlight);
    if (!specificFlights.contains(newSpecificFlight)) {
      registry.add(newSpecificFlight);
      specificFlights.add(newSpecificFlight);
    }
    return newSpecificFlight;
  }

  public SpecificFlight addSpecificFlight(SpecificFlight aSpecificFlight) {
    if (!specificFlights.contains(aSpecificFlight))
      specificFlights.add(aSpecificFlight);
    return aSpecificFlight;
  }

  /* This class does not drive EmployeeRole and therefore sets the association unidirectionally.*/
  public void setSupervisor(EmployeeRole aSupervisor) {
    supervisor = aSupervisor;
  }

  public void delete() {
    //Delete all many ends first.
    for (EmployeeRole aEmployeeRole : employeeRoles) {
      aEmployeeRole.delete();
    }
    employeeRoles.clear();

    //Delete all 1 ends.
    supervisor.delete();
  }

  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 deleteSpecificFlight(SpecificFlight aSpecificFlight) {

    if (specificFlights.contains(aSpecificFlight)) {
      specificFlights.remove(aSpecificFlight);

      //registry.removeObj(registry.getKey(aSpecificFlight));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (employeeRoles.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("jobFunction", getJobFunction());
    return obj;
  }

}