Search in sources :

Example 1 with AreaReferenceDto

use of de.symeda.sormas.api.infrastructure.area.AreaReferenceDto 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 AreaReferenceDto

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

the class ReferenceDtoHtmlProviderTest method testApplyPreventsHtmlInjection.

@Test
public void testApplyPreventsHtmlInjection() {
    ReferenceDtoHtmlProvider renderer = new ReferenceDtoHtmlProvider();
    ReferenceDto dto = new AreaReferenceDto("<script>alert(1);</script>", "<script>alert(2);</script>");
    String result = renderer.apply(dto);
    assertFalse(result.contains("<script>alert(1);</script>"));
    assertFalse(result.contains("<script>alert(2);</script>"));
}
Also used : ReferenceDto(de.symeda.sormas.api.ReferenceDto) AreaReferenceDto(de.symeda.sormas.api.infrastructure.area.AreaReferenceDto) AreaReferenceDto(de.symeda.sormas.api.infrastructure.area.AreaReferenceDto) Test(org.junit.Test)

Example 3 with AreaReferenceDto

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

the class CampaignFormDataFacadeEjb method getDiagramData.

@Override
public List<CampaignDiagramDataDto> getDiagramData(List<CampaignDiagramSeries> diagramSeries, CampaignDiagramCriteria campaignDiagramCriteria) {
    List<CampaignDiagramDataDto> resultData = new ArrayList<>();
    final AreaReferenceDto area = campaignDiagramCriteria.getArea();
    final RegionReferenceDto region = campaignDiagramCriteria.getRegion();
    final DistrictReferenceDto district = campaignDiagramCriteria.getDistrict();
    final CampaignReferenceDto campaign = campaignDiagramCriteria.getCampaign();
    for (CampaignDiagramSeries series : diagramSeries) {
        // @formatter:off
        final String areaFilter = area != null ? " AND " + Area.TABLE_NAME + "." + Area.UUID + " = :areaUuid" : "";
        final String regionFilter = region != null ? " AND " + CampaignFormData.REGION + "." + Region.UUID + " = :regionUuid" : "";
        final String districtFilter = district != null ? " AND " + CampaignFormData.DISTRICT + "." + District.UUID + " = :districtUuid" : "";
        final String campaignFilter = campaign != null ? " AND " + Campaign.TABLE_NAME + "." + Campaign.UUID + " = :campaignUuid" : "";
        // @formatter:on
        // SELECT
        StringBuilder selectBuilder = new StringBuilder("SELECT ").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.UUID).append(" as formUuid,").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.FORM_ID).append(" as formId");
        if (series.getFieldId() != null) {
            selectBuilder.append(", jsonData->>'").append(CampaignFormDataEntry.ID).append("' as fieldId, jsonMeta->>'").append(CampaignFormElement.CAPTION).append("' as fieldCaption,").append("CASE WHEN (jsonMeta ->> '").append(CampaignFormElement.TYPE).append("') = '").append(CampaignFormElementType.NUMBER.toString()).append("' THEN sum(cast_to_int(jsonData->>'").append(CampaignFormDataEntry.VALUE).append("', 0)) ELSE sum(CASE WHEN(jsonData->>'").append(CampaignFormDataEntry.VALUE).append("') = '").append(series.getReferenceValue()).append("' THEN 1 ELSE 0 END) END as sumValue,");
        } else {
            selectBuilder.append(", null as fieldId, null as fieldCaption, count(formId) as sumValue,");
        }
        final String jurisdictionGrouping;
        switch(campaignDiagramCriteria.getCampaignJurisdictionLevelGroupBy()) {
            case REGION:
                appendInfrastructureSelection(selectBuilder, Region.TABLE_NAME, Region.NAME);
                break;
            case DISTRICT:
                appendInfrastructureSelection(selectBuilder, District.TABLE_NAME, District.NAME);
                break;
            case COMMUNITY:
                appendInfrastructureSelection(selectBuilder, Community.TABLE_NAME, Community.NAME);
                break;
            case AREA:
            default:
                appendInfrastructureSelection(selectBuilder, Area.TABLE_NAME, Area.NAME);
        }
        // JOINS
        StringBuilder joinBuilder = new StringBuilder(" LEFT JOIN ").append(CampaignFormMeta.TABLE_NAME).append(" ON ").append(CampaignFormData.CAMPAIGN_FORM_META).append("_id = ").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.ID).append(" LEFT JOIN ").append(Region.TABLE_NAME).append(" ON ").append(CampaignFormData.REGION).append("_id =").append(Region.TABLE_NAME).append(".").append(Region.ID).append(" LEFT JOIN ").append(Area.TABLE_NAME).append(" ON ").append(Region.AREA).append("_id = ").append(Area.TABLE_NAME).append(".").append(Area.ID).append(" LEFT JOIN ").append(District.TABLE_NAME).append(" ON ").append(CampaignFormData.DISTRICT).append("_id = ").append(District.TABLE_NAME).append(".").append(District.ID).append(" LEFT JOIN ").append(Community.TABLE_NAME).append(" ON ").append(CampaignFormData.COMMUNITY).append("_id = ").append(Community.TABLE_NAME).append(".").append(Community.ID).append(" LEFT JOIN ").append(Campaign.TABLE_NAME).append(" ON ").append(CampaignFormData.CAMPAIGN).append("_id = ").append(Campaign.TABLE_NAME).append(".").append(Campaign.ID);
        if (series.getFieldId() != null) {
            joinBuilder.append(", json_array_elements(").append(CampaignFormData.FORM_VALUES).append(") as jsonData, json_array_elements(").append(CampaignFormMeta.CAMPAIGN_FORM_ELEMENTS).append(") as jsonMeta");
        }
        // WHERE
        StringBuilder whereBuilder = new StringBuilder(" WHERE ").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.FORM_ID).append(" = :campaignFormMetaId");
        if (series.getFieldId() != null) {
            whereBuilder.append(" AND jsonData->>'").append(CampaignFormDataEntry.ID).append("' = :campaignFormDataId").append(" AND jsonData->>'").append(CampaignFormDataEntry.VALUE).append("' IS NOT NULL AND jsonData->>'").append(CampaignFormDataEntry.ID).append("' = jsonMeta->>'").append(CampaignFormElement.ID).append("'");
        }
        whereBuilder.append(areaFilter).append(regionFilter).append(districtFilter).append(campaignFilter);
        // GROUP BY
        StringBuilder groupByBuilder = new StringBuilder(" GROUP BY ").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.UUID).append(",").append(CampaignFormMeta.TABLE_NAME).append(".").append(CampaignFormMeta.FORM_ID);
        if (series.getFieldId() != null) {
            groupByBuilder.append(", jsonData->>'").append(CampaignFormDataEntry.ID).append("', jsonMeta->>'").append(CampaignFormElement.CAPTION).append("', jsonMeta->>'").append(CampaignFormElement.TYPE).append("'");
        }
        switch(campaignDiagramCriteria.getCampaignJurisdictionLevelGroupBy()) {
            case REGION:
                jurisdictionGrouping = ", " + Region.TABLE_NAME + "." + Region.UUID + ", " + Region.TABLE_NAME + "." + Region.NAME;
                break;
            case DISTRICT:
                jurisdictionGrouping = ", " + District.TABLE_NAME + "." + District.UUID + ", " + District.TABLE_NAME + "." + District.NAME;
                break;
            case COMMUNITY:
                jurisdictionGrouping = ", " + Community.TABLE_NAME + "." + Community.UUID + ", " + Community.TABLE_NAME + "." + Community.NAME;
                break;
            case AREA:
            default:
                jurisdictionGrouping = ", " + Area.TABLE_NAME + "." + Area.UUID + ", " + Area.TABLE_NAME + "." + Area.NAME;
        }
        groupByBuilder.append(jurisdictionGrouping);
        // @formatter:off
        Query seriesDataQuery = em.createNativeQuery(selectBuilder.toString() + " FROM " + CampaignFormData.TABLE_NAME + joinBuilder + whereBuilder + groupByBuilder);
        // @formatter:on
        seriesDataQuery.setParameter("campaignFormMetaId", series.getFormId());
        if (area != null) {
            seriesDataQuery.setParameter("areaUuid", area.getUuid());
        }
        if (region != null) {
            seriesDataQuery.setParameter("regionUuid", region.getUuid());
        }
        if (district != null) {
            seriesDataQuery.setParameter("districtUuid", district.getUuid());
        }
        if (campaign != null) {
            seriesDataQuery.setParameter("campaignUuid", campaign.getUuid());
        }
        if (series.getFieldId() != null) {
            seriesDataQuery.setParameter("campaignFormDataId", series.getFieldId());
        }
        @SuppressWarnings("unchecked") List<Object[]> resultList = seriesDataQuery.getResultList();
        resultData.addAll(resultList.stream().map((result) -> new CampaignDiagramDataDto((String) result[0], (String) result[1], (String) result[2], (String) result[3], (Number) result[4], (String) result[5], (String) result[6], series.getStack())).collect(Collectors.toList()));
    }
    return resultData;
}
Also used : CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) CampaignDiagramSeries(de.symeda.sormas.api.campaign.diagram.CampaignDiagramSeries) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) AreaReferenceDto(de.symeda.sormas.api.infrastructure.area.AreaReferenceDto) CampaignDiagramDataDto(de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto) CampaignReferenceDto(de.symeda.sormas.api.campaign.CampaignReferenceDto)

