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    public MSGTEST02() {
039        super();
040    }
041
042    public MSGTEST02( boolean breakOnError ) {
043        super(breakOnError);
044    }
045
046    private String makeStrFromArray( int[] validFields ){
047        if ( validFields == null ) return null;
048        StringBuilder result = new StringBuilder();
049        for (int validField : validFields) {
050            result.append(validField);
051            result.append(", ");
052        }
053        result.delete( result.length()-2, result.length()-1 );
054        return result.toString(  );
055    }
056
057    public ISOComponent validate(ISOComponent m) throws org.jpos.iso.ISOException {
058        LogEvent evt = new LogEvent( this, "validate" );
059        try {
060            super.validate ( m );
061            ISOMsg msg = (ISOMsg)m;
062            int[] validFields = { 0,1 };
063            if ( !msg.hasFields( validFields ) ){
064                ISOVError e = new ISOVError( "Fields " + makeStrFromArray( validFields ) + " must appear in msg.", "002" );
065                if ( msg instanceof ISOVMsg )
066                    ((ISOVMsg)msg).addISOVError( e );
067                else
068                    msg = new ISOVMsg( msg, e );
069                if ( breakOnError )
070                    throw new ISOVException ( "Error on msg. " , msg );
071            }
072            /** field interdependency **/
073            if ( msg.getString( 0 ).endsWith( "1" )  && !msg.getString( 1 ).endsWith( "0" ) ){
074                ISOVError e = new ISOVError( "If field 0 ends with 1 then field 1 must end with 0.", "003" );
075                if ( msg instanceof ISOVMsg )
076                    ((ISOVMsg)msg).addISOVError( e );
077                else
078                    msg = new ISOVMsg( msg, e );
079                if ( breakOnError )
080                    throw new ISOVException ( "Error on msg. " , msg );
081            }
082            return msg;
083        } finally {
084            Logger.log( evt );
085        }
086    }
087}