Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 6d65466

Browse files
1 parent 89d2790 commit 6d65466

9 files changed

Lines changed: 59 additions & 207 deletions

File tree

sormas-api/src/main/java/de/symeda/sormas/api/region/CountryDto.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package de.symeda.sormas.api.region;
22

3+
import javax.validation.constraints.Size;
4+
35
import de.symeda.sormas.api.EntityDto;
46
import de.symeda.sormas.api.utils.DataHelper;
57

@@ -15,7 +17,9 @@ public class CountryDto extends EntityDto {
1517

1618
private String defaultName;
1719
private String externalId;
20+
@Size(min = 3, max = 3)
1821
private String isoCode;
22+
@Size(min = 1, max = 3)
1923
private String unoCode;
2024
private boolean archived;
2125

sormas-api/src/main/resources/countries_ar.properties

Lines changed: 0 additions & 179 deletions
This file was deleted.

sormas-backend/src/main/resources/sql/sormas_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5559,7 +5559,7 @@ CREATE TABLE country (
55595559
changedate timestamp not null,
55605560
archived boolean not null default false,
55615561
defaultname varchar(255),
5562-
externalid varchar(4) unique,
5562+
externalid varchar(255),
55635563
isocode varchar(3) unique,
55645564
unocode varchar(3) unique,
55655565
primary key(id)

sormas-ui/src/main/java/de/symeda/sormas/ui/caze/importer/CountryImporter.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import java.io.File;
66
import java.io.IOException;
77
import java.lang.reflect.InvocationTargetException;
8+
import java.text.MessageFormat;
89
import java.util.function.Consumer;
910

11+
import javax.validation.constraints.Size;
12+
1013
import org.apache.commons.lang3.StringUtils;
1114

1215
import com.opencsv.exceptions.CsvValidationException;
@@ -88,7 +91,7 @@ private void insertColumnEntryIntoData(CountryDto newEntityDto, String value, St
8891
} else {
8992
PropertyDescriptor pd = new PropertyDescriptor(headerPathElementName, currentElement.getClass());
9093
Class<?> propertyType = pd.getPropertyType();
91-
94+
validateFieldLength(headerPathElementName, value);
9295
if (!executeDefaultInvokings(pd, currentElement, value, entityPropertyPath)) {
9396
throw new UnsupportedOperationException(
9497
I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, propertyType.getName()));
@@ -110,6 +113,24 @@ private void insertColumnEntryIntoData(CountryDto newEntityDto, String value, St
110113
}
111114
}
112115

116+
private void validateFieldLength(String field, String value) throws ImportErrorException, InvalidColumnException {
117+
try {
118+
Size size = CountryDto.class.getDeclaredField(field).getAnnotation(Size.class);
119+
if (StringUtils.isNotEmpty(value) && size != null) {
120+
if (value.length() < size.min()) {
121+
String message = "The value {0} has length {1} but the minimum length is {2}";
122+
throw new ImportErrorException(MessageFormat.format(message, value, value.length(), size.min()));
123+
}
124+
if (value.length() > size.max()) {
125+
String message = "The value {0} has length {1} but the maximum length is {2}";
126+
throw new ImportErrorException(MessageFormat.format(message, value, value.length(), size.max()));
127+
}
128+
}
129+
} catch (NoSuchFieldException e) {
130+
throw new InvalidColumnException(field);
131+
}
132+
}
133+
113134
public void startImport(Consumer<StreamResource> errorReportConsumer, UI currentUI) throws IOException, CsvValidationException {
114135
startImport(errorReportConsumer, currentUI, true);
115136
}

sormas-ui/src/main/java/de/symeda/sormas/ui/configuration/AbstractConfigurationView.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,18 @@ public void refreshMenu(SubMenu menu, Label infoLabel, Label infoLabelSub, Strin
6565
params);
6666

6767
if (UserProvider.getCurrent().hasUserRight(UserRight.INFRASTRUCTURE_VIEW)) {
68+
menu.addView(
69+
CountriesView.VIEW_NAME,
70+
I18nProperties.getPrefixCaption("View", CountriesView.VIEW_NAME.replaceAll("/", ".") + ".short", ""),
71+
null,
72+
false);
6873
if (FacadeProvider.getFeatureConfigurationFacade().isFeatureEnabled(FeatureType.INFRASTRUCTURE_TYPE_AREA)) {
6974
menu.addView(
7075
AreasView.VIEW_NAME,
7176
I18nProperties.getPrefixCaption("View", AreasView.VIEW_NAME.replaceAll("/", ".") + ".short", ""),
7277
null,
7378
false);
7479
}
75-
menu.addView(
76-
CountriesView.VIEW_NAME,
77-
I18nProperties.getPrefixCaption("View", CountriesView.VIEW_NAME.replaceAll("/", ".") + ".short", ""),
78-
null,
79-
false);
8080
menu.addView(
8181
RegionsView.VIEW_NAME,
8282
I18nProperties.getPrefixCaption("View", RegionsView.VIEW_NAME.replaceAll("/", ".") + ".short", ""),

sormas-ui/src/main/java/de/symeda/sormas/ui/configuration/infrastructure/ImportAllCountriesLayout.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public ImportAllCountriesLayout() {
3333
resetDownloadErrorReportButton();
3434
try {
3535
CountryImporter importer = new CountryImporter(countriesFile, currentUser);
36+
importer.setCsvSeparator(',');
3637
importer.startImport(this::extendDownloadErrorReportButton, currentUI);
3738
} catch (IOException | CsvValidationException e) {
3839
new Notification(

sormas-ui/src/main/java/de/symeda/sormas/ui/contact/importer/ContactImporter.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,20 @@ protected ImportLineResult importDataFromCsvLine(
9696
newContactTemp.setReportingUser(currentUser);
9797

9898
boolean contactHasImportError =
99-
insertRowIntoData(values, entityClasses, entityPropertyPaths, true, new Function<ImportCellData, Exception>() {
100-
101-
@Override
102-
public Exception apply(ImportCellData importColumnInformation) {
103-
// If the cell entry is not empty, try to insert it into the current contact or person object
104-
if (!StringUtils.isEmpty(importColumnInformation.getValue())) {
105-
try {
106-
insertColumnEntryIntoData(
107-
newContactTemp,
108-
newPersonTemp,
109-
importColumnInformation.getValue(),
110-
importColumnInformation.getEntityPropertyPath());
111-
} catch (ImportErrorException | InvalidColumnException e) {
112-
return e;
113-
}
99+
insertRowIntoData(values, entityClasses, entityPropertyPaths, true, importColumnInformation -> {
100+
// If the cell entry is not empty, try to insert it into the current contact or person object
101+
if (!StringUtils.isEmpty(importColumnInformation.getValue())) {
102+
try {
103+
insertColumnEntryIntoData(
104+
newContactTemp,
105+
newPersonTemp,
106+
importColumnInformation.getValue(),
107+
importColumnInformation.getEntityPropertyPath());
108+
} catch (ImportErrorException | InvalidColumnException e) {
109+
return e;
114110
}
115-
return null;
116111
}
112+
return null;
117113
});
118114

119115
// try to assign the contact to an existing case

sormas-ui/src/main/java/de/symeda/sormas/ui/importer/DataImporter.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public abstract class DataImporter {
9494
* Whether or not the current import has resulted in at least one error.
9595
*/
9696
private boolean hasImportError;
97+
/**
98+
* CSV separator used in the file
99+
*/
100+
private char csvSeparator;
97101

98102
protected UserReferenceDto currentUser;
99103
private CSVWriter errorReportCsvWriter;
@@ -108,6 +112,8 @@ public DataImporter(File inputFile, boolean hasEntityClassRow, UserReferenceDto
108112
ImportExportUtils.TEMP_FILE_PREFIX + "_error_report_" + DataHelper.getShortUuid(currentUser.getUuid()) + "_"
109113
+ DateHelper.formatDateForExport(new Date()) + ".csv");
110114
this.errorReportFilePath = errorReportFilePath.toString();
115+
116+
this.csvSeparator = FacadeProvider.getConfigFacade().getCsvSeparator();
111117
}
112118

113119
/**
@@ -202,9 +208,9 @@ public ImportResultStatus runImport() throws IOException, InvalidColumnException
202208

203209
try (CSVReader csvReader = CSVUtils.createCSVReader(
204210
Files.newBufferedReader(inputFile.toPath(), UTF_8),
205-
FacadeProvider.getConfigFacade().getCsvSeparator(),
211+
this.csvSeparator,
206212
new CSVCommentLineValidator())) {
207-
errorReportCsvWriter = CSVUtils.createCSVWriter(createErrorReportWriter(), FacadeProvider.getConfigFacade().getCsvSeparator());
213+
errorReportCsvWriter = CSVUtils.createCSVWriter(createErrorReportWriter(), this.csvSeparator);
208214

209215
// Build dictionary of entity headers
210216
String[] entityClasses;
@@ -299,7 +305,7 @@ protected StreamResource createErrorReportStreamResource() {
299305
protected int readImportFileLength(File inputFile) throws IOException, CsvValidationException {
300306
int importFileLength = 0;
301307
try (CSVReader caseCountReader =
302-
CSVUtils.createCSVReader(new FileReader(inputFile), FacadeProvider.getConfigFacade().getCsvSeparator(), new CSVCommentLineValidator())) {
308+
CSVUtils.createCSVReader(new FileReader(inputFile), this.csvSeparator, new CSVCommentLineValidator())) {
303309

304310
while (readNextValidLine(caseCountReader) != null) {
305311
importFileLength++;
@@ -516,4 +522,8 @@ private String[] readNextValidLine(CSVReader csvReader) throws IOException, CsvV
516522
while (isCommentLine);
517523
return nextValidLine;
518524
}
525+
526+
public void setCsvSeparator(char csvSeparator) {
527+
this.csvSeparator = csvSeparator;
528+
}
519529
}

sormas-ui/src/main/java/de/symeda/sormas/ui/importer/InfrastructureImporter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.lang.reflect.InvocationTargetException;
88
import java.util.List;
99

10-
import de.symeda.sormas.api.region.CountryDto;
1110
import org.apache.commons.lang3.StringUtils;
1211

1312
import de.symeda.sormas.api.EntityDto;
@@ -46,7 +45,7 @@ protected ImportLineResult importDataFromCsvLine(
4645
String[] entityProperties,
4746
String[][] entityPropertyPaths,
4847
boolean firstLine)
49-
throws IOException, InvalidColumnException, InterruptedException {
48+
throws IOException, InvalidColumnException {
5049

5150
// Check whether the new line has the same length as the header line
5251
if (values.length > entityProperties.length) {

0 commit comments

Comments
 (0)