/*This code was generated using the UMPLE modeling language!
 Date of generation: 2008/07/30 10:29:33*/
/*This class drives: PartOfPublication, Copy*/

package library.core;

import java.util.*;
import library.*;
import library.json.*;

public class EditionOrIssue {
  //Class datatypes
  private String issueNumber;

  private String publishedDate;

  //Class association variables
  private List<PartOfPublication> tableOfContentss;

  private List<Copy> copys;

  private Publisher publisher;

  private List<Author> authors;

  private Publication publication;

  //Registry of our system.
  LibraryRegistry registry = LibraryRegistry.getInstance();

  //Constructor
  public EditionOrIssue(String aIssueNumber, String aPublishedDate,
    Publisher aPublisher, Publication aPublication) {
    issueNumber = aIssueNumber;
    publishedDate = aPublishedDate;
    tableOfContentss = new ArrayList<PartOfPublication>();
    registry.add(tableOfContentss);

    copys = new ArrayList<Copy>();
    registry.add(copys);

    publisher = aPublisher;
    publisher.addEditionOrIssue(this);

    authors = new ArrayList<Author>();
    registry.add(authors);

    publication = aPublication;
    publication.addEditionOrIssue(this);

  }

  public boolean setIssueNumber(String aIssueNumber) {
    issueNumber = aIssueNumber;
    return true;
  }

  public boolean setPublishedDate(String aPublishedDate) {
    publishedDate = aPublishedDate;
    return true;
  }

  public String getIssueNumber() {
    return issueNumber;
  }

  public String getPublishedDate() {
    return publishedDate;
  }

  public List<PartOfPublication> getTableOfContentss() {
    return tableOfContentss;
  }

  public List<Copy> getCopys() {
    return copys;
  }

  public Publisher getPublisher() {
    return publisher;
  }

  public List<Author> getAuthors() {
    return authors;
  }

  public Publication getPublication() {
    return publication;
  }

  public PartOfPublication addTableOfContents(String aTitle,
    String aPageNumber, PartOfPublication aPartOfPublication,
    EditionOrIssue aEditionOrIssue) {
    PartOfPublication newPartOfPublication;
    newPartOfPublication =
      new PartOfPublication(aTitle, aPageNumber, aPartOfPublication, this);
    if (!tableOfContentss.contains(newPartOfPublication)) {
      registry.add(newPartOfPublication);
      tableOfContentss.add(newPartOfPublication);
    }
    return newPartOfPublication;
  }

  public PartOfPublication addTableOfContents(PartOfPublication aTableOfContents) {
    if (!tableOfContentss.contains(aTableOfContents))
      tableOfContentss.add(aTableOfContents);
    return aTableOfContents;
  }

  public Copy addCopy(String aBarCodeNumber, EditionOrIssue aEditionOrIssue) {
    Copy newCopy;
    newCopy = new Copy(aBarCodeNumber, this);
    if (!copys.contains(newCopy)) {
      registry.add(newCopy);
      copys.add(newCopy);
    }
    return newCopy;
  }

  public Copy addCopy(Copy aCopy) {
    if (!copys.contains(aCopy))
      copys.add(aCopy);
    return aCopy;
  }

  public Author addAuthor(String aName) {
    Author newAuthor;
    newAuthor = new Author(aName);
    if (!authors.contains(newAuthor)) {
      registry.add(newAuthor);
      authors.add(newAuthor);
    }
    return newAuthor;
  }

  public Author addAuthor(Author aAuthor) {
    if (!authors.contains(aAuthor))
      authors.add(aAuthor);
    return aAuthor;
  }

  /* This class does not drive Publisher and therefore sets the association unidirectionally.*/
  public void setPublisher(Publisher aPublisher) {
    publisher = aPublisher;
  }

  /* This class does not drive Publication and therefore sets the association unidirectionally.*/
  public void setPublication(Publication aPublication) {
    publication = aPublication;
  }

  public void delete() {
    //Delete all many ends first.
    for (PartOfPublication aPartOfPublication : tableOfContentss) {
      aPartOfPublication.delete();
    }
    tableOfContentss.clear();

    //Delete all many ends first.
    for (Copy aCopy : copys) {
      aCopy.delete();
    }
    copys.clear();

    //Delete all 1 ends.
    publisher.deleteEditionOrIssue(this);
    publication.deleteEditionOrIssue(this);
  }

  public void deleteTableOfContents(PartOfPublication aPartOfPublication) {

    if (tableOfContentss.contains(aPartOfPublication)) {
      tableOfContentss.remove(aPartOfPublication);

      //registry.removeObj(registry.getKey(aPartOfPublication));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteCopy(Copy aCopy) {

    if (copys.contains(aCopy)) {
      copys.remove(aCopy);

      //registry.removeObj(registry.getKey(aCopy));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public void deleteAuthor(Author aAuthor) {

    if (authors.contains(aAuthor)) {
      authors.remove(aAuthor);

      //registry.removeObj(registry.getKey(aAuthor));
    } else
    //Throw an UmpleException .. to be implemented.
    {
    }
  }

  public boolean areManyEndsNull() {
    if (tableOfContentss.size() == 0 && copys.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("issueNumber", getIssueNumber());
    obj.put("publishedDate", getPublishedDate());
    return obj;
  }

}