package org.biomoby.registry.sync.handler;

import org.biomoby.registry.rdfagent.util.CentralAdmin;
import org.biomoby.registry.rdfagent.util.Constants;
import org.biomoby.registry.sync.filtering.ServiceFilter;
import org.biomoby.shared.MobyException;
import org.biomoby.shared.MobyService;

/**
 * Class to synchronize the services in the biomoby centrals.
 * 
 * @author groscurt
 */
public class ServiceHandler extends AbstractMobyHandler< MobyService > {

	public ServiceHandler(String authority) throws MobyException {
		super(authority);
	}

	/**
	 * method which prints out a message depending on the given code. The code represents the result
	 * of the unregistration process.
	 * 
	 * @param code
	 *            the message code from the unregistration process
	 * @return
	 * @see CentralAdmin#processCentralAmdinCode(int)
	 */
	private boolean processCentralAdminCode( int code ) {
		switch ( code ) {
			case 101:
				logger.warning( "Central admin didnt receive an service provider authority" );
				break;

			case 102:
				logger.warning( "Central Admin didnt receive a service name to remove" );
				break;

			case 103:
				logger.warning( "Central Admin didnt receive a phrase." );
				break;

			case 200:
				logger.info( "Service was successfully removed." );
				return true;

			case 404:
				logger.severe( "Oddly enough, the service wasnt found in the registry!?!" );
				break;

			case 501:
				logger
						.severe( "The keyphrase specified in the config file didn't match the one in the registry's DB config file." );
				break;

			case 505:
				logger.severe( "Something weird happened with the registry. Please check your registry's error logs." );
				break;

			case 506:
				logger.severe( "Central Admin may not have understood our request." );
				break;

			case 507:
				logger.severe( "There was an AxisFault. Are you online? Are the username and password correct?" );
				break;

			case 508:
				logger.severe( "The url and/or uri for the Central Admin service were invalid." );
				break;

			default:
				logger.severe( "Central Admin returned an unknown code: " + code + "" );
				break;
		}
		return false;
	}

	public boolean unregister( MobyService service ) {

		// the central admin class enables to delete a service
		// another possibility would be to use the unregisterService method in the Central class,
		// but this procedure require the lack of the rdf at the signature URL, so a abritrary
		// unregister at the local (!) central is impossible
		CentralAdmin centralAdmin = new CentralAdmin( Constants.REGISTRY_REMOVAL_ENDPOINT, Constants.REGISTRY_REMOVAL_URI,
				Constants.REGISTRY_REMOVAL_USERNAME, Constants.REGISTRY_REMOVAL_PASSWORD, Constants.REGISTRY_KEYPHRASE );

		// delete the service
		int code = centralAdmin.deleteService( service.getName(), service.getAuthority() );
		processCentralAdminCode( code );

		if ( code == 200 ) {
			logger.info( service.getName() + " was unregistered at localCentral" );
		}

		// 200 means successfull
		return code == 200;
	}

	public boolean register( MobyService service ) {
		// check whether the service shall be ignored due to the defined filter rules
		if ( ServiceFilter.getServiceFilter().isServiceFiltered( service ) ) {
			logger.warning( "The service " + service.getUniqueName() + " is filtered and not added to the registry" );
			return false;
		}

		try {
			// try to register the service
			localCentral.registerService( service );
		}
		catch ( Exception e ) {
			logger.warning( "The service " + service.getUniqueName() + " could " + "not been added !\n"
					+ e.getLocalizedMessage() );
			return false;
		}

		return true;
	}
}