/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 11:21:38*/
/*This class drives: Transaction*/

package insurance.core;

import java.util.*;
import java.sql.Date;
import insurance.*;
import insurance.json.*;

public class InsurancePolicy {
  //Class datatypes
  private String policyNumber;

  private String monthlyPremium;

  private String starDate;

  private String endDate;

  private String insuradValue;

  //Class association variables
  private List<Transaction> transactions;

  private Person holder;

  //Registry of our system.
  InsuranceRegistry registry = InsuranceRegistry.getInstance();

  //Constructor
  public InsurancePolicy(String aPolicyNumber, String aMonthlyPremium,
    String aStarDate, String aEndDate, String aInsuradValue, Person aHolder) {
    policyNumber = aPolicyNumber;
    monthlyPremium = aMonthlyPremium;
    starDate = aStarDate;
    endDate = aEndDate;
    insuradValue = aInsuradValue;
    transactions = new ArrayList<Transaction>();
    registry.add(transactions);

    holder = aHolder;
    holder.addInsurancePolicy(this);

  }

  public boolean setPolicyNumber(String aPolicyNumber) {
    policyNumber = aPolicyNumber;
    return true;
  }

  public boolean setMonthlyPremium(String aMonthlyPremium) {
    monthlyPremium = aMonthlyPremium;
    return true;
  }

  public boolean setStarDate(String aStarDate) {
    starDate = aStarDate;
    return true;
  }

  public boolean setEndDate(String aEndDate) {
    endDate = aEndDate;
    return true;
  }

  public boolean setInsuradValue(String aInsuradValue) {
    insuradValue = aInsuradValue;
    return true;
  }

  public String getPolicyNumber() {
    return policyNumber;
  }

  public String getMonthlyPremium() {
    return monthlyPremium;
  }

  public String getStarDate() {
    return starDate;
  }

  public String getEndDate() {
    return endDate;
  }

  public String getInsuradValue() {
    return insuradValue;
  }

  public List<Transaction> getTransactions() {
    return transactions;
  }

  public Person getHolder() {
    return holder;
  }

  public Transaction addTransaction(String aDate,
    InsurancePolicy aInsurancePolicy) {
    Transaction newTransaction;
    newTransaction = new Transaction(aDate, this);
    if (!transactions.contains(newTransaction)) {
      registry.add(newTransaction);
      transactions.add(newTransaction);
    }
    return newTransaction;
  }

  public Transaction addTransaction(Transaction aTransaction) {
    if (!transactions.contains(aTransaction))
      transactions.add(aTransaction);
    return aTransaction;
  }

  /* This class does not drive Person and therefore sets the association unidirectionally.*/
  public void setHolder(Person aHolder) {
    holder = aHolder;
  }

  public void delete() {
    //Delete all many ends first.
    for (Transaction aTransaction : transactions) {
      aTransaction.delete();
    }
    transactions.clear();

    //Delete all 1 ends.
    holder.deleteInsurancePolicy(this);
  }

  public void deleteTransaction(Transaction aTransaction) {

    if (transactions.contains(aTransaction)) {
      transactions.remove(aTransaction);

      //registry.removeObj(registry.getKey(aTransaction));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (transactions.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("policyNumber", getPolicyNumber());
    obj.put("monthlyPremium", getMonthlyPremium());
    obj.put("starDate", getStarDate());
    obj.put("endDate", getEndDate());
    obj.put("insuradValue", getInsuradValue());
    return obj;
  }

}