/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 11:46:04*/
/*This class drives: PhoneCall, TelephoneNumber*/

package telesystem.core;

import java.util.*;
import java.sql.Time;
import telesystem.*;
import telesystem.json.*;

public class TelephoneNumber {
  //Class datatypes
  private String digits;

  //Class association variables
  private List<PhoneCall> belongsTos;

  private List<PhoneCall> involvedIns;

  private List<TelephoneNumber> componentNumbers;

  private TelephoneNumber telephoneNumber;

  private VoiceMailBox voiceMailBox;

  private List<Feature> features;

  private List<PhoneLine> phoneLines;

  //Registry of our system.
  TelesystemRegistry registry = TelesystemRegistry.getInstance();

  //Constructor
  public TelephoneNumber(String aDigits, VoiceMailBox aVoiceMailBox) {
    digits = aDigits;
    belongsTos = new ArrayList<PhoneCall>();
    registry.add(belongsTos);

    involvedIns = new ArrayList<PhoneCall>();
    registry.add(involvedIns);

    componentNumbers = new ArrayList<TelephoneNumber>();
    registry.add(componentNumbers);

    voiceMailBox = aVoiceMailBox;
    voiceMailBox.addTelephoneNumber(this);

    features = new ArrayList<Feature>();
    registry.add(features);

    phoneLines = new ArrayList<PhoneLine>();
    registry.add(phoneLines);

  }

  public boolean setDigits(String aDigits) {
    digits = aDigits;
    return true;
  }

  public String getDigits() {
    return digits;
  }

  public List<PhoneCall> getBelongsTos() {
    return belongsTos;
  }

  public List<PhoneCall> getInvolvedIns() {
    return involvedIns;
  }

  public List<TelephoneNumber> getComponentNumbers() {
    return componentNumbers;
  }

  public TelephoneNumber getTelephoneNumber() {
    return telephoneNumber;
  }

  public VoiceMailBox getVoiceMailBox() {
    return voiceMailBox;
  }

  public List<Feature> getFeatures() {
    return features;
  }

  public List<PhoneLine> getPhoneLines() {
    return phoneLines;
  }

  public PhoneCall addBelongsTo(String aIsOnHold, String aStartTime,
    String aDuration, TelephoneNumber aOriginator) {
    PhoneCall newPhoneCall;
    newPhoneCall = new PhoneCall(aIsOnHold, aStartTime, aDuration, aOriginator);
    if (!belongsTos.contains(newPhoneCall)) {
      registry.add(newPhoneCall);
      belongsTos.add(newPhoneCall);
    }
    return newPhoneCall;
  }

  public PhoneCall addBelongsTo(PhoneCall aBelongsTo) {
    if (!belongsTos.contains(aBelongsTo))
      belongsTos.add(aBelongsTo);
    return aBelongsTo;
  }

  public PhoneCall addInvolvedIn(String aIsOnHold, String aStartTime,
    String aDuration, TelephoneNumber aOriginator) {
    PhoneCall newPhoneCall;
    newPhoneCall = new PhoneCall(aIsOnHold, aStartTime, aDuration, aOriginator);
    if (!involvedIns.contains(newPhoneCall)) {
      registry.add(newPhoneCall);
      involvedIns.add(newPhoneCall);
    }
    return newPhoneCall;
  }

  public PhoneCall addInvolvedIn(PhoneCall aInvolvedIn) {
    if (!involvedIns.contains(aInvolvedIn))
      involvedIns.add(aInvolvedIn);
    return aInvolvedIn;
  }

  public TelephoneNumber addComponentNumber(String aDigits,
    VoiceMailBox aVoiceMailBox) {
    TelephoneNumber newTelephoneNumber;
    newTelephoneNumber = new TelephoneNumber(aDigits, aVoiceMailBox);
    if (!componentNumbers.contains(newTelephoneNumber)) {
      registry.add(newTelephoneNumber);
      componentNumbers.add(newTelephoneNumber);
    }
    return newTelephoneNumber;
  }

