/** 
 * 
 * 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.store.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.jms.JMSException;
import javax.transaction.xa.XAException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.io.WireFormat;
import org.codehaus.activemq.message.ActiveMQXid;
import org.codehaus.activemq.service.Transaction;
import org.codehaus.activemq.service.TransactionManager;
import org.codehaus.activemq.store.PreparedTransactionStore;

/**
 * @version $Revision: 1.2 $
 */
public class JDBCPreparedTransactionStore implements PreparedTransactionStore {
    private static final Log log = LogFactory.getLog(JDBCMessageStore.class);

    private final WireFormat wireFormat;
    private final JDBCAdapter adapter;
	private final JDBCPersistenceAdapter persistenceAdapter;

    public JDBCPreparedTransactionStore(JDBCPersistenceAdapter persistenceAdapter, JDBCAdapter adapter, WireFormat wireFormat) {
        this.persistenceAdapter = persistenceAdapter;
		this.adapter = adapter;
        this.wireFormat = wireFormat;
    }

    public ActiveMQXid[] getXids() throws XAException {

        // Pull xids out of the DB and put them in here.
        List list = new ArrayList();

        Connection c = null;
        try {
            c = persistenceAdapter.getConnection();            
            adapter.doGetXids(c, list);
        } catch (SQLException e) {
            log.error("Failed to recover prepared transaction log: " + e, e);
            throw new XAException("Failed to recover container. Reason: " + e);
        } finally {
        	persistenceAdapter.returnConnection(c);
        } 
        
        ActiveMQXid[] answer = new ActiveMQXid[list.size()];
        list.toArray(answer);
        return answer;
    }

    public void remove(ActiveMQXid xid) throws XAException {
        
        // Get a connection and remove the message from the DB
        Connection c = null;
        try {
            c = persistenceAdapter.getConnection();            
            adapter.doRemoveXid(c, xid);
        } catch (SQLException e) {
            throw new XAException("Failed to remove prepared transaction: " + xid + ". Reason: " + e);
        } finally {
        	persistenceAdapter.returnConnection(c);
        } 
        
    }

    public void put(ActiveMQXid xid, Transaction transaction) throws XAException {
        
        // Serialize the Message..
        String id = xid.toLocalTransactionId();
        byte data[];
        try {
            data = xid.toBytes();
        } catch (Exception e) {
            throw new XAException("Failed to store prepared transaction: " + xid + ". Reason: " + e);
        }
        
        // Get a connection and insert the message into the DB.
        Connection c = null;
        try {
            c = persistenceAdapter.getConnection();            
            adapter.doAddXid(c, xid, data);            
        } catch (SQLException e) {
            throw new XAException("Failed to store prepared transaction: " + xid + ". Reason: " + e);
        } finally {
        	persistenceAdapter.returnConnection(c);
        } 
    }

    public void loadPreparedTransactions(TransactionManager transactionManager) throws XAException {
        
        Connection c = null;
        try {
            c = persistenceAdapter.getConnection();            
            adapter.doLoadPreparedTransactions(c, transactionManager);
        } catch (SQLException e) {
            log.error("Failed to recover prepared transaction log: " + e, e);
            throw new XAException("Failed to recover prepared transaction log. Reason: " + e);
        } finally {
        	persistenceAdapter.returnConnection(c);
        } 

    }

    public void start() throws JMSException {
    }

    public synchronized void stop() throws JMSException {
    }

}
