package eu.dnetlib.xml.database.exist;

import java.util.List;

import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;

import eu.dnetlib.xml.database.Trigger;
import eu.dnetlib.xml.database.XMLDBResultSet;

/**
 * Synchronized version of the ExistDatabase interface. Needed because it seems that eXist 1.2.4 has some serious
 * problems with concurrent access.
 * 
 * @author marko
 * 
 */
public class SyncExistDatabase extends ExistDatabase { // NOPMD

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#collectionExists(java.lang.String)
	 */
	@Override
	public boolean collectionExists(final String collection) throws XMLDBException {
		synchronized (this) {
			return super.collectionExists(collection);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#createResultSet(org.xmldb.api.base.ResourceSet)
	 */
	@Override
	protected ExistResultSet createResultSet(final ResourceSet res) {
		return new SyncExistResultSet(res, this);
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#create(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void create(final String name, final String collection, final String content) throws XMLDBException {
		synchronized (this) {
			super.create(name, collection, content);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#createCollection(java.lang.String, boolean)
	 */
	@Override
	public void createCollection(final String collection, final boolean recursive) throws XMLDBException {
		synchronized (this) {
			super.createCollection(collection, recursive);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#createCollection(java.lang.String)
	 */
	@Override
	public void createCollection(final String collection) throws XMLDBException {
		synchronized (this) {
			super.createCollection(collection);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#isRunning()
	 */
	@Override
	public boolean isRunning() {
		synchronized (this) {
			return super.isRunning();
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#list(java.lang.String)
	 */
	@Override
	public List<String> list(final String collection) throws XMLDBException {
		synchronized (this) {
			return super.list(collection);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#listChildCollections(java.lang.String)
	 */
	@Override
	public List<String> listChildCollections(final String collection) throws XMLDBException {
		synchronized (this) {
			return super.listChildCollections(collection);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#read(java.lang.String, java.lang.String)
	 */
	@Override
	public String read(final String name, final String collection) throws XMLDBException {
		synchronized (this) {
			return super.read(name, collection);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#registerTrigger(eu.dnetlib.xml.database.Trigger, java.lang.String)
	 */
	@Override
	public void registerTrigger(final Trigger trigger, final String collection) throws XMLDBException {
		synchronized (this) {
			super.registerTrigger(trigger, collection);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#remove(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean remove(final String name, final String collection) throws XMLDBException {
		synchronized (this) {
			return super.remove(name, collection);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#removeCollection(java.lang.String)
	 */
	@Override
	public void removeCollection(final String collection) throws XMLDBException {
		synchronized (this) {
			super.removeCollection(collection);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#update(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public void update(final String name, final String collection, final String content) throws XMLDBException {
		synchronized (this) {
			super.update(name, collection, content);
		}
	}

	/** 
	 * {@inheritDoc}
	 * @see eu.dnetlib.xml.database.exist.ExistDatabase#xquery(java.lang.String)
	 */
	@Override
	public XMLDBResultSet xquery(final String xquery) throws XMLDBException {
		synchronized (this) {
			return super.xquery(xquery);
		}
	}

}
