package org.cleartk.classifier;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.resource.ResourceInitializationException;
import org.cleartk.util.UIMAUtil;

public class DataWriterAnnotator<OUTCOME_TYPE> extends InstanceConsumer<OUTCOME_TYPE> {

	/**
	 * The name of the directory where the training data will be written.
	 */
	public static final String PARAM_OUTPUT_DIRECTORY = "OutputDirectory";
	
	public static final String PARAM_DATAWRITER_FACTORY_CLASS = "DataWriterFactoryClass";

	
	@Override
	public void initialize(UimaContext context) throws ResourceInitializationException {
		super.initialize(context);
		
		String outputDirectoryPath = (String) UIMAUtil.getRequiredConfigParameterValue(context, PARAM_OUTPUT_DIRECTORY);
		File outputDirectory = new File(outputDirectoryPath);

		// Instantiate the data writer
		try {
			String dataWriterFactoryClassName = (String) UIMAUtil.getRequiredConfigParameterValue(context, PARAM_DATAWRITER_FACTORY_CLASS);
			Class<? extends DataWriterFactory> dataWriterFactoryClass = (Class<? extends DataWriterFactory>) Class.forName(dataWriterFactoryClassName);
			DataWriterFactory dataWriterFactory = dataWriterFactoryClass.newInstance();
			this.dataWriter = (DataWriter<OUTCOME_TYPE>) dataWriterFactory.getDataWriter(outputDirectory);
		}
		catch (ClassNotFoundException e) {
			throw new ResourceInitializationException(e);
		}
		catch (InstantiationException e) {
			throw new ResourceInitializationException(e);
		}
		catch (IllegalAccessException e) {
			throw new ResourceInitializationException(e);
		}		
	}

	
	@Override
	public void collectionProcessComplete() throws AnalysisEngineProcessException {
		super.collectionProcessComplete();

		try {
			dataWriter.finish();
		}
		catch (IOException e) {
			throw new AnalysisEngineProcessException(e);
		}
	}

	
	public OUTCOME_TYPE consume(Instance<OUTCOME_TYPE> instance) {
		try {
			dataWriter.write(instance);
		}
		catch (IOException e) {
			throw new RuntimeException(e);
		}
		return null;
	}

	public List<?> consumeSequence(List<?> instances) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean expectsOutcomes() {
		return true;
	}
	
	private DataWriter<OUTCOME_TYPE> dataWriter;

}

@@ -263,32 +263,72 @@
 ationException;%0A
+import org.cleartk.util.ReflectionUtil;%0A
 import org.clear
@@ -345,16 +345,16 @@
 MAUtil;%0A
-
 %0Apublic 
@@ -1020,23 +1020,25 @@
 r%0A%09%09
-try %7B%0A%09%09%09String
+DataWriterFactory
  dat
@@ -1055,707 +1055,203 @@
 tory
-ClassName = (String) UIMAUtil.getRequiredConfigParameterValue(context, PARAM_DATAWRITER_FACTORY_CLASS);%0A%09%09%09Class%3C? extends DataWriterFactory%3E dataWriterFactoryClass = (Class%3C? extends DataWriterFactory%3E) Class.forName(dataWriterFactoryClassName);%0A%09%09%09DataWriterFactory dataWriterFactory = dataWriterFactoryClass.newInstance();%0A%09%09%09this.dataWriter = (DataWriter%3COUTCOME_TYPE%3E) dataWriterFactory.getDataWriter(outputDirectory);%0A%09%09%7D%0A%09%09catch (ClassNotFoundException e) %7B%0A%09%09%09throw new ResourceInitializationException(e);%0A%09%09%7D%0A%09%09catch (InstantiationException e) %7B%0A%09%09%09throw new ResourceInitializationException(e);%0A%09%09%7D%0A%09%09catch (IllegalAccessException e) %7B%0A%09%09%09throw new ResourceInitializationException(e);%0A%09%09%7D%09%09
+ = UIMAUtil.create(%0A%09%09%09%09context, PARAM_DATAWRITER_FACTORY_CLASS, DataWriterFactory.class);%0A%09%09this.dataWriter = ReflectionUtil.uncheckedCast(%0A%09%09%09%09dataWriterFactory.getDataWriter(outputDirectory));
 %0A%09%7D%0A

Simplify DataWriterAnnotator using UIMAUtil.create.