Search in sources :

Example 41 with IllegalQueryException

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

the class EnrollmentCriteriaMapper method getFromUrl.

/**
 * Returns a ProgramInstanceQueryParams based on the given input.
 *
 * @param ou the set of organisation unit identifiers.
 * @param ouMode the OrganisationUnitSelectionMode.
 * @param lastUpdated the last updated for PI.
 * @param lastUpdatedDuration the last updated duration filter.
 * @param program the Program identifier.
 * @param programStatus the ProgramStatus in the given program.
 * @param programStartDate the start date for enrollment in the given
 *        Program.
 * @param programEndDate the end date for enrollment in the given Program.
 * @param trackedEntityType the TrackedEntityType uid.
 * @param trackedEntityInstance the TrackedEntityInstance uid.
 * @param followUp indicates follow up status in the given Program.
 * @param page the page number.
 * @param pageSize the page size.
 * @param totalPages indicates whether to include the total number of pages.
 * @param skipPaging whether to skip paging.
 * @param includeDeleted whether to include soft deleted ones
 * @return a ProgramInstanceQueryParams.
 */
@Transactional(readOnly = true)
public ProgramInstanceQueryParams getFromUrl(Set<String> ou, OrganisationUnitSelectionMode ouMode, Date lastUpdated, String lastUpdatedDuration, String program, ProgramStatus programStatus, Date programStartDate, Date programEndDate, String trackedEntityType, String trackedEntityInstance, Boolean followUp, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, boolean includeDeleted, List<OrderCriteria> orderCriteria) {
    ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
    Set<OrganisationUnit> possibleSearchOrgUnits = new HashSet<>();
    User user = currentUserService.getCurrentUser();
    if (user != null) {
        possibleSearchOrgUnits = user.getTeiSearchOrganisationUnitsWithFallback();
    }
    if (ou != null) {
        for (String orgUnit : ou) {
            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnit);
            if (organisationUnit == null) {
                throw new IllegalQueryException("Organisation unit does not exist: " + orgUnit);
            }
            if (!organisationUnitService.isInUserHierarchy(organisationUnit.getUid(), possibleSearchOrgUnits)) {
                throw new IllegalQueryException("Organisation unit is not part of the search scope: " + orgUnit);
            }
            params.getOrganisationUnits().add(organisationUnit);
        }
    }
    Program pr = program != null ? programService.getProgram(program) : null;
    if (program != null && pr == null) {
        throw new IllegalQueryException("Program does not exist: " + program);
    }
    TrackedEntityType te = trackedEntityType != null ? trackedEntityTypeService.getTrackedEntityType(trackedEntityType) : null;
    if (trackedEntityType != null && te == null) {
        throw new IllegalQueryException("Tracked entity does not exist: " + program);
    }
    TrackedEntityInstance tei = trackedEntityInstance != null ? trackedEntityInstanceService.getTrackedEntityInstance(trackedEntityInstance) : null;
    if (trackedEntityInstance != null && tei == null) {
        throw new IllegalQueryException("Tracked entity instance does not exist: " + program);
    }
    params.setProgram(pr);
    params.setProgramStatus(programStatus);
    params.setFollowUp(followUp);
    params.setLastUpdated(lastUpdated);
    params.setLastUpdatedDuration(lastUpdatedDuration);
    params.setProgramStartDate(programStartDate);
    params.setProgramEndDate(programEndDate);
    params.setTrackedEntityType(te);
    params.setTrackedEntityInstanceUid(Optional.ofNullable(tei).map(BaseIdentifiableObject::getUid).orElse(null));
    params.setOrganisationUnitMode(ouMode);
    params.setPage(page);
    params.setPageSize(pageSize);
    params.setTotalPages(totalPages);
    params.setSkipPaging(skipPaging);
    params.setIncludeDeleted(includeDeleted);
    params.setUser(user);
    params.setOrder(toOrderParams(orderCriteria));
    return params;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

Example 42 with IllegalQueryException

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

the class RequestToSearchParamsMapper method getQueryItem.

private QueryItem getQueryItem(String item) {
    String[] split = item.split(DimensionalObject.DIMENSION_NAME_SEP);
    if (split == null || split.length % 2 != 1) {
        throw new IllegalQueryException("Query item or filter is invalid: " + item);
    }
    QueryItem queryItem = getItem(split[0]);
    if (split.length > 1) {
        for (int i = 1; i < split.length; i += 2) {
            QueryOperator operator = QueryOperator.fromString(split[i]);
            queryItem.getFilters().add(new QueryFilter(operator, split[i + 1]));
        }
    }
    return queryItem;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) QueryFilter(org.hisp.dhis.common.QueryFilter) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) QueryOperator(org.hisp.dhis.common.QueryOperator)

