/** 
 * 
 * Copyright 2004 Hiram Chirino
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.codehaus.activemq.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.broker.Broker;
import org.codehaus.activemq.service.Transaction;
import org.codehaus.activemq.service.TransactionTask;

import javax.transaction.xa.XAException;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Keeps track of all the actions the need to be done when
 * a transaction does a commit or rollback.
 *
 * @version $Revision: 1.1 $
 */
public abstract class AbstractTransaction implements Transaction, Externalizable {

    static final private Log log = LogFactory.getLog(AbstractTransaction.class);

    static final public byte START_STATE = 0;      // can go to: 1,2,3
    static final public byte IN_USE_STATE = 1;     // can go to: 2,3
    static final public byte PREPARED_STATE = 2;   // can go to: 3
    static final public byte FINISHED_STATE = 3;


    private ArrayList prePrepareTasks = new ArrayList();
    private ArrayList postCommitTasks = new ArrayList();
    private ArrayList postRollbackTasks = new ArrayList();
    private byte state = START_STATE;
    private transient Broker broker;

    protected AbstractTransaction(Broker broker) {
        this.broker = broker;
    }

    public Broker getBroker() {
        return broker;
    }

    /**
     * Called after deserialization to register the broker
     *
     * @param broker
     */
    public void setBroker(Broker broker) {
        this.broker = broker;
    }

    public byte getState() {
        return state;
    }

    public void setState(byte state) {
        this.state = state;
    }

    public void addPostCommitTask(TransactionTask r) {
        postCommitTasks.add(r);
        if (state == START_STATE) {
            state = IN_USE_STATE;
        }
    }

    public void addPostRollbackTask(TransactionTask r) {
        postRollbackTasks.add(r);
        if (state == START_STATE) {
            state = IN_USE_STATE;
        }
    }

    public void addPrePrepareTask(TransactionTask r) {
        prePrepareTasks.add(r);
        if (state == START_STATE) {
            state = IN_USE_STATE;
        }
    }

    public void prePrepare() throws Throwable {

        // Is it ok to call prepare now given the state of the
        // transaction?
        switch (state) {
            case START_STATE:
            case IN_USE_STATE:
                break;
            default:
                XAException xae = new XAException("Prepare cannot be called now.");
                xae.errorCode = XAException.XAER_PROTO;
                throw xae;
        }

        // Run the prePrepareTasks
        for (Iterator iter = prePrepareTasks.iterator(); iter.hasNext();) {
            TransactionTask r = (TransactionTask) iter.next();
            r.execute(broker);
        }
    }

    protected void postCommit() throws Throwable {
        // Run the postCommitTasks
        for (Iterator iter = postCommitTasks.iterator(); iter.hasNext();) {
            TransactionTask r = (TransactionTask) iter.next();
            r.execute(broker);
        }
    }

    public void postRollback() throws Throwable {
        // Run the postRollbackTasks
        for (Iterator iter = postRollbackTasks.iterator(); iter.hasNext();) {
            TransactionTask r = (TransactionTask) iter.next();
            r.execute(broker);
        }
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        state = in.readByte();
        prePrepareTasks = readTaskList(in);
        postCommitTasks = readTaskList(in);
        postRollbackTasks = readTaskList(in);
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeByte(state);
        writeTaskList(prePrepareTasks, out);
        writeTaskList(postCommitTasks, out);
        writeTaskList(postRollbackTasks, out);
    }

    public String toString() {
        return super.toString() + "[prePrepares=" + prePrepareTasks + "; postCommits=" + postCommitTasks
                + "; postRollbacks=" + postRollbackTasks + "]";
    }

    // Implementation methods
    //-------------------------------------------------------------------------
    protected ArrayList readTaskList(ObjectInput in) throws IOException {
        int size = in.readInt();
        ArrayList answer = new ArrayList(size);
        for (int i = 0; i < size; i++) {
            answer.add(readTask(in));
        }
        return answer;
    }

    protected void writeTaskList(ArrayList tasks, ObjectOutput out) throws IOException {
        int size = tasks.size();
        out.writeInt(size);
        for (int i = 0; i < size; i++) {
            writeTask((TransactionTask) tasks.get(i), out);
        }
    }

    protected TransactionTask readTask(ObjectInput in) throws IOException {
        return PacketTransactionTask.readTask(in);
    }

    protected void writeTask(TransactionTask task, ObjectOutput out) throws IOException {
        PacketTransactionTask.writeTask(task, out);
    }
}
