Search in sources :

Example 1 with RegionDto

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

the class DataImporter method executeDefaultInvoke.

/**
 * Contains checks for the most common data types for entries in the import file. This method should be called
 * in every subclass whenever data from the import file is supposed to be written to the entity in question.
 * Additional invokes need to be executed manually in the subclass.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected boolean executeDefaultInvoke(PropertyDescriptor pd, Object element, String entry, String[] entryHeaderPath) throws InvocationTargetException, IllegalAccessException, ImportErrorException {
    Class<?> propertyType = pd.getPropertyType();
    if (propertyType.isEnum()) {
        Enum enumValue = null;
        Class<Enum> enumType = (Class<Enum>) propertyType;
        try {
            enumValue = Enum.valueOf(enumType, entry.toUpperCase());
        } catch (IllegalArgumentException e) {
        // ignore
        }
        if (enumValue == null) {
            enumValue = enumCaptionCache.getEnumByCaption(enumType, entry);
        }
        pd.getWriteMethod().invoke(element, enumValue);
        return true;
    }
    if (propertyType.isAssignableFrom(Date.class)) {
        String dateFormat = I18nProperties.getUserLanguage().getDateFormat();
        try {
            pd.getWriteMethod().invoke(element, DateHelper.parseDateWithException(entry, dateFormat));
            return true;
        } catch (ParseException e) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importInvalidDate, pd.getName(), DateHelper.getAllowedDateFormats(dateFormat)));
        }
    }
    if (propertyType.isAssignableFrom(Integer.class)) {
        pd.getWriteMethod().invoke(element, Integer.parseInt(entry));
        return true;
    }
    if (propertyType.isAssignableFrom(Double.class)) {
        pd.getWriteMethod().invoke(element, Double.parseDouble(entry));
        return true;
    }
    if (propertyType.isAssignableFrom(Float.class)) {
        pd.getWriteMethod().invoke(element, Float.parseFloat(entry));
        return true;
    }
    if (propertyType.isAssignableFrom(Boolean.class) || propertyType.isAssignableFrom(boolean.class)) {
        pd.getWriteMethod().invoke(element, DataHelper.parseBoolean(entry));
        return true;
    }
    if (propertyType.isAssignableFrom(AreaReferenceDto.class)) {
        List<AreaReferenceDto> areas = FacadeProvider.getAreaFacade().getByName(entry, false);
        if (areas.isEmpty()) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
        } else if (areas.size() > 1) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importAreaNotUnique, entry, buildEntityProperty(entryHeaderPath)));
        } else {
            pd.getWriteMethod().invoke(element, areas.get(0));
            return true;
        }
    }
    if (propertyType.isAssignableFrom(SubcontinentReferenceDto.class)) {
        List<SubcontinentReferenceDto> subcontinents = FacadeProvider.getSubcontinentFacade().getByDefaultName(entry, false);
        if (subcontinents.isEmpty()) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
        } else if (subcontinents.size() > 1) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importSubcontinentNotUnique, entry, buildEntityProperty(entryHeaderPath)));
        } else {
            pd.getWriteMethod().invoke(element, subcontinents.get(0));
            return true;
        }
    }
    if (propertyType.isAssignableFrom(CountryReferenceDto.class)) {
        List<CountryReferenceDto> countries = FacadeProvider.getCountryFacade().getByDefaultName(entry, false);
        if (countries.isEmpty()) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
        } else if (countries.size() > 1) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importSubcontinentNotUnique, entry, buildEntityProperty(entryHeaderPath)));
        } else {
            pd.getWriteMethod().invoke(element, countries.get(0));
            return true;
        }
    }
    if (propertyType.isAssignableFrom(ContinentReferenceDto.class)) {
        List<ContinentReferenceDto> continents = FacadeProvider.getContinentFacade().getByDefaultName(entry, false);
        if (continents.isEmpty()) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
        } else if (continents.size() > 1) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importSubcontinentNotUnique, entry, buildEntityProperty(entryHeaderPath)));
        } else {
            pd.getWriteMethod().invoke(element, continents.get(0));
            return true;
        }
    }
    if (propertyType.isAssignableFrom(RegionReferenceDto.class)) {
        List<RegionDto> regions = FacadeProvider.getRegionFacade().getByName(entry, false);
        if (regions.isEmpty()) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
        } else if (regions.size() > 1) {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importRegionNotUnique, entry, buildEntityProperty(entryHeaderPath)));
        } else {
            RegionDto region = regions.get(0);
            CountryReferenceDto serverCountry = FacadeProvider.getCountryFacade().getServerCountry();
            if (region.getCountry() != null && !region.getCountry().equals(serverCountry)) {
                throw new ImportErrorException(I18nProperties.getValidationError(Validations.importRegionNotInServerCountry, entry, buildEntityProperty(entryHeaderPath)));
            } else {
                pd.getWriteMethod().invoke(element, region.toReference());
                return true;
            }
        }
    }
    if (propertyType.isAssignableFrom(UserReferenceDto.class)) {
        UserDto user = FacadeProvider.getUserFacade().getByUserName(entry);
        if (user != null) {
            pd.getWriteMethod().invoke(element, user.toReference());
            return true;
        } else {
            throw new ImportErrorException(I18nProperties.getValidationError(Validations.importEntryDoesNotExist, entry, buildEntityProperty(entryHeaderPath)));
        }
    }
    if (propertyType.isAssignableFrom(String.class)) {
        pd.getWriteMethod().invoke(element, entry);
        return true;
    }
    return false;
}
Also used : ImportErrorException(de.symeda.sormas.api.importexport.ImportErrorException) SubcontinentReferenceDto(de.symeda.sormas.api.infrastructure.subcontinent.SubcontinentReferenceDto) UserDto(de.symeda.sormas.api.user.UserDto) RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto) CountryReferenceDto(de.symeda.sormas.api.infrastructure.country.CountryReferenceDto) ContinentReferenceDto(de.symeda.sormas.api.infrastructure.continent.ContinentReferenceDto) AreaReferenceDto(de.symeda.sormas.api.infrastructure.area.AreaReferenceDto) ParseException(java.text.ParseException)

Example 2 with RegionDto

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

the class CaseStatisticsFacadeEjbTest method testQueryCaseCountPopulation.

@Test
public void testQueryCaseCountPopulation() {
    RDCF rdcf = creator.createRDCF("Region", "District", "Community", "Facility");
    UserDto user = creator.createUser(rdcf.region.getUuid(), rdcf.district.getUuid(), rdcf.facility.getUuid(), "Surv", "Sup", UserRole.SURVEILLANCE_SUPERVISOR);
    PersonDto cazePerson = creator.createPerson("Case", "Person");
    cazePerson.setApproximateAge(30);
    cazePerson.setApproximateAgeReferenceDate(new Date());
    cazePerson.setApproximateAgeType(ApproximateAgeType.YEARS);
    cazePerson = getPersonFacade().savePerson(cazePerson);
    CaseDataDto caze = creator.createCase(user.toReference(), cazePerson.toReference(), Disease.EVD, CaseClassification.PROBABLE, InvestigationStatus.PENDING, new Date(), rdcf);
    caze = getCaseFacade().getCaseDataByUuid(caze.getUuid());
    StatisticsCaseCriteria criteria = new StatisticsCaseCriteria();
    criteria.regions(Arrays.asList(rdcf.region));
    List<StatisticsCaseCountDto> results = getCaseStatisticsFacade().queryCaseCount(criteria, StatisticsCaseAttribute.JURISDICTION, StatisticsCaseSubAttribute.REGION, null, null, true, false, null);
    assertNull(results.get(0).getPopulation());
    PopulationDataDto populationData = PopulationDataDto.build(new Date());
    RegionDto region = getRegionFacade().getByUuid(rdcf.region.getUuid());
    region.setGrowthRate(10f);
    getRegionFacade().save(region);
    populationData.setRegion(rdcf.region);
    populationData.setPopulation(new Integer(10000));
    getPopulationDataFacade().savePopulationData(Arrays.asList(populationData));
    results = getCaseStatisticsFacade().queryCaseCount(criteria, StatisticsCaseAttribute.JURISDICTION, StatisticsCaseSubAttribute.REGION, null, null, true, false, LocalDate.now().getYear() + 2);
    // List should have one entry
    assertEquals(Integer.valueOf(12214), results.get(0).getPopulation());
}
Also used : RDCF(de.symeda.sormas.backend.TestDataCreator.RDCF) CaseDataDto(de.symeda.sormas.api.caze.CaseDataDto) PersonDto(de.symeda.sormas.api.person.PersonDto) UserDto(de.symeda.sormas.api.user.UserDto) StatisticsCaseCriteria(de.symeda.sormas.api.statistics.StatisticsCaseCriteria) PopulationDataDto(de.symeda.sormas.api.infrastructure.PopulationDataDto) RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto) StatisticsCaseCountDto(de.symeda.sormas.api.statistics.StatisticsCaseCountDto) Date(java.util.Date) LocalDate(java.time.LocalDate) AbstractBeanTest(de.symeda.sormas.backend.AbstractBeanTest) Test(org.junit.Test)

Example 3 with RegionDto

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

the class RegionFacadeEjb method toDto.

public RegionDto toDto(Region entity) {
    if (entity == null) {
        return null;
    }
    RegionDto dto = new RegionDto();
    DtoHelper.fillDto(dto, entity);
    dto.setName(entity.getName());
    dto.setEpidCode(entity.getEpidCode());
    dto.setGrowthRate(entity.getGrowthRate());
    dto.setArchived(entity.isArchived());
    dto.setExternalID(entity.getExternalID());
    dto.setArea(AreaFacadeEjb.toReferenceDto(entity.getArea()));
    dto.setCountry(CountryFacadeEjb.toReferenceDto(entity.getCountry()));
    dto.setCentrallyManaged(entity.isCentrallyManaged());
    return dto;
}
Also used : RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto)

Example 4 with RegionDto

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

the class TestDataCreator method createRDP.

public RDP createRDP() {
    RegionDto region = createRegion("Region");
    DistrictDto district = createDistrict("District", region.toReference());
    PointOfEntryDto pointOfEntry = createPointOfEntry("POE", region.toReference(), district.toReference());
    return new RDP(region, district, pointOfEntry);
}
Also used : DistrictDto(de.symeda.sormas.api.infrastructure.district.DistrictDto) PointOfEntryDto(de.symeda.sormas.api.infrastructure.pointofentry.PointOfEntryDto) RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto)

Example 5 with RegionDto

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

the class DtoEntityTest method testDtoTooOld.

@Test(expected = OutdatedEntityException.class)
public void testDtoTooOld() {
    RegionDto region = RegionDto.build();
    region.setName("Region1");
    getRegionFacade().save(region);
    region.setName("Region2");
    getRegionFacade().save(region);
}
Also used : RegionDto(de.symeda.sormas.api.infrastructure.region.RegionDto) Test(org.junit.Test)

Aggregations

RegionDto (de.symeda.sormas.api.infrastructure.region.RegionDto)12 Date (java.util.Date)5 Test (org.junit.Test)5 DistrictDto (de.symeda.sormas.api.infrastructure.district.DistrictDto)3 UserDto (de.symeda.sormas.api.user.UserDto)3 AbstractBeanTest (de.symeda.sormas.backend.AbstractBeanTest)3 ImportErrorException (de.symeda.sormas.api.importexport.ImportErrorException)2 PopulationDataDto (de.symeda.sormas.api.infrastructure.PopulationDataDto)2 CommunityDto (de.symeda.sormas.api.infrastructure.community.CommunityDto)2 RegionReferenceDto (de.symeda.sormas.api.infrastructure.region.RegionReferenceDto)2 ArrayList (java.util.ArrayList)2 AgeGroup (de.symeda.sormas.api.AgeGroup)1 FacadeProvider (de.symeda.sormas.api.FacadeProvider)1 CaseDataDto (de.symeda.sormas.api.caze.CaseDataDto)1 I18nProperties (de.symeda.sormas.api.i18n.I18nProperties)1 Validations (de.symeda.sormas.api.i18n.Validations)1 ImportCellData (de.symeda.sormas.api.importexport.ImportCellData)1 InvalidColumnException (de.symeda.sormas.api.importexport.InvalidColumnException)1 ValueSeparator (de.symeda.sormas.api.importexport.ValueSeparator)1 PopulationDataCriteria (de.symeda.sormas.api.infrastructure.PopulationDataCriteria)1