Skip to content
Snippets Groups Projects

Add a CSV Service

5 files
+ 268
0
Compare changes
  • Side-by-side
  • Inline

Files

/*
* Portal - A site for direct access to the Labrador system as a teacher or admin
* Copyright (C) 2020- Delft University of Technology
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package nl.tudelft.portal.service;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.opencsv.*;
import com.opencsv.exceptions.CsvValidationException;
@Service
public class CSVService {
@Value("${portal.csv-separator}")
private char separator;
/**
* Gets the response for sending a csv file.
*
* @param path the path to the file to be sent
*/
public ResponseEntity<Resource> getResponse(Path path) throws MalformedURLException {
Resource resource = new UrlResource("file:" + path);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("text/csv"))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + path + "\"")
.body(resource);
}
/**
* Reads data from a CSV file.
*
* @param path the path to the file
* @return the data from the CSV file
* @throws IOException if the file path is invalid or bad things happen during the read
* @throws CsvValidationException if a user defined validator fails
*/
public List<String[]> readCSV(Path path) throws IOException, CsvValidationException {
return readCSV(path, true, 0);
}
/**
* Reads the data from a CSV file.
*
* @param path the path to the file which should be read
* @param ignoreQuotations whether to ignore quotation marks in the parsing
* @param skipLines the number of lines to skip
* @return the data from the CSV file
* @throws IOException if the file path is invalid or bad things happen during the read
* @throws CsvValidationException if a user defined validator fails
*/
public List<String[]> readCSV(Path path, boolean ignoreQuotations, int skipLines)
throws IOException, CsvValidationException {
// initialize the parser of the csv
CSVParser parser = new CSVParserBuilder()
.withSeparator(separator)
.withIgnoreQuotations(ignoreQuotations)
.build();
// initialize the reader of the csv
Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
CSVReader csvReader = new CSVReaderBuilder(reader)
.withCSVParser(parser)
.withSkipLines(skipLines)
.build();
// read the data from the csv
List<String[]> data = new ArrayList<>();
String[] line;
while ((line = csvReader.readNext()) != null) {
data.add(line);
}
// close the readers
reader.close();
csvReader.close();
return data;
}
/**
* Writes data to a csv file.
*
* @param data the data to be written
* @param directory the path to the file directory
* @param fileName the name of the file
* @throws IOException if the filename is wrong or an error occurs during closing of the writer
*/
public void writeToCSV(List<String[]> data, Path directory, String fileName) throws IOException {
if (!directory.toFile().exists()) {
directory.toFile().mkdirs();
}
CSVWriter writer = new CSVWriter(new FileWriter(
Path.of(directory.toString(), fileName).toFile()),
separator,
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
writer.writeAll(data);
writer.close();
}
/**
* Converts a string into a valid filename.
*
* @param string the string to be converted
* @return a string which has the format of a valid filename
*/
public String convertToValidFileName(String string) {
if (string == null || string.isBlank()) {
return "unknown";
}
StringBuilder filename = new StringBuilder();
for (char c : string.toCharArray()) {
if (Character.isDigit(c) || Character.isAlphabetic(c)) {
filename.append(c);
} else if (c == ' ') {
filename.append('_');
} else if (c == '/') {
filename.append('-');
}
}
return filename.toString();
}
}
Loading