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.packager;
020
021import org.jpos.iso.*;
022import org.jpos.iso.validator.ISOVException;
023import org.jpos.util.LogEvent;
024import org.jpos.util.Logger;
025
026import java.util.Map;
027
028/**
029 * Base Packager class envolving validators. It implements
030 * ISOValidator interface and define an implementation for validate method.
031 * Validation is for composed components.
032 * <p>Title: jPOS</p>
033 * <p>Description: Java Framework for Financial Systems</p>
034 * <p>Copyright: Copyright (c) 2000 jPOS.org.  All rights reserved.</p>
035 * <p>Company: www.jPOS.org</p>
036 * @author Jose Eduardo Leon
037 * @version 1.0
038 */
039public class ISOBaseValidatingPackager extends ISOBasePackager implements ISOValidator {
040
041    /** Default constructor. */
042    public ISOBaseValidatingPackager() {
043        super();
044    }
045
046    public ISOComponent validate(ISOComponent m) throws ISOException {
047        LogEvent evt = new LogEvent( this, "validate" );
048        try {
049            ISOComponent c;
050            Map fields = m.getChildren();
051            /** Field  validations **/
052            for (ISOValidator aFldVld : fldVld) {
053                if (aFldVld != null && (c = (ISOComponent) fields.get(Integer.valueOf(((ISOFieldValidator) aFldVld).getFieldId()))) != null) {
054                    try {
055                        m.set(aFldVld.validate(c));
056                    } catch (ISOVException e) {
057                        if (!e.treated()) {
058                            m.set(e.getErrComponent());
059                            e.setTreated(true);
060                        }
061                        evt.addMessage("Component Validation Error.");
062                        throw e;
063                    }
064                }
065            }
066            /** msg validations **/
067            try {
068                if ( msgVld != null ){
069                    for (ISOBaseValidator aMsgVld : this.msgVld) {
070                        if (aMsgVld != null)
071                            m = aMsgVld.validate(m);
072                    }
073                }
074            }
075            catch (ISOVException ex) {
076                evt.addMessage( "Component Validation Error." );
077                throw ex;
078            }
079            return m;
080        }
081        finally {
082            Logger.log( evt );
083        }
084    }
085
086//    public void setFieldValidator( ISOFieldValidator[] fvlds ){
087//        this.fldVld = fvlds;
088//    }
089
090    /**
091     * Sets the array of field validators.
092     * @param fvlds the field validator array
093     */
094    public void setFieldValidator( ISOValidator[] fvlds ){
095        this.fldVld = fvlds;
096    }
097
098    /**
099     * Sets the array of message-level validators.
100     * @param msgVlds the message validator array
101     */
102    public void setMsgValidator( ISOBaseValidator[] msgVlds ){
103        this.msgVld = msgVlds;
104    }
105
106    /** Message level validators **/
107    protected ISOBaseValidator[] msgVld;
108    /** field validator array. **/
109//    protected ISOFieldValidator[] fldVld;
110    protected ISOValidator[] fldVld;
111}