package uk.org.taverna.scufl2.api.annotation;

import java.net.URI;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import uk.org.taverna.scufl2.api.common.AbstractNamedChild;
import uk.org.taverna.scufl2.api.common.Named;
import uk.org.taverna.scufl2.api.common.Visitor;
import uk.org.taverna.scufl2.api.common.WorkflowBean;
import uk.org.taverna.scufl2.api.property.PropertyObject;
import uk.org.taverna.scufl2.api.property.PropertyResource;

/**
 * An annotation of a WorkflowBean.
 * <p>
 * Modelled after http://openannotation.org/spec/core/20120509
 * 
 * @author Stian Soiland-Reyes
 *
 */
public class Annotation extends AbstractNamedChild implements Named {
	private Calendar annotated;
	private URI annotator;
	private List<PropertyObject> bodyStatements = new ArrayList<PropertyObject>();
	private Calendar generated = new GregorianCalendar();
	private URI generator;
	private WorkflowBean target;

	public Annotation(WorkflowBean target) {
		setTarget(target);
	}

	public Annotation() {
	}
	
	@Override
	public boolean accept(Visitor visitor) {
		visitor.visitEnter(this);
		for (WorkflowBean b : getBodyStatements()) {
			if (! b.accept(visitor)) {
				break;
			}
		}
		return visitor.visitLeave(this);		
	}
	public Calendar getAnnotated() {
		return annotated;
	}
	public URI getAnnotator() {
		return annotator;
	}
	public List<PropertyObject> getBodyStatements() {
		return bodyStatements;
	}
	public Calendar getGenerated() {
		return generated;
	}
	public URI getGenerator() {
		return generator;
	}
	public WorkflowBean getTarget() {
		if (target == null) {
			return this;
		}
		return target;
	}
	public void setAnnotated(Calendar annotated) {
		this.annotated = annotated;
	}
	public void setAnnotator(URI annotator) {
		this.annotator = annotator;
	}
	public void setBodyStatements(List<PropertyObject> bodyStatements) {
		if (bodyStatements == null) { 
			throw new NullPointerException("bodyStatements can't be null");
		}
		this.bodyStatements = bodyStatements;
	}

	public void setGenerated(Calendar generated) {
		this.generated = generated;
	}
	public void setGenerator(URI generator) {
		this.generator = generator;
	}
	public void setTarget(WorkflowBean target) {
		if (target == null) {
			throw new NullPointerException("Target can't be null");
		}
		this.target = target;
	}

	
}

@@ -241,16 +241,120 @@
 amed
+;%0D%0Aimport uk.org.taverna.scufl2.api.common.AbstractNamedChild;%0D%0Aimport uk.org.taverna.scufl2.api.common.
 Child;%0D%0A
 impo
@@ -341,32 +341,32 @@
 .common.Child;%0D%0A
-
 import uk.org.ta
@@ -502,16 +502,76 @@
 wBean;%0D%0A
+import uk.org.taverna.scufl2.api.container.WorkflowBundle;%0D%0A
 import u
@@ -889,21 +889,16 @@
 actNamed
-Child
  impleme
@@ -906,16 +906,39 @@
 ts Named
+, Child%3CWorkflowBundle%3E
  %7B%0D%0A%09pri
@@ -1146,24 +1146,24 @@
 generator;%0D%0A
-
 %09private Wor
@@ -1180,16 +1180,49 @@
 target;%0D
+%0A%09private WorkflowBundle parent;%0D
 %0A%0D%0A%09publ
@@ -2682,14 +2682,365 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09@Override%0D%0A%09public WorkflowBundle getParent() %7B%0D%0A%09%09return this.parent;%0D%0A%09%7D%0D%0A%0D%0A%09@Override%0D%0A%09public void setParent(WorkflowBundle parent) %7B%0D%0A%09%09if (this.parent != null && this.parent != parent) %7B%0D%0A%09%09%09this.parent.getAnnotations().remove(this);%0D%0A%09%09%7D%0D%0A%09%09this.parent = parent;%0D%0A%09%09if (parent != null) %7B%0D%0A%09%09%09parent.getAnnotations().add(this);%0D%0A%09%09%7D%0D%0A%0D%0A%09%7D%0D%0A%0D%0A%0D%0A
 %09%0D%0A%7D%0D%0A

Make Annotation a parent so that it can get a URI