/**
   * Returns the collection of annotations to add.
   *
   * @see AnnotationUI#getAnnotationToSave()
   */
  protected List<AnnotationData> getAnnotationToSave() {
    List<AnnotationData> l = new ArrayList<AnnotationData>();
    DocComponent doc;
    Iterator<DocComponent> i;
    Object object;
    AnnotationData annotation;
    Iterator j;
    List<Long> ids;
    Collection original;
    long id;
    if (tagFlag) {
      original = model.getTags();
      j = original.iterator();
      ids = new ArrayList<Long>();
      while (j.hasNext()) {
        annotation = (AnnotationData) j.next();
        ids.add(annotation.getId());
      }

      i = tagsDocList.iterator();
      /*
      while (i.hasNext()) {
      	doc = i.next();
      	object = doc.getData();
      	if (object instanceof TagAnnotationData) {
      		annotation = (AnnotationData) object;
      		id = annotation.getId();
      		if (!ids.contains(id) && !added.contains(id)) {
      			added.add(id);
      			l.add(annotation);
      		}
      	}
      }
      */
      Map<Long, Integer> map = new HashMap<Long, Integer>();
      Map<Long, AnnotationData> annotations = new HashMap<Long, AnnotationData>();
      Integer count;
      while (i.hasNext()) {
        doc = i.next();
        object = doc.getData();
        if (object instanceof TagAnnotationData) {
          annotation = (AnnotationData) object;
          id = annotation.getId();
          if (!ids.contains(id)) {
            l.add(annotation);
          } else {
            count = map.get(id);
            if (count != null) {
              count++;
              map.put(id, count);
            } else {
              count = 1;
              annotations.put(id, annotation);
              map.put(id, count);
            }
          }
        }
      }

      // check the count
      Entry entry;
      Iterator k = map.entrySet().iterator();
      while (k.hasNext()) {
        entry = (Entry) k.next();
        count = (Integer) entry.getValue();
        if (count > 1) l.add(annotations.get(entry.getKey()));
      }
    }
    i = tagsDocList.iterator();
    while (i.hasNext()) {
      doc = i.next();
      object = doc.getData();
      if (doc.hasBeenModified()) {
        annotation = (AnnotationData) object;
        if (!l.contains(annotation)) l.add(annotation);
      }
    }
    if (docFlag) {
      original = model.getAttachments();
      j = original.iterator();
      ids = new ArrayList<Long>();
      while (j.hasNext()) {
        ids.add(((AnnotationData) j.next()).getId());
      }
      i = filesDocList.iterator();
      while (i.hasNext()) {
        doc = i.next();
        object = doc.getData();
        if (object instanceof FileAnnotationData) {
          annotation = (AnnotationData) object;
          id = annotation.getId();
          if (!ids.contains(id)) l.add(annotation);
        }
      }
    }
    if (selectedValue != initialValue) l.add(new RatingAnnotationData(selectedValue));

    if (!model.hasBeenPublished()) {
      if (publishedBox.isSelected()) {
        BooleanAnnotationData data = new BooleanAnnotationData(true);
        data.setNameSpace(BooleanAnnotationData.INSIGHT_PUBLISHED_NS);
        l.add(data);
      }
    }
    return l;
  }
  /**
   * Returns the collection of annotation to remove.
   *
   * @see AnnotationUI#getAnnotationToRemove()
   */
  protected List<Object> getAnnotationToRemove() {
    List<Object> l = new ArrayList<Object>();
    if (selectedValue != initialValue && selectedValue == 0) {
      RatingAnnotationData rating = model.getUserRatingAnnotation();
      if (rating != null) l.add(rating);
    }
    List<Long> idsToKeep;
    DocComponent doc;
    long id;

    Object object;
    Iterator<DocComponent> i;
    Collection original;
    Iterator j;
    if (tagFlag && !model.isMultiSelection()) {
      idsToKeep = new ArrayList<Long>();

      TagAnnotationData tag;
      i = tagsDocList.iterator();
      while (i.hasNext()) {
        doc = i.next();
        object = doc.getData();
        if (object instanceof TagAnnotationData) {
          tag = (TagAnnotationData) object;
          id = tag.getId();
          if (id > 0) idsToKeep.add(id);
        }
      }

      original = model.getTags();
      j = original.iterator();
      while (j.hasNext()) {
        tag = (TagAnnotationData) j.next();
        id = tag.getId();
        if (!idsToKeep.contains(id)) // && model.isAnnotationToDelete(tag))
        l.add(tag);
      }
    }
    if (docFlag) {
      idsToKeep = new ArrayList<Long>();
      i = filesDocList.iterator();
      FileAnnotationData fa;
      while (i.hasNext()) {
        doc = i.next();
        object = doc.getData();
        if (object instanceof FileAnnotationData) {
          fa = (FileAnnotationData) object;
          id = fa.getId();
          if (id > 0) idsToKeep.add(id);
        }
      }
      original = model.getAttachments();
      j = original.iterator();
      while (j.hasNext()) {
        fa = (FileAnnotationData) j.next();
        id = fa.getId();
        if (!idsToKeep.contains(id)) //  && model.isAnnotationToDelete(fa))
        l.add(fa);
      }
    }
    if (model.hasBeenPublished()) {
      if (!publishedBox.isSelected()) {
        BooleanAnnotationData b = model.getPublishedAnnotation();
        if (b.getValue().booleanValue()) l.add(b);
      }
    }
    return l;
  }