All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.way.io.TableReaderXls Maven / Gradle / Ivy

/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.io;

import java.util.List;
import java.util.stream.Stream;

import br.com.objectos.comuns.io.Line;
import br.com.objectos.comuns.io.ParsedLines;
import br.com.objectos.comuns.io.xls.XlsFile;
import br.com.objectos.core.collections.ImmutableList;
import br.com.objectos.core.collections.MoreCollectors;
import br.com.objectos.way.io.AbstractTableParser.Sheet;
import br.com.objectos.way.io.Table.Error;

/**
 * @author [email protected] (Marcio Endo)
 */
class TableReaderXls implements TableSet {

  private final XlsFile xls;
  private final AbstractTableParser parser;
  private final AbstractTableFilter filter;

  public TableReaderXls(XlsFile xls, AbstractTableParser parser, AbstractTableFilter filter) {
    this.xls = xls;
    this.parser = parser;
    this.filter = filter;
  }

  @Override
  public Table sheetNamed(String name) {
    Sheet sheet = AbstractTableParser.sheetNamed(name);
    ParsedLines lines = xls.withSheetName(name).getLines();

    List protos = lines.stream()
        .map(line -> new Wrapper(sheet, line))
        .map(Wrapper::parse)
        .collect(MoreCollectors.toImmutableList());

    List valids = protos.stream()
        .map(o -> o.entity)
        .filter(input -> input != null && filter.apply(input))
        .collect(MoreCollectors.toImmutableList());

    List errors = protos.stream()
        .flatMap(Wrapper::toErrors)
        .collect(MoreCollectors.toImmutableList());

    return new TablePojo(valids, errors);
  }

  @Override
  public Table sheetNamed(String template, Object... args) {
    String name = String.format(template, args);
    return sheetNamed(name);
  }

  @Override
  public Table get() {
    throw new UnsupportedOperationException();
  }

  private class Wrapper {

    final Sheet sheet;

    final Line line;

    List errors = ImmutableList.of();

    T entity;

    public Wrapper(Sheet sheet, Line line) {
      this.sheet = sheet;
      this.line = line;
    }

    public Wrapper parse() {
      try {
        entity = parser.parseLine(sheet, line);
      } catch (Exception e) {
        Error error = TableRowError.fromException(line, e);
        errors = ImmutableList.of(error);
      }
      return this;
    }

    public Stream toErrors() {
      return errors.stream();
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy