/**
 * PitchDestinationDataProvider.java
 * Created Jun 12, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.web.title.search;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.tandbergtv.watchpoint.pmm.entities.ContainerType;
import com.tandbergtv.watchpoint.pmm.entities.IContainer;
import com.tandbergtv.watchpoint.pmm.entities.Partner;
import com.tandbergtv.watchpoint.pmm.entities.PartnerType;
import com.tandbergtv.watchpoint.pmm.web.util.CommonUtils;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.core.service.cache.ICacheService;
import com.tandbergtv.workflow.web.page.ISearchFieldDataProvider;

/**
 * Gets the list of destinations for a pitch schedule
 * 
 * @author Sahil Verma
 */
public class PitchDestinationDataProvider implements ISearchFieldDataProvider {

	/* (non-Javadoc)
	 * @see com.tandbergtv.workflow.web.page.ISearchFieldDataProvider#getData()
	 */
	@SuppressWarnings("unchecked")
	public Map<String, String> getData() {
		Map<String, String> data = new HashMap<String, String>();
		ICacheService<IContainer> cache =
			(ICacheService<IContainer>)ServiceRegistry.getDefault().lookup("Container Cache");
		
		/* The key is the context ID */
		for (Serializable key : cache.getKeys()) {
			IContainer container = cache.get(key);
			String name = container.getContainerName();
			
			if (container.getContainerType() == ContainerType.PARTNER) {
				Partner partner = Partner.class.cast(container);
				
				if (partner.getType() == PartnerType.DISTRIBUTION)
					data.put(String.valueOf(key), name);
			} else if (container.getContainerType() == ContainerType.SERVICE) {
				data.put(String.valueOf(key), name);
			}
		}
		return CommonUtils.sort(data);
	}
}
