/**
 * GetAllFilesMessageHandler.java
 * Created on Jun 26, 2009
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.contentmgmt.communication.handlers;

import com.tandbergtv.cms.portal.util.transaction.Transactional;
import com.tandbergtv.metadatamanager.model.Asset;
import com.tandbergtv.metadatamanager.model.File;
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.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;

/**
 * @author Vlada Jakobac
 * 
 */
public class GetAllFilesMessageHandler implements MessageHandler {

	private static final String LIST_OF_FILES = "listOfFiles";
	private static final String TITLE_ID = "titleId";

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.tandbergtv.watchpoint.pmm.communication.MessageHandler#handleMessage
	 * (com.tandbergtv.workflow.message.WorkflowMessage)
	 */
	@Override
	@Transactional
	public WorkflowMessage handleMessage(WorkflowMessage message)
			throws Exception {

		Util.validateRequired(message, TITLE_ID);

		/* Get the title Id */
		Long titleId = Util.getLongValue(message, TITLE_ID);

		/* 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.getWithHistory(titleId);
		} catch (Exception e) {
			String msg = "Failed to read the Title, 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);
		}

		/* Get the root asset from the title and get all files */
		Asset groupAsset = title.getAsset();
		if (groupAsset == null) {
			String msg = "Did not get a group object for a given title.";
			throw new MessageHandlerException(
					HandlerErrorCode.OBJECT_NOT_PRESENT, msg);
		}

		/* Build a comma separated list of all the files */
		StringBuilder listOfFiles = new StringBuilder();
		boolean first = true;
		for (File file : groupAsset.getAllDescendantFiles()) {
			if (file.getURL() == null)
				continue;

			if (!first)
				listOfFiles.append(",");
			else
				first = false;
			listOfFiles.append(file.getURL());
		}

		/* Build the response Workflow Message */
		IMessageUID uid = message.getMessageUID();
		IMessageKey key = message.getKey();
		WorkflowMessage response = new WorkflowMessage(uid, key,
				MessageType.ack);
		response.putValue(LIST_OF_FILES, listOfFiles.toString());

		return response;
	}
}
