/**
   * 1. get fileComponent from action
   *
   * <p>2. validate fileName to see if filePrefix is allowed
   *
   * <p>3. validate file format with the import file template
   *
   * <p>4.
   *
   * @see
   *     com.pc.core.web.interceptor.AroundInterceptor#before(com.opensymphony.xwork2.ActionInvocation)
   */
  public void before(ActionInvocation invocation) throws Exception {

    Action action = (Action) invocation.getAction();
    HttpServletRequest request =
        (HttpServletRequest)
            invocation.getInvocationContext().get(ServletActionContext.HTTP_REQUEST);

    if (action instanceof ImportPreparation && request instanceof MultiPartRequestWrapper) {

      ServletContext servletContext = ServletActionContext.getServletContext();
      ActionContext invocationContext = invocation.getInvocationContext();

      ImportPreparation importPreparation = (ImportPreparation) action;

      // 1. get fileComponent from valueStack
      FileComponent fileComponent =
          (FileComponent) invocation.getStack().findValue("fileComponent");

      if (fileComponent == null || fileComponent.getUpload() == null)
        throw new ImportException(
            "error.upload.file.empty", "r:" + importPreparation.getErrorReturn() + "/import/error");

      // 2. validate fileName
      String fileExt = fileComponent.getFileExtension();
      if (!Arrays.asList(this.allowedPrefix).contains(fileExt)) {
        throw new ImportException(
            "error.upload.file-ext.not-allowed",
            "r:" + importPreparation.getErrorReturn() + "/import/error");
      }

      // Create CsvReader to parse the file
      CsvReader csvReader = new CsvReader(new FileReader(fileComponent.getUpload()));

      try {

        // get file header information from cache
        String header =
            (String)
                servletContext.getAttribute(
                    importPreparation.getTarget().toUpperCase() + "_HEADERS");
        String[] headerKeys = header.split(",");

        // 3. validate file format
        if (csvReader.readHeaders()) {
          if (headerKeys.length != csvReader.getHeaderCount()) {
            throw new ImportException(
                "error.upload.file-format.mismatch",
                "r:" + importPreparation.getErrorReturn() + "/import/error");
          }
        }

        // Read data from CsvReader
        List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        while (csvReader.readRecord()) {
          Map<String, String> record = new LinkedHashMap<String, String>();
          for (int i = 0; i < headerKeys.length; i++) {
            record.put(headerKeys[i], csvReader.get(i));
          }
          data.add(record);
        }

        // 4. validate data
        importPreparation.validate(data);

        // 5. set data
        OgnlContextState.setCreatingNullObjects(invocationContext.getContextMap(), true);
        OgnlContextState.setDenyMethodExecution(invocationContext.getContextMap(), true);
        OgnlContextState.setReportingConversionErrors(invocationContext.getContextMap(), true);

        prepareImportData(invocation, importPreparation.getDataName(), data);

      } finally {

        // release all the resource anyway
        csvReader.close();

        OgnlContextState.setCreatingNullObjects(invocationContext.getContextMap(), true);
        OgnlContextState.setDenyMethodExecution(invocationContext.getContextMap(), true);
        OgnlContextState.setReportingConversionErrors(invocationContext.getContextMap(), true);
      }
    }
  }
  public PaymentStatistic processKZ(File csvFile, LoggingHandler lh) throws IOException {

    CsvReader reader = null;
    ArrayList<PaymentRecord> recordList = new ArrayList();
    Double sum = 0.0;
    PaymentStatistic statisticAll = new PaymentStatistic();

    try {

      reader = new CsvReader(csvFile.getCanonicalPath());
      reader.setDelimiter(';');
      reader.readHeaders();

      // Headers ohne Spaces mittels trim
      String[] headers = new String[reader.getHeaderCount()];
      for (int i = 0; i < reader.getHeaderCount(); i++) {
        headers[i] = reader.getHeader(i).trim();
      }
      reader.setHeaders(headers);
      // http://www.csvreader.com/java_csv_samples.php
      // http://javacsv.sourceforge.net/

      PaymentRecord record = new PaymentRecord(reader);
      recordList.add(record);

      while (true) {
        try {

          PaymentRecord newRecord = new PaymentRecord(reader);

          if (newRecord.getKontoNr() == record.getKontoNr()) {
            // nicht verarbeiten, gleicher Kunde
            recordList.add(newRecord);
            sum = sum + newRecord.getBetragNr();
            record = newRecord;
            continue;
          }

          // unterschiedliche Kundennummer, alte Belege verarbeiten
          if (sum == 0.0) {
            PaymentStatistic statistic = processKZPaymentRecord(recordList, lh);
            statisticAll.addStatistic(statistic);
          }
          // System.out.println("Kunde " + record.getKontoStr() + "OP: " + sum);
          recordList = new ArrayList();
          sum = 0.0;
          recordList.add(newRecord);
          sum = sum + newRecord.getBetragNr();
          record = newRecord;
          continue;

        } catch (Throwable th) {
          // kein neuer record mehr, Liste verarbeiten
          if (sum == 0.0) {
            PaymentStatistic statistic = processKZPaymentRecord(recordList, lh);
            statisticAll.addStatistic(statistic);
          }
        }
        break;
      }

      reader.close();

    } catch (Throwable th) {
      lh.getLogger(Globals.LOGGINGKZ).log(Level.SEVERE, null, th);
      return statisticAll;
    }
    return statisticAll;
  }