/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 12:12:16*/
/*This class drives: */

package ElectorialSystem.core;

import java.util.*;
import ElectorialSystem.*;
import ElectorialSystem.json.*;

public class Candidature {
  //Class datatypes
  private String isIncumbent;

  //Class association variables
  private ElectionForPosition electionForPosition;

  private Candidate candidate;

  private List<Voter> voters;

  //Registry of our system.
  ElectorialSystemRegistry registry = ElectorialSystemRegistry.getInstance();

  //Constructor
  public Candidature(String aIsIncumbent,
    ElectionForPosition aElectionForPosition, Candidate aCandidate) {
    isIncumbent = aIsIncumbent;
    electionForPosition = aElectionForPosition;
    electionForPosition.addCandidature(this);

    candidate = aCandidate;
    candidate.addCandidature(this);

    voters = new ArrayList<Voter>();
    registry.add(voters);

  }

  public boolean setIsIncumbent(String aIsIncumbent) {
    isIncumbent = aIsIncumbent;
    return true;
  }

  public String getIsIncumbent() {
    return isIncumbent;
  }

  public ElectionForPosition getElectionForPosition() {
    return electionForPosition;
  }

  public Candidate getCandidate() {
    return candidate;
  }

  public List<Voter> getVoters() {
    return voters;
  }

  public Voter addVoter(String aName, String aAddress,
    PollInElection aPollInElection, ElectoralDistrict aElectoralDistrict) {
    Voter newVoter;
    newVoter = new Voter(aName, aAddress, aPollInElection, 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 ElectionForPosition and therefore sets the association unidirectionally.*/
  public void setElectionForPosition(ElectionForPosition aElectionForPosition) {
    electionForPosition = aElectionForPosition;
  }

  /* This class does not drive Candidate and therefore sets the association unidirectionally.*/
  public void setCandidate(Candidate aCandidate) {
    candidate = aCandidate;
  }

  public void delete() {
    //Delete all 1 ends.
    electionForPosition.deleteCandidature(this);
    candidate.deleteCandidature(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() {
    return true;
  }

  /***********************************
   * 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("isIncumbent", getIsIncumbent());
    return obj;
  }

}