package org.biomoby.registry.sync;

import org.biomoby.registry.sync.handler.AbstractMobyHandler;
import org.biomoby.registry.sync.handler.DataTypeHandler;
import org.biomoby.registry.sync.handler.NamespaceHandler;
import org.biomoby.registry.sync.handler.ServiceHandler;
import org.biomoby.registry.sync.handler.ServiceTypeHandler;
import org.biomoby.shared.MobyDataType;
import org.biomoby.shared.MobyException;
import org.biomoby.shared.MobyNamespace;
import org.biomoby.shared.MobyService;
import org.biomoby.shared.MobyServiceType;

/**
 * Class to control the synchronization of the two moby centrals.
 * 
 * @author groscurt
 */
public class MobySynchronizeImpl extends AbstractMobySynchronize {
	public MobySynchronizeImpl() throws MobyException {
		super();
	}

	/**
	 * @see MobySynchronize#updateNamespaces()
	 */
	public void updateNamespaces() throws MobyException {        
		// retrieve the namespaces from the master central
        logger.info("Fetching the namespaces from the central repository... may take a while");
        MobyNamespace[] centralNamespaces = masterCentral.getFullNamespaces();
		logger.fine( "Retrieved the central namespace list (" + centralNamespaces.length + ")" );

        logger.info("Fetching the namespaces from the local repository... may take a while");
		// retrieve the namespaces from the local central
        MobyNamespace[] localNamespaces = localCentral.getFullNamespaces();
		
		logger.fine( "Retrieved the local datatype list (" + localNamespaces.length + ")" );
		AbstractMobyHandler< MobyNamespace > namespaceHandler = new NamespaceHandler( authority );
		namespaceHandler.synchronizeCentrals( centralNamespaces, localNamespaces );

		logger.info( "finished updating the namespaces !" );
	}

	public void updateDatatypes() throws MobyException {
		// retrieve the datatypes from the central
        logger.info("Fetching the datatypes from the central repository.. may take a while");
        MobyDataType[] centralDataTypes = masterCentral.getDataTypes();
		logger.fine( "Retrieved the central datatype list (" + centralDataTypes.length + ")" );

        logger.info("Fetching the datatypes from the local repository.. may take a while");
		// retrieve the datatypes from the local
        MobyDataType[] localDataTypes = localCentral.getDataTypes();
		logger.fine( "Retrieved the local datatype list (" + localDataTypes.length + ")" );

		AbstractMobyHandler< MobyDataType > dataTypeHandler = new DataTypeHandler( authority );
		dataTypeHandler.synchronizeCentrals( centralDataTypes, localDataTypes );

		logger.info( "finished updating the datatypes !" );
	}

	public void updateServiceTypes() throws MobyException {
		// retrieve the servicetypes from the central
        logger.info("Fetching the service types from the central repository.. may take a while");
        MobyServiceType[] centralServiceTypes = masterCentral.getFullServiceTypes();
		logger.fine( "Retrieved the central servicetype list (" + centralServiceTypes.length + ")" );

        logger.info("Fetching the service types from the local repository.. may take a while");
		// retrieve the service types from the local
        MobyServiceType[] localServiceTypes = localCentral.getFullServiceTypes();
		logger.fine( "Retrieved the local servicetype list (" + localServiceTypes.length + ")" );

		AbstractMobyHandler< MobyServiceType > serviceTypeHandler = new ServiceTypeHandler( authority );
		serviceTypeHandler.synchronizeCentrals( centralServiceTypes, localServiceTypes );

		logger.info( "finished updating the servicesTypes !" );
	}

	public void updateServices() throws MobyException {
		// retrieve services from the central
        logger.info("Fetching the services from the central repository.. may take a while");
        MobyService[] centralServices = masterCentral.getServices();
		logger.fine( "Retrieved the central service list (" + centralServices.length + ")" );

        logger.info("Fetching the services from the local repository.. may take a while");
		// retrieve services from the local
		MobyService[] localServices = localCentral.getServices();
		logger.fine( "Retrieved the local service list (" + localServices.length + ")" );

		AbstractMobyHandler< MobyService > handler = new ServiceHandler( authority );
		handler.synchronizeCentrals( centralServices, localServices );

		logger.info( "finished updating the services !" );
	}
}