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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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 @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed
- 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)

## [1.26.1] - 2024-04-08

Expand Down
2 changes: 1 addition & 1 deletion server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ test {
implementation("com.sun.activation:javax.activation:1.2.0")
implementation("de.grundid.opendatalab:geojson-jackson:1.8")
implementation("org.jamel.dbf:dbf-reader:0.2.0")
implementation("org.dom4j:dom4j:2.1.1")
implementation("org.dom4j:dom4j:2.1.3")

//base jersey and jackson
implementation("jakarta.ws.rs:jakarta.ws.rs-api:3.1.0")
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 @@ -263,7 +268,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 @@ -354,7 +359,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 @@ -393,7 +398,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 @@ -520,7 +525,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 @@ -927,8 +932,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")
}

4 changes: 2 additions & 2 deletions server/tools-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ apply plugin: 'java'

dependencies {
implementation 'commons-lang:commons-lang:2.6'
implementation 'org.dom4j:dom4j:2.0.0'
implementation 'org.dom4j:dom4j:2.1.3'
implementation 'log4j:log4j:1.2.17.norce'
}
}
Loading