Skip to content

Commit

Permalink
Fix issue with empty paradata
Browse files Browse the repository at this point in the history
  • Loading branch information
alicela committed Sep 12, 2023
1 parent 8213eea commit 2094a22
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 1.4.1 - [2023-09-12]

### Fixed
- Fix issue with empty paradata (missing start session)

## 1.4.0 - [2023-08-22]

### Added
Expand Down
4 changes: 2 additions & 2 deletions kraftwerk-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
<parent>
<groupId>fr.insee.kraftwerk</groupId>
<artifactId>kraftwerk</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<artifactId>kraftwerk-api</artifactId>
<name>kraftwerk-api</name>
<packaging>jar</packaging>

<properties>
<kraftwerk.version>1.4.0</kraftwerk.version>
<kraftwerk.version>1.4.1</kraftwerk.version>
</properties>

<dependencies>
Expand Down
4 changes: 2 additions & 2 deletions kraftwerk-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>fr.insee.kraftwerk</groupId>
<artifactId>kraftwerk</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>kraftwerk-core</artifactId>
Expand Down Expand Up @@ -65,7 +65,7 @@
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.5.2</version>
<version>5.8</version>
</dependency>

<!-- VTL 2 with Trevas -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ public long createLengthSessionsVariable() {
}

public String getVariableStart() {
if (getSessions().size()==0) return "0";
return Long.toString(getSessions().get(0).getInitialization());
}

public String getVariableEnd() {
if (getSessions().size()==0) return "0";
return Long.toString(getLastSession().getTermination());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Map.Entry;
import java.util.Scanner;

import com.opencsv.CSVWriter;
import com.opencsv.CSVWriterBuilder;
import com.opencsv.ICSVWriter;

import fr.insee.kraftwerk.core.Constants;
Expand All @@ -34,11 +34,16 @@ private CsvTableWriter() {
}


private static CSVWriter setCSVWriter(Path filePath) throws IOException {
private static ICSVWriter setCSVWriter(Path filePath) throws IOException {
File file = filePath.toFile();
FileWriter outputFile = new FileWriter(file, StandardCharsets.UTF_8, true);
return new CSVWriter(outputFile, Constants.CSV_OUTPUTS_SEPARATOR, Constants.getCsvOutputQuoteChar(),
ICSVWriter.DEFAULT_ESCAPE_CHARACTER, ICSVWriter.DEFAULT_LINE_END);

return new CSVWriterBuilder(outputFile)
.withSeparator(Constants.CSV_OUTPUTS_SEPARATOR)
.withQuoteChar(Constants.getCsvOutputQuoteChar())
.withEscapeChar(ICSVWriter.DEFAULT_ESCAPE_CHARACTER)
.withLineEnd(ICSVWriter.DEFAULT_LINE_END)
.build();
}

/**
Expand All @@ -49,14 +54,13 @@ private static CSVWriter setCSVWriter(Path filePath) throws IOException {
*/
public static void updateCsvTable(Dataset dataset, Path filePath, Map<String,VariablesMap> metadataVariables, String datasetName) {
File file = filePath.toFile();
try (CSVWriter writer = setCSVWriter(filePath)){
try (ICSVWriter writer = setCSVWriter(filePath)){
String[] headers = getHeaders(file);

List<String> variablesSpec = initializeVariablesSpec(metadataVariables, datasetName);

//All the variables of the dataset
List<String> variablesDataset = new ArrayList<>(dataset.getDataStructure().keySet());
variablesDataset.add("New_var_test");
ArrayList<String> columns = getColumns(datasetName, variablesSpec, variablesDataset);

String[] columnsTable = convertWithStream(columns);
Expand All @@ -77,6 +81,7 @@ public static void updateCsvTable(Dataset dataset, Path filePath, Map<String,Var
for (int i = 0; i < dataset.getDataPoints().size(); i++) {
DataPoint dataPoint = dataset.getDataPoints().get(i);
String[] csvRow = new String[rowSize];

for (String variableName : variablesDataset) {
if(!variablesNotInHeaders.contains(variableName)){
int csvColumn = Arrays.asList(headers).indexOf(variableName);
Expand All @@ -86,7 +91,6 @@ public static void updateCsvTable(Dataset dataset, Path filePath, Map<String,Var
}
}
writer.writeNext(csvRow);

}
} catch (IOException e) {
log.error(String.format("IOException occurred when trying to update CSV table: %s", filePath));
Expand Down Expand Up @@ -137,7 +141,7 @@ private static String[] getHeaders(File file) throws FileNotFoundException {
*/
public static void writeCsvTable(Dataset dataset, Path filePath, Map<String,VariablesMap> metadataVariables, String datasetName) {
// File connection
try (CSVWriter writer = setCSVWriter(filePath)){
try (ICSVWriter writer = setCSVWriter(filePath)){

// Safety check
if (dataset.getDataStructure().size() == 0) {
Expand Down

0 comments on commit 2094a22

Please sign in to comment.