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

Commit 5d4f115

Browse files
SORMAS-Foundation#2993 add import all button
1 parent 7b0b2f5 commit 5d4f115

10 files changed

Lines changed: 133 additions & 68 deletions

File tree

sormas-api/src/main/java/de/symeda/sormas/api/i18n/Captions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,4 +1782,5 @@ public interface Captions {
17821782
String WeeklyReportRegionSummary_officers = "WeeklyReportRegionSummary.officers";
17831783
String WeeklyReportRegionSummary_officerZeroReports = "WeeklyReportRegionSummary.officerZeroReports";
17841784
String weeklyReportsInDistrict = "weeklyReportsInDistrict";
1785+
String actionImportAllCountries = "actionImportAllCountries";
17851786
}

sormas-api/src/main/java/de/symeda/sormas/api/i18n/Strings.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,4 +1080,6 @@ public interface Strings {
10801080
String years = "years";
10811081
String yes = "yes";
10821082
String yesterday = "yesterday";
1083+
String headingImportAllCountries = "headingImportAllCountries";
1084+
String infoImportAllCountries = "infoImportAllCountries";
10831085
}

sormas-api/src/main/java/de/symeda/sormas/api/importexport/ImportFacade.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public interface ImportFacade {
6969

7070
String getCountryImportTemplateFilePath();
7171

72+
String getAllCountriesImportFilePath();
73+
7274
String getRegionImportTemplateFilePath();
7375

7476
String getDistrictImportTemplateFilePath();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ actionDeselectAll=Deselect all
9696
actionDiscard=Discard
9797
actionGenerate=Generate
9898
actionImport=Import
99+
actionImportAllCountries=Import all countries
99100
actionLogout=Logout
100101
actionNewEntry=New entry
101102
actionOkay=Okay

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ headingImportFacilities = Import Facilities
357357
headingImportPointsOfEntry = Import Points of Entry
358358
headingImportPopulationData = Import Population Data
359359
headingImportAreas = Import Areas
360-
headingImportCountries= Import Countries
360+
headingImportCountries = Import Countries
361+
headingImportAllCountries = Import All Countries
361362
headingImportRegions= Import Regions
362363
headingInformationSource = Source of Information
363364
headingIntroduction = Introduction
@@ -491,6 +492,7 @@ infoDownloadImportTemplate = You can use this template .csv file to bring your d
491492
infoDownloadErrorReport = If there were any rows that could not be imported, you will be offered a .csv file containing all these rows as well as the error descriptions.
492493
infoDownloadImportGuide = If this is your first time importing data into SORMAS, we strongly recommend to read the import guide first.
493494
infoExportNoFilters = <b>Warning:</b> No filters have been selected. Export may take a while.
495+
infoImportAllCountries = This will import all countries. You will receive a notification when the import process has finished.
494496
infoImportCsvFile = Depending on the amount of rows you want to import, this may take a while. You will receive a notification when the import process has finished.
495497
infoImportProcess = %d rows are being imported. The import process might take a while.<br/>You will be able to review and solve any errors after the import process has been completed.
496498
infoImportSimilarity = One of the cases you tried to import is similar to an already existing case in the SORMAS database. Please check whether the case to import is a duplicate of any of the cases in the list and, if so, select that case and confirm.

sormas-backend/src/main/java/de/symeda/sormas/backend/importexport/ImportFacadeEjb.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
import de.symeda.sormas.api.utils.CSVCommentLineValidator;
109109
import de.symeda.sormas.api.utils.CSVUtils;
110110
import de.symeda.sormas.api.utils.DependingOnFeatureType;
111-
import de.symeda.sormas.backend.campaign.form.CampaignFormMetaFacadeEjb;
112111
import de.symeda.sormas.backend.campaign.form.CampaignFormMetaFacadeEjb.CampaignFormMetaFacadeEjbLocal;
113112
import de.symeda.sormas.backend.common.ConfigFacadeEjb.ConfigFacadeEjbLocal;
114113
import de.symeda.sormas.backend.disease.DiseaseConfigurationFacadeEjb.DiseaseConfigurationFacadeEjbLocal;
@@ -135,6 +134,7 @@ public class ImportFacadeEjb implements ImportFacade {
135134
private static final String POPULATION_DATA_IMPORT_TEMPLATE_FILE_NAME = ImportExportUtils.FILE_PREFIX + "_import_population_data_template.csv";
136135
private static final String AREA_IMPORT_TEMPLATE_FILE_NAME = ImportExportUtils.FILE_PREFIX + "_import_area_template.csv";
137136
private static final String COUNTRY_IMPORT_TEMPLATE_FILE_NAME = ImportExportUtils.FILE_PREFIX + "_import_country_template.csv";
137+
private static final String ALL_COUNTRIES_IMPORT_FILE_NAME = ImportExportUtils.FILE_PREFIX + "_import_all_countries.csv";
138138
private static final String REGION_IMPORT_TEMPLATE_FILE_NAME = ImportExportUtils.FILE_PREFIX + "_import_region_template.csv";
139139
private static final String DISTRICT_IMPORT_TEMPLATE_FILE_NAME = ImportExportUtils.FILE_PREFIX + "_import_district_template.csv";
140140
private static final String COMMUNITY_IMPORT_TEMPLATE_FILE_NAME = ImportExportUtils.FILE_PREFIX + "_import_community_template.csv";
@@ -389,12 +389,18 @@ public String getAreaImportTemplateFilePath() {
389389

390390
@Override
391391
public String getCountryImportTemplateFilePath() {
392-
393392
Path exportDirectory = Paths.get(configFacade.getGeneratedFilesPath());
394393
Path filePath = exportDirectory.resolve(COUNTRY_IMPORT_TEMPLATE_FILE_NAME);
395394
return filePath.toString();
396395
}
397396

397+
@Override
398+
public String getAllCountriesImportFilePath() {
399+
Path exportDirectory = Paths.get(configFacade.getGeneratedFilesPath());
400+
Path filePath = exportDirectory.resolve(ALL_COUNTRIES_IMPORT_FILE_NAME);
401+
return filePath.toString();
402+
}
403+
398404
@Override
399405
public String getRegionImportTemplateFilePath() {
400406

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),
5562+
externalid varchar(4) unique,
55635563
isocode varchar(3) unique,
55645564
unocode varchar(3) unique,
55655565
primary key(id)

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class CountriesView extends AbstractConfigurationView {
5555
private CountriesGrid grid;
5656
protected Button createButton;
5757
protected Button importButton;
58+
protected Button importAllButton;
5859
private MenuBar bulkOperationsDropdown;
5960

6061
public CountriesView() {
@@ -82,8 +83,14 @@ public CountriesView() {
8283
window.setCaption(I18nProperties.getString(Strings.headingImportCountries));
8384
window.addCloseListener(c -> grid.reload());
8485
}, ValoTheme.BUTTON_PRIMARY);
85-
8686
addHeaderComponent(importButton);
87+
88+
importAllButton = ButtonHelper.createIconButton(Captions.actionImportAllCountries, VaadinIcons.UPLOAD, e -> {
89+
Window window = VaadinUiUtil.showPopupWindow(new ImportAllCountriesLayout());
90+
window.setCaption(I18nProperties.getString(Strings.headingImportAllCountries));
91+
window.addCloseListener(c -> grid.reload());
92+
}, ValoTheme.BUTTON_PRIMARY);
93+
addHeaderComponent(importAllButton);
8794
}
8895

8996
if (UserProvider.getCurrent().hasUserRight(UserRight.INFRASTRUCTURE_EXPORT)) {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package de.symeda.sormas.ui.configuration.infrastructure;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Paths;
6+
7+
import com.opencsv.exceptions.CsvValidationException;
8+
import com.vaadin.server.Page;
9+
import com.vaadin.ui.Button;
10+
import com.vaadin.ui.Notification;
11+
12+
import de.symeda.sormas.api.FacadeProvider;
13+
import de.symeda.sormas.api.i18n.Captions;
14+
import de.symeda.sormas.api.i18n.I18nProperties;
15+
import de.symeda.sormas.api.i18n.Strings;
16+
import de.symeda.sormas.api.infrastructure.InfrastructureType;
17+
import de.symeda.sormas.ui.importer.AbstractImportLayout;
18+
import de.symeda.sormas.ui.importer.DataImporter;
19+
import de.symeda.sormas.ui.importer.ImportLayoutComponent;
20+
import de.symeda.sormas.ui.importer.InfrastructureImporter;
21+
22+
@SuppressWarnings("serial")
23+
public class ImportAllCountriesLayout extends AbstractImportLayout {
24+
25+
public ImportAllCountriesLayout() {
26+
super();
27+
28+
addImportAllCountriesCsvComponent(1, (event) -> {
29+
String templateFilePath = FacadeProvider.getImportFacade().getAllCountriesImportFilePath();
30+
File templateFile = Paths.get(templateFilePath).toFile();
31+
resetDownloadErrorReportButton();
32+
try {
33+
DataImporter importer = new InfrastructureImporter(templateFile, currentUser, InfrastructureType.COUNTRY);
34+
importer.startImport(this::extendDownloadErrorReportButton, currentUI, true);
35+
} catch (IOException | CsvValidationException e) {
36+
new Notification(
37+
I18nProperties.getString(Strings.headingImportFailed),
38+
I18nProperties.getString(Strings.messageImportFailed),
39+
Notification.Type.ERROR_MESSAGE,
40+
false).show(Page.getCurrent());
41+
}
42+
});
43+
44+
addDownloadErrorReportComponent(2);
45+
}
46+
47+
protected void addImportAllCountriesCsvComponent(int step, Button.ClickListener clickListener) {
48+
String headline = I18nProperties.getString(Strings.headingImportAllCountries);
49+
String infoText = I18nProperties.getString(Strings.infoImportAllCountries);
50+
ImportLayoutComponent importCsvComponent = new ImportLayoutComponent(step, headline, infoText, null, I18nProperties.getCaption(Captions.actionImport));
51+
importCsvComponent.getButton().addClickListener(clickListener);
52+
addComponent(importCsvComponent);
53+
}
54+
}

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

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -128,65 +128,60 @@ public void startImport(Consumer<StreamResource> errorReportConsumer, UI current
128128
window.setClosable(false);
129129
currentUI.addWindow(window);
130130

131-
Thread importThread = new Thread() {
132-
133-
@Override
134-
public void run() {
135-
try {
136-
currentUI.setPollInterval(300);
137-
138-
ImportResultStatus importResult = runImport();
139-
140-
// Display a window presenting the import result
141-
currentUI.access(() -> {
142-
window.setClosable(true);
143-
progressLayout.makeClosable(window::close);
144-
145-
if (importResult == ImportResultStatus.COMPLETED) {
146-
progressLayout.displaySuccessIcon();
147-
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportSuccessful));
148-
} else if (importResult == ImportResultStatus.COMPLETED_WITH_ERRORS) {
149-
progressLayout.displayWarningIcon();
150-
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportPartiallySuccessful));
151-
} else if (importResult == ImportResultStatus.CANCELED) {
152-
progressLayout.displaySuccessIcon();
153-
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportCanceled));
154-
} else {
155-
progressLayout.displayWarningIcon();
156-
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportCanceledErrors));
157-
}
158-
159-
window.addCloseListener(e -> {
160-
if (importResult == ImportResultStatus.COMPLETED_WITH_ERRORS || importResult == ImportResultStatus.CANCELED_WITH_ERRORS) {
161-
StreamResource streamResource = createErrorReportStreamResource();
162-
errorReportConsumer.accept(streamResource);
163-
}
164-
});
131+
Thread importThread = new Thread(() -> {
132+
try {
133+
currentUI.setPollInterval(300);
134+
135+
ImportResultStatus importResult = runImport();
136+
137+
// Display a window presenting the import result
138+
currentUI.access(() -> {
139+
window.setClosable(true);
140+
progressLayout.makeClosable(window::close);
141+
142+
if (importResult == ImportResultStatus.COMPLETED) {
143+
progressLayout.displaySuccessIcon();
144+
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportSuccessful));
145+
} else if (importResult == ImportResultStatus.COMPLETED_WITH_ERRORS) {
146+
progressLayout.displayWarningIcon();
147+
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportPartiallySuccessful));
148+
} else if (importResult == ImportResultStatus.CANCELED) {
149+
progressLayout.displaySuccessIcon();
150+
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportCanceled));
151+
} else {
152+
progressLayout.displayWarningIcon();
153+
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportCanceledErrors));
154+
}
165155

