Search in sources :

Example 6 with CommunityDto

use of de.symeda.sormas.api.infrastructure.community.CommunityDto in project SORMAS-Project by hzi-braunschweig.

the class DashboardFacadeEjbTest method testDiseaseBurdenForDashboard.

@Test
public void testDiseaseBurdenForDashboard() {
    Date referenceDate = new Date();
    TestDataCreator.RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
    CommunityDto community2 = creator.createCommunity("Community2", rdcf.district);
    TestDataCreator.RDCF rdcf2 = new TestDataCreator.RDCF(rdcf.region, rdcf.district, community2.toReference(), creator.createFacility("Facility2", rdcf.region, rdcf.district, community2.toReference()).toReference());
    UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
    PersonDto cazePerson = creator.createPerson("Case", "Person");
    CaseDataDto caze = creator.createCase(user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, DateHelper.subtractDays(referenceDate, 2), rdcf);
    PersonDto cazePerson2 = creator.createPerson("Case", "Person2");
    CaseDataDto caze2 = creator.createCase(user.toReference(), cazePerson2.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, DateHelper.addDays(referenceDate, 1), rdcf2);
    PersonDto cazePerson3 = creator.createPerson("Case", "Person3");
    CaseDataDto caze3 = creator.createCase(user.toReference(), cazePerson3.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, DateHelper.addDays(referenceDate, 2), rdcf);
    PersonDto cazePerson4 = creator.createPerson("Case", "Person4");
    CaseDataDto caze4 = creator.createCase(user.toReference(), cazePerson4.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, referenceDate, rdcf2);
    List<DiseaseBurdenDto> diseaseBurdenForDashboard = getDashboardFacade().getDiseaseBurden(rdcf.region, rdcf.district, DateHelper.getStartOfDay(referenceDate), DateHelper.getEndOfDay(DateHelper.addDays(referenceDate, 10)), DateHelper.getStartOfDay(DateHelper.subtractDays(referenceDate, 10)), DateHelper.getEndOfDay(DateHelper.subtractDays(referenceDate, 1)), NewCaseDateType.MOST_RELEVANT);
    DiseaseBurdenDto evdBurden = diseaseBurdenForDashboard.stream().filter(dto -> dto.getDisease() == Disease.EVD).findFirst().get();
    assertEquals(new Long(3), evdBurden.getCaseCount());
    assertEquals(new Long(1), evdBurden.getPreviousCaseCount());
    assertEquals(rdcf.district.getCaption(), evdBurden.getLastReportedDistrictName());
}
Also used : DiseaseBurdenDto(de.symeda.sormas.api.disease.DiseaseBurdenDto) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) PersonDto(de.symeda.sormas.api.person.PersonDto) UserDto(de.symeda.sormas.api.user.UserDto) CommunityDto(de.symeda.sormas.api.infrastructure.community.CommunityDto) TestDataCreator(de.symeda.sormas.backend.TestDataCreator) Date(java.util.Date) Test(org.junit.Test) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest)

Example 7 with CommunityDto

use of de.symeda.sormas.api.infrastructure.community.CommunityDto 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;
    }
}
Also used : PopulationDataCriteria(de.symeda.sormas.api.infrastructure.PopulationDataCriteria) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) Date(java.util.Date) FacadeProvider(de.symeda.sormas.api.FacadeProvider) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) ValueSeparator(de.symeda.sormas.api.importexport.ValueSeparator) RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto) Function(java.util.function.Function) AgeGroup(de.symeda.sormas.api.AgeGroup) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ArrayList(java.util.ArrayList) CommunityDto(de.symeda.sormas.api.infrastructure.community.CommunityDto) Validations(de.symeda.sormas.api.i18n.Validations) DataHelper(de.symeda.sormas.api.utils.DataHelper) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) UserDto(de.symeda.sormas.api.user.UserDto) Sex(de.symeda.sormas.api.person.Sex) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IOException(java.io.IOException) ImportCellData(de.symeda.sormas.api.importexport.ImportCellData) File(java.io.File) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) List(java.util.List) PopulationDataDto(de.symeda.sormas.api.infrastructure.PopulationDataDto) DistrictDto(de.symeda.sormas.api.infrastructure.district.DistrictDto) Optional(java.util.Optional) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) ArrayList(java.util.ArrayList) RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IOException(java.io.IOException) DistrictDto(de.symeda.sormas.api.infrastructure.district.DistrictDto) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) ImportCellData(de.symeda.sormas.api.importexport.ImportCellData) PopulationDataCriteria(de.symeda.sormas.api.infrastructure.PopulationDataCriteria) CommunityDto(de.symeda.sormas.api.infrastructure.community.CommunityDto) PopulationDataDto(de.symeda.sormas.api.infrastructure.PopulationDataDto)

