AfgifteChecker.java

  1. /*
  2. *  Copyright (C) 2019  B3Partners B.V.

  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.

  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. * GNU Affero General Public License for more details.

  11. * You should have received a copy of the GNU Affero General Public License
  12. * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package nl.b3p.brmo.loader.checks;

  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.sql.SQLException;
  20. import java.util.List;
  21. import java.util.Map;
  22. import nl.b3p.brmo.loader.StagingProxy;
  23. import nl.b3p.brmo.loader.entity.Bericht;
  24. import nl.b3p.brmo.loader.entity.LaadProces;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;

  27. /**
  28.  * @author Meine Toonen
  29.  */
  30. public class AfgifteChecker {
  31.   private static final Log log = LogFactory.getLog(AfgifteChecker.class);

  32.   private List<Afgifte> afgiftes;
  33.   private StagingProxy staging;

  34.   public void init(String input, StagingProxy staging) throws IOException {
  35.     try (FileInputStream fin = new FileInputStream(new File(input))) {
  36.       init(fin, staging);
  37.     }
  38.   }

  39.   public void init(InputStream input, StagingProxy staging) throws IOException {
  40.     this.staging = staging;
  41.     // parse csv/excel
  42.     AfgiftelijstParser ap = new AfgiftelijstParser();
  43.     afgiftes = ap.parse(input);
  44.   }

  45.   public void check() {
  46.     afgiftes.forEach(this::check);
  47.   }

  48.   /**
  49.    * afgifte controle.
  50.    *
  51.    * @param afgifte de te controleren afgifte
  52.    */
  53.   private void check(Afgifte afgifte) {
  54.     try {
  55.       LaadProces lp = staging.getLaadProcesByRestoredFilename(afgifte.getBestandsnaam());
  56.       // kijk of afgifte bestaat in staging
  57.       if (lp != null) {
  58.         afgifte.setFoundInStaging(true);
  59.         processFoundLaadprocess(afgifte, lp);
  60.       }
  61.     } catch (SQLException ex) {
  62.       log.error("Error querying staging for laadproces for afgifte: " + afgifte.toString(), ex);
  63.     }
  64.   }

  65.   /**
  66.    * zoek de status van alle bijbehorende berichten op en leg die vast in de afgifte.
  67.    *
  68.    * @param afgifte de te controleren afgifte
  69.    * @param lp het laadproces waar de berichten bij zoeken
  70.    * @throws SQLException if any
  71.    */
  72.   private void processFoundLaadprocess(Afgifte afgifte, LaadProces lp) throws SQLException {
  73.     List<Bericht> berichten = staging.getBerichtByLaadProces(lp);
  74.     Map<Bericht.STATUS, Integer> counts = afgifte.getStatussen();
  75.     berichten.stream()
  76.         .peek(
  77.             (bericht) -> {
  78.               if (!counts.containsKey(bericht.getStatus())) {
  79.                 counts.put(bericht.getStatus(), 0);
  80.               }
  81.             })
  82.         .forEachOrdered(
  83.             (bericht) -> counts.put(bericht.getStatus(), counts.get(bericht.getStatus()) + 1));
  84.   }

  85.   public File getResults(String input, String f) throws IOException {
  86.     return getResults(input, new File(f));
  87.   }

  88.   public File getResults(String input, File f) throws IOException {
  89.     AfgiftelijstReport reporter = new AfgiftelijstReport();

  90.     reporter.createReport(afgiftes, input, f);
  91.     return f;
  92.   }
  93. }