package de.unistuttgart.quadrama.core;

import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.factory.AnnotationFactory;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;

import de.unistuttgart.ims.drama.api.Author;
import de.unistuttgart.ims.drama.api.Drama;
import de.unistuttgart.ims.drama.api.Translator;

public class SetDramaMetaData extends JCasAnnotator_ImplBase {

	public static final String PARAM_AUTHOR_NAME = "Author Name";
	public static final String PARAM_AUTHOR_PND = "Author PND";
	public static final String PARAM_DRAMAID = "Drama Id";
	public static final String PARAM_DRAMATITLE = "Drama Title";

	public static final String PARAM_REFERENCEDATE = "Reference Date";

	public static final String PARAM_TRANSLATION = "Translation";
	public static final String PARAM_TRANSLATOR_NAME = "Translator Name";
	public static final String PARAM_TRANSLATOR_PND = "Translator PND";

	@ConfigurationParameter(name = PARAM_AUTHOR_NAME, mandatory = false)
	String authorName = null;

	@ConfigurationParameter(name = PARAM_AUTHOR_PND, mandatory = false)
	String authorPnd = null;

	@ConfigurationParameter(name = PARAM_DRAMAID, mandatory = false)
	String dramaId = null;

	@ConfigurationParameter(name = PARAM_DRAMATITLE, mandatory = false)
	String dramaTitle = null;

	@ConfigurationParameter(name = PARAM_REFERENCEDATE, mandatory = false)
	int referenceDate = -1;

	@ConfigurationParameter(name = PARAM_TRANSLATION, mandatory = false)
	boolean translation = false;

	@ConfigurationParameter(name = PARAM_TRANSLATOR_NAME, mandatory = false)
	String translatorName = null;

	@ConfigurationParameter(name = PARAM_TRANSLATOR_PND, mandatory = false)
	String translatorPnd = null;

	@Override
	public void process(JCas jcas) throws AnalysisEngineProcessException {
		Drama drama = JCasUtil.selectSingle(jcas, Drama.class);

		Author author = AnnotationFactory.createAnnotation(jcas, 0, 1, Author.class);
		if (authorName != null)
			author.setName(authorName);
		if (authorPnd != null)
			author.setPnd(authorPnd);
		if (dramaId != null)
			drama.setDocumentId(dramaId);
		if (referenceDate > 0)
			drama.setReferenceDate(referenceDate);
		if (dramaTitle != null)
			drama.setDocumentTitle(dramaTitle);

		drama.setTranslation(translation);
		Translator translator;
		if (JCasUtil.exists(jcas, Translator.class)) {
			translator = JCasUtil.selectSingle(jcas, Translator.class);
		} else {
			translator = AnnotationFactory.createAnnotation(jcas, 0, 1, Translator.class);
		}
		if (translatorName != null)
			translator.setName(translatorName);
		if (translatorPnd != null)
			translator.setPnd(translatorPnd);

	}
}

@@ -2427,16 +2427,38 @@
 ation);%0A
+%09%09if (translation) %7B%0A%09
 %09%09Transl
@@ -2472,24 +2472,25 @@
 anslator;%0A%09%09
+%09
 if (JCasUtil
@@ -2524,16 +2524,17 @@
 ass)) %7B%0A
+%09
 %09%09%09trans
@@ -2590,16 +2590,17 @@
 ass);%0A%09%09
+%09
 %7D else %7B
@@ -2600,16 +2600,17 @@
  else %7B%0A
+%09
 %09%09%09trans
@@ -2685,18 +2685,20 @@
 ass);%0A%09%09
+%09
 %7D%0A
+%09
 %09%09if (tr
@@ -2711,32 +2711,33 @@
 orName != null)%0A
+%09
 %09%09%09translator.se
@@ -2755,24 +2755,25 @@
 latorName);%0A
+%09
 %09%09if (transl
@@ -2792,16 +2792,17 @@
 ull)%0A%09%09%09
+%09
 translat
@@ -2827,14 +2827,17 @@
 orPnd);%0A
+%09%09%7D
 %0A%09%7D%0A%7D%0A

fix for spurious translator entries