/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2017 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.transaction.participant;

import java.io.Serializable;

import org.jpos.core.Configurable;
import org.jpos.core.Configuration;
import org.jpos.core.ConfigurationException;
import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOSource;
import org.jpos.transaction.Context;
import org.jpos.transaction.AbortParticipant;
import org.jpos.transaction.ContextConstants;

import static org.jpos.transaction.ContextConstants.SOURCE;
import static org.jpos.transaction.ContextConstants.TX;

@SuppressWarnings("unused")
public class SendResponse implements AbortParticipant, Configurable {
    private String source;
    private String response;

    public int prepare (long id, Serializable context) {
        Context ctx = (Context) context;
        ISOSource source = (ISOSource) ctx.get (SOURCE.toString());
        if (source == null || !source.isConnected())
            return ABORTED | READONLY | NO_JOIN;

        return PREPARED | READONLY;
    }
    public void commit (long id, Serializable context) { 
        sendResponse(id, (Context) context);
    }
    public void abort (long id, Serializable context) { 
        sendResponse(id, (Context) context);
    }
    private void sendResponse (long id, Context ctx) {
        ISOSource src = (ISOSource) ctx.get (source);
        ISOMsg resp = (ISOMsg) ctx.get (response);
        try {
            if (ctx.getResult().hasInhibit()) {
                ctx.log("*** RESPONSE INHIBITED ***");
            } else if (ctx.get (TX.toString()) != null) {
                ctx.log("*** PANIC - TX not null - RESPONSE OMITTED ***");
            } else if (resp == null) {
                ctx.log (response + " not present");
            } else if (src == null) {
                ctx.log (source + " not present");
            } else if (!src.isConnected())
                ctx.log (source + " is no longer connected");
            else
                src.send (resp);
        } catch (Throwable t) {
            ctx.log(t);
        }
    }

    @Override
    public void setConfiguration(Configuration cfg) throws ConfigurationException {
        source = cfg.get ("source", ContextConstants.SOURCE.toString());
        response = cfg.get ("response", ContextConstants.RESPONSE.toString());
    }
}

@@ -1188,16 +1188,78 @@
 SOURCE;%0A
+import static org.jpos.transaction.ContextConstants.RESPONSE;%0A
 import s
@@ -1608,33 +1608,27 @@
 tx.get (
-SOURCE.toString()
+this.source
 );%0A     
@@ -1815,33 +1815,32 @@
 zable context) %7B
- 
 %0A        sendRes
@@ -1930,17 +1930,16 @@
 ntext) %7B
- 
 %0A       
@@ -2914,16 +2914,18 @@
  source 
+  
 = cfg.ge
@@ -2937,33 +2937,18 @@
 ource%22, 
-ContextConstants.
+  
 SOURCE.t
@@ -2999,33 +2999,16 @@
 ponse%22, 
-ContextConstants.
 RESPONSE

use the already configured source value