/*
 * Created on Jul 3, 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.Schedule;
import com.tandbergtv.watchpoint.pmm.entities.ScheduleStatus;
import com.tandbergtv.watchpoint.pmm.schedule.ISchedulePersistenceService;
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.WorkflowPayload;
import com.tandbergtv.workflow.message.WorkflowMessage.MessageType;

/**
 * Gets the Status for an existing Schedule.
 * 
 * @author Vijay Silva
 */
public class GetScheduleStatusMessageHandler implements MessageHandler {

	/* The Schedule Id Parameter in the input message */
	private static final String SCHEDULE_ID_PARAM = "scheduleId";

	/* The Title Status Parameter in the response message */
	private static final String SCHEDULE_STATUS_PARAM = "status";

	/**
	 * Gets the Schedule Id from the message and determines the status if such a schedule exists.
	 * 
	 * @see com.tandbergtv.watchpoint.pmm.communication.MessageHandler#handleMessage(com.tandbergtv.workflow.message.WorkflowMessage)
	 */
	public WorkflowMessage handleMessage(WorkflowMessage message) throws MessageHandlerException {
		/* Get the schedule Id */
		Long scheduleId = null;
		try {
			scheduleId = Util.getLongValue(message, SCHEDULE_ID_PARAM);
		} catch (Exception e) {
			WorkflowPayload payload = message.getPayload();
			String value = (payload != null) ? payload.getValue(SCHEDULE_ID_PARAM) : null;
			if (value == null)
				value = "";

			String msg = "The Schedule Id is not a valid number, received value: " + value;
			throw new MessageHandlerException(HandlerErrorCode.INVALID_INPUT, msg, e);
		}

		if (scheduleId == 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 schedule */
		ServiceRegistry registry = ServiceRegistry.getDefault();
		ISchedulePersistenceService service = registry.lookup(ISchedulePersistenceService.class);

		/* Get the Schedule using the Service */
		Schedule schedule = null;
		try {
			schedule = service.get(scheduleId);
		} catch (Exception e) {
			String msg = "Failed to read the Schedule from the persistence service, error: "
					+ e.getMessage();
			throw new MessageHandlerException(HandlerErrorCode.OBJECT_NOT_PRESENT, msg, e);
		}

		if (schedule == null) {
			String msg = "Did not find a Schedule with id=" + scheduleId
					+ " 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);
		ScheduleStatus status = schedule.getStatus();
		String statusValue = (status != null) ? status.toString() : "";
		response.putValue(SCHEDULE_STATUS_PARAM, statusValue);

		return response;
	}
}
