Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #299 - upgraded OpenCSV to version 5.9 #300

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
- Geotools to version 31.3 [#249](https://github.com/IN-CORE/incore-services/issues/249)
- Jersey to version 3.1.7 [#250](https://github.com/IN-CORE/incore-services/issues/250)
- OpenCSV to version 5.9 [#299](https://github.com/IN-CORE/incore-services/issues/299)
- Jackson library to 2.17.0 [#303](https://github.com/IN-CORE/incore-services/issues/303)

## [1.26.1] - 2024-04-08
Expand Down
2 changes: 1 addition & 1 deletion server/data-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
dependencies {
implementation("com.google.inject:guice:7.0.0")

implementation("com.opencsv:opencsv:4.6")
implementation("com.opencsv:opencsv:5.9")

implementation("org.jsoup:jsoup:1.16.1")
implementation("com.github.lookfirst:sardine:5.12")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

package edu.illinois.ncsa.incore.service.data.controllers;

import com.opencsv.exceptions.CsvValidationException;
import edu.illinois.ncsa.incore.common.HazardConstants;
import edu.illinois.ncsa.incore.common.auth.Authorizer;
import edu.illinois.ncsa.incore.common.auth.IAuthorizer;
Expand Down Expand Up @@ -974,6 +975,9 @@ public Dataset uploadFiles(@Parameter(name = "Dataset Id from data service", req
} catch (IncoreHTTPException e) {
logger.error(e.getMessage());
throw new IncoreHTTPException(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage());
} catch (CsvValidationException e) {
logger.error(e.getMessage());
throw new RuntimeException(e);
}
// clean up
FileUtils.deleteTmpDir(geoPkgFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.github.sardine.DavResource;
import com.github.sardine.Sardine;
import com.github.sardine.SardineFactory;
import com.opencsv.exceptions.CsvValidationException;
import edu.illinois.ncsa.incore.common.exceptions.IncoreHTTPException;
import edu.illinois.ncsa.incore.service.data.dao.HttpDownloader;
import edu.illinois.ncsa.incore.service.data.dao.IRepository;
Expand Down Expand Up @@ -694,7 +695,7 @@ public static File loadZipdataFromRepository(String inId) throws IOException {
* @throws URISyntaxException
* @throws IOException
*/
public static File joinShpTable(Dataset dataset, IRepository repository, boolean isRename) throws IncoreHTTPException, IOException {
public static File joinShpTable(Dataset dataset, IRepository repository, boolean isRename) throws IncoreHTTPException, IOException, CsvValidationException {
List<FileDescriptor> csvFDs = dataset.getFileDescriptors();
File csvFile = null;
File shapeFile = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@

package edu.illinois.ncsa.incore.service.data.utils;

import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvException;
import com.opencsv.exceptions.CsvValidationException;
import edu.illinois.ncsa.incore.common.exceptions.IncoreHTTPException;
import edu.illinois.ncsa.incore.common.utils.GeoUtils;
import edu.illinois.ncsa.incore.service.data.models.Dataset;
Expand Down Expand Up @@ -269,7 +274,7 @@ public static SimpleFeatureCollection getSimpleFeatureCollectionFromGeopackage(F
* @return
* @throws IOException
*/
public static File joinTableShapefile(Dataset dataset, List<File> shpfiles, File csvFile, boolean isRename) throws IOException {
public static File joinTableShapefile(Dataset dataset, List<File> shpfiles, File csvFile, boolean isRename) throws IOException, CsvValidationException {
String outFileName = FilenameUtils.getBaseName(csvFile.getName()) + "." + FileUtils.EXTENSION_SHP;

// create temp dir and copy files to temp dir
Expand Down Expand Up @@ -360,7 +365,7 @@ public static SimpleFeatureSource createShapefileSource(String shapefilePath) th
}

public static SimpleFeatureCollection createCsvFeatureFromCsvType(String csvFilePath, SimpleFeatureType featureType)
throws IOException {
throws IOException, CsvValidationException {
// create SimpleFeatureCollection from csv file
List<SimpleFeature> csvFeatures = new ArrayList<>();

Expand Down Expand Up @@ -399,7 +404,7 @@ public static Object convertStringToType(String value, Class<?> outType) {
}
}

public static SimpleFeatureType createCsvFeatureType(String csvFilePath, String sourceName) throws IOException {
public static SimpleFeatureType createCsvFeatureType(String csvFilePath, String sourceName) throws IOException, CsvValidationException {
// read CSV file to get column names and types
try (CSVReader reader = new CSVReader(new FileReader(csvFilePath))) {
String[] header = reader.readNext(); // assuming the first row is the header
Expand Down Expand Up @@ -526,7 +531,7 @@ public static void addAttributesWithoutDuplicate(SimpleFeatureTypeBuilder builde
}
}

public static File joinTableGeopackage(Dataset dataset, String gpkgFileName, File csvFile, boolean isRename) throws IOException {
public static File joinTableGeopackage(Dataset dataset, String gpkgFileName, File csvFile, boolean isRename) throws IOException, CsvException {
// set geometry factory
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
String outFileName = FilenameUtils.getBaseName(csvFile.getName()) + "." + FileUtils.EXTENSION_SHP;
Expand Down Expand Up @@ -933,8 +938,9 @@ public static File outToGpkgFile(File geoPkgPathFile, DefaultFeatureCollection c
* @throws IOException
*/
@SuppressWarnings("resource")
public static List<String[]> readCsvFile(File inCsv) throws IOException {
CSVReader reader = new CSVReader(new FileReader(inCsv), ',', '"', 1);
public static List<String[]> readCsvFile(File inCsv) throws IOException, CsvException {
CSVParser parser = new CSVParserBuilder().withSeparator(',').withQuoteChar('"').build();
CSVReader reader = new CSVReaderBuilder(new FileReader(inCsv)).withSkipLines(1).withCSVParser(parser).build();
List<String[]> rows = reader.readAll();
return rows;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
*******************************************************************************/
package edu.illinois.ncsa.incore.service.data.controllers;

import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import edu.illinois.ncsa.incore.common.HazardConstants;
import edu.illinois.ncsa.incore.service.data.models.Dataset;
import mocks.MockApplication;
Expand All @@ -29,7 +33,9 @@
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -185,4 +191,17 @@ public void testFindHazardDatasetTypeWithBadDataType() throws IOException {
assertFalse(isHazardDataset);
}

@Test
public void testReadCSV() throws Exception {
// Verifies the CSV reader is parsing correctly and ignoring the header
StringBuilder csv = new StringBuilder();
csv.append("\"guid\",\"LS_0\", \"LS_1\"").append("\n");
csv.append("\"some-guid-1\",\"0.0\"\"0.0\"").append("\n");
csv.append("\"some-guid-2\",\"0.0\",\"0.0\"");
CSVParser parser = new CSVParserBuilder().withSeparator(',').withQuoteChar('"').build();
CSVReader reader = new CSVReaderBuilder(new StringReader(csv.toString())).withSkipLines(1).withCSVParser(parser).build();
List<String[]> rows = reader.readAll();
assertEquals(2, rows.size());
}

}
2 changes: 1 addition & 1 deletion server/semantics-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ dependencies {
implementation group: 'org.freemarker', name: 'freemarker', version: '2.3.32'
implementation group: 'org.apache.jena', name: 'jena-core', version: '3.1.1'
implementation project(':semantic-core')
implementation("com.opencsv:opencsv:4.6")
implementation("com.opencsv:opencsv:5.9")
}

Loading