package demo.transformers;

import org.apache.commons.lang.text.StrTokenizer;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

import demo.domain.LogMessage;

public class CsvToLogMessage extends AbstractTransformer {

	@Override
	protected Object doTransform(Object src, String encoding)
			throws TransformerException {

		logger.info("CsvToXml: doTransform: src = "+src);

		// TODO: add error handling - exercise for reader :-)

		// parse csv string into tokens and create LogMessage
		StrTokenizer tokenizer = StrTokenizer.getCSVInstance((String) src);
		String appName = tokenizer.nextToken();
		String timeStamp = tokenizer.nextToken();
		String logLevel = tokenizer.nextToken();
		String shortMsg = tokenizer.nextToken();
		String longMsg = tokenizer.nextToken();
		
		LogMessage lm = new LogMessage(appName, timeStamp, logLevel, shortMsg, longMsg);
		logger.info("CsvToLogMessage: transformed CSV message to LogMessage:\n"+lm);

		return lm;
	}

}
