package com.tandbergtv.workflow.adaptor.handler;


import org.apache.log4j.Logger;
import org.w3c.dom.Document;

import com.tandbergtv.workflow.adaptor.conf.IHandlerConfiguration;
import com.tandbergtv.workflow.adaptor.handler.AbstractHandler;
import com.tandbergtv.workflow.adaptor.handler.HandlerException;
import com.tandbergtv.workflow.message.HTTPMessage;
import com.tandbergtv.workflow.message.HTTPPayload;
import com.tandbergtv.workflow.message.IMessage;
import com.tandbergtv.workflow.message.WPCLCommand;
import com.tandbergtv.workflow.message.WorkflowMessage;
import com.tandbergtv.workflow.message.WorkflowMessage.MessageType;
import com.tandbergtv.workflow.message.util.Marshaller;
import com.tandbergtv.workflow.message.util.Unmarshaller;
import com.tandbergtv.workflow.util.XMLDocumentUtility;

/**
 * Handler for the XPortPackager asynchronous callback messages to 
 * covert to NACK response based on some response flag.
 * 
 * @author Kinjal Mehta
 */
public class XportPackagerCallbackHandler extends AbstractHandler {
	
	private final String XPORT_PACKAGER_RESPONSE = "XPORT_PACKAGER_RESPONSE_PARAMETER";
	
	private final String XPORT_PACKAGER_FAILURE_STRING = "XPORT_PACKAGER_RESPONSE_VALUE";
	
	private final String WPCL_COMMAND = "COMMAND"; 
	
	private static Logger logger = Logger.getLogger(XportPackagerCallbackHandler.class);
		
	/**
	 * Creates a XportPackagerCallbackHandler
	 */
	public XportPackagerCallbackHandler() {
		super();
	}

	/**
	 * Creates a XportPackagerCallbackHandler
	 * @param conf
	 */
	public XportPackagerCallbackHandler(IHandlerConfiguration conf) {
		super(conf);
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.workflow.adaptor.handler.IHandler#process(com.tandbergtv.workflow.message.IMessage)
	 */
	public IMessage process(IMessage msg) throws HandlerException {
		HTTPMessage result = null; 
		
		try {
			
			Document document = XMLDocumentUtility.loadXml(msg.getPayload().getContent());
			WorkflowMessage message = Unmarshaller.unmarshal(document);
			
			/* set command */
			String command = getConfiguration().getParameterValue(WPCL_COMMAND);
			message.setCommand(new WPCLCommand(command));
			
			String responseParameter = getConfiguration().getParameterValue(XPORT_PACKAGER_RESPONSE);
			String responseValue = getConfiguration().getParameterValue(XPORT_PACKAGER_FAILURE_STRING);
			
			logger.info("Response parameter read = " + responseParameter);
			if (message.getValue(responseParameter).equalsIgnoreCase(responseValue))
				message.setType(MessageType.nack);
										
			document = Marshaller.newMarshaller().marshal(message);
			
			HTTPPayload payload = new HTTPPayload(XMLDocumentUtility.convertToString(document));
			result = new HTTPMessage(payload);
		} catch (Exception ex) {
			throw new HandlerException(ex);
		}

		return result;
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.workflow.adaptor.handler.IHandler#postProcess(com.tandbergtv.workflow.message.IMessage)
	 */
	public IMessage postProcess(IMessage msg) throws HandlerException {
		return msg;
	}
}