Example 8 with CommunityDto

use of de.symeda.sormas.api.infrastructure.community.CommunityDto in project SORMAS-Project by hzi-braunschweig.

the class InfrastructureImporter method insertColumnEntryIntoData.

/**
 * Inserts the entry of a single cell into the infrastructure object.
 */
private void insertColumnEntryIntoData(EntityDto newEntityDto, String value, String[] entityPropertyPath) throws InvalidColumnException, ImportErrorException {
    Object currentElement = newEntityDto;
    for (int i = 0; i < entityPropertyPath.length; i++) {
        String headerPathElementName = entityPropertyPath[i];
        try {
            if (i != entityPropertyPath.length - 1) {
                currentElement = new PropertyDescriptor(headerPathElementName, currentElement.getClass()).getReadMethod().invoke(currentElement);
            } else {
                PropertyDescriptor pd = new PropertyDescriptor(headerPathElementName, currentElement.getClass());
                Class<?> propertyType = pd.getPropertyType();
                // is referenced in the imported object does not exist in the database
                if (!executeDefaultInvoke(pd, currentElement, value, entityPropertyPath)) {
                    if (propertyType.isAssignableFrom(DistrictReferenceDto.class)) {
                        List<DistrictReferenceDto> district;
                        switch(type) {
                            case COMMUNITY:
                                district = FacadeProvider.getDistrictFacade().getByName(value, ((CommunityDto) newEntityDto).getRegion(), false);
                                break;
                            case FACILITY:
                                district = FacadeProvider.getDistrictFacade().getByName(value, ((FacilityDto) newEntityDto).getRegion(), false);
                                break;
                            case POINT_OF_ENTRY:
                                district = FacadeProvider.getDistrictFacade().getByName(value, ((PointOfEntryDto) newEntityDto).getRegion(), false);
                                break;
                            default:
                                throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, propertyType.getName()));
                        }
                        if (district.isEmpty()) {
                            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrRegion, value, buildEntityProperty(entityPropertyPath)));
                        } else if (district.size() > 1) {
                            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importDistrictNotUnique, value, buildEntityProperty(entityPropertyPath)));
                        } else {
                            pd.getWriteMethod().invoke(currentElement, district.get(0));
                        }
                    } else if (propertyType.isAssignableFrom(CommunityReferenceDto.class)) {
                        List<CommunityReferenceDto> community;
                        if (type == InfrastructureType.FACILITY) {
                            community = FacadeProvider.getCommunityFacade().getByName(value, ((FacilityDto) newEntityDto).getDistrict(), false);
                        } else {
                            throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, propertyType.getName()));
                        }
                        if (community.isEmpty()) {
                            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExistDbOrRegion, value, buildEntityProperty(entityPropertyPath)));
                        } else if (community.size() > 1) {
                            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importDistrictNotUnique, value, buildEntityProperty(entityPropertyPath)));
                        } else {
                            pd.getWriteMethod().invoke(currentElement, community.get(0));
                        }
                    } else {
                        throw new UnsupportedOperationException(I18nProperties.getValidationError(Validations.importPropertyTypeNotAllowed, propertyType.getName()));
                    }
                }
            }
        } catch (IntrospectionException e) {
            throw new InvalidColumnException(buildEntityProperty(entityPropertyPath));
        } catch (InvocationTargetException | IllegalAccessException e) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importErrorInColumn, buildEntityProperty(entityPropertyPath)));
        } catch (IllegalArgumentException e) {
            throw new ImportErrorException(value, buildEntityProperty(entityPropertyPath));
        } catch (ImportErrorException e) {
            throw e;
        } catch (Exception e) {
            logger.error("Unexpected error when trying to import infrastructure data: " + e.getMessage());
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importUnexpectedError));
        }
    }
    ImportLineResultDto<EntityDto> constraintErrors = validateConstraints(newEntityDto);
    if (constraintErrors.isError()) {
        throw new ImportErrorException(constraintErrors.getMessage());
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IntrospectionException(java.beans.IntrospectionException) FacilityDto(de.symeda.sormas.api.infrastructure.facility.FacilityDto) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) InvocationTargetException(java.lang.reflect.InvocationTargetException) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) IOException(java.io.IOException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityDto(de.symeda.sormas.api.EntityDto) CommunityReferenceDto(de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto) InvalidColumnException(de.symeda.sormas.api.importexport.InvalidColumnException) CommunityDto(de.symeda.sormas.api.infrastructure.community.CommunityDto) PointOfEntryDto(de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryDto) List(java.util.List)

Example 9 with CommunityDto

use of de.symeda.sormas.api.infrastructure.community.CommunityDto in project SORMAS-Project by hzi-braunschweig.

the class CommunitiesGrid method setLazyDataProvider.

public void setLazyDataProvider() {
    DataProvider<CommunityDto, CommunityCriteria> dataProvider = DataProvider.fromFilteringCallbacks(query -> FacadeProvider.getCommunityFacade().getIndexList(query.getFilter().orElse(null), query.getOffset(), query.getLimit(), query.getSortOrders().stream().map(sortOrder -> new SortProperty(sortOrder.getSorted(), sortOrder.getDirection() == SortDirection.ASCENDING)).collect(Collectors.toList())).stream(), query -> {
        return (int) FacadeProvider.getCommunityFacade().count(query.getFilter().orElse(null));
    });
    setDataProvider(dataProvider);
    setSelectionMode(SelectionMode.NONE);
}
Also used : SortProperty(de.symeda.sormas.api.utils.SortProperty) CommunityDto(de.symeda.sormas.api.infrastructure.community.CommunityDto) CommunityCriteria(de.symeda.sormas.api.infrastructure.community.CommunityCriteria)

Example 10 with CommunityDto

use of de.symeda.sormas.api.infrastructure.community.CommunityDto in project SORMAS-Project by hzi-braunschweig.

the class InfrastructureController method editCommunity.

public void editCommunity(String uuid) {
    CommunityDto community = FacadeProvider.getCommunityFacade().getByUuid(uuid);
    CommitDiscardWrapperComponent<CommunityEditForm> editComponent = getCommunityEditComponent(community);
    String caption = I18nProperties.getString(Strings.edit) + " " + community.getName();
    VaadinUiUtil.showModalPopupWindow(editComponent, caption);
}
Also used : CommunityDto(de.symeda.sormas.api.infrastructure.community.CommunityDto)

Aggregations

CommunityDto (de.symeda.sormas.api.infrastructure.community.CommunityDto)11 DistrictDto (de.symeda.sormas.api.infrastructure.district.DistrictDto)3 FacilityDto (de.symeda.sormas.api.infrastructure.facility.FacilityDto)3 RegionDto (de.symeda.sormas.api.infrastructure.region.RegionDto)3 Date (java.util.Date)3 FacadeProvider (de.symeda.sormas.api.FacadeProvider)2 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)2 ImportErrorException (de.symeda.sormas.api.importexport.ImportErrorException)2 InvalidColumnException (de.symeda.sormas.api.importexport.InvalidColumnException)2 CommunityReferenceDto (de.symeda.sormas.api.infrastructure.community.CommunityReferenceDto)2 DistrictReferenceDto (de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)2 PointOfEntryDto (de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryDto)2 UserDto (de.symeda.sormas.api.user.UserDto)2 ValidationRuntimeException (de.symeda.sormas.api.utils.ValidationRuntimeException)2 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)2 IOException (java.io.IOException)2 List (java.util.List)2 Test (org.junit.Test)2 Page (com.vaadin.server.Page)1 Alignment (com.vaadin.ui.Alignment)1