package com.tandbergtv.watchpoint.pmm.util;

import java.util.ArrayList;
import java.util.Collection;

import com.tandbergtv.workflow.core.TaskVariable;
import com.tandbergtv.workflow.core.WorkflowTemplate;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.driver.service.ITemplateLoaderService;

public class TemplateProvider {

	/**
	 * Returns back a list of templates that have any of their start variables
	 * listed in the variable names passed in.
	 * 
	 * @param variablenames
	 * @return
	 */
	public Collection<WorkflowTemplate> getTemplates(Collection<String> variablenames) {
		Collection<WorkflowTemplate> templates = new ArrayList<WorkflowTemplate>();
		ITemplateLoaderService service = ServiceRegistry.getDefault().lookup(ITemplateLoaderService.class);
		
		if (service == null)
			throw new RuntimeException("Don't know how to get templates");
		
		for (WorkflowTemplate template : service.getLatestTemplates()) {
			for (TaskVariable variable : template.getStartTaskVariables()) {
				String name = variable.getVariableName();
				
				if (variablenames.contains(name)) {
					templates.add(template);
					continue;
				}
			}
		}
		
		return templates;
	}
}
