package eu.dnetlib.enabling.manager.msro;

import javax.annotation.Resource;
import javax.xml.ws.Endpoint;
import javax.xml.ws.wsaddressing.W3CEndpointReference;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import eu.dnetlib.enabling.is.sn.rmi.ISSNException;
import eu.dnetlib.enabling.is.sn.rmi.ISSNService;
import eu.dnetlib.enabling.tools.ServiceLocator;
import eu.dnetlib.soap.EndpointReferenceBuilder;

/**
 * This component takes care of subscribing the MSRO service to interesting events.
 * @author marko
 *
 */
public class MSROSubscriberImpl implements MSROSubscriber {

	/**
	 * logger.
	 */
	private static final Log log = LogFactory.getLog(MSROSubscriberImpl.class); // NOPMD by marko on 11/24/08 5:02 PM

	/**
	 * notification endpoint (normally the msro service).
	 */
	private Endpoint endpoint;

	/**
	 * is sn locator.
	 */
	private ServiceLocator<ISSNService> snLocator;

	/**
	 * injected EPR builder.
	 */
	@Resource(name = "jaxwsEndpointReferenceBuilder")
	private EndpointReferenceBuilder<Endpoint> eprBuilder;

	/**
	 * {@inheritDoc}
	 * @throws ISSNException could happen
	 * @see eu.dnetlib.enabling.manager.msro.MSROSubscriber#subscribeAll()
	 */
	public void subscribeAll() throws ISSNException {
		log.info("Subscribing msro service");

		final W3CEndpointReference endpointReference = eprBuilder.getEndpointReference(getEndpoint());
		snLocator.getService().subscribe(endpointReference, "CREATE/RepositoryServiceResourceType", 0);
		snLocator.getService().subscribe(endpointReference, "DELETE/RepositoryServiceResourceType", 0);
		snLocator.getService().subscribe(endpointReference, "UPDATE/RepositoryServiceResourceType", 0);
		snLocator.getService().subscribe(endpointReference, "UPDATE/*/*/RESOURCE_PROFILE/BODY/BLACKBOARD/LAST_RESPONSE", 0);

		// real
//		snLocator.getService().subscribe(endpointReference, "UPDATE/MDStoreDSResourceType/*/RESOURCE_PROFILE/BODY/STATUS/LAST_STORAGE_DATE", 0);

		// for mock mdstore service
		snLocator.getService().subscribe(endpointReference, "UPDATE/MDStoreDSResourceType/*/RESOURCE_PROFILE[.//METADATA_FORMAT = 'DMF']/BODY/STATUS/LAST_STORAGE_DATE", 0);
	}

	public void setEndpoint(final Endpoint endpoint) {
		this.endpoint = endpoint;
	}

	public Endpoint getEndpoint() {
		return endpoint;
	}

	public void setEprBuilder(final EndpointReferenceBuilder<Endpoint> eprBuilder) {
		this.eprBuilder = eprBuilder;
	}

	public EndpointReferenceBuilder<Endpoint> getEprBuilder() {
		return eprBuilder;
	}

	public ServiceLocator<ISSNService> getSnLocator() {
		return snLocator;
	}

	public void setSnLocator(final ServiceLocator<ISSNService> snLocator) {
		this.snLocator = snLocator;
	}

}
