BaseCsvImporter.java

package org.dynamoframework.importer.impl;

/*-
 * #%L
 * Dynamo Framework
 * %%
 * Copyright (C) 2014 - 2024 Open Circle Solutions
 * %%
 * 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.
 * #L%
 */

import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvException;
import org.apache.commons.io.input.CharSequenceReader;
import org.dynamoframework.exception.OCSImportException;
import org.dynamoframework.configuration.DynamoPropertiesHolder;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Base class for importing CSV files
 *
 * @author bas.rutten
 */
public class BaseCsvImporter extends BaseTextImporter {

	/**
	 * Counts the number of rows in the file
	 */
	@Override
	public int countRows(byte[] bytes, int sheetIndex) {
		List<String[]> lines = readCsvFile(bytes, DynamoPropertiesHolder.getDynamoProperties().getCsv().getSeparatorChar(), DynamoPropertiesHolder.getDynamoProperties().getCsv().getQuoteChar());
		return lines.size();
	}

	/**
	 * Reads a CSV file into a List of String arrays
	 *
	 * @param bytes     the raw content of the CSV file
	 * @param separator the record separator
	 * @param quote     the quote char
	 * @return the result of the read action
	 */
	protected List<String[]> readCsvFile(byte[] bytes, char separator, char quote) {
		try (CharSequenceReader seq = new CharSequenceReader(new String(bytes, StandardCharsets.UTF_8));
			 CSVReader reader = new CSVReaderBuilder(seq)
				 .withCSVParser(new CSVParserBuilder().withSeparator(separator).withQuoteChar(quote).build())
				 .build()) {
			return reader.readAll();
		} catch (IOException | CsvException ex) {
			throw new OCSImportException(ex.getMessage(), ex);
		}
	}

}