/*
 * Created on Jul 2, 2008 (C) Copyright TANDBERG Television Ltd.
 */

package com.tandbergtv.watchpoint.pmm.communication.handlers;

import com.tandbergtv.watchpoint.communication.Util;
import com.tandbergtv.watchpoint.pmm.communication.HandlerErrorCode;
import com.tandbergtv.watchpoint.pmm.communication.MessageHandler;
import com.tandbergtv.watchpoint.pmm.communication.MessageHandlerException;
import com.tandbergtv.watchpoint.pmm.entities.Title;
import com.tandbergtv.watchpoint.pmm.entities.TitleStatus;
import com.tandbergtv.watchpoint.pmm.title.ITitlePersistenceService;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.message.IMessageKey;
import com.tandbergtv.workflow.message.IMessageUID;
import com.tandbergtv.workflow.message.WorkflowMessage;
import com.tandbergtv.workflow.message.WorkflowMessage.MessageType;

/**
 * Gets the Title Status for the input Title Id. Should be used specifically for Root Titles.
 * 
 * @author Vijay Silva
 */
public class GetTitleStatusMessageHandler implements MessageHandler {

	/* The Title Id Parameter in the input message */
	private static final String TITLE_ID_PARAM = "titleId";

	/* The Title Status Parameter in the response message */
	private static final String TITLE_STATUS_PARAM = "status";

	/**
	 * Gets the Title Id from the message and determines the title status if such a title exists.
	 * 
	 * @see com.tandbergtv.watchpoint.pmm.communication.MessageHandler#handleMessage(com.tandbergtv.workflow.message.WorkflowMessage)
	 */
	public WorkflowMessage handleMessage(WorkflowMessage message) throws MessageHandlerException {
		/* Get the title Id */
		Long titleId = null;
		try {
			titleId = Util.getLongValue(message, TITLE_ID_PARAM);
		} catch (Exception e) {
			String value = message.getValue(TITLE_ID_PARAM);
			if (value == null)
				value = "";
			String msg = "The Title Id is not a valid number, received value: " + value;
			throw new MessageHandlerException(HandlerErrorCode.INVALID_INPUT, msg, e);
		}

		if (titleId == null) {
			String msg = "The Title Id is missing or blank.";
			throw new MessageHandlerException(HandlerErrorCode.INVALID_INPUT, msg);
		}

		/* Get the Service Registry to allow fetching of the title */
		ServiceRegistry registry = ServiceRegistry.getDefault();
		ITitlePersistenceService service = registry.lookup(ITitlePersistenceService.class);

		/* Get the Title using the Service */
		Title title = null;
		try {
			title = service.get(titleId);
		} catch (Exception e) {
			String msg = "Failed to read the Title from the persistence service, error: "
					+ e.getMessage();
			throw new MessageHandlerException(HandlerErrorCode.OBJECT_NOT_PRESENT, msg, e);
		}

		if (title == null) {
			String msg = "Did not get a Title from the persistence service.";
			throw new MessageHandlerException(HandlerErrorCode.OBJECT_NOT_PRESENT, msg);
		}

		/* Build the response Workflow Message */
		IMessageUID uid = message.getMessageUID();
		IMessageKey key = message.getKey();
		WorkflowMessage response = new WorkflowMessage(uid, key, MessageType.ack);
		TitleStatus status = title.getStatus();
		String statusValue = (status != null) ? status.toString() : "";
		response.putValue(TITLE_STATUS_PARAM, statusValue);

		return response;
	}
}
