/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 11:21:38*/
/*This class drives: Person*/

package insurance.core;

import java.sql.Date;
import java.util.*;
import insurance.*;
import insurance.json.*;

public class LifeInsurancePolicy extends InsurancePolicy {
  //Class datatypes

  //Class association variables
  private Person insuredLife;

  private List<Person> beneficiarys;

  //Registry of our system.
  InsuranceRegistry registry = InsuranceRegistry.getInstance();

  //Constructor
  public LifeInsurancePolicy(String aPolicyNumber, String aMonthlyPremium,
    String aStarDate, String aEndDate, String aInsuradValue, Person aHolder,
    Person aInsuredLife) {
    super(aPolicyNumber, aMonthlyPremium, aStarDate, aEndDate, aInsuradValue,
      aHolder);
    insuredLife = aInsuredLife;
    insuredLife.addLifeInsurancePolicy(this);

    beneficiarys = new ArrayList<Person>();
    registry.add(beneficiarys);

  }

  public Person getInsuredLife() {
    return insuredLife;
  }

  public List<Person> getBeneficiarys() {
    return beneficiarys;
  }

  public Person addBeneficiary(String aName, String aAddress,
    String aDateOfBirth) {
    Person newPerson;
    newPerson = new Person(aName, aAddress, aDateOfBirth);
    if (!beneficiarys.contains(newPerson)) {
      registry.add(newPerson);
      beneficiarys.add(newPerson);
    }
    return newPerson;
  }

  public Person addBeneficiary(Person aBeneficiary) {
    if (!beneficiarys.contains(aBeneficiary))
      beneficiarys.add(aBeneficiary);
    return aBeneficiary;
  }

  /* This class does not drive Person and therefore sets the association unidirectionally.*/
  public void setInsuredLife(Person aInsuredLife) {
    insuredLife = aInsuredLife;
  }

  public void delete() {
    //Delete all many ends first.
    for (Person aPerson : beneficiarys) {
      aPerson.delete();
    }
    beneficiarys.clear();

    //Delete all 1 ends.
    insuredLife.deleteLifeInsurancePolicy(this);
  }

  public void deleteBeneficiary(Person aPerson) {

    if (beneficiarys.contains(aPerson)) {
      beneficiarys.remove(aPerson);

      //registry.removeObj(registry.getKey(aPerson));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (beneficiarys.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));
    return obj;
  }

}