/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/31 09:24:36*/
/*This class drives: Person*/

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 Union parents;

  private List<Person> adoptiveParentss;

  private List<Person> adoptedChilds;

  //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);

    adoptiveParentss = new ArrayList<Person>();
    registry.add(adoptiveParentss);

    adoptedChilds = new ArrayList<Person>();
    registry.add(adoptedChilds);

  }

  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 Union getParents() {
    return parents;
  }

  public List<Person> getAdoptiveParentss() {
    return adoptiveParentss;
  }

  public List<Person> getAdoptedChilds() {
    return adoptedChilds;
  }

  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 Person addAdoptiveParents(String aName, String aSex,
    String aPlaceOfBirth, String aDateOfBirth, String aPlaceOfDeath,
    String aDateOfDeath) throws Exception {
    if (!(adoptiveParentss.size() == 2)) {
      Person newPerson;
      newPerson =
        new Person(aName, aSex, aPlaceOfBirth, aDateOfBirth, aPlaceOfDeath,
          aDateOfDeath);
      if (!adoptiveParentss.contains(newPerson)) {
        adoptiveParentss.add(newPerson);
        registry.add(newPerson);
      }
      return newPerson;
    } else
      throw new Exception("No more space in the set.");
  }

  public Person addAdoptiveParents(Person aAdoptiveParents) throws Exception {
    if (adoptiveParentss.size() < 2) {
      if (!adoptiveParentss.contains(aAdoptiveParents))
        adoptiveParentss.add(aAdoptiveParents);
      return aAdoptiveParents;
    } else
      throw new Exception("No more space in the set.");
  }

  public Person addAdoptedChild(String aName, String aSex,
    String aPlaceOfBirth, String aDateOfBirth, String aPlaceOfDeath,
    String aDateOfDeath) throws Exception {
    Person newPerson;
    newPerson =
      new Person(aName, aSex, aPlaceOfBirth, aDateOfBirth, aPlaceOfDeath,
        aDateOfDeath);
    if (!adoptedChilds.contains(newPerson)) {
      registry.add(newPerson);
      adoptedChilds.add(newPerson);
    }
    return newPerson;
  }

  public Person addAdoptedChild(Person aAdoptedChild) {
    if (!adoptedChilds.contains(aAdoptedChild))
      adoptedChilds.add(aAdoptedChild);
    return aAdoptedChild;
  }

  /* 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 (Person aPerson : adoptiveParentss) {
      aPerson.delete();
    }
    adoptiveParentss.clear();

    //Delete all many ends first.
    for (Person aPerson : adoptedChilds) {
      aPerson.delete();
    }
    adoptedChilds.clear();

    //Delete all 1 ends.
    parents.deletePartner(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 deleteParents(Union aUnion) {

    if (aUnion.equals(parents)) {
      parents = null;

      registry.removeObj(registry.getKey(aUnion));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteAdoptiveParents(Person aPerson) {

    if (adoptiveParentss.contains(aPerson)) {
      adoptiveParentss.remove(aPerson);

      //registry.removeObj(registry.getKey(aPerson));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteAdoptedChild(Person aPerson) {

    if (adoptedChilds.contains(aPerson)) {
      adoptedChilds.remove(aPerson);

      //registry.removeObj(registry.getKey(aPerson));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (adoptedChilds.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;
  }

}