/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 12:12:16*/
/*This class drives: PollInElection, ElectionForPosition*/

package ElectorialSystem.core;

import java.sql.Date;
import java.util.*;
import ElectorialSystem.*;
import ElectorialSystem.json.*;

public class Election {
  //Class datatypes
  private Date date;

  //Class association variables
  private List<PollInElection> pollInElections;

  private List<ElectionForPosition> electionForPositions;

  //Registry of our system.
  ElectorialSystemRegistry registry = ElectorialSystemRegistry.getInstance();

  //Constructor
  public Election(Date aDate) {
    date = aDate;
    pollInElections = new ArrayList<PollInElection>();
    registry.add(pollInElections);

    electionForPositions = new ArrayList<ElectionForPosition>();
    registry.add(electionForPositions);

  }

  public boolean setDate(Date aDate) {
    date = aDate;
    return true;
  }

  public Date getDate() {
    return date;
  }

  public List<PollInElection> getPollInElections() {
    return pollInElections;
  }

  public List<ElectionForPosition> getElectionForPositions() {
    return electionForPositions;
  }

  public PollInElection addPollInElection(int aNumber, Election aElection) {
    PollInElection newPollInElection;
    newPollInElection = new PollInElection(aNumber, this);
    if (!pollInElections.contains(newPollInElection)) {
      registry.add(newPollInElection);
      pollInElections.add(newPollInElection);
    }
    return newPollInElection;
  }

  public PollInElection addPollInElection(PollInElection aPollInElection) {
    if (!pollInElections.contains(aPollInElection))
      pollInElections.add(aPollInElection);
    return aPollInElection;
  }

  public ElectionForPosition addElectionForPosition(Position aPosition) {
    ElectionForPosition newElectionForPosition;
    newElectionForPosition = new ElectionForPosition(this, aPosition);
    if (!electionForPositions.contains(newElectionForPosition)) {
      registry.add(newElectionForPosition);
      electionForPositions.add(newElectionForPosition);
    }
    return newElectionForPosition;
  }

  public ElectionForPosition addElectionForPosition(
    ElectionForPosition aElectionForPosition) {
    if (!electionForPositions.contains(aElectionForPosition))
      electionForPositions.add(aElectionForPosition);
    return aElectionForPosition;
  }

  public void delete() {
    //Delete all many ends first.
    for (PollInElection aPollInElection : pollInElections) {
      aPollInElection.delete();
    }
    pollInElections.clear();

    //Delete all many ends first.
    for (ElectionForPosition aElectionForPosition : electionForPositions) {
      aElectionForPosition.delete();
    }
    electionForPositions.clear();

  }

  public void deletePollInElection(PollInElection aPollInElection) {

    if (pollInElections.contains(aPollInElection)) {
      pollInElections.remove(aPollInElection);

      //registry.removeObj(registry.getKey(aPollInElection));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteElectionForPosition(ElectionForPosition aElectionForPosition) {

    if (electionForPositions.contains(aElectionForPosition)) {
      electionForPositions.remove(aElectionForPosition);

      //registry.removeObj(registry.getKey(aElectionForPosition));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (pollInElections.size() == 0 && electionForPositions.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("date", getDate());
    return obj;
  }

}