Search in sources :

Example 1 with QueryFilter

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

the class DefaultTrackedEntityInstanceService method getQueryItem.

/**
     * Creates a QueryItem from the given item string. Item is on format
     * {attribute-id}:{operator}:{filter-value}[:{operator}:{filter-value}].
     * Only the attribute-id is mandatory.
     */
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 (// Filters specified
    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 2 with QueryFilter

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

the class DefaultTrackedEntityInstanceService method getFromUrl.

@Override
public TrackedEntityInstanceQueryParams getFromUrl(String query, Set<String> attribute, Set<String> filter, Set<String> ou, OrganisationUnitSelectionMode ouMode, String program, ProgramStatus programStatus, Boolean followUp, Date lastUpdatedStartDate, Date lastUpdatedEndDate, Date programEnrollmentStartDate, Date programEnrollmentEndDate, Date programIncidentStartDate, Date programIncidentEndDate, String trackedEntity, EventStatus eventStatus, Date eventStartDate, Date eventEndDate, boolean skipMeta, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, boolean includeDeleted, List<String> orders) {
    TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
    QueryFilter queryFilter = getQueryFilter(query);
    if (attribute != null) {
        for (String attr : attribute) {
            QueryItem it = getQueryItem(attr);
            params.getAttributes().add(it);
        }
    }
    if (filter != null) {
        for (String filt : filter) {
            QueryItem it = getQueryItem(filt);
            params.getFilters().add(it);
        }
    }
    if (ou != null) {
        for (String orgUnit : ou) {
            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnit);
            if (organisationUnit == null) {
                throw new IllegalQueryException("Organisation unit does not exist: " + 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);
    }
    TrackedEntity te = trackedEntity != null ? trackedEntityService.getTrackedEntity(trackedEntity) : null;
    if (trackedEntity != null && te == null) {
        throw new IllegalQueryException("Tracked entity does not exist: " + program);
    }
    params.setQuery(queryFilter).setProgram(pr).setProgramStatus(programStatus).setFollowUp(followUp).setLastUpdatedStartDate(lastUpdatedStartDate).setLastUpdatedEndDate(lastUpdatedEndDate).setProgramEnrollmentStartDate(programEnrollmentStartDate).setProgramEnrollmentEndDate(programEnrollmentEndDate).setProgramIncidentStartDate(programIncidentStartDate).setProgramIncidentEndDate(programIncidentEndDate).setTrackedEntity(te).setOrganisationUnitMode(ouMode).setEventStatus(eventStatus).setEventStartDate(eventStartDate).setEventEndDate(eventEndDate).setSkipMeta(skipMeta).setPage(page).setPageSize(pageSize).setTotalPages(totalPages).setSkipPaging(skipPaging).setIncludeDeleted(includeDeleted).setOrders(orders);
    return params;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryFilter(org.hisp.dhis.common.QueryFilter) Program(org.hisp.dhis.program.Program) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)

Example 3 with QueryFilter

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

the class DefaultTrackedEntityInstanceService method getQueryFilter.

/**
     * Creates a QueryFilter from the given query string. Query is on format
     * {operator}:{filter-value}. Only the filter-value is mandatory. The EQ
     * QueryOperator is used as operator if not specified.
     */
private QueryFilter getQueryFilter(String query) {
    if (query == null || query.isEmpty()) {
        return null;
    }
    if (!query.contains(DimensionalObject.DIMENSION_NAME_SEP)) {
        return new QueryFilter(QueryOperator.EQ, query);
    } else {
        String[] split = query.split(DimensionalObject.DIMENSION_NAME_SEP);
        if (split == null || split.length != 2) {
            throw new IllegalQueryException("Query has invalid format: " + query);
        }
        QueryOperator op = QueryOperator.fromString(split[0]);
        return new QueryFilter(op, split[1]);
    }
}
Also used : QueryFilter(org.hisp.dhis.common.QueryFilter) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) QueryOperator(org.hisp.dhis.common.QueryOperator)

Example 4 with QueryFilter

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

the class HibernateTrackedEntityInstanceStore method buildTrackedEntityInstanceHql.

private String buildTrackedEntityInstanceHql(TrackedEntityInstanceQueryParams params) {
    String hql = "select distinct tei from TrackedEntityInstance tei left join tei.trackedEntityAttributeValues";
    SqlHelper hlp = new SqlHelper(true);
    if (params.hasTrackedEntity()) {
        hql += hlp.whereAnd() + "tei.trackedEntity.uid='" + params.getTrackedEntity().getUid() + "'";
    }
    if (params.hasLastUpdatedStartDate()) {
        hql += hlp.whereAnd() + "tei.lastUpdated >= '" + getMediumDateString(params.getLastUpdatedStartDate()) + "'";
    }
    if (params.hasLastUpdatedEndDate()) {
        hql += hlp.whereAnd() + "tei.lastUpdated < '" + getMediumDateString(params.getLastUpdatedEndDate()) + "'";
    }
    if (params.hasOrganisationUnits()) {
        params.handleOrganisationUnits();
        if (params.isOrganisationUnitMode(OrganisationUnitSelectionMode.DESCENDANTS)) {
            String ouClause = "(";
            SqlHelper orHlp = new SqlHelper(true);
            for (OrganisationUnit organisationUnit : params.getOrganisationUnits()) {
                ouClause += orHlp.or() + "tei.organisationUnit.path LIKE '" + organisationUnit.getPath() + "%'";
            }
            ouClause += ")";
            hql += hlp.whereAnd() + ouClause;
        } else {
            hql += hlp.whereAnd() + "tei.organisationUnit.uid in (" + getQuotedCommaDelimitedString(getUids(params.getOrganisationUnits())) + ")";
        }
    }
    if (params.hasQuery()) {
        QueryFilter queryFilter = params.getQuery();
        String filter = queryFilter.getSqlFilter(queryFilter.getFilter());
        hql += hlp.whereAnd() + " exists (from TrackedEntityAttributeValue teav where teav.entityInstance=tei";
        hql += " and teav.plainValue " + queryFilter.getSqlOperator() + filter + ")";
    }
    if (params.hasFilters()) {
        for (QueryItem queryItem : params.getFilters()) {
            for (QueryFilter queryFilter : queryItem.getFilters()) {
                String filter = queryFilter.getSqlFilter(StringUtils.lowerCase(queryFilter.getFilter()));
                hql += hlp.whereAnd() + " exists (from TrackedEntityAttributeValue teav where teav.entityInstance=tei";
                hql += " and teav.attribute.uid='" + queryItem.getItemId() + "'";
                if (queryItem.isNumeric()) {
                    hql += " and teav.plainValue " + queryFilter.getSqlOperator() + filter + ")";
                } else {
                    hql += " and lower(teav.plainValue) " + queryFilter.getSqlOperator() + filter + ")";
                }
            }
        }
    }
    if (params.hasProgram()) {
        hql += hlp.whereAnd() + "exists (from ProgramInstance pi where pi.entityInstance=tei";
        hql += " and pi.program.uid = '" + params.getProgram().getUid() + "'";
        if (params.hasProgramStatus()) {
            hql += hlp.whereAnd() + "pi.status = " + params.getProgramStatus();
        }
        if (params.hasFollowUp()) {
            hql += hlp.whereAnd() + "pi.followup = " + params.getFollowUp();
        }
        if (params.hasProgramEnrollmentStartDate()) {
            hql += hlp.whereAnd() + "pi.enrollmentDate >= '" + getMediumDateString(params.getProgramEnrollmentStartDate()) + "'";
        }
        if (params.hasProgramEnrollmentEndDate()) {
            hql += hlp.whereAnd() + "pi.enrollmentDate < '" + getMediumDateString(params.getProgramEnrollmentEndDate()) + "'";
        }
        if (params.hasProgramIncidentStartDate()) {
            hql += hlp.whereAnd() + "pi.incidentDate >= '" + getMediumDateString(params.getProgramIncidentStartDate()) + "'";
        }
        if (params.hasProgramIncidentEndDate()) {
            hql += hlp.whereAnd() + "pi.incidentDate < '" + getMediumDateString(params.getProgramIncidentEndDate()) + "'";
        }
        hql += " and pi.deleted is false";
        hql += ")";
    }
    hql += hlp.whereAnd() + " tei.deleted is false ";
    return hql;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) QueryItem(org.hisp.dhis.common.QueryItem) QueryFilter(org.hisp.dhis.common.QueryFilter) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString)

