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.iso.validator;
020
021import org.jpos.iso.*;
022import org.jpos.util.LogEvent;
023import org.jpos.util.Logger;
024
025/**
026 * Tester to validate msg.
027 * <p>Title: jPOS</p>
028 * <p>Description: Java Framework for Financial Systems</p>
029 * <p>Copyright: Copyright (c) 2000 jPOS.org.  All rights reserved.</p>
030 * <p>Company: www.jPOS.org</p>
031 * @author Jose Eduardo Leon
032 * @version 1.0
033 */
034public class MSGTEST extends ISOBaseValidator {
035
036    /** Default constructor. */
037    public MSGTEST() {
038        super();
039    }
040
041    /**
042     * Constructs the validator with the requested error behaviour.
043     *
044     * @param breakOnError when {@code true}, validation throws on the first error
045     */
046    public MSGTEST( boolean breakOnError ) {
047        super(breakOnError);
048    }
049
050    private String makeStrFromArray( int[] validFields ){
051        if ( validFields == null ) return null;
052        StringBuilder result = new StringBuilder();
053        for (int validField : validFields) {
054            result.append(validField);
055            result.append(", ");
056        }
057        result.delete( result.length()-2, result.length()-1 );
058        return result.toString(  );
059    }
060
061    public ISOComponent validate(ISOComponent m) throws org.jpos.iso.ISOException {
062        LogEvent evt = new LogEvent( this, "validate" );
063        try {
064            super.validate ( m );
065            ISOMsg msg = (ISOMsg)m;
066            int[] validFields = { 3,7,11 };
067            if ( !msg.hasFields( validFields ) ){
068                ISOVError e = new ISOVError( "Fields " + makeStrFromArray( validFields ) + " must appear in msg.", "001" );
069                if ( msg instanceof ISOVMsg )
070                    ((ISOVMsg)msg).addISOVError( e );
071                else
072                    msg = new ISOVMsg( msg, e );
073                if ( breakOnError )
074                    throw new ISOVException ( "Error on msg. " , msg );
075            }
076            return msg;
077        } finally {
078            Logger.log( evt );
079        }
080    }
081}