AfgifteNummerScanUitvoerenActionBean.java

  1. /*
  2.  * Copyright (C) 2019 B3Partners B.V.
  3.  *
  4.  * This program is free software: you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation, either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  */
  17. package nl.b3p.brmo.service.stripes;

  18. import java.io.PrintWriter;
  19. import java.io.StringWriter;
  20. import java.util.Date;
  21. import net.sourceforge.stripes.action.ActionBean;
  22. import net.sourceforge.stripes.action.ActionBeanContext;
  23. import net.sourceforge.stripes.action.After;
  24. import net.sourceforge.stripes.action.Before;
  25. import net.sourceforge.stripes.action.DefaultHandler;
  26. import net.sourceforge.stripes.action.ForwardResolution;
  27. import net.sourceforge.stripes.action.Resolution;
  28. import net.sourceforge.stripes.action.SimpleMessage;
  29. import net.sourceforge.stripes.validation.SimpleError;
  30. import net.sourceforge.stripes.validation.Validate;
  31. import nl.b3p.brmo.persistence.staging.AfgifteNummerScannerProces;
  32. import nl.b3p.brmo.persistence.staging.AutomatischProces;
  33. import nl.b3p.brmo.service.scanner.AbstractExecutableProces;
  34. import nl.b3p.brmo.service.scanner.AfgifteNummerScanner;
  35. import nl.b3p.brmo.service.scanner.ProgressUpdateListener;
  36. import org.stripesstuff.plugin.waitpage.WaitPage;
  37. import org.stripesstuff.stripersist.EntityTypeConverter;
  38. import org.stripesstuff.stripersist.Stripersist;

  39. /**
  40.  * actionbean voor uitvoeren van afgiftenummer scan.
  41.  *
  42.  * @since 2.0.0
  43.  * @author mprins
  44.  */
  45. public class AfgifteNummerScanUitvoerenActionBean implements ActionBean, ProgressUpdateListener {

  46.   private static final String JSP = "/WEB-INF/jsp/beheer/afgiftenummerscanneruitvoeren.jsp";

  47.   @Validate(converter = EntityTypeConverter.class)
  48.   private AfgifteNummerScannerProces proces;

  49.   private ActionBeanContext context;

  50.   private String exceptionStacktrace;

  51.   private String status;

  52.   private long total;

  53.   private boolean complete;

  54.   private Date start;

  55.   private Date update;

  56.   private StringBuilder log = new StringBuilder();

  57.   @DefaultHandler
  58.   @WaitPage(path = JSP, delay = 1000, refresh = 1000)
  59.   public Resolution execute() {
  60.     String samenvatting = null;

  61.     if (proces == null) {
  62.       getContext().getMessages().add(new SimpleMessage("Proces ongeldig!"));
  63.       completed();
  64.       return new ForwardResolution(JSP);
  65.     }

  66.     proces = Stripersist.getEntityManager().find(AfgifteNummerScannerProces.class, proces.getId());
  67.     AfgifteNummerScanner _proces =
  68.         (AfgifteNummerScanner) AbstractExecutableProces.getProces(proces);

  69.     try {
  70.       _proces.execute(this);
  71.       this.proces.setOntbrekendeNummersGevonden(_proces.getOntbrekendeNummersGevonden());
  72.       getContext().getMessages().add(new SimpleMessage("Afgiftenummer scan is afgerond."));
  73.       samenvatting =
  74.           "Er zijn "
  75.               + (_proces.getOntbrekendeNummersGevonden() ? "" : "geen")
  76.               + " ontbrekende afgiftenummers geconstateerd.";
  77.       getContext().getMessages().add(new SimpleMessage(samenvatting));
  78.     } catch (Exception ex) {
  79.       this.proces.setStatus(AutomatischProces.ProcessingStatus.ERROR);
  80.       Stripersist.getEntityManager().merge(this.proces);
  81.       exception(ex);
  82.       samenvatting =
  83.           "Er is een fout opgetreden tijdens het bepalen van ontbrekende afgiftenummers.";
  84.       getContext()
  85.           .getMessages()
  86.           .add(
  87.               new SimpleError(
  88.                   "Er is een fout opgetreden tijdens het bepalen van ontbrekende afgiftenummers. {2}",
  89.                   ex.getMessage()));
  90.     } finally {
  91.       this.completed();
  92.       this.proces.updateSamenvattingEnLogfile(this.log.toString());
  93.       // de hele log is te groot voor de samenvatting, maar #updateSamenvattingEnLogfile is
  94.       // wel handig om te gebruiken
  95.       this.proces.setSamenvatting(samenvatting);

  96.       if (Stripersist.getEntityManager().getTransaction().isActive()) {
  97.         Stripersist.getEntityManager().getTransaction().commit();
  98.       }
  99.     }
  100.     return new ForwardResolution(JSP);
  101.   }

  102.   @Override
  103.   public void total(long total) {
  104.     this.total = total;
  105.   }

  106.   @Override
  107.   public void progress(long progress) {
  108.     // this.processed = progress;
  109.     this.total = progress;
  110.     this.update = new Date();
  111.   }

  112.   @Override
  113.   public void exception(Throwable t) {
  114.     StringWriter sw = new StringWriter();
  115.     t.printStackTrace(new PrintWriter(sw));
  116.     this.exceptionStacktrace = sw.toString();
  117.   }

  118.   @Override
  119.   public void updateStatus(String status) {
  120.     this.status = status;
  121.   }

  122.   @Override
  123.   public void addLog(String line) {
  124.     this.log.append(line).append("\n");
  125.   }

  126.   @Before
  127.   public void before() {
  128.     start = new Date();
  129.   }

  130.   @After
  131.   public void completed() {
  132.     complete = true;
  133.     proces.setLastrun(this.update);
  134.   }

  135.   // <editor-fold defaultstate="collapsed" desc="getters en setters">
  136.   @Override
  137.   public void setContext(ActionBeanContext context) {
  138.     this.context = context;
  139.   }

  140.   @Override
  141.   public ActionBeanContext getContext() {
  142.     return this.context;
  143.   }

  144.   public AfgifteNummerScannerProces getProces() {
  145.     return proces;
  146.   }

  147.   public void setProces(AfgifteNummerScannerProces proces) {
  148.     this.proces = proces;
  149.   }

  150.   public String getLog() {
  151.     return log.toString();
  152.   }

  153.   public void setLog(String log) {
  154.     this.log = new StringBuilder(log);
  155.   }

  156.   public String getStatus() {
  157.     return status;
  158.   }

  159.   public void setStatus(String status) {
  160.     this.status = status;
  161.   }

  162.   public String getExceptionStacktrace() {
  163.     return exceptionStacktrace;
  164.   }

  165.   public void setExceptionStacktrace(String exceptionStacktrace) {
  166.     this.exceptionStacktrace = exceptionStacktrace;
  167.   }

  168.   public boolean isComplete() {
  169.     return complete;
  170.   }

  171.   public void setComplete(boolean complete) {
  172.     this.complete = complete;
  173.   }

  174.   public Date getStart() {
  175.     return start;
  176.   }

  177.   public void setStart(Date start) {
  178.     this.start = start;
  179.   }

  180.   public Date getUpdate() {
  181.     return update;
  182.   }

  183.   public void setUpdate(Date update) {
  184.     this.update = update;
  185.   }

  186.   public long getTotal() {
  187.     return this.total;
  188.   }

  189.   public void setTotal(long total) {
  190.     this.total = total;
  191.   }
  192.   // </editor-fold>
  193. }