/**
 * TitleSearchService.java
 * Created Apr 30, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.title.search;

import static com.tandbergtv.watchpoint.pmm.title.search.TitleSearchKey.TITLE_PROVIDER_ID;

import java.util.Collection;

import com.tandbergtv.cms.portal.util.transaction.Transactional;
import com.tandbergtv.watchpoint.pmm.entities.Title;
import com.tandbergtv.watchpoint.pmm.title.provider.ITitleProviderRegistry;
import com.tandbergtv.watchpoint.pmm.title.provider.ITitleSearchStrategy;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.driver.search.ValueParameter;
import com.tandbergtv.workflow.util.SearchCriteria;

/**
 * Default implementation of the title search service
 * 
 * @author Sahil Verma
 */
public class TitleSearchService implements ITitleSearchService {

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.title.search.ITitleSearchService#getSearchResult(com.tandbergtv.workflow.util.SearchCriteria)
	 */
	@Transactional
	public TitleSearchResult getSearchResult(SearchCriteria criteria) {
		ITitleSearchStrategy strategy = this.getTitleSearchStrategy(criteria);
		return new TitleSearchResult(strategy.search(criteria), strategy.count(criteria));
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.title.search.ITitleSearchService#count(com.tandbergtv.workflow.util.SearchCriteria)
	 */
	@Transactional
	public int count(SearchCriteria criteria) {
		return getTitleSearchStrategy(criteria).count(criteria);
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.title.search.ITitleSearchService#search(com.tandbergtv.workflow.util.SearchCriteria)
	 */
	@Transactional
	public Collection<Title> search(SearchCriteria criteria) {
		return getTitleSearchStrategy(criteria).search(criteria);
	}
	
	/**
	 * Determines which {@link ITitleSearchStrategy} to use based on the search criteria
	 * 
	 * @param criteria
	 * @return
	 */
	private ITitleSearchStrategy getTitleSearchStrategy(SearchCriteria criteria) {
		ValueParameter parameter = criteria.getParameter(TITLE_PROVIDER_ID.toString(), ValueParameter.class);
		ITitleProviderRegistry registry = ServiceRegistry.getDefault().lookup(ITitleProviderRegistry.class);
		
		if (parameter == null)
			return registry.getDefaultProvider().getSearchStrategy();
		
		/* This parameter is of no further use to the actual search strategy */
		criteria.removeParameter(TITLE_PROVIDER_ID.toString());
		
		String id = parameter.getValue().toString();
		
		return registry.getProvider(id).getSearchStrategy();
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.workflow.core.service.Service#getServiceName()
	 */
	public String getServiceName() {
		return "Title Search";
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.workflow.core.service.ServiceLifecycle#start()
	 */
	public void start() {
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.workflow.core.service.ServiceLifecycle#stop()
	 */
	public void stop() {
	}
}