Example 4 with AreaReferenceDto

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

the class CampaignFormDataFacadeEjb method getDiagramDataByAgeGroup.

public List<CampaignDiagramDataDto> getDiagramDataByAgeGroup(CampaignDiagramSeries diagramSeriesTotal, CampaignDiagramSeries diagramSeries, CampaignDiagramCriteria campaignDiagramCriteria) {
    List<CampaignDiagramDataDto> resultData = new ArrayList<>();
    final AreaReferenceDto area = campaignDiagramCriteria.getArea();
    final RegionReferenceDto region = campaignDiagramCriteria.getRegion();
    final DistrictReferenceDto district = campaignDiagramCriteria.getDistrict();
    final CampaignJurisdictionLevel grouping = campaignDiagramCriteria.getCampaignJurisdictionLevelGroupBy();
    if (grouping == CampaignJurisdictionLevel.AREA) {
        List<Area> areas = areaService.getAll();
        areas.forEach(areaItem -> {
            Integer population = populationDataFacadeEjb.getAreaPopulation(areaItem.getUuid(), diagramSeriesTotal.getPopulationGroup());
            if (population == 0) {
                resultData.add(new CampaignDiagramDataDto(areaItem.getName(), 0, areaItem.getUuid(), areaItem.getName(), diagramSeries.getFieldId(), diagramSeries.getFormId(), false));
            } else {
                resultData.add(new CampaignDiagramDataDto(areaItem.getName(), population, areaItem.getUuid(), areaItem.getName(), diagramSeries.getFieldId(), diagramSeries.getFormId(), true));
            }
        });
    } else if (grouping == CampaignJurisdictionLevel.REGION) {
        List<RegionReferenceDto> regions;
        if (area != null)
            regions = regionFacadeEjb.getAllActiveByArea(area.getUuid());
        else
            regions = regionFacadeEjb.getAllActiveAsReference();
        // this should not be needed
        // if (regions.isEmpty()) {
        // resultData.add(
        // new CampaignDiagramDataDto(
        // area.getCaption(),
        // 0,
        // area.getUuid(),
        // area.getCaption(),
        // diagramSeries.getFieldId(),
        // diagramSeries.getFormId(),
        // false));
        // } else {
        regions.stream().forEach(regionReferenceDto -> {
            PopulationDataCriteria criteria = new PopulationDataCriteria();
            criteria.sexIsNull(true);
            criteria.region(regionReferenceDto);
            criteria.ageGroup(diagramSeriesTotal.getPopulationGroup());
            List<PopulationDataDto> populationDataDto = populationDataFacadeEjb.getPopulationData(criteria);
            Integer populationSum = 0;
            if (!populationDataDto.isEmpty()) {
                populationSum = populationDataDto.stream().mapToInt(e -> e.getPopulation()).sum();
                resultData.add(new CampaignDiagramDataDto(regionReferenceDto.getCaption(), populationSum, regionReferenceDto.getUuid(), regionReferenceDto.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), true));
            } else {
                resultData.add(new CampaignDiagramDataDto(regionReferenceDto.getCaption(), 0, regionReferenceDto.getUuid(), regionReferenceDto.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), false));
            }
        });
    // }
    } else if (grouping == CampaignJurisdictionLevel.DISTRICT || Objects.isNull(district)) {
        List<DistrictReferenceDto> districts;
        if (region != null) {
            districts = districtFacadeEjb.getAllActiveByRegion(region.getUuid());
        } else if (area != null) {
            districts = districtFacadeEjb.getAllActiveByArea(area.getUuid());
        } else {
            districts = districtFacadeEjb.getAllActiveAsReference();
        }
        // this should not be needed
        // if (districts.isEmpty()) {
        // resultData.add(
        // new CampaignDiagramDataDto(
        // region.getCaption(),
        // 0,
        // region.getUuid(),
        // region.getCaption(),
        // diagramSeries.getFieldId(),
        // diagramSeries.getFormId(),
        // false));
        // } else {
        districts.stream().forEach(districtReferenceDto -> {
            PopulationDataCriteria criteria = new PopulationDataCriteria();
            criteria.sexIsNull(true);
            criteria.district(districtReferenceDto);
            criteria.region(region);
            criteria.ageGroup(diagramSeriesTotal.getPopulationGroup());
            List<PopulationDataDto> populationDataDtoList = populationDataFacadeEjb.getPopulationData(criteria);
            Integer populationSum = 0;
            if (!populationDataDtoList.isEmpty()) {
                populationSum = populationDataDtoList.stream().mapToInt(e -> e.getPopulation()).sum();
                resultData.add(new CampaignDiagramDataDto(districtReferenceDto.getCaption(), populationSum, districtReferenceDto.getUuid(), districtReferenceDto.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), true));
            } else {
                resultData.add(new CampaignDiagramDataDto(districtReferenceDto.getCaption(), populationSum, districtReferenceDto.getUuid(), districtReferenceDto.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), false));
            }
        });
    // }
    } else if (district != null) {
        resultData.add(new CampaignDiagramDataDto(district.getCaption(), 0, district.getUuid(), district.getCaption(), diagramSeries.getFieldId(), diagramSeries.getFormId(), true));
    }
    return resultData;
}
Also used : PopulationDataCriteria(de.symeda.sormas.api.infrastructure.PopulationDataCriteria) DtoHelper(de.symeda.sormas.backend.util.DtoHelper) Join(javax.persistence.criteria.Join) CampaignFormDataReferenceDto(de.symeda.sormas.api.campaign.data.CampaignFormDataReferenceDto) AbstractDomainObject(de.symeda.sormas.backend.common.AbstractDomainObject) Date(java.util.Date) I18nProperties(de.symeda.sormas.api.i18n.I18nProperties) CampaignDiagramCriteria(de.symeda.sormas.api.campaign.diagram.CampaignDiagramCriteria) CampaignFormMetaFacadeEjb(de.symeda.sormas.backend.campaign.form.CampaignFormMetaFacadeEjb) CommunityFacadeEjb(de.symeda.sormas.backend.infrastructure.community.CommunityFacadeEjb) DistrictService(de.symeda.sormas.backend.infrastructure.district.DistrictService) Valid(javax.validation.Valid) CampaignService(de.symeda.sormas.backend.campaign.CampaignService) Predicate(javax.persistence.criteria.Predicate) UserFacadeEjb(de.symeda.sormas.backend.user.UserFacadeEjb) CampaignDiagramSeries(de.symeda.sormas.api.campaign.diagram.CampaignDiagramSeries) Area(de.symeda.sormas.backend.infrastructure.area.Area) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JoinType(javax.persistence.criteria.JoinType) CampaignFormDataFacade(de.symeda.sormas.api.campaign.data.CampaignFormDataFacade) Stateless(javax.ejb.Stateless) CampaignFormElementType(de.symeda.sormas.api.campaign.form.CampaignFormElementType) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) CommunityService(de.symeda.sormas.backend.infrastructure.community.CommunityService) Region(de.symeda.sormas.backend.infrastructure.region.Region) District(de.symeda.sormas.backend.infrastructure.district.District) NotNull(javax.validation.constraints.NotNull) CampaignJurisdictionLevel(de.symeda.sormas.api.campaign.CampaignJurisdictionLevel) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Query(javax.persistence.Query) UserService(de.symeda.sormas.backend.user.UserService) CampaignFormDataCriteria(de.symeda.sormas.api.campaign.data.CampaignFormDataCriteria) DistrictFacadeEjb(de.symeda.sormas.backend.infrastructure.district.DistrictFacadeEjb) Campaign(de.symeda.sormas.backend.campaign.Campaign) Order(javax.persistence.criteria.Order) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) ValidationRuntimeException(de.symeda.sormas.api.utils.ValidationRuntimeException) CampaignFormMeta(de.symeda.sormas.backend.campaign.form.CampaignFormMeta) Community(de.symeda.sormas.backend.infrastructure.community.Community) QueryHelper(de.symeda.sormas.backend.util.QueryHelper) CampaignFormElement(de.symeda.sormas.api.campaign.form.CampaignFormElement) PopulationDataFacadeEjb(de.symeda.sormas.backend.infrastructure.PopulationDataFacadeEjb) ArrayList(java.util.ArrayList) CampaignFormDataDto(de.symeda.sormas.api.campaign.data.CampaignFormDataDto) CampaignDiagramDataDto(de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto) LocalBean(javax.ejb.LocalBean) Expression(javax.persistence.criteria.Expression) CampaignFormDataEntry(de.symeda.sormas.api.campaign.data.CampaignFormDataEntry) CriteriaBuilderHelper(de.symeda.sormas.backend.common.CriteriaBuilderHelper) AreaService(de.symeda.sormas.backend.infrastructure.area.AreaService) EJB(javax.ejb.EJB) Root(javax.persistence.criteria.Root) ModelConstants(de.symeda.sormas.backend.util.ModelConstants) CampaignFormMetaService(de.symeda.sormas.backend.campaign.form.CampaignFormMetaService) CampaignFormDataIndexDto(de.symeda.sormas.api.campaign.data.CampaignFormDataIndexDto) Validations(de.symeda.sormas.api.i18n.Validations) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) CampaignReferenceDto(de.symeda.sormas.api.campaign.CampaignReferenceDto) EntityManager(javax.persistence.EntityManager) PersistenceContext(javax.persistence.PersistenceContext) SortProperty(de.symeda.sormas.api.utils.SortProperty) RegionFacadeEjb(de.symeda.sormas.backend.infrastructure.region.RegionFacadeEjb) RegionService(de.symeda.sormas.backend.infrastructure.region.RegionService) UserRight(de.symeda.sormas.api.user.UserRight) PopulationDataDto(de.symeda.sormas.api.infrastructure.PopulationDataDto) CampaignFacadeEjb(de.symeda.sormas.backend.campaign.CampaignFacadeEjb) AreaReferenceDto(de.symeda.sormas.api.infrastructure.area.AreaReferenceDto) Collections(java.util.Collections) ArrayList(java.util.ArrayList) CampaignJurisdictionLevel(de.symeda.sormas.api.campaign.CampaignJurisdictionLevel) DistrictReferenceDto(de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto) Area(de.symeda.sormas.backend.infrastructure.area.Area) RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) PopulationDataCriteria(de.symeda.sormas.api.infrastructure.PopulationDataCriteria) AreaReferenceDto(de.symeda.sormas.api.infrastructure.area.AreaReferenceDto) List(java.util.List) ArrayList(java.util.ArrayList) PopulationDataDto(de.symeda.sormas.api.infrastructure.PopulationDataDto) CampaignDiagramDataDto(de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto)

