/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/31 09:24:36*/
/*This class drives: Person, Person*/

package Geneology.core;

import java.util.*;
import java.sql.Date;
import Geneology.*;
import Geneology.json.*;

public class Union {
  //Class datatypes
  private String placeOfMarriage;

  private String dateOfMarriage;

  private String dateOfDivorce;

  //Class association variables
  private List<Person> partners;

  private List<Person> childs;

  //Registry of our system.
  GeneologyRegistry registry = GeneologyRegistry.getInstance();

  //Constructor
  public Union(String aPlaceOfMarriage, String aDateOfMarriage,
    String aDateOfDivorce) {
    placeOfMarriage = aPlaceOfMarriage;
    dateOfMarriage = aDateOfMarriage;
    dateOfDivorce = aDateOfDivorce;
    partners = new ArrayList<Person>();
    registry.add(partners);

    childs = new ArrayList<Person>();
    registry.add(childs);

  }

  public boolean setPlaceOfMarriage(String aPlaceOfMarriage) {
    placeOfMarriage = aPlaceOfMarriage;
    return true;
  }

  public boolean setDateOfMarriage(String aDateOfMarriage) {
    dateOfMarriage = aDateOfMarriage;
    return true;
  }

  public boolean setDateOfDivorce(String aDateOfDivorce) {
    dateOfDivorce = aDateOfDivorce;
    return true;
  }

  public String getPlaceOfMarriage() {
    return placeOfMarriage;
  }

  public String getDateOfMarriage() {
    return dateOfMarriage;
  }

  public String getDateOfDivorce() {
    return dateOfDivorce;
  }

  public List<Person> getPartners() {
    return partners;
  }

  public List<Person> getChilds() {
    return childs;
  }

  public Person addPartner(String aName, String aSex, String aPlaceOfBirth,
    String aDateOfBirth, String aPlaceOfDeath, String aDateOfDeath)
    throws Exception {
    if (!(partners.size() == 2)) {
      Person newPerson;
      newPerson =
        new Person(aName, aSex, aPlaceOfBirth, aDateOfBirth, aPlaceOfDeath,
          aDateOfDeath);
      if (!partners.contains(newPerson)) {
        partners.add(newPerson);
        registry.add(newPerson);
      }
      return newPerson;
    } else
      throw new Exception("No more space in the set.");
  }

  public Person addPartner(Person aPartner) throws Exception {
    if (partners.size() < 2) {
      if (!partners.contains(aPartner))
        partners.add(aPartner);
      return aPartner;
    } else
      throw new Exception("No more space in the set.");
  }

  public Person addChild(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 (!childs.contains(newPerson)) {
      registry.add(newPerson);
      childs.add(newPerson);
    }
    return newPerson;
  }

  public Person addChild(Person aChild) {
    if (!childs.contains(aChild))
      childs.add(aChild);
    return aChild;
  }

  public void delete() {
    //Delete all many ends first.
    for (Person aPerson : partners) {
      aPerson.delete();
    }
    partners.clear();

    //Delete all many ends first.
    for (Person aPerson : childs) {
      aPerson.delete();
    }
    childs.clear();

  }

  public void deletePartner(Person aPerson) {

    if (partners.contains(aPerson)) {
      partners.remove(aPerson);

      //registry.removeObj(registry.getKey(aPerson));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteChild(Person aPerson) {

    if (childs.contains(aPerson)) {
      childs.remove(aPerson);

      //registry.removeObj(registry.getKey(aPerson));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (childs.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("placeOfMarriage", getPlaceOfMarriage());
    obj.put("dateOfMarriage", getDateOfMarriage());
    obj.put("dateOfDivorce", getDateOfDivorce());
    return obj;
  }

}