Example 43 with IllegalQueryException

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

the class DefaultFollowupAnalysisService method validationError.

private IllegalQueryException validationError(ErrorCode error, Object... args) {
    ErrorMessage message = new ErrorMessage(error, args);
    log.warn(String.format("Followup analysis request validation failed, code: '%s', message: '%s'", error, message.getMessage()));
    return new IllegalQueryException(message);
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 44 with IllegalQueryException

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

the class MinMaxOutlierDetectionManager method getOutlierValues.

/**
 * Returns a list of outlier data values based on min-max values for the
 * given request.
 *
 * @param request the {@link OutlierDetectionRequest}.
 * @return a list of {@link OutlierValue}.
 */
public List<OutlierValue> getOutlierValues(OutlierDetectionRequest request) {
    final String ouPathClause = getOrgUnitPathClause(request.getOrgUnits());
    // @formatter:off
    final String sql = "select de.uid as de_uid, ou.uid as ou_uid, coc.uid as coc_uid, aoc.uid as aoc_uid, " + "de.name as de_name, ou.name as ou_name, coc.name as coc_name, aoc.name as aoc_name, " + "pe.startdate as pe_start_date, pt.name as pt_name, " + "dv.value::double precision as value, dv.followup as follow_up, " + "least(abs(dv.value::double precision - mm.minimumvalue), " + "abs(dv.value::double precision - mm.maximumvalue)) as bound_abs_dev, " + "mm.minimumvalue as lower_bound, " + "mm.maximumvalue as upper_bound " + "from datavalue dv " + "inner join dataelement de on dv.dataelementid = de.dataelementid " + "inner join categoryoptioncombo coc on dv.categoryoptioncomboid = coc.categoryoptioncomboid " + "inner join categoryoptioncombo aoc on dv.attributeoptioncomboid = aoc.categoryoptioncomboid " + "inner join period pe on dv.periodid = pe.periodid " + "inner join periodtype pt on pe.periodtypeid = pt.periodtypeid " + "inner join organisationunit ou on dv.sourceid = ou.organisationunitid " + // Min-max value join
    "inner join minmaxdataelement mm on (dv.dataelementid = mm.dataelementid " + "and dv.sourceid = mm.sourceid and dv.categoryoptioncomboid = mm.categoryoptioncomboid) " + "where dv.dataelementid in (:data_element_ids) " + "and pe.startdate >= :start_date " + "and pe.enddate <= :end_date " + "and " + ouPathClause + " " + "and dv.deleted is false " + // Filter for values outside the min-max range
    "and (dv.value::double precision < mm.minimumvalue or dv.value::double precision > mm.maximumvalue) " + // Order and limit
    "order by bound_abs_dev desc " + "limit :max_results;";
    // @formatter:on
    final SqlParameterSource params = new MapSqlParameterSource().addValue("data_element_ids", request.getDataElementIds()).addValue("start_date", request.getStartDate()).addValue("end_date", request.getEndDate()).addValue("max_results", request.getMaxResults());
    final Calendar calendar = PeriodType.getCalendar();
    try {
        return jdbcTemplate.query(sql, params, getRowMapper(calendar));
    } catch (DataIntegrityViolationException ex) {
        // Casting non-numeric data to double, catching exception is faster
        // than filtering
        log.error(ErrorCode.E2208.getMessage(), ex);
        throw new IllegalQueryException(ErrorCode.E2208);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Calendar(org.hisp.dhis.calendar.Calendar) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 45 with IllegalQueryException

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

the class UserLookupController method lookUpFeedbackRecipients.

@GetMapping(value = "/feedbackRecipients")
public UserLookups lookUpFeedbackRecipients(@RequestParam String query) {
    UserGroup feedbackRecipients = config.getConfiguration().getFeedbackRecipients();
    if (feedbackRecipients == null) {
        throw new IllegalQueryException(new ErrorMessage(ErrorCode.E6200));
    }
    UserQueryParams params = new UserQueryParams().setQuery(query).setUserGroups(Sets.newHashSet(feedbackRecipients)).setCanSeeOwnUserAuthorityGroups(true).setMax(25);
    List<UserLookup> users = userService.getUsers(params).stream().map(UserLookup::fromUser).collect(Collectors.toList());
    return new UserLookups(users);
}
Also used : IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) UserLookup(org.hisp.dhis.webapi.webdomain.user.UserLookup) UserLookups(org.hisp.dhis.webapi.webdomain.user.UserLookups) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage) UserQueryParams(org.hisp.dhis.user.UserQueryParams) UserGroup(org.hisp.dhis.user.UserGroup) GetMapping(org.springframework.web.bind.annotation.GetMapping)

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