package eu.dnetlib.enabling.aas.ismock;

import java.util.List;

import org.springframework.context.ApplicationContext;

import eu.dnetlib.enabling.aas.service.A2Constants;
import eu.dnetlib.enabling.aas.utils.ResourcesGenerator;
import eu.dnetlib.enabling.aas.utils.SpringUtils;
import eu.dnetlib.enabling.is.store.rmi.ISStoreException;
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;


/**
 * Initializes exist db by creating required collections in database.
 * @author mhorst
 *
 */
public class ExistDbInitializer {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws ISStoreException {
		System.out.println("Creating required collections...");
		ApplicationContext ctx = SpringUtils.getSpringContext(SpringUtils.DEFAULT_INTEGRATION_RESOURCE);
		ISStoreService storeService = (ISStoreService) ctx.getBean("MockISStoreService");

		List<String> colNames = storeService.getFileColls(); 
				
		if (!isNameAmongNames(colNames, A2Constants.IS_COLLECTION_SECPROF_PATH.substring("db/".length()))) {
			System.out.println("SecProf collection not found! Initializing SecProf collection...");
			boolean res = storeService.createFileColl(A2Constants.IS_COLLECTION_SECPROF_PATH);
			if (res)
				System.out.println("SecProf collection initialized successfully.");
			else
				System.err.println("SecProf collection NOT initialized! Unknown error occured!");
		} else {
			System.out.println("No need for creating SecProf collection.");
		}

		if (!isNameAmongNames(colNames, A2Constants.IS_COLLECTION_SECCTX_PATH.substring("db/".length()))) {
			System.out.println("SecCtx collection not found! Initializing SecCtx collection...");
			boolean res = storeService.createFileColl(A2Constants.IS_COLLECTION_SECCTX_PATH);
			if (res)
				System.out.println("SecCtx collection initialized successfully.");
			else
				System.err.println("SecCtx collection NOT initialized! Unknown error occured!");
		} else {
			System.out.println("No need for creating SecCtx collection.");
		}
		
		if (!isNameAmongNames(colNames, A2Constants.IS_COLLECTION_SECPOLICY_PATH.substring("db/".length()))) {
			System.out.println("SecurityPolicies collection not found! Initializing SecurityPolicies collection...");
			boolean res = storeService.createFileColl(A2Constants.IS_COLLECTION_SECPOLICY_PATH);
			if (res)
				System.out.println("SecurityPolicies collection initialized successfully.");
			else
				System.err.println("SecurityPolicies collection NOT initialized! Unknown error occured!");
		} else {
			System.out.println("No need for creating SecurityPolicies collection.");
		}
		
		//registering test file col
		if (!isNameAmongNames(colNames, ResourcesGenerator.IS_COLLECTION_DRIVER_TEST_PATH.substring("db/".length()))) {
			System.out.println("DriverTest collection not found! Initializing DriverTest collection...");
			boolean res = storeService.createFileColl(ResourcesGenerator.IS_COLLECTION_DRIVER_TEST_PATH);
			if (res)
				System.out.println("DriverTest collection initialized successfully.");
			else
				System.err.println("DriverTest collection NOT initialized! Unknown error occured!");
		} else {
			System.out.println("No need for creating DriverTest collection.");
		}
		
	}
	
	static boolean isNameAmongNames(List<String> names, String name) {
		if (names==null || name==null)
			return false;
		for (String currentName : names) {
			if (name.equals(currentName))
				return true;
		}
		return false;
	}

}
