/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 12:12:16*/
/*This class drives: Voter*/

package ElectorialSystem.core;

import java.sql.Date;
import java.util.*;
import ElectorialSystem.*;
import ElectorialSystem.json.*;

public class PollInElection {
  //Class datatypes
  private int number;

  //Class association variables
  private Election election;

  private List<Voter> voters;

  //Registry of our system.
  ElectorialSystemRegistry registry = ElectorialSystemRegistry.getInstance();

  //Constructor
  public PollInElection(int aNumber, Election aElection) {
    number = aNumber;
    election = aElection;
    election.addPollInElection(this);

    voters = new ArrayList<Voter>();
    registry.add(voters);

  }

  public boolean setNumber(int aNumber) {
    number = aNumber;
    return true;
  }

  public int getNumber() {
    return number;
  }

  public Election getElection() {
    return election;
  }

  public List<Voter> getVoters() {
    return voters;
  }

  public Voter addVoter(String aName, String aAddress,
    PollInElection aPollInElection, ElectoralDistrict aElectoralDistrict) {
    Voter newVoter;
    newVoter = new Voter(aName, aAddress, this, aElectoralDistrict);
    if (!voters.contains(newVoter)) {
      registry.add(newVoter);
      voters.add(newVoter);
    }
    return newVoter;
  }

  public Voter addVoter(Voter aVoter) {
    if (!voters.contains(aVoter))
      voters.add(aVoter);
    return aVoter;
  }

  /* This class does not drive Election and therefore sets the association unidirectionally.*/
  public void setElection(Election aElection) {
    election = aElection;
  }

  public void delete() {
    //Delete all many ends first.
    for (Voter aVoter : voters) {
      aVoter.delete();
    }
    voters.clear();

    //Delete all 1 ends.
    election.deletePollInElection(this);
  }

  public void deleteVoter(Voter aVoter) {

    if (voters.contains(aVoter)) {
      voters.remove(aVoter);

      //registry.removeObj(registry.getKey(aVoter));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (voters.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("number", getNumber());
    return obj;
  }

}