/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 12:30:06*/
/*This class drives: Employee, Division*/

package BankingSystem.core.tangableResources;

import java.util.*;
import BankingSystem.*;
import BankingSystem.core.humanResources.*;
import BankingSystem.core.intangableResources.*;
import BankingSystem.json.*;

public class Division {
  //Class datatypes
  private String name;

  //Class association variables
  private List<Employee> employees;

  private List<Division> subDivisions;

  private Division division;

  //Registry of our system.
  BankingSystemRegistry registry = BankingSystemRegistry.getInstance();

  //Constructor
  public Division(String aName) {
    name = aName;
    employees = new ArrayList<Employee>();
    registry.add(employees);

    subDivisions = new ArrayList<Division>();
    registry.add(subDivisions);

  }

  public boolean setName(String aName) {
    name = aName;
    return true;
  }

  public String getName() {
    return name;
  }

  public List<Employee> getEmployees() {
    return employees;
  }

  public List<Division> getSubDivisions() {
    return subDivisions;
  }

  public Division getDivision() {
    return division;
  }

  public Employee addEmployee(Person aPerson, Division aDivision) {
    Employee newEmployee;
    newEmployee = new Employee(aPerson, this);
    if (!employees.contains(newEmployee)) {
      registry.add(newEmployee);
      employees.add(newEmployee);
    }
    return newEmployee;
  }

  public Employee addEmployee(Employee aEmployee) {
    if (!employees.contains(aEmployee))
      employees.add(aEmployee);
    return aEmployee;
  }

  public Division addSubDivision(String aName) {
    Division newDivision;
    newDivision = new Division(aName);
    if (!subDivisions.contains(newDivision)) {
      registry.add(newDivision);
      subDivisions.add(newDivision);
    }
    return newDivision;
  }

  public Division addSubDivision(Division aSubDivision) {
    if (!subDivisions.contains(aSubDivision))
      subDivisions.add(aSubDivision);
    return aSubDivision;
  }

  /* This class does not drive Division and therefore sets the association unidirectionally.*/
  public void setDivision(Division aDivision) {
    division = aDivision;
  }

  public void delete() {
    //Delete all many ends first.
    for (Employee aEmployee : employees) {
      aEmployee.delete();
    }
    employees.clear();

    //Delete all many ends first.
    for (Division aDivision : subDivisions) {
      aDivision.delete();
    }
    subDivisions.clear();

    //Delete all 1 ends.
    division.deleteSubDivision(this);
  }

  public void deleteEmployee(Employee aEmployee) {

    if (employees.contains(aEmployee)) {
      employees.remove(aEmployee);

      //registry.removeObj(registry.getKey(aEmployee));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteSubDivision(Division aDivision) {

    if (subDivisions.contains(aDivision)) {
      subDivisions.remove(aDivision);

      //registry.removeObj(registry.getKey(aDivision));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteDivision(Division aDivision) {

    if (aDivision.equals(division)) {
      division = null;

      registry.removeObj(registry.getKey(aDivision));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (employees.size() == 0 && subDivisions.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());
    return obj;
  }

}