/**
 * PlannerSourceDataProvider.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;

/**
 * Returns a list of source partners 
 * 
 * @author Sahil Verma
 */
public class PlannerSourceDataProvider 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");
		
		for (Serializable key : cache.getKeys()) {
			IContainer container = cache.get(key);
			
			if (container.getContainerType() == ContainerType.PARTNER) {
				Partner partner = Partner.class.cast(container);
				
				if (partner.getType() == PartnerType.SOURCE)
					data.put(String.valueOf(partner.getId()), partner.getName());
			}
		}
		
		return CommonUtils.sort(data);
	}
}