Example 5 with QueryFilter

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

the class ProgramStageDataEntrySMSListener method getParams.

private TrackedEntityInstanceQueryParams getParams(TrackedEntityAttribute attribute, IncomingSms sms, Program program, Set<OrganisationUnit> ous) {
    TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
    QueryFilter queryFilter = new QueryFilter();
    queryFilter.setOperator(QueryOperator.LIKE);
    queryFilter.setFilter(sms.getOriginator());
    QueryItem item = new QueryItem(attribute);
    item.getFilters().add(queryFilter);
    item.setValueType(ValueType.PHONE_NUMBER);
    params.setProgram(program);
    params.setOrganisationUnits(ous);
    params.getFilters().add(item);
    return params;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) QueryFilter(org.hisp.dhis.common.QueryFilter) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)

Aggregations

QueryFilter (org.hisp.dhis.common.QueryFilter)27 QueryItem (org.hisp.dhis.common.QueryItem)25 QueryOperator (org.hisp.dhis.common.QueryOperator)9 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)8 SqlHelper (org.hisp.dhis.commons.util.SqlHelper)6 Test (org.junit.jupiter.api.Test)6 TextUtils.getQuotedCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)5 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)4 DimensionalObject (org.hisp.dhis.common.DimensionalObject)4 DateUtils.getMediumDateString (org.hisp.dhis.util.DateUtils.getMediumDateString)4 HashSet (java.util.HashSet)3 List (java.util.List)3 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)3 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)3 BaseDimensionalItemObject (org.hisp.dhis.common.BaseDimensionalItemObject)3 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)3 Date (java.util.Date)2 Map (java.util.Map)2