  public TelephoneNumber addComponentNumber(TelephoneNumber aComponentNumber) {
    if (!componentNumbers.contains(aComponentNumber))
      componentNumbers.add(aComponentNumber);
    return aComponentNumber;
  }

  public Feature addFeature(String aDescription) {
    Feature newFeature;
    newFeature = new Feature(aDescription);
    if (!features.contains(newFeature)) {
      registry.add(newFeature);
      features.add(newFeature);
    }
    return newFeature;
  }

  public Feature addFeature(Feature aFeature) {
    if (!features.contains(aFeature))
      features.add(aFeature);
    return aFeature;
  }

  public PhoneLine addPhoneLine(String aLocation, String aDigOrAnalog) {
    PhoneLine newPhoneLine;
    newPhoneLine = new PhoneLine(aLocation, aDigOrAnalog);
    if (!phoneLines.contains(newPhoneLine)) {
      registry.add(newPhoneLine);
      phoneLines.add(newPhoneLine);
    }
    return newPhoneLine;
  }

  public PhoneLine addPhoneLine(PhoneLine aPhoneLine) {
    if (!phoneLines.contains(aPhoneLine))
      phoneLines.add(aPhoneLine);
    return aPhoneLine;
  }

  /* This class does not drive TelephoneNumber and therefore sets the association unidirectionally.*/
  public void setTelephoneNumber(TelephoneNumber aTelephoneNumber) {
    telephoneNumber = aTelephoneNumber;
  }

  /* This class does not drive VoiceMailBox and therefore sets the association unidirectionally.*/
  public void setVoiceMailBox(VoiceMailBox aVoiceMailBox) {
    voiceMailBox = aVoiceMailBox;
  }

  public void delete() {
    //Delete all many ends first.
    for (PhoneCall aPhoneCall : belongsTos) {
      aPhoneCall.delete();
    }
    belongsTos.clear();

    //Delete all many ends first.
    for (PhoneCall aPhoneCall : involvedIns) {
      aPhoneCall.delete();
    }
    involvedIns.clear();

    //Delete all many ends first.
    for (TelephoneNumber aTelephoneNumber : componentNumbers) {
      aTelephoneNumber.delete();
    }
    componentNumbers.clear();

    //Delete all 1 ends.
    telephoneNumber.deleteComponentNumber(this);
    voiceMailBox.deleteTelephoneNumber(this);
  }

  public void deleteBelongsTo(PhoneCall aPhoneCall) {

    if (belongsTos.contains(aPhoneCall)) {
      belongsTos.remove(aPhoneCall);

      //registry.removeObj(registry.getKey(aPhoneCall));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteInvolvedIn(PhoneCall aPhoneCall) {

    if (involvedIns.contains(aPhoneCall)) {
      involvedIns.remove(aPhoneCall);

      //registry.removeObj(registry.getKey(aPhoneCall));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteComponentNumber(TelephoneNumber aTelephoneNumber) {

    if (componentNumbers.contains(aTelephoneNumber)) {
      componentNumbers.remove(aTelephoneNumber);

      //registry.removeObj(registry.getKey(aTelephoneNumber));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteTelephoneNumber(TelephoneNumber aTelephoneNumber) {

    if (aTelephoneNumber.equals(telephoneNumber)) {
      telephoneNumber = null;

      registry.removeObj(registry.getKey(aTelephoneNumber));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteFeature(Feature aFeature) {

    if (features.contains(aFeature)) {
      features.remove(aFeature);

      //registry.removeObj(registry.getKey(aFeature));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deletePhoneLine(PhoneLine aPhoneLine) {

    if (phoneLines.contains(aPhoneLine)) {
      phoneLines.remove(aPhoneLine);

      //registry.removeObj(registry.getKey(aPhoneLine));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (belongsTos.size() == 0 && involvedIns.size() == 0
      && componentNumbers.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("digits", getDigits());
    return obj;
  }

}