/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 12:12:16*/
/*This class drives: Candidature*/

package ElectorialSystem.core;

import java.sql.Date;
import java.util.*;
import ElectorialSystem.*;
import ElectorialSystem.json.*;

public class ElectionForPosition {
  //Class datatypes

  //Class association variables
  private Election election;

  private List<Candidature> candidatures;

  private Position position;

  //Registry of our system.
  ElectorialSystemRegistry registry = ElectorialSystemRegistry.getInstance();

  //Constructor
  public ElectionForPosition(Election aElection, Position aPosition) {
    election = aElection;
    election.addElectionForPosition(this);

    candidatures = new ArrayList<Candidature>();
    registry.add(candidatures);

    position = aPosition;
    position.addElectionForPosition(this);

  }

  public Election getElection() {
    return election;
  }

  public List<Candidature> getCandidatures() {
    return candidatures;
  }

  public Position getPosition() {
    return position;
  }

  public Candidature addCandidature(String aIsIncumbent,
    ElectionForPosition aElectionForPosition, Candidate aCandidate) {
    Candidature newCandidature;
    newCandidature = new Candidature(aIsIncumbent, this, aCandidate);
    if (!candidatures.contains(newCandidature)) {
      registry.add(newCandidature);
      candidatures.add(newCandidature);
    }
    return newCandidature;
  }

  public Candidature addCandidature(Candidature aCandidature) {
    if (!candidatures.contains(aCandidature))
      candidatures.add(aCandidature);
    return aCandidature;
  }

  /* This class does not drive Election and therefore sets the association unidirectionally.*/
  public void setElection(Election aElection) {
    election = aElection;
  }

  /* This class does not drive Position and therefore sets the association unidirectionally.*/
  public void setPosition(Position aPosition) {
    position = aPosition;
  }

  public void delete() {
    //Delete all many ends first.
    for (Candidature aCandidature : candidatures) {
      aCandidature.delete();
    }
    candidatures.clear();

    //Delete all 1 ends.
    election.deleteElectionForPosition(this);
    position.deleteElectionForPosition(this);
  }

  public void deleteCandidature(Candidature aCandidature) {

    if (candidatures.contains(aCandidature)) {
      candidatures.remove(aCandidature);

      //registry.removeObj(registry.getKey(aCandidature));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (candidatures.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;
  }

}