/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/31 09:26:55*/
/*This class drives: Adoption*/

package Geneology.core;

import java.util.*;
import java.sql.Date;
import Geneology.*;
import Geneology.json.*;

public class Person {
  //Class datatypes
  private String name;

  private String sex;

  private String placeOfBirth;

  private String dateOfBirth;

  private String placeOfDeath;

  private String dateOfDeath;

  //Class association variables
  private List<Union> unions;

  private List<Adoption> adoptions;

  private Union parents;

  //Registry of our system.
  GeneologyRegistry registry = GeneologyRegistry.getInstance();

  //Constructor
  public Person(String aName, String aSex, String aPlaceOfBirth,
    String aDateOfBirth, String aPlaceOfDeath, String aDateOfDeath)
    throws Exception {
    name = aName;
    sex = aSex;
    placeOfBirth = aPlaceOfBirth;
    dateOfBirth = aDateOfBirth;
    placeOfDeath = aPlaceOfDeath;
    dateOfDeath = aDateOfDeath;
    unions = new ArrayList<Union>();
    registry.add(unions);

    adoptions = new ArrayList<Adoption>();
    registry.add(adoptions);

  }

  public boolean setName(String aName) {
    name = aName;
    return true;
  }

  public boolean setSex(String aSex) {
    sex = aSex;
    return true;
  }

  public boolean setPlaceOfBirth(String aPlaceOfBirth) {
    placeOfBirth = aPlaceOfBirth;
    return true;
  }

  public boolean setDateOfBirth(String aDateOfBirth) {
    dateOfBirth = aDateOfBirth;
    return true;
  }

  public boolean setPlaceOfDeath(String aPlaceOfDeath) {
    placeOfDeath = aPlaceOfDeath;
    return true;
  }

  public boolean setDateOfDeath(String aDateOfDeath) {
    dateOfDeath = aDateOfDeath;
    return true;
  }

  public String getName() {
    return name;
  }

  public String getSex() {
    return sex;
  }

  public String getPlaceOfBirth() {
    return placeOfBirth;
  }

  public String getDateOfBirth() {
    return dateOfBirth;
  }

  public String getPlaceOfDeath() {
    return placeOfDeath;
  }

  public String getDateOfDeath() {
    return dateOfDeath;
  }

  public List<Union> getUnions() {
    return unions;
  }

  public List<Adoption> getAdoptions() {
    return adoptions;
  }

  public Union getParents() {
    return parents;
  }

  public Union addUnion(String aPlaceOfMarriage, String aDateOfMarriage,
    String aDateOfDivorce) {
    Union newUnion;
    newUnion = new Union(aPlaceOfMarriage, aDateOfMarriage, aDateOfDivorce);
    if (!unions.contains(newUnion)) {
      registry.add(newUnion);
      unions.add(newUnion);
    }
    return newUnion;
  }

  public Union addUnion(Union aUnion) {
    if (!unions.contains(aUnion))
      unions.add(aUnion);
    return aUnion;
  }

  public Adoption addAdoption(String aDateOfAdoption) {
    Adoption newAdoption;
    newAdoption = new Adoption(aDateOfAdoption);
    if (!adoptions.contains(newAdoption)) {
      registry.add(newAdoption);
      adoptions.add(newAdoption);
    }
    return newAdoption;
  }

  public Adoption addAdoption(Adoption aAdoption) {
    if (!adoptions.contains(aAdoption))
      adoptions.add(aAdoption);
    return aAdoption;
  }

  /* This class does not drive Union and therefore sets the association unidirectionally.*/
  public void setParents(Union aParents) {
    parents = aParents;
  }

  public void delete() {
    //Delete all many ends first.
    for (Adoption aAdoption : adoptions) {
      aAdoption.delete();
    }
    adoptions.clear();

    //Delete all 1 ends.
    parents.deleteParent(this);
  }

  public void deleteUnion(Union aUnion) {

    if (unions.contains(aUnion)) {
      unions.remove(aUnion);

      //registry.removeObj(registry.getKey(aUnion));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteAdoption(Adoption aAdoption) {

    if (adoptions.contains(aAdoption)) {
      adoptions.remove(aAdoption);

      //registry.removeObj(registry.getKey(aAdoption));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteParents(Union aUnion) {

    if (aUnion.equals(parents)) {
      parents = null;

      registry.removeObj(registry.getKey(aUnion));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (adoptions.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("sex", getSex());
    obj.put("placeOfBirth", getPlaceOfBirth());
    obj.put("dateOfBirth", getDateOfBirth());
    obj.put("placeOfDeath", getPlaceOfDeath());
    obj.put("dateOfDeath", getDateOfDeath());
    return obj;
  }

}