/**
 * UpdateTitleMessageHandler.java
 * Created on Jul 13, 2009
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.communication.handlers;

import com.tandbergtv.cms.portal.util.transaction.Transactional;
import com.tandbergtv.watchpoint.communication.Util;
import com.tandbergtv.watchpoint.pmm.communication.MessageHandler;
import com.tandbergtv.watchpoint.pmm.core.IPMMService;
import com.tandbergtv.watchpoint.pmm.core.MetadataMapInfo;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.message.WorkflowMessage;
import com.tandbergtv.workflow.message.WorkflowMessage.MessageType;

/**
 * @author Vlada Jakobac
 * 
 */
public class UpdateTitleMessageHandler implements MessageHandler {

	private static final String DUPLICATE_ASSETS = "duplicateAssets";
	private static final String DUPLICATE_ASSET_IDS = "duplicateAssetIds";
	private static final String METADATA_PATH = "filePath";
	private static final String ASSET_ID = "assetId";
	private static final String SOURCE_COMPONENT_NAME = "sourceComponentName";
	private static final String SOURCE_ENTITY_NAME = "sourceEntityName";
	private static final String SOURCEID = "sourceId";

	private static final String STATUS = "status";
	private static final String MAP_RESPONSE_STATUS_MAPPED = "Map Successful";
	private static final String MAP_RESPONSE_STATUS_NOTMAPPED = "Manual Mapping Required";
	private static final String MAP_RESPONSE_STATUS_DUPLICATE_ASSETS = "Manual Asset Mapping Required";
	private static final String MAPPED_TITLES = "mappedTitles";
	/*
	 * (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, METADATA_PATH, ASSET_ID,
				SOURCE_COMPONENT_NAME, SOURCEID);

		String metadataFilePath = Util.getStringValueTrimmed(message,
				METADATA_PATH);
		Long assetId = Util.getLongValue(message, ASSET_ID);
		String componentName = Util.getStringValueTrimmed(message,
				SOURCE_COMPONENT_NAME);
		String entityName = Util.getStringValueTrimmed(message,
				SOURCE_ENTITY_NAME);
		String sourceId = Util.getStringValueTrimmed(message, SOURCEID);

		IPMMService pmmService = ServiceRegistry.getDefault().lookup(
				IPMMService.class);
		MetadataMapInfo mapInfo = pmmService.metadataArrived(metadataFilePath, assetId,
				true, componentName, entityName, sourceId);

		WorkflowMessage response = new WorkflowMessage(message.getMessageUID(),
				message.getKey(), MessageType.ack);
		if (mapInfo.getStatus() == MetadataMapInfo.Status.TITLES_NOT_FOUND) {
			response.putValue(STATUS, MAP_RESPONSE_STATUS_NOTMAPPED);
		} else if (mapInfo.getStatus() == MetadataMapInfo.Status.DUPLICATE_ASSETS_FOUND) {
			response.putValue(STATUS, MAP_RESPONSE_STATUS_DUPLICATE_ASSETS);
			String duplicateAssets = mapInfo.getDuplicateAssets();
			int firstBarIndex = duplicateAssets.indexOf("|");
			if (firstBarIndex != -1){
				int lastBarIndex = duplicateAssets.lastIndexOf("|");
				if (lastBarIndex != -1) {
					String assetIdsList = duplicateAssets.substring(firstBarIndex+1, lastBarIndex);
					response.putValue(DUPLICATE_ASSET_IDS, assetIdsList);
					int bracketIndex = duplicateAssets.lastIndexOf("]");
					if (bracketIndex != -1) {
						String assetNameList = duplicateAssets.substring(lastBarIndex+1, bracketIndex);
						response.putValue(DUPLICATE_ASSETS, assetNameList);
					}				
				}
			}			
		} else {
			response.putValue(STATUS, MAP_RESPONSE_STATUS_MAPPED);			
		}
		response.putValue(MAPPED_TITLES, mapInfo.getMappedTitles());

		return response;
	}

}
