Search in sources :

Example 21 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class AbstractEnrollmentService method checkAttributes.

private void checkAttributes(org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance, Enrollment enrollment, ImportOptions importOptions, ImportConflicts importConflicts) {
    Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
    Map<String, String> attributeValueMap = Maps.newHashMap();
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    for (ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getProgramAttributes()) {
        mandatoryMap.put(programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.isMandatory());
    }
    // ignore attributes which do not belong to this program
    trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> mandatoryMap.containsKey(value.getAttribute())).forEach(value -> attributeValueMap.put(value.getAttribute().getUid(), value.getValue()));
    for (Attribute attribute : enrollment.getAttributes()) {
        attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
        validateAttributeType(attribute, importOptions, importConflicts);
    }
    List<String> errors = trackerAccessManager.canRead(importOptions.getUser(), trackedEntityInstance);
    if (!errors.isEmpty()) {
        throw new IllegalQueryException(errors.toString());
    }
    checkAttributeForMandatoryMaxLengthAndUniqueness(trackedEntityInstance, importOptions, importConflicts, mandatoryMap, attributeValueMap);
    if (!attributeValueMap.isEmpty()) {
        importConflicts.addConflict(ATTRIBUTE_ATTRIBUTE, "Only program attributes is allowed for enrollment " + attributeValueMap);
    }
    if (!program.getSelectEnrollmentDatesInFuture()) {
        if (Objects.nonNull(enrollment.getEnrollmentDate()) && enrollment.getEnrollmentDate().after(new Date())) {
            importConflicts.addConflict("Enrollment.date", "Enrollment Date can't be future date :" + enrollment.getEnrollmentDate());
        }
    }
    if (!program.getSelectIncidentDatesInFuture()) {
        if (Objects.nonNull(enrollment.getIncidentDate()) && enrollment.getIncidentDate().after(new Date())) {
            importConflicts.addConflict("Enrollment.incidentDate", "Incident Date can't be future date :" + enrollment.getIncidentDate());
        }
    }
}
Also used : Authorities(org.hisp.dhis.security.Authorities) FIRST_PAGE(org.hisp.dhis.common.SlimPager.FIRST_PAGE) EventService(org.hisp.dhis.dxf2.events.event.EventService) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) NoteHelper(org.hisp.dhis.dxf2.events.NoteHelper) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) SetValuedMap(org.apache.commons.collections4.SetValuedMap) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) Transactional(javax.transaction.Transactional) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) QueryService(org.hisp.dhis.query.QueryService) Event(org.hisp.dhis.dxf2.events.event.Event) Objects(java.util.Objects) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TEA_VALUE_MAX_LENGTH(org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CollectionUtils.isNotEmpty(org.apache.commons.collections4.CollectionUtils.isNotEmpty) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) Constants(org.hisp.dhis.dxf2.Constants) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) GeoUtils(org.hisp.dhis.system.util.GeoUtils) ObjectUtils.defaultIfNull(org.apache.commons.lang3.ObjectUtils.defaultIfNull) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Notifier(org.hisp.dhis.system.notification.Notifier) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) SlimPager(org.hisp.dhis.common.SlimPager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) User(org.hisp.dhis.user.User) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) I18nManager(org.hisp.dhis.i18n.I18nManager) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) IdSchemes(org.hisp.dhis.common.IdSchemes) Pager(org.hisp.dhis.common.Pager) RelationshipService(org.hisp.dhis.dxf2.events.relationship.RelationshipService) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DEFAULT_PAGE_SIZE(org.hisp.dhis.common.Pager.DEFAULT_PAGE_SIZE) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Maps(com.google.common.collect.Maps) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) Note(org.hisp.dhis.dxf2.events.event.Note) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) Collectors.toList(java.util.stream.Collectors.toList) CollectionUtils(org.hisp.dhis.commons.collection.CollectionUtils) FeatureType(org.hisp.dhis.organisationunit.FeatureType) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) ProgramService(org.hisp.dhis.program.ProgramService) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Date(java.util.Date)

Example 22 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class AttributeOptionComboLoader method getAttributeOptionCombo.

private CategoryOptionCombo getAttributeOptionCombo(CategoryCombo categoryCombo, Set<String> opts, String attributeOptionCombo, IdScheme idScheme) {
    if (categoryCombo == null) {
        throw new IllegalQueryException("Illegal category combo");
    }
    // ---------------------------------------------------------------------
    // Attribute category options validation
    // ---------------------------------------------------------------------
    CategoryOptionCombo attrOptCombo = null;
    if (opts != null) {
        Set<CategoryOption> categoryOptions = new HashSet<>();
        for (String uid : opts) {
            CategoryOption categoryOption = getCategoryOption(idScheme, uid);
            if (categoryOption == null) {
                throw new IllegalQueryException("Illegal category option identifier: " + uid);
            }
            categoryOptions.add(categoryOption);
        }
        final String id = resolveCategoryComboId(categoryCombo, idScheme);
        attrOptCombo = getAttributeOptionCombo(idScheme, id, categoryOptions);
        if (attrOptCombo == null) {
            throw new IllegalQueryException("Attribute option combo does not exist for given category combo and category options");
        }
    } else if (attributeOptionCombo != null) {
        attrOptCombo = getCategoryOptionCombo(idScheme, attributeOptionCombo);
    }
    if (attrOptCombo == null) {
        attrOptCombo = getDefault();
    }
    if (attrOptCombo == null) {
        throw new IllegalQueryException("Default attribute option combo does not exist");
    }
    return attrOptCombo;
}
Also used : CategoryOption(org.hisp.dhis.category.CategoryOption) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) HashSet(java.util.HashSet)

Example 23 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultEventQueryValidator method validate.

// -------------------------------------------------------------------------
// EventQueryValidator implementation
// -------------------------------------------------------------------------
@Override
public void validate(EventQueryParams params) throws IllegalQueryException, MaintenanceModeException {
    queryValidator.validateMaintenanceMode();
    ErrorMessage error = validateForErrorMessage(params);
    if (error != null) {
        log.warn(String.format("Event analytics validation failed, code: '%s', message: '%s'", error.getErrorCode(), error.getMessage()));
        throw new IllegalQueryException(error);
    }
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 24 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultEventDataQueryService method getValueDimension.

private DimensionalItemObject getValueDimension(String value) {
    if (value == null) {
        return null;
    }
    DataElement de = dataElementService.getDataElement(value);
    if (de != null && de.isNumericType()) {
        return de;
    }
    TrackedEntityAttribute at = attributeService.getTrackedEntityAttribute(value);
    if (at != null && at.isNumericType()) {
        return at;
    }
    throw new IllegalQueryException(new ErrorMessage(ErrorCode.E7223, value));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 25 with IllegalQueryException

use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.

the class DefaultProgramInstanceService method validate.

@Override
public void validate(ProgramInstanceQueryParams params) throws IllegalQueryException {
    String violation = null;
    if (params == null) {
        throw new IllegalQueryException("Params cannot be null");
    }
    User user = params.getUser();
    if (!params.hasOrganisationUnits() && !(params.isOrganisationUnitMode(ALL) || params.isOrganisationUnitMode(ACCESSIBLE))) {
        violation = "At least one organisation unit must be specified";
    }
    if (params.isOrganisationUnitMode(ACCESSIBLE) && (user == null || !user.hasDataViewOrganisationUnitWithFallback())) {
        violation = "Current user must be associated with at least one organisation unit when selection mode is ACCESSIBLE";
    }
    if (params.hasProgram() && params.hasTrackedEntityType()) {
        violation = "Program and tracked entity cannot be specified simultaneously";
    }
    if (params.hasProgramStatus() && !params.hasProgram()) {
        violation = "Program must be defined when program status is defined";
    }
    if (params.hasFollowUp() && !params.hasProgram()) {
        violation = "Program must be defined when follow up status is defined";
    }
    if (params.hasProgramStartDate() && !params.hasProgram()) {
        violation = "Program must be defined when program start date is specified";
    }
    if (params.hasProgramEndDate() && !params.hasProgram()) {
        violation = "Program must be defined when program end date is specified";
    }
    if (params.hasLastUpdated() && params.hasLastUpdatedDuration()) {
        violation = "Last updated and last updated duration cannot be specified simultaneously";
    }
    if (params.hasLastUpdatedDuration() && DateUtils.getDuration(params.getLastUpdatedDuration()) == null) {
        violation = "Duration is not valid: " + params.getLastUpdatedDuration();
    }
    if (violation != null) {
        log.warn("Validation failed: " + violation);
        throw new IllegalQueryException(violation);
    }
}
Also used : User(org.hisp.dhis.user.User) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Aggregations

IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)98 Test (org.junit.jupiter.api.Test)26 ErrorMessage (org.hisp.dhis.feedback.ErrorMessage)22 HashSet (java.util.HashSet)17 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)17 User (org.hisp.dhis.user.User)14 QueryItem (org.hisp.dhis.common.QueryItem)13 ArrayList (java.util.ArrayList)12 Date (java.util.Date)11 Program (org.hisp.dhis.program.Program)11 QueryFilter (org.hisp.dhis.common.QueryFilter)10 TrackedEntityInstanceCriteria (org.hisp.dhis.webapi.controller.event.webrequest.TrackedEntityInstanceCriteria)10 Transactional (org.springframework.transaction.annotation.Transactional)10 QueryOperator (org.hisp.dhis.common.QueryOperator)9 List (java.util.List)8 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 DataElement (org.hisp.dhis.dataelement.DataElement)8 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)7 Map (java.util.Map)6 Set (java.util.Set)6