/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/29 14:29:19*/
/*This class drives: PersonRole*/

package Airline.humanResources;

import java.util.*;
import Airline.*;
import Airline.flights.*;
import Airline.json.*;

public class Person {
  //Class datatypes
  private String name;

  private int idNumber;

  //Class association variables
  private List<PersonRole> personRoles;

  private Airline airline;

  //Registry of our system.
  AirlineRegistry registry = AirlineRegistry.getInstance();

  //Constructor
  public Person(String aName, int aIdNumber, Airline aAirline) {
    name = aName;
    idNumber = aIdNumber;
    personRoles = new ArrayList<PersonRole>();
    registry.add(personRoles);

    airline = aAirline;
    airline.addPerson(this);

  }

  public boolean setName(String aName) {
    name = aName;
    return true;
  }

  public boolean setIdNumber(int aIdNumber) {
    idNumber = aIdNumber;
    return true;
  }

  public String getName() {
    return name;
  }

  public int getIdNumber() {
    return idNumber;
  }

  public List<PersonRole> getPersonRoles() {
    return personRoles;
  }

  public Airline getAirline() {
    return airline;
  }

  public PersonRole addPersonRole() throws Exception {
    if (!(personRoles.size() == 2)) {
      PersonRole newPersonRole;
      newPersonRole = new PersonRole(this);
      if (!personRoles.contains(newPersonRole)) {
        personRoles.add(newPersonRole);
        registry.add(newPersonRole);
      }
      return newPersonRole;
    } else
      throw new Exception("No more space in the set.");
  }

  public PersonRole addPersonRole(PersonRole aPersonRole) throws Exception {
    if (personRoles.size() < 2) {
      if (!personRoles.contains(aPersonRole))
        personRoles.add(aPersonRole);
      return aPersonRole;
    } else
      throw new Exception("No more space in the set.");
  }

  /* This class does not drive Airline and therefore sets the association unidirectionally.*/
  public void setAirline(Airline aAirline) {
    airline = aAirline;
  }

  public void delete() {
    //Delete all many ends first.
    for (PersonRole aPersonRole : personRoles) {
      aPersonRole.delete();
    }
    personRoles.clear();

    //Delete all 1 ends.
    airline.deletePerson(this);
  }

  public void deletePersonRole(PersonRole aPersonRole) {

    if (personRoles.contains(aPersonRole)) {
      personRoles.remove(aPersonRole);

      //registry.removeObj(registry.getKey(aPersonRole));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    return true;
  }

  /***********************************
   * 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("idNumber", getIdNumber());
    return obj;
  }

}