/*
 * Created on Oct 7, 2010
 * 
 * (C) Copyright TANDBERG Television Inc.
 */

package com.ericsson.metadatamanager.specimpl.osvod;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.tandbergtv.metadatamanager.spec.IIdentifier;
import com.tandbergtv.metadatamanager.specimpl.IdentifierBase;
import com.tandbergtv.metadatamanager.util.MappingFileParser;

/**
 * Identifier for new VOD Identifier
 * 
 * @author Vijay Silva
 */
public class OpenStreamVODIdentifier extends IdentifierBase implements IIdentifier {

	/* Serialization UID */
	private static final long serialVersionUID = -1933336632860939768L;

	/**
	 * The XPath for the Asset ID field in the VOD Specification
	 */
	public static String ASSETID_XPATH = "Asset_ID";

	/**
	 * The XPath for the Provider ID field in the VOD Specification
	 */
	public static String PROVIDERID_XPATH = "Provider_ID";

	/* The key values */
	private String assetId;
	private String providerId;

	/**
	 * Default Constructor
	 */
	public OpenStreamVODIdentifier() {
	}

	/**
	 * Constructor
	 * 
	 * @param assetId The asset ID
	 * @param providerId The provider ID
	 */
	public OpenStreamVODIdentifier(String assetId, String providerId) {
		setAssetId(assetId);
		setProviderId(providerId);
	}

	/*
	 * Get the mapping between the
	 * 
	 * @see com.tandbergtv.metadatamanager.spec.IIdentifier#getTTVPaths()
	 */
	@Override
	public Map<String, String> getTTVPaths() {
		Map<String, String> ttvPaths = new HashMap<String, String>();
		URL mappingResourceURL = getMappingResourceUrl();

		/* Get the TTV XPath for the Asset ID XPath for this specification */
		String assetXpath = MappingFileParser.getMapping(mappingResourceURL, ASSETID_XPATH);
		if (assetXpath == null || assetXpath.trim().length() == 0) {
			throw new RuntimeException("Identifying field: " + ASSETID_XPATH
			        + " cannot be mapped to TTV xpath.");
		}
		ttvPaths.put(ASSETID_XPATH, assetXpath);

		/* Get the TTV XPath for the Provider ID XPath for this specification */
		String providerXpath = MappingFileParser.getMapping(mappingResourceURL, PROVIDERID_XPATH);
		if (providerXpath == null || providerXpath.trim().length() == 0) {
			throw new RuntimeException("Identifying field: " + PROVIDERID_XPATH
			        + " cannot be mapped to TTV xpath.");
		}
		ttvPaths.put(PROVIDERID_XPATH, providerXpath);

		return ttvPaths;
	}

	/*
	 * Get the map of the metadata fields that make up the key for the asset. The map contains the
	 * VOD Specification field name mapped to the value.
	 * 
	 * @see com.tandbergtv.metadatamanager.spec.IIdentifier#getSpecIdentifiers()
	 */
	@Override
	public Map<String, String> getSpecIdentifiers() {
		Map<String, String> map = new HashMap<String, String>();
		map.put(PROVIDERID_XPATH, providerId);
		map.put(ASSETID_XPATH, assetId);
		return map;
	}

	/*
	 * Update the identifier values from the map. The map contains the VOD Specification field name
	 * mapped to the value.
	 * 
	 * @see com.tandbergtv.metadatamanager.spec.IIdentifier#setSpecIdentifiers(java.util.Map)
	 */
	@Override
	public void setSpecIdentifiers(Map<String, String> identifiers) {
		/* Get the asset and provider Id from the identifiers map */
		String assetId = null;
		String providerId = null;
		if (identifiers != null) {
			assetId = identifiers.get(ASSETID_XPATH);
			providerId = identifiers.get(PROVIDERID_XPATH);
		}

		/* Update the asset and provider ID */
		setAssetId(assetId);
		setProviderId(providerId);
	}

	/*
	 * Check if the AssetID and ProviderID values are not blank.
	 * 
	 * @see com.tandbergtv.metadatamanager.spec.IIdentifier#isValidIdentifier()
	 */
	@Override
	public boolean isValidIdentifier() {
		return ((!isBlank(getAssetId())) && (!isBlank(getProviderId())));
	}

	/**
	 * @return the assetId
	 */
	public String getAssetId() {
		return assetId;
	}

	/**
	 * @param assetId the assetId to set
	 */
	public void setAssetId(String assetId) {
		this.assetId = assetId;
	}

	/**
	 * @return the providerId
	 */
	public String getProviderId() {
		return providerId;
	}

	/**
	 * @param providerId the providerId to set
	 */
	public void setProviderId(String providerId) {
		this.providerId = providerId;
	}

	/*
	 * Check if the value is blank
	 */
	private boolean isBlank(String value) {
		return (value == null || value.trim().length() == 0);
	}

	/*
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		/* Use default hash code is identifier is not valid */
		if (!isValidIdentifier()) {
			return super.hashCode();
		}

		/* Generate the hash code based on assetId and providerId */
		final int prime = 37;
		int result = 3;
		result = prime * result + ((assetId == null) ? 0 : assetId.hashCode());
		result = prime * result + ((providerId == null) ? 0 : providerId.hashCode());
		return result;
	}

	/*
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		/* Same object in memory */
		if (this == obj)
			return true;

		/* Check if this identifier is valid, only then compare */
		if (!this.isValidIdentifier()) {
			return false;
		}

		/* Compare with the other identifier */
		if (obj instanceof OpenStreamVODIdentifier) {
			OpenStreamVODIdentifier other = (OpenStreamVODIdentifier) obj;
			if (assetId != null && providerId != null) {
				return (assetId.equals(other.getAssetId()) && providerId.equals(other
				        .getProviderId()));
			}
		}

		return false;
	}
}
