/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.datastore;

import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.config.ClientPreferences;
import com.b2international.snowowl.datastore.cdo.ICDOConnectionManager;
import com.b2international.snowowl.datastore.serviceconfig.ServiceConfigJobManager;
import com.b2international.snowowl.datastore.serviceconfig.ServiceConfigJobManager.IServiceConfigJobChangeListener;
import com.b2international.snowowl.datastore.tasks.ITaskStateManager;
import com.b2international.snowowl.datastore.tasks.Task;
import com.b2international.snowowl.datastore.tasks.TaskManager;

/**
 * The activator class controls the plug-in life cycle
 */
public class DatastoreActivator implements BundleActivator, IServiceConfigJobChangeListener {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.b2international.terminology.datastore"; //$NON-NLS-1$

	private static BundleContext context;

	public static BundleContext getContext() {
		return context;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(final BundleContext context) throws Exception {
		DatastoreActivator.context = context;
		ServiceConfigJobManager.INSTANCE.addListener(this);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(final BundleContext context) throws Exception {
		DatastoreActivator.context = null;
		ServiceConfigJobManager.INSTANCE.removeListener(this);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.b2international.snowowl.datastore.serviceconfig.ServiceConfigJobManager.IServiceConfigJobChangeListener#done(org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	public void done(final IProgressMonitor monitor) {
		
		final IBranchPathMap userBranchPathMap = getTaskStateManager().getBranchPathMapConfiguration(getUserId(), false);
		final TaskManager taskManager = new TaskManager(new UserBranchPathMap(userBranchPathMap.asMap(getRepositoryUuids())));
		ApplicationContext.getInstance().registerService(TaskManager.class, taskManager);
		final ClientPreferences clientConfiguration = ApplicationContext.getInstance().getService(ClientPreferences.class);
		
		if (clientConfiguration.hasLastActiveTaskId()) {
			final String lastActiveTaskId = clientConfiguration.getLastActiveTaskId();
			final Task lastActiveTask = ApplicationContext.getInstance().getService(TaskManager.class).getTask(lastActiveTaskId);
			
			if (null != lastActiveTask) {
				taskManager.activateTask(lastActiveTask, monitor);
			}
		}
	}
	
	/*returns with the task state manager service*/
	private ITaskStateManager getTaskStateManager() {
		return ApplicationContext.getInstance().getService(ITaskStateManager.class);
	}
	
	/*returns with the user ID*/
	private String getUserId() {
		return getConnectionManager().getUserId();
	}

	/*returns with the connection manager*/
	private ICDOConnectionManager getConnectionManager() {
		return ApplicationContext.getInstance().getService(ICDOConnectionManager.class);
	}
	
	/*returns with the repository UUIDs*/
	private Set<String> getRepositoryUuids() {
		return getConnectionManager().uuidKeySet();
	}
	
	
}
@@ -673,81 +673,8 @@
 e;%0A%0A
-import java.util.Set;%0A%0Aimport org.eclipse.core.runtime.IProgressMonitor;%0A
 impo
@@ -758,598 +758,8 @@
 t;%0A%0A
-import com.b2international.snowowl.core.ApplicationContext;%0Aimport com.b2international.snowowl.core.config.ClientPreferences;%0Aimport com.b2international.snowowl.datastore.cdo.ICDOConnectionManager;%0Aimport com.b2international.snowowl.datastore.serviceconfig.ServiceConfigJobManager;%0Aimport com.b2international.snowowl.datastore.serviceconfig.ServiceConfigJobManager.IServiceConfigJobChangeListener;%0Aimport com.b2international.snowowl.datastore.tasks.ITaskStateManager;%0Aimport com.b2international.snowowl.datastore.tasks.Task;%0Aimport com.b2international.snowowl.datastore.tasks.TaskManager;%0A%0A
 /**%0A
@@ -879,41 +879,8 @@
 ator
-, IServiceConfigJobChangeListener
  %7B%0A%0A
@@ -1110,2234 +1110,224 @@
 %0A%09%0A%09
-/*%0A%09 * (non-Javadoc)%0A%09 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)%0A%09 */%0A%09public void start(final BundleContext context) throws Exception %7B%0A%09%09DatastoreActivator.context = context;%0A%09%09ServiceConfigJobManager.INSTANCE.addListener(this);%0A%09%7D%0A%09%0A%09/*%0A%09 * (non-Javadoc)%0A%09 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)%0A%09 */%0A%09public void stop(final BundleContext context) throws Exception %7B%0A%09%09DatastoreActivator.context = null;%0A%09%09ServiceConfigJobManager.INSTANCE.removeListener(this);%0A%09%7D%0A%09%0A%09/*%0A%09 * (non-Javadoc)%0A%09 * @see com.b2international.snowowl.datastore.serviceconfig.ServiceConfigJobManager.IServiceConfigJobChangeListener#done(org.eclipse.core.runtime.IProgressMonitor)%0A%09 */%0A%09@Override%0A%09public void done(final IProgressMonitor monitor) %7B%0A%09%09%0A%09%09final IBranchPathMap userBranchPathMap = getTaskStateManager().getBranchPathMapConfiguration(getUserId(), false);%0A%09%09final TaskManager taskManager = new TaskManager(new UserBranchPathMap(userBranchPathMap.asMap(getRepositoryUuids())));%0A%09%09ApplicationContext.getInstance().registerService(TaskManager.class, taskManager);%0A%09%09final ClientPreferences clientConfiguration = ApplicationContext.getInstance().getService(ClientPreferences.class);%0A%09%09%0A%09%09if (clientConfiguration.hasLastActiveTaskId()) %7B%0A%09%09%09final String lastActiveTaskId = clientConfiguration.getLastActiveTaskId();%0A%09%09%09final Task lastActiveTask = ApplicationContext.getInstance().getService(TaskManager.class).getTask(lastActiveTaskId);%0A%09%09%09%0A%09%09%09if (null != lastActiveTask) %7B%0A%09%09%09%09taskManager.activateTask(lastActiveTask, monitor);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A%09/*returns with the task state manager service*/%0A%09private ITaskStateManager getTaskStateManager() %7B%0A%09%09return ApplicationContext.getInstance().getService(ITaskStateManager.class);%0A%09%7D%0A%09%0A%09/*returns with the user ID*/%0A%09private String getUserId() %7B%0A%09%09return getConnectionManager().getUserId();%0A%09%7D%0A%0A%09/*returns with the connection manager*/%0A%09private ICDOConnectionManager getConnectionManager() %7B%0A%09%09return ApplicationContext.getInstance().getService(ICDOConnectionManager.class);%0A%09%7D%0A%09%0A%09/*returns with the repository UUIDs*/%0A%09private Set%3CString%3E getRepositoryUuids() %7B%0A%09%09return getConnectionManager().uuidKeySet();%0A%09%7D%0A%09
+public void start(final BundleContext context) throws Exception %7B%0A%09%09DatastoreActivator.context = context;%0A%09%7D%0A%09%0A%09public void stop(final BundleContext context) throws Exception %7B%0A%09%09DatastoreActivator.context = null;%0A%09%7D
 %0A%09%0A%7D

remove activation of lastActiveTask after init