/**
 * ScheduleActivator.java
 * Created Jun 2, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.schedule;

import com.tandbergtv.watchpoint.pmm.schedule.notify.IScheduleNotifier;
import com.tandbergtv.watchpoint.pmm.schedule.notify.ScheduleNotifierService;
import com.tandbergtv.watchpoint.pmm.schedule.search.IScheduleSearchService;
import com.tandbergtv.watchpoint.pmm.schedule.search.ScheduleSearchService;
import com.tandbergtv.workflow.core.service.Service;
import com.tandbergtv.workflow.core.service.ServiceRegistry;

/**
 * Activator for schedule management
 * 
 * @author Sahil Verma
 */
public class ScheduleActivator {
	
	public void start() {
		addService(new SchedulePersistenceService());
		addService(new ScheduleSearchService());
		addService(new ScheduleNotifierService());
	}
	
	public void stop() {
		removeService(IScheduleNotifier.class);
		removeService(IScheduleSearchService.class);
		removeService(ISchedulePersistenceService.class);
	}
	
	/**
	 * Adds the specified service
	 * 
	 * @param service
	 */
	private void addService(Service service) {
		ServiceRegistry.getDefault().register(service.getServiceName(), service);
		
		service.start();
	}
	
	/**
	 * Removes the specified service
	 * 
	 * @param service
	 */
	private void removeService(Class<? extends Service> clazz) {
		ServiceRegistry registry = ServiceRegistry.getDefault();
		Service service = registry.lookup(clazz);
		
		if (service != null) {
			service.stop();
			registry.unregister(service);
		}
	}	
}
