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    public ISOBaseValidatingPackager() {
042        super();
043    }
044
045    public ISOComponent validate(ISOComponent m) throws ISOException {
046        LogEvent evt = new LogEvent( this, "validate" );
047        try {
048            ISOComponent c;
049            Map fields = m.getChildren();
050            /** Field  validations **/
051            for (ISOValidator aFldVld : fldVld) {
052                if (aFldVld != null && (c = (ISOComponent) fields.get(Integer.valueOf(((ISOFieldValidator) aFldVld).getFieldId()))) != null) {
053                    try {
054                        m.set(aFldVld.validate(c));
055                    } catch (ISOVException e) {
056                        if (!e.treated()) {
057                            m.set(e.getErrComponent());
058                            e.setTreated(true);
059                        }
060                        evt.addMessage("Component Validation Error.");
061                        throw e;
062                    }
063                }
064            }
065            /** msg validations **/
066            try {
067                if ( msgVld != null ){
068                    for (ISOBaseValidator aMsgVld : this.msgVld) {
069                        if (aMsgVld != null)
070                            m = aMsgVld.validate(m);
071                    }
072                }
073            }
074            catch (ISOVException ex) {
075                evt.addMessage( "Component Validation Error." );
076                throw ex;
077            }
078            return m;
079        }
080        finally {
081            Logger.log( evt );
082        }
083    }
084
085//    public void setFieldValidator( ISOFieldValidator[] fvlds ){
086//        this.fldVld = fvlds;
087//    }
088
089    public void setFieldValidator( ISOValidator[] fvlds ){
090        this.fldVld = fvlds;
091    }
092
093
094    public void setMsgValidator( ISOBaseValidator[] msgVlds ){
095        this.msgVld = msgVlds;
096    }
097
098    /** Message level validators **/
099    protected ISOBaseValidator[] msgVld;
100    /** field validator array. **/
101//    protected ISOFieldValidator[] fldVld;
102    protected ISOValidator[] fldVld;
103}