001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.transaction;
020
021import java.io.Serializable;
022
023/** Event describing a transaction status transition. */
024public class TransactionStatusEvent {
025    int session;
026    long id;
027    long timestamp;
028    String info;
029    State state;
030    Serializable context;
031
032    /** Enumeration of transaction lifecycle states. */
033    public enum State {
034        /** Transaction is ready for processing. */
035        READY(0),
036        /** Transaction is in the prepare phase. */
037        PREPARING(1),
038        /** Transaction is preparing for abort. */
039        PREPARING_FOR_ABORT(2),
040        /** Transaction is committing. */
041        COMMITING(3),
042        /** Transaction is aborting. */
043        ABORTING(4),
044        /** Transaction processing is complete. */
045        DONE(5),
046        /** Transaction is paused. */
047        PAUSED(6);
048
049        int state;
050        String[] stateAsString = new String[] {
051            "Ready", "Preparing", "Preparing for abort", "Commiting", "Aborting", "Done", "Paused"
052        };
053        State (int state) {
054            this.state = state;
055        }
056        public String toString () {
057            return stateAsString [state];
058        }
059        /**
060         * Returns the numeric representation of the state.
061         *
062         * @return integer state code
063         */
064        public int intValue() {
065            return state;
066        }
067    }
068
069    /**
070     * Creates a transaction status event.
071     *
072     * @param session session identifier
073     * @param state transaction state
074     * @param id transaction identifier
075     * @param info human-readable status information
076     * @param context transaction context when available
077     */
078    public TransactionStatusEvent (int session, State state, long id, String info, Serializable context) {
079        super();
080        this.session = session;
081        this.state = state;
082        this.id = id;
083        this.info = info;
084        this.context = context;
085        timestamp = System.nanoTime();
086    }
087    public String toString() {
088        return String.format("%02d %08d %s %s", session, id, state.toString(), info);
089    }
090    /**
091     * Returns the session identifier.
092     *
093     * @return session identifier
094     */
095    public int getSession() {
096        return session;
097    }
098    /**
099     * Returns the transaction identifier.
100     *
101     * @return transaction identifier
102     */
103    public long getId() {
104        return id;
105    }
106    /**
107     * Returns the status information string.
108     *
109     * @return status information
110     */
111    public String getInfo() {
112        return info;
113    }
114    /**
115     * Returns the transaction state.
116     *
117     * @return current state
118     */
119    public State getState() {
120        return state;
121    }
122    /**
123     * Returns the transaction state as display text.
124     *
125     * @return state name
126     */
127    public String getStateAsString () {
128        return state.toString();
129    }
130    /**
131     * Returns the transaction context.
132     *
133     * @return transaction context
134     */
135    public Serializable getContext(){
136        return context;
137    }
138}