package org.biomoby.registry.sync;

import java.util.Properties;
import java.util.logging.Logger;

import org.biomoby.client.CentralDigestImpl;
import org.biomoby.registry.sync.logging.MobySecondaryLogging;
import org.biomoby.shared.MobyException;

/**
 * Basic abstract class for the synchronization of a local moby central with a master moby central.<br>
 * The four moby datatypes - <code>MobyNamespace</code>,<code>MobyDataType</code>, <code>MobyServiceType</code>,
 * <code>MobyService</code> - are updated sequentially.<br>
 * If one of these types shall not be updated one can change the behavior of the update process in the configuration
 * file.
 * 
 * @author groscurt
 */
public abstract class AbstractMobySynchronize implements MobySynchronize {
    protected static Logger logger = Logger.getLogger( MobySecondaryLogging.LOGGER_NAME );

    protected CentralDigestImpl masterCentral;
    protected CentralDigestImpl localCentral;
    protected String authority;

    public AbstractMobySynchronize() throws MobyException {
        masterCentral = CentralFactory.getMasterCentral();
        localCentral = CentralFactory.getLocalCentral();
    }

    /**
     * Updates the local registry.
     * 
     * @param configuration
     *            the configuration to see which types shall be updated
     * @throws MobyException
     *             whether an update failed
     */
    public void updateRegistry( Properties configuration ) throws MobyException {
        authority = configuration.getProperty( "local.authority" );

        if ( authority == null || authority.equals( "" ) ) {
            throw new MobyException( "No authority is given in the configuration file !" );
        }

        if ( "1".equals( configuration.getProperty( "sync.namespaces" ) ) ) {
            logger.info( "Start to update the namespaces !" );
            // sync the namespaces
            updateNamespaces();
        }

        if ( "1".equals( configuration.getProperty( "sync.datatypes" ) ) ) {
            logger.info( "Start to update the datatypes !" );
            // sync the datatypes
            updateDatatypes();
        }

        if ( "1".equals( configuration.getProperty( "sync.servicetypes" ) ) ) {
            logger.info( "Start to update the servicetypes !" );
            // sync the servicetypes
            updateServiceTypes();
        }

        if ( "1".equals( configuration.getProperty( "sync.services" ) ) ) {
            logger.info( "Start to update the services !" );
            // sync the service
            updateServices();
        }
    }
}