166-
currentUI.setPollInterval(-1);
167-
});
168-
} catch (InvalidColumnException e) {
169-
currentUI.access(() -> {
170-
window.setClosable(true);
171-
progressLayout.makeClosable(window::close);
172-
progressLayout.displayErrorIcon();
173-
progressLayout
174-
.setInfoLabelText(String.format(I18nProperties.getString(Strings.messageImportInvalidColumn), e.getColumnName()));
175-
currentUI.setPollInterval(-1);
176-
});
177-
} catch (Exception e) {
178-
logger.error(e.getMessage(), e);
179-
180-
currentUI.access(() -> {
181-
window.setClosable(true);
182-
progressLayout.makeClosable(window::close);
183-
progressLayout.displayErrorIcon();
184-
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportFailedFull));
185-
currentUI.setPollInterval(-1);
156+
window.addCloseListener(e -> {
157+
if (importResult == ImportResultStatus.COMPLETED_WITH_ERRORS || importResult == ImportResultStatus.CANCELED_WITH_ERRORS) {
158+
StreamResource streamResource = createErrorReportStreamResource();
159+
errorReportConsumer.accept(streamResource);
160+
}
186161
});
187-
}
162+
163+
currentUI.setPollInterval(-1);
164+
});
165+
} catch (InvalidColumnException e) {
166+
currentUI.access(() -> {
167+
window.setClosable(true);
168+
progressLayout.makeClosable(window::close);
169+
progressLayout.displayErrorIcon();
170+
progressLayout.setInfoLabelText(String.format(I18nProperties.getString(Strings.messageImportInvalidColumn), e.getColumnName()));
171+
currentUI.setPollInterval(-1);
172+
});
173+
} catch (Exception e) {
174+
logger.error(e.getMessage(), e);
175+
176+
currentUI.access(() -> {
177+
window.setClosable(true);
178+
progressLayout.makeClosable(window::close);
179+
progressLayout.displayErrorIcon();
180+
progressLayout.setInfoLabelText(I18nProperties.getString(Strings.messageImportFailedFull));
181+
currentUI.setPollInterval(-1);
182+
});
188183
}
189-
};
184+
});
190185

191186
importThread.start();
192187
}
@@ -199,12 +194,10 @@ public ImportResultStatus runImport() throws IOException, InvalidColumnException
199194

200195
long t0 = System.currentTimeMillis();
201196

202-
CSVReader csvReader = null;
203-
try {
204-
csvReader = CSVUtils.createCSVReader(
197+
try (CSVReader csvReader = CSVUtils.createCSVReader(
205198
Files.newBufferedReader(inputFile.toPath(), UTF_8),
206199
FacadeProvider.getConfigFacade().getCsvSeparator(),
207-
new CSVCommentLineValidator());
200+
new CSVCommentLineValidator())) {
208201
errorReportCsvWriter = CSVUtils.createCSVWriter(createErrorReportWriter(), FacadeProvider.getConfigFacade().getCsvSeparator());
209202

210203
// Build dictionary of entity headers
@@ -265,9 +258,6 @@ public ImportResultStatus runImport() throws IOException, InvalidColumnException
265258
return ImportResultStatus.COMPLETED;
266259
}
267260
} finally {
268-
if (csvReader != null) {
269-
csvReader.close();
270-
}
271261
if (errorReportCsvWriter != null) {
272262
errorReportCsvWriter.close();
273263
}

0 commit comments

Comments
 (0)