Example 5 with AreaReferenceDto

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

the class CampaignFormDataEditForm method addFields.

@SuppressWarnings("deprecation")
@Override
protected void addFields() {
    ComboBox cbCampaign = addField(CampaignFormDataDto.CAMPAIGN, ComboBox.class);
    cbCampaign.addItems(FacadeProvider.getCampaignFacade().getAllActiveCampaignsAsReference());
    ComboBox cbRegion = addInfrastructureField(CampaignFormDataDto.REGION);
    ComboBox cbDistrict = addInfrastructureField(CampaignFormDataDto.DISTRICT);
    ComboBox cbCommunity = addInfrastructureField(CampaignFormDataDto.COMMUNITY);
    addField(CampaignFormDataDto.FORM_DATE, DateField.class);
    setRequired(true, CampaignFormDataDto.CAMPAIGN, CampaignFormDataDto.FORM_DATE, CampaignFormDataDto.REGION, CampaignFormDataDto.DISTRICT, CampaignFormDataDto.COMMUNITY);
    addInfrastructureListeners(cbRegion, cbDistrict, cbCommunity);
    cbRegion.addItems(FacadeProvider.getRegionFacade().getAllActiveByServerCountry());
    final UserDto currentUser = UserProvider.getCurrent().getUser();
    final RegionReferenceDto currentUserRegion = currentUser.getRegion();
    if (FacadeProvider.getFeatureConfigurationFacade().isFeatureEnabled(FeatureType.INFRASTRUCTURE_TYPE_AREA)) {
        ComboBox cbArea = addCustomField(CampaignFormDataEditForm.AREA, AreaReferenceDto.class, ComboBox.class);
        cbArea.setCaption(I18nProperties.getCaption(Captions.CampaignFormData_area));
        setRequired(true, CampaignFormDataEditForm.AREA);
        cbArea.addItems(FacadeProvider.getAreaFacade().getAllActiveAsReference());
        cbArea.addValueChangeListener(e -> {
            AreaReferenceDto area = (AreaReferenceDto) e.getProperty().getValue();
            if (area == null) {
                cbRegion.setValue(null);
            }
            FieldHelper.updateItems(cbRegion, area != null ? FacadeProvider.getRegionFacade().getAllActiveByArea(area.getUuid()) : FacadeProvider.getRegionFacade().getAllActiveByServerCountry());
        });
        cbRegion.addValueChangeListener(e -> {
            RegionReferenceDto region = (RegionReferenceDto) e.getProperty().getValue();
            if (Objects.nonNull(region)) {
                cbArea.setValue(FacadeProvider.getRegionFacade().getByUuid(region.getUuid()).getArea());
            }
        });
        if (currentUserRegion != null) {
            final AreaReferenceDto area = FacadeProvider.getRegionFacade().getByUuid(currentUserRegion.getUuid()).getArea();
            cbArea.setValue(area);
            if (currentUserRegion != null) {
                cbArea.setEnabled(false);
            }
        }
    }
    if (currentUserRegion != null) {
        cbRegion.setValue(currentUserRegion);
        cbRegion.setEnabled(false);
    }
    if (currentUser.getDistrict() != null) {
        cbDistrict.setValue(currentUser.getDistrict());
        cbDistrict.setEnabled(false);
    }
    if (currentUser.getCommunity() != null) {
        cbCommunity.setValue(currentUser.getCommunity());
        cbCommunity.setEnabled(false);
    }
}
Also used : RegionReferenceDto(de.symeda.sormas.api.infrastructure.region.RegionReferenceDto) ComboBox(com.vaadin.v7.ui.ComboBox) UserDto(de.symeda.sormas.api.user.UserDto) AreaReferenceDto(de.symeda.sormas.api.infrastructure.area.AreaReferenceDto)

Aggregations

AreaReferenceDto (de.symeda.sormas.api.infrastructure.area.AreaReferenceDto)6 RegionReferenceDto (de.symeda.sormas.api.infrastructure.region.RegionReferenceDto)4 DistrictReferenceDto (de.symeda.sormas.api.infrastructure.district.DistrictReferenceDto)3 UserDto (de.symeda.sormas.api.user.UserDto)3 CampaignJurisdictionLevel (de.symeda.sormas.api.campaign.CampaignJurisdictionLevel)2 CampaignReferenceDto (de.symeda.sormas.api.campaign.CampaignReferenceDto)2 CampaignDiagramDataDto (de.symeda.sormas.api.campaign.diagram.CampaignDiagramDataDto)2 CampaignDiagramSeries (de.symeda.sormas.api.campaign.diagram.CampaignDiagramSeries)2 ArrayList (java.util.ArrayList)2 Query (javax.persistence.Query)2 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)2 ComboBox (com.vaadin.v7.ui.ComboBox)1 ReferenceDto (de.symeda.sormas.api.ReferenceDto)1 CampaignFormDataCriteria (de.symeda.sormas.api.campaign.data.CampaignFormDataCriteria)1 CampaignFormDataDto (de.symeda.sormas.api.campaign.data.CampaignFormDataDto)1 CampaignFormDataEntry (de.symeda.sormas.api.campaign.data.CampaignFormDataEntry)1 CampaignFormDataFacade (de.symeda.sormas.api.campaign.data.CampaignFormDataFacade)1 CampaignFormDataIndexDto (de.symeda.sormas.api.campaign.data.CampaignFormDataIndexDto)1 CampaignFormDataReferenceDto (de.symeda.sormas.api.campaign.data.CampaignFormDataReferenceDto)1 CampaignDiagramCriteria (de.symeda.sormas.api.campaign.diagram.CampaignDiagramCriteria)1