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.participant;
020
021import org.jdom2.Element;
022import org.jpos.core.ConfigurationException;
023import org.jpos.transaction.GroupSelector;
024import org.jpos.util.LogEvent;
025import org.jpos.util.Logger;
026
027import java.io.Serializable;
028
029/**
030 * @author  AMarques
031 */
032public class BSHGroupSelector extends BSHTransactionParticipant implements GroupSelector {
033    
034    protected BSHMethod selectMethod;
035    
036    public void setConfiguration(Element e) throws ConfigurationException {
037        super.setConfiguration(e);
038        try {
039            selectMethod = BSHMethod.createBshMethod(e.getChild("select"));
040        } catch (Exception ex) {
041            throw new ConfigurationException(ex.getMessage(), ex);
042        }
043    }    
044    
045    public String select(long id, java.io.Serializable context) {
046        LogEvent ev = new LogEvent(this, "select");
047        String result = null;
048        if (selectMethod != null) {
049            try {
050                result = (String) executeMethod(selectMethod, id, context, ev, "result");
051            } catch (Exception ex) {
052                ev.addMessage(ex);
053            }
054        } 
055        if (result == null) {
056            result = defaultSelect(id, context);
057        }
058        ev.addMessage("result", result);
059        Logger.log(ev);
060        return result;
061    }
062    
063    public String defaultSelect(long id, Serializable context) {
064        return "";
065    }
066        
067}