//$Id: ClusterEvent.java 2134 2005-11-27 23:14:00Z brihaye $
package org.exist.cluster;

import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XMLResource;

import java.io.Serializable;

/**
 * Created by Francesco Mondora.
 *
 * @author Francesco Mondora aka Makkina
 *         Date: Aug 30, 2004
 *         Time: 3:42:17 PM
 *         Revision $Revision: 2134 $
 */
public abstract class ClusterEvent implements Serializable {
    
    public static final int NO_EVENT = -1;

    private static final long serialVersionUID = 0L;

    protected String collectionName;
    protected String documentName;
    private int id = NO_EVENT;
    private int counter = 1;


    public ClusterEvent(){
    }
    protected ClusterEvent(String documentName, String collectionName) {
        this.documentName = documentName;
        this.collectionName = collectionName;
    }

    public String getCollectionName()
    {
        return collectionName;
    }

    public String getDocumentName()
    {
        return documentName;
    }

    public boolean equals( Object o ){
        if( o instanceof ClusterEvent ) return (this.hashCode()==((ClusterEvent) o).hashCode());
        else return false;
    }

    /**
     * Execute the current command.
     */
    public abstract void execute() throws ClusterException;

    public XMLResource getResource() throws XMLDBException {
        Collection collection = getCollection();
        return (XMLResource) collection.createResource(documentName, "XMLResource");

    }

    public org.xmldb.api.base.Collection getCollection( String cName ) throws XMLDBException {
        //todo: get the admin password
        return DatabaseManager.getCollection("xmldb:exist://" + cName, ClusterComunication.getDbaUser(), ClusterComunication.getDbaPwd());
    }

    public org.xmldb.api.base.Collection getCollection() throws XMLDBException {
        //todo: get the admin password
        return getCollection( collectionName );
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
}
