use of de.symeda.sormas.api.infrastructure.region.RegionDto in project SORMAS-Project by hzi-braunschweig.
the class InfrastructureController method editRegion.
public void editRegion(String uuid) {
RegionDto region = FacadeProvider.getRegionFacade().getByUuid(uuid);
CommitDiscardWrapperComponent<RegionEditForm> editComponent = getRegionEditComponent(region);
String caption = I18nProperties.getString(Strings.edit) + " " + region.getName();
VaadinUiUtil.showModalPopupWindow(editComponent, caption);
}
use of de.symeda.sormas.api.infrastructure.region.RegionDto in project SORMAS-Project by hzi-braunschweig.
the class RegionBackendTest method testHandlePulledList.
@Test
public void testHandlePulledList() {
long startRegionCount = DatabaseHelper.getRegionDao().countOf();
List<RegionDto> regions = new ArrayList<>();
RegionDto region1 = RegionDto.build();
region1.setCreationDate(new Date());
region1.setChangeDate(new Date());
region1.setName("TestA");
regions.add(region1);
RegionDto region2 = RegionDto.build();
region2.setCreationDate(new Date());
region2.setChangeDate(new Date());
region2.setName("TestB");
regions.add(region2);
// this should cause a roll-back
region2.setUuid(null);
boolean hadException = false;
try {
new RegionDtoHelper().handlePulledList(DatabaseHelper.getRegionDao(), regions);
} catch (DaoException | NoConnectionException | ServerConnectionException | ServerCommunicationException e) {
hadException = true;
}
assertTrue(hadException);
long regionCount = DatabaseHelper.getRegionDao().countOf();
assertEquals(startRegionCount, regionCount);
// now it should work
region2.setUuid(DataHelper.createUuid());
hadException = false;
try {
new RegionDtoHelper().handlePulledList(DatabaseHelper.getRegionDao(), regions);
} catch (DaoException | NoConnectionException | ServerConnectionException | ServerCommunicationException e) {
hadException = true;
}
assertFalse(hadException);
regionCount = DatabaseHelper.getRegionDao().countOf();
assertEquals(startRegionCount + 2, regionCount);
}
use of de.symeda.sormas.api.infrastructure.region.RegionDto in project SORMAS-Project by hzi-braunschweig.
the class RegionFacadeEjbTest method testGetAllAfter.
@Test
public void testGetAllAfter() throws InterruptedException {
creator.createRegion("region1");
getRegionService().doFlush();
Date date = new Date();
List<RegionDto> results = getRegionFacade().getAllAfter(date);
// List should be empty
assertEquals(0, results.size());
// delay to ignore known rounding issues in change date filter
Thread.sleep(1);
String regionName = "region2";
creator.createRegion(regionName);
results = getRegionFacade().getAllAfter(date);
// List should have one entry
assertEquals(1, results.size());
assertEquals(regionName, results.get(0).getName());
}
use of de.symeda.sormas.api.infrastructure.region.RegionDto in project SORMAS-Project by hzi-braunschweig.
the class PopulationDataFacadeEjbTest method testGetProjectedRegionPopulation.
@Test
public void testGetProjectedRegionPopulation() {
RDCFEntities rdcf = creator.createRDCFEntities("Region", "District", "Community", "Facility");
RegionDto region = getRegionFacade().getByUuid(rdcf.region.getUuid());
region.setGrowthRate(2.7f);
getRegionFacade().save(region);
creator.createPopulationData(new RegionReferenceDto(rdcf.region.getUuid(), null, null), null, 450000, DateHelper.subtractYears(new Date(), 3));
assertEquals(new Integer(487440), getPopulationDataFacade().getProjectedRegionPopulation(rdcf.region.getUuid()));
}
use of de.symeda.sormas.api.infrastructure.region.RegionDto in project SORMAS-Project by hzi-braunschweig.
the class PopulationDataImporter method importDataFromCsvLine.
@Override
protected ImportLineResult importDataFromCsvLine(String[] values, String[] entityClasses, String[] entityProperties, String[][] entityPropertyPaths, boolean firstLine) throws IOException, InvalidColumnException, InterruptedException {
// Check whether the new line has the same length as the header line
if (values.length > entityProperties.length) {
writeImportError(values, I18nProperties.getValidationError(Validations.importLineTooLong));
return ImportLineResult.ERROR;
}
// Reference population data that contains the region, district and community for this line
RegionReferenceDto region = null;
DistrictReferenceDto district = null;
CommunityReferenceDto community = null;
// Retrieve the region and district from the database or throw an error if more or less than one entry have been retrieved
for (int i = 0; i < entityProperties.length; i++) {
if (PopulationDataDto.REGION.equalsIgnoreCase(entityProperties[i])) {
List<RegionReferenceDto> regions = FacadeProvider.getRegionFacade().getReferencesByName(values[i], false);
if (regions.size() != 1) {
writeImportError(values, new ImportErrorException(values[i], entityProperties[i]).getMessage());
return ImportLineResult.ERROR;
}
region = regions.get(0);
}
if (PopulationDataDto.DISTRICT.equalsIgnoreCase(entityProperties[i])) {
if (DataHelper.isNullOrEmpty(values[i])) {
district = null;
} else {
List<DistrictReferenceDto> districts = FacadeProvider.getDistrictFacade().getByName(values[i], region, false);
if (districts.size() != 1) {
writeImportError(values, new ImportErrorException(values[i], entityProperties[i]).getMessage());
return ImportLineResult.ERROR;
}
district = districts.get(0);
}
}
if (PopulationDataDto.COMMUNITY.equalsIgnoreCase(entityProperties[i])) {
if (DataHelper.isNullOrEmpty(values[i])) {
community = null;
} else {
List<CommunityReferenceDto> communities = FacadeProvider.getCommunityFacade().getByName(values[i], district, false);
if (communities.size() != 1) {
writeImportError(values, new ImportErrorException(values[i], entityProperties[i]).getMessage());
return ImportLineResult.ERROR;
}
community = communities.get(0);
}
}
}
// The region and district that will be used to save the population data to the database
final RegionReferenceDto finalRegion = region;
final DistrictReferenceDto finalDistrict = district;
final CommunityReferenceDto finalCommunity = community;
// Retrieve the existing population data for the region and district
PopulationDataCriteria criteria = new PopulationDataCriteria().region(finalRegion);
if (finalCommunity == null) {
criteria.communityIsNull(true);
} else {
criteria.community(finalCommunity);
}
if (district == null) {
criteria.districtIsNull(true);
} else {
criteria.district(finalDistrict);
}
List<PopulationDataDto> existingPopulationDataList = FacadeProvider.getPopulationDataFacade().getPopulationData(criteria);
List<PopulationDataDto> modifiedPopulationDataList = new ArrayList<PopulationDataDto>();
boolean populationDataHasImportError = insertRowIntoData(values, entityClasses, entityPropertyPaths, false, new Function<ImportCellData, Exception>() {
@Override
public Exception apply(ImportCellData cellData) {
try {
if (PopulationDataDto.REGION.equalsIgnoreCase(cellData.getEntityPropertyPath()[0]) || PopulationDataDto.DISTRICT.equalsIgnoreCase(cellData.getEntityPropertyPath()[0]) || PopulationDataDto.COMMUNITY.equalsIgnoreCase(cellData.getEntityPropertyPath()[0])) {
// Ignore the region, district and community columns
} else if (RegionDto.GROWTH_RATE.equalsIgnoreCase(cellData.getEntityPropertyPath()[0])) {
// Update the growth rate of the region or district
if (!DataHelper.isNullOrEmpty(cellData.getValue())) {
Float growthRate = Float.parseFloat(cellData.getValue());
if (finalCommunity != null) {
CommunityDto communityDto = FacadeProvider.getCommunityFacade().getByUuid(finalCommunity.getUuid());
communityDto.setGrowthRate(growthRate);
FacadeProvider.getCommunityFacade().save(communityDto);
} else if (finalDistrict != null) {
DistrictDto districtDto = FacadeProvider.getDistrictFacade().getByUuid(finalDistrict.getUuid());
districtDto.setGrowthRate(growthRate);
FacadeProvider.getDistrictFacade().save(districtDto);
} else {
RegionDto regionDto = FacadeProvider.getRegionFacade().getByUuid(finalRegion.getUuid());
regionDto.setGrowthRate(growthRate);
FacadeProvider.getRegionFacade().save(regionDto);
}
}
} else {
// Add the data from the currently processed cell to a new population data object
PopulationDataDto newPopulationData = PopulationDataDto.build(collectionDate);
insertCellValueIntoData(newPopulationData, cellData.getValue(), cellData.getEntityPropertyPath());
Optional<PopulationDataDto> existingPopulationData = existingPopulationDataList.stream().filter(populationData -> populationData.getAgeGroup() == newPopulationData.getAgeGroup() && populationData.getSex() == newPopulationData.getSex()).findFirst();
// Check whether this population data set already exists in the database; if yes, override it
if (existingPopulationData.isPresent()) {
existingPopulationData.get().setPopulation(newPopulationData.getPopulation());
existingPopulationData.get().setCollectionDate(collectionDate);
modifiedPopulationDataList.add(existingPopulationData.get());
} else {
newPopulationData.setRegion(finalRegion);
newPopulationData.setDistrict(finalDistrict);
newPopulationData.setCommunity(finalCommunity);
modifiedPopulationDataList.add(newPopulationData);
}
}
} catch (ImportErrorException | InvalidColumnException | NumberFormatException e) {
return e;
}
return null;
}
});
// Validate and save the population data object into the database if the import has no errors
if (!populationDataHasImportError) {
try {
FacadeProvider.getPopulationDataFacade().savePopulationData(modifiedPopulationDataList);
return ImportLineResult.SUCCESS;
} catch (ValidationRuntimeException e) {
writeImportError(values, e.getMessage());
return ImportLineResult.ERROR;
}
} else {
return ImportLineResult.ERROR;
}
}
Aggregations