Search in sources :

Example 56 with IllegalQueryException

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

the class TrackedEntityInstanceQueryTest method testTeiQueryParamsWithoutEitherProgramOrTrackedEntityType.

@Test
void testTeiQueryParamsWithoutEitherProgramOrTrackedEntityType() {
    TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
    params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
    IllegalQueryException exception = assertThrows(IllegalQueryException.class, () -> instanceService.validate(params));
    assertEquals("Either Program or Tracked entity type should be specified", exception.getMessage());
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 57 with IllegalQueryException

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

the class DefaultCompleteDataSetRegistrationExchangeService method validate.

public void validate(ExportParams params) throws IllegalQueryException {
    ErrorMessage error = null;
    if (params == null) {
        throw new IllegalQueryException(ErrorCode.E2000);
    }
    if (params.getDataSets().isEmpty()) {
        error = new ErrorMessage(ErrorCode.E2013);
    }
    if (!params.hasPeriods() && !params.hasStartEndDate() && !params.hasCreated() && !params.hasCreatedDuration()) {
        error = new ErrorMessage(ErrorCode.E2002);
    }
    if (params.hasPeriods() && params.hasStartEndDate()) {
        error = new ErrorMessage(ErrorCode.E2003);
    }
    if (params.hasStartEndDate() && params.getStartDate().after(params.getEndDate())) {
        error = new ErrorMessage(ErrorCode.E2004);
    }
    if (params.hasCreatedDuration() && DateUtils.getDuration(params.getCreatedDuration()) == null) {
        error = new ErrorMessage(ErrorCode.E2005);
    }
    if (!params.hasOrganisationUnits() && !params.hasOrganisationUnitGroups()) {
        error = new ErrorMessage(ErrorCode.E2006);
    }
    if (params.isIncludeChildren() && params.hasOrganisationUnitGroups()) {
        error = new ErrorMessage(ErrorCode.E2007);
    }
    if (params.isIncludeChildren() && !params.hasOrganisationUnits()) {
        error = new ErrorMessage(ErrorCode.E2008);
    }
    if (params.hasLimit() && params.getLimit() < 0) {
        error = new ErrorMessage(ErrorCode.E2009);
    }
    if (error != null) {
        log.warn(String.format("Complete data set registration validation failed, code: '%s', message: '%s'", error.getErrorCode(), error.getMessage()));
        throw new IllegalQueryException(error);
    }
    limitToValidIdSchemes(params);
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 58 with IllegalQueryException

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

the class AbstractEnrollmentService method getEnrollment.

@Override
public Enrollment getEnrollment(User user, ProgramInstance programInstance, TrackedEntityInstanceParams params, boolean skipOwnershipCheck) {
    Enrollment enrollment = new Enrollment();
    enrollment.setEnrollment(programInstance.getUid());
    List<String> errors = trackerAccessManager.canRead(user, programInstance, skipOwnershipCheck);
    if (!errors.isEmpty()) {
        throw new IllegalQueryException(errors.toString());
    }
    if (programInstance.getEntityInstance() != null) {
        enrollment.setTrackedEntityType(programInstance.getEntityInstance().getTrackedEntityType().getUid());
        enrollment.setTrackedEntityInstance(programInstance.getEntityInstance().getUid());
    }
    if (programInstance.getOrganisationUnit() != null) {
        enrollment.setOrgUnit(programInstance.getOrganisationUnit().getUid());
        enrollment.setOrgUnitName(programInstance.getOrganisationUnit().getName());
    }
    if (programInstance.getGeometry() != null) {
        enrollment.setGeometry(programInstance.getGeometry());
    }
    enrollment.setCreated(DateUtils.getIso8601NoTz(programInstance.getCreated()));
    enrollment.setCreatedAtClient(DateUtils.getIso8601NoTz(programInstance.getCreatedAtClient()));
    enrollment.setLastUpdated(DateUtils.getIso8601NoTz(programInstance.getLastUpdated()));
    enrollment.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programInstance.getLastUpdatedAtClient()));
    enrollment.setProgram(programInstance.getProgram().getUid());
    enrollment.setStatus(EnrollmentStatus.fromProgramStatus(programInstance.getStatus()));
    enrollment.setEnrollmentDate(programInstance.getEnrollmentDate());
    enrollment.setIncidentDate(programInstance.getIncidentDate());
    enrollment.setFollowup(programInstance.getFollowup());
    enrollment.setCompletedDate(programInstance.getEndDate());
    enrollment.setCompletedBy(programInstance.getCompletedBy());
    enrollment.setStoredBy(programInstance.getStoredBy());
    enrollment.setCreatedByUserInfo(programInstance.getCreatedByUserInfo());
    enrollment.setLastUpdatedByUserInfo(programInstance.getLastUpdatedByUserInfo());
    enrollment.setDeleted(programInstance.isDeleted());
    enrollment.getNotes().addAll(NoteHelper.convertNotes(programInstance.getComments()));
    if (params.isIncludeEvents()) {
        for (ProgramStageInstance programStageInstance : programInstance.getProgramStageInstances()) {
            if ((params.isIncludeDeleted() || !programStageInstance.isDeleted()) && trackerAccessManager.canRead(user, programStageInstance, true).isEmpty()) {
                enrollment.getEvents().add(eventService.getEvent(programStageInstance, params.isDataSynchronizationQuery(), true));
            }
        }
    }
    if (params.isIncludeRelationships()) {
        for (RelationshipItem relationshipItem : programInstance.getRelationshipItems()) {
            enrollment.getRelationships().add(relationshipService.getRelationship(relationshipItem.getRelationship(), RelationshipParams.FALSE, user));
        }
    }
    return enrollment;
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem)

Example 59 with IllegalQueryException

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

the class AbstractEventService method getEventsGrid.

@Transactional(readOnly = true)
@Override
public Grid getEventsGrid(EventSearchParams params) {
    User user = currentUserService.getCurrentUser();
    if (params.getProgramStage() == null || params.getProgramStage().getProgram() == null) {
        throw new IllegalQueryException("Program stage can not be null");
    }
    if (params.getProgramStage().getProgramStageDataElements() == null) {
        throw new IllegalQueryException("Program stage should have at least one data element");
    }
    List<OrganisationUnit> organisationUnits = getOrganisationUnits(params, user);
    params.handleCurrentUserSelectionMode(user);
    if (params.isIncludeAllDataElements()) {
        for (ProgramStageDataElement pde : params.getProgramStage().getProgramStageDataElements()) {
            QueryItem qi = new QueryItem(pde.getDataElement(), pde.getDataElement().getLegendSet(), pde.getDataElement().getValueType(), pde.getDataElement().getAggregationType(), pde.getDataElement().hasOptionSet() ? pde.getDataElement().getOptionSet() : null);
            params.getDataElements().add(qi);
        }
    } else {
        if (params.getDataElements().isEmpty()) {
            for (ProgramStageDataElement pde : params.getProgramStage().getProgramStageDataElements()) {
                if (pde.getDisplayInReports()) {
                    QueryItem qi = new QueryItem(pde.getDataElement(), pde.getDataElement().getLegendSet(), pde.getDataElement().getValueType(), pde.getDataElement().getAggregationType(), pde.getDataElement().hasOptionSet() ? pde.getDataElement().getOptionSet() : null);
                    params.getDataElements().add(qi);
                }
            }
        }
    }
    // ---------------------------------------------------------------------
    // Grid headers
    // ---------------------------------------------------------------------
    Grid grid = new ListGrid();
    for (String col : STATIC_EVENT_COLUMNS) {
        grid.addHeader(new GridHeader(col, col));
    }
    for (QueryItem item : params.getDataElements()) {
        grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getName()));
    }
    List<Map<String, String>> events = eventStore.getEventsGrid(params, organisationUnits);
    for (Map<String, String> event : events) {
        grid.addRow();
        for (String col : STATIC_EVENT_COLUMNS) {
            grid.addValue(event.get(col));
        }
        for (QueryItem item : params.getDataElements()) {
            grid.addValue(event.get(item.getItemId()));
        }
    }
    Map<String, Object> metaData = new HashMap<>();
    if (params.isPaging()) {
        final Pager pager;
        if (params.isTotalPages()) {
            int count = eventStore.getEventCount(params, organisationUnits);
            pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
        } else {
            pager = handleLastPageFlag(params, grid);
        }
        metaData.put(PAGER_META_KEY, pager);
    }
    grid.setMetaData(metaData);
    return grid;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryItem(org.hisp.dhis.common.QueryItem) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) ListGrid(org.hisp.dhis.system.grid.ListGrid) GridHeader(org.hisp.dhis.common.GridHeader) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) SlimPager(org.hisp.dhis.common.SlimPager) Pager(org.hisp.dhis.common.Pager) Map(java.util.Map) HashMap(java.util.HashMap) Collections.emptyMap(java.util.Collections.emptyMap) CachingMap(org.hisp.dhis.commons.collection.CachingMap) Transactional(org.springframework.transaction.annotation.Transactional)

Example 60 with IllegalQueryException

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

the class InputUtils method getAttributeOptionCombo.

/**
 * Validates and retrieves the attribute option combo. 409 conflict as
 * status code along with a textual message will be set on the response in
 * case of invalid input.
 *
 * @param categoryCombo the category combo.
 * @param opts list of category option uid.
 * @param attributeOptionCombo the explicit attribute option combo
 *        identifier.
 * @return the attribute option combo identified from the given input, or
 *         null if the input was invalid.
 */
public 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 = idObjectManager.getObject(CategoryOption.class, idScheme, uid);
            if (categoryOption == null) {
                throw new IllegalQueryException("Illegal category option identifier: " + uid);
            }
            categoryOptions.add(categoryOption);
        }
        attrOptCombo = categoryService.getCategoryOptionCombo(categoryCombo, 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 = categoryService.getCategoryOptionCombo(attributeOptionCombo);
    }
    if (attrOptCombo == null) {
        attrOptCombo = categoryService.getDefaultCategoryOptionCombo();
    }
    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)

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