Search in sources :

Example 1 with Parser

use of com.bakdata.conquery.models.preproc.parser.Parser in project conquery by bakdata.

the class Preprocessor method applyOutputs.

/**
 * Apply each output for a single row. Returning all resulting values.
 */
private static Object[] applyOutputs(List<OutputDescription.Output> outputs, PPColumn[] columns, String[] row, long lineId) throws ParsingException, OutputDescription.OutputException {
    Object[] outRow = new Object[outputs.size()];
    for (int index = 0; index < outputs.size(); index++) {
        final OutputDescription.Output out = outputs.get(index);
        try {
            final Parser parser = columns[index].getParser();
            final Object result = out.createOutput(row, parser, lineId);
            if (result == null) {
                continue;
            }
            outRow[index] = result;
        } catch (Exception e) {
            throw new OutputDescription.OutputException(out.getDescription(), e);
        }
    }
    return outRow;
}
Also used : OutputDescription(com.bakdata.conquery.models.preproc.outputs.OutputDescription) ParsingException(com.bakdata.conquery.models.exceptions.ParsingException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Parser(com.bakdata.conquery.models.preproc.parser.Parser) CsvParser(com.univocity.parsers.csv.CsvParser)

Example 2 with Parser

use of com.bakdata.conquery.models.preproc.parser.Parser in project conquery by bakdata.

the class CompoundDateRangeOutput method createForHeaders.

@Override
public Output createForHeaders(Object2IntArrayMap<String> headers, DateReader dateReader) {
    assertRequiredHeaders(headers, startColumn, endColumn);
    final int startIndex = headers.getInt(startColumn);
    final int endIndex = headers.getInt(endColumn);
    // Obviously this mean doing the work twice, but it's still better than storing the data twice also.
    return new Output() {

        @Override
        protected Object parseLine(String[] row, Parser type, long sourceLine) throws ParsingException {
            if (Strings.isNullOrEmpty(row[startIndex]) && Strings.isNullOrEmpty(row[endIndex])) {
                return false;
            }
            if (!allowOpen && (Strings.isNullOrEmpty(row[startIndex]) || Strings.isNullOrEmpty(row[endIndex]))) {
                throw new IllegalArgumentException("Open Ranges are not allowed.");
            }
            final LocalDate start = dateReader.parseToLocalDate(row[startIndex]);
            final LocalDate end = dateReader.parseToLocalDate(row[endIndex]);
            return // Since it's not possible that BOTH are null either of them being null already implies an open and therefore valid range.
            (start == null || end == null) || // row is included if start <= end
            start.isBefore(end) || start.isEqual(end);
        }
    };
}
Also used : LocalDate(java.time.LocalDate) CompoundDateRangeParser(com.bakdata.conquery.models.preproc.parser.specific.CompoundDateRangeParser) Parser(com.bakdata.conquery.models.preproc.parser.Parser)

Example 3 with Parser

use of com.bakdata.conquery.models.preproc.parser.Parser in project conquery by bakdata.

the class DateRangeOutput method createForHeaders.

@Override
public Output createForHeaders(Object2IntArrayMap<String> headers, DateReader dateReader) {
    assertRequiredHeaders(headers, startColumn, endColumn);
    final int startIndex = headers.getInt(startColumn);
    final int endIndex = headers.getInt(endColumn);
    return new Output() {

        @Override
        protected Object parseLine(String[] row, Parser type, long sourceLine) throws ParsingException {
            if (Strings.isNullOrEmpty(row[startIndex]) && Strings.isNullOrEmpty(row[endIndex])) {
                return null;
            }
            if (!allowOpen && (Strings.isNullOrEmpty(row[startIndex]) || Strings.isNullOrEmpty(row[endIndex]))) {
                throw new IllegalArgumentException("Open Ranges are not allowed.");
            }
            LocalDate start = dateReader.parseToLocalDate(row[startIndex]);
            LocalDate end = dateReader.parseToLocalDate(row[endIndex]);
            return CDateRange.of(start, end);
        }
    };
}
Also used : LocalDate(java.time.LocalDate) Parser(com.bakdata.conquery.models.preproc.parser.Parser) DateParser(com.bakdata.conquery.models.preproc.parser.specific.DateParser) DateRangeParser(com.bakdata.conquery.models.preproc.parser.specific.DateRangeParser)

Aggregations

Parser (com.bakdata.conquery.models.preproc.parser.Parser)3 LocalDate (java.time.LocalDate)2 ParsingException (com.bakdata.conquery.models.exceptions.ParsingException)1 OutputDescription (com.bakdata.conquery.models.preproc.outputs.OutputDescription)1 CompoundDateRangeParser (com.bakdata.conquery.models.preproc.parser.specific.CompoundDateRangeParser)1 DateParser (com.bakdata.conquery.models.preproc.parser.specific.DateParser)1 DateRangeParser (com.bakdata.conquery.models.preproc.parser.specific.DateRangeParser)1 CsvParser (com.univocity.parsers.csv.CsvParser)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1