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 * VALIDATES FIELD INTERDEPENDENCY. IF FIELD 0 ENDS WITH 1 THEN
027 * FIELD 1 MUST END WITH 0.
028 * ONLY TEST PURPOSE.
029 * <p>Title: jPOS</p>
030 * <p>Description: Java Framework for Financial Systems</p>
031 * <p>Copyright: Copyright (c) 2000 jPOS.org.  All rights reserved.</p>
032 * <p>Company: www.jPOS.org</p>
033 * @author Jose Eduardo Leon
034 * @version 1.0
035 */
036public class MSGTEST02 extends ISOBaseValidator {
037
038    /** Default constructor. */
039    public MSGTEST02() {
040        super();
041    }
042
043    /**
044     * Constructs the validator with the requested error behaviour.
045     *
046     * @param breakOnError when {@code true}, validation throws on the first error
047     */
048    public MSGTEST02( boolean breakOnError ) {
049        super(breakOnError);
050    }
051
052    private String makeStrFromArray( int[] validFields ){
053        if ( validFields == null ) return null;
054        StringBuilder result = new StringBuilder();
055        for (int validField : validFields) {
056            result.append(validField);
057            result.append(", ");
058        }
059        result.delete( result.length()-2, result.length()-1 );
060        return result.toString(  );
061    }
062
063    public ISOComponent validate(ISOComponent m) throws org.jpos.iso.ISOException {
064        LogEvent evt = new LogEvent( this, "validate" );
065        try {
066            super.validate ( m );
067            ISOMsg msg = (ISOMsg)m;
068            int[] validFields = { 0,1 };
069            if ( !msg.hasFields( validFields ) ){
070                ISOVError e = new ISOVError( "Fields " + makeStrFromArray( validFields ) + " must appear in msg.", "002" );
071                if ( msg instanceof ISOVMsg )
072                    ((ISOVMsg)msg).addISOVError( e );
073                else
074                    msg = new ISOVMsg( msg, e );
075                if ( breakOnError )
076                    throw new ISOVException ( "Error on msg. " , msg );
077            }
078            /** field interdependency **/
079            if ( msg.getString( 0 ).endsWith( "1" )  && !msg.getString( 1 ).endsWith( "0" ) ){
080                ISOVError e = new ISOVError( "If field 0 ends with 1 then field 1 must end with 0.", "003" );
081                if ( msg instanceof ISOVMsg )
082                    ((ISOVMsg)msg).addISOVError( e );
083                else
084                    msg = new ISOVMsg( msg, e );
085                if ( breakOnError )
086                    throw new ISOVException ( "Error on msg. " , msg );
087            }
088            return msg;
089        } finally {
090            Logger.log( evt );
091        }
092    }
093}