/*
 * Copyright 2021 Optica Abrechnungszentrum Dr. Gueldener GmbH
 */
package gdv.xport.event;

import gdv.xport.config.Config;
import gdv.xport.satz.Satz;
import gdv.xport.util.SimpleConstraintViolation;
import net.sf.oval.ConstraintViolation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * Der Klasse SatzValidator ist als {@link ImportListener} ausgelegt. Damit
 * kann er waehrend eines Imports ueber den {@link gdv.xport.DatenpaketStreamer}
 * registriert werden und den Import ueberwachen (validieren).
 *
 * @author oboehm
 * @since 5.4 (12.11.21)
 */
public class SatzValidator implements ImportListener {

    private static final Logger LOG = LogManager.getLogger();
    private final Config config;
    private final List<ConstraintViolation> violations = new ArrayList<>();
    private int satzNr = 1;
    private int tdsNr = 1;

    public SatzValidator() {
        this(Config.STRICT);
    }

    public SatzValidator(Config config) {
        this.config = config;
    }

    @Override
    public void notice(Satz satz) {
        List<ConstraintViolation> constraintViolations = satz.validate(config);
        if (!constraintViolations.isEmpty()) {
            LOG.warn("Record {} Satz {} {}:", tdsNr, satzNr, satz.toShortString());
            for (ConstraintViolation cv : constraintViolations) {
                LOG.warn("\t* {}", cv);
                if (cv instanceof SimpleConstraintViolation) {
                    logViolations((SimpleConstraintViolation) cv);
                }
            }
        }
        satzNr++;
        tdsNr += satz.getNumberOfTeildatensaetze();
        violations.addAll(constraintViolations);
    }

    private static void logViolations(SimpleConstraintViolation scv) {
        for (ConstraintViolation cv : scv.getViolations()) {
            LOG.warn("\t\t* {}", cv);
        }
    }

    public List<ConstraintViolation> getViolations() {
        return violations;
    }

}

@@ -12,58 +12,618 @@
 ight
+ (c)
  2021 
-Optica Abrechnungszentrum Dr. Gueldener GmbH
+by Oli B.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *   http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A *%0A * (c)reated 12-11-2021 by $USER
 %0A */

comment header korrigiert