/*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.util.*;
import ElectorialSystem.*;
import ElectorialSystem.json.*;

public class Candidate {
  //Class datatypes
  private String name;

  private int phoneNumber;

  private String address;

  //Class association variables
  private List<Candidature> candidatures;

  //Registry of our system.
  ElectorialSystemRegistry registry = ElectorialSystemRegistry.getInstance();

  //Constructor
  public Candidate(String aName, int aPhoneNumber, String aAddress) {
    name = aName;
    phoneNumber = aPhoneNumber;
    address = aAddress;
    candidatures = new ArrayList<Candidature>();
    registry.add(candidatures);

  }

  public boolean setName(String aName) {
    name = aName;
    return true;
  }

  public boolean setPhoneNumber(int aPhoneNumber) {
    phoneNumber = aPhoneNumber;
    return true;
  }

  public boolean setAddress(String aAddress) {
    address = aAddress;
    return true;
  }

  public String getName() {
    return name;
  }

  public int getPhoneNumber() {
    return phoneNumber;
  }

  public String getAddress() {
    return address;
  }

  public List<Candidature> getCandidatures() {
    return candidatures;
  }

  public Candidature addCandidature(String aIsIncumbent,
    ElectionForPosition aElectionForPosition, Candidate aCandidate) {
    Candidature newCandidature;
    newCandidature = new Candidature(aIsIncumbent, aElectionForPosition, this);
    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;
  }

  public void delete() {
    //Delete all many ends first.
    for (Candidature aCandidature : candidatures) {
      aCandidature.delete();
    }
    candidatures.clear();

  }

  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));
    obj.put("name", getName());
    obj.put("phoneNumber", getPhoneNumber());
    obj.put("address", getAddress());
    return obj;
  }

}