use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class TrackedEntityCriteriaMapper method map.
@Transactional(readOnly = true)
public TrackedEntityInstanceQueryParams map(TrackedEntityInstanceCriteria criteria) {
TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
final Date programEnrollmentStartDate = ObjectUtils.firstNonNull(criteria.getProgramEnrollmentStartDate(), criteria.getProgramStartDate());
final Date programEnrollmentEndDate = ObjectUtils.firstNonNull(criteria.getProgramEnrollmentEndDate(), criteria.getProgramEndDate());
Set<OrganisationUnit> possibleSearchOrgUnits = new HashSet<>();
User user = currentUserService.getCurrentUser();
if (user != null) {
possibleSearchOrgUnits = user.getTeiSearchOrganisationUnitsWithFallback();
}
QueryFilter queryFilter = getQueryFilter(criteria.getQuery());
Map<String, TrackedEntityAttribute> attributes = attributeService.getAllTrackedEntityAttributes().stream().collect(Collectors.toMap(TrackedEntityAttribute::getUid, att -> att));
if (criteria.getAttribute() != null) {
for (String attr : criteria.getAttribute()) {
QueryItem it = getQueryItem(attr, attributes);
params.getAttributes().add(it);
}
}
if (criteria.getFilter() != null) {
for (String filt : criteria.getFilter()) {
QueryItem it = getQueryItem(filt, attributes);
params.getFilters().add(it);
}
}
for (String orgUnit : criteria.getOrgUnits()) {
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);
}
validateAssignedUser(criteria);
if (criteria.getOuMode() == OrganisationUnitSelectionMode.CAPTURE && user != null) {
params.getOrganisationUnits().addAll(user.getOrganisationUnits());
}
Program program = validateProgram(criteria);
List<OrderParam> orderParams = toOrderParams(criteria.getOrder());
validateOrderParams(program, orderParams, attributes);
params.setQuery(queryFilter).setProgram(program).setProgramStage(validateProgramStage(criteria, program)).setProgramStatus(criteria.getProgramStatus()).setFollowUp(criteria.getFollowUp()).setLastUpdatedStartDate(criteria.getLastUpdatedStartDate()).setLastUpdatedEndDate(criteria.getLastUpdatedEndDate()).setLastUpdatedDuration(criteria.getLastUpdatedDuration()).setProgramEnrollmentStartDate(programEnrollmentStartDate).setProgramEnrollmentEndDate(programEnrollmentEndDate).setProgramIncidentStartDate(criteria.getProgramIncidentStartDate()).setProgramIncidentEndDate(criteria.getProgramIncidentEndDate()).setTrackedEntityType(validateTrackedEntityType(criteria)).setOrganisationUnitMode(criteria.getOuMode()).setEventStatus(criteria.getEventStatus()).setEventStartDate(criteria.getEventStartDate()).setEventEndDate(criteria.getEventEndDate()).setAssignedUserSelectionMode(criteria.getAssignedUserMode()).setAssignedUsers(criteria.getAssignedUsers()).setTrackedEntityInstanceUids(criteria.getTrackedEntityInstances()).setSkipMeta(criteria.isSkipMeta()).setPage(criteria.getPage()).setPageSize(criteria.getPageSize()).setTotalPages(criteria.isTotalPages()).setSkipPaging(criteria.isSkipPaging()).setIncludeDeleted(criteria.isIncludeDeleted()).setIncludeAllAttributes(criteria.isIncludeAllAttributes()).setUser(user).setOrders(orderParams);
return params;
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class TrackedEntityCriteriaMapper method validateProgramStage.
private ProgramStage validateProgramStage(TrackedEntityInstanceCriteria criteria, Program program) {
final String programStage = criteria.getProgramStage();
ProgramStage ps = programStage != null ? getProgramStageFromProgram(program, programStage) : null;
if (programStage != null && ps == null) {
throw new IllegalQueryException("Program does not contain the specified programStage: " + programStage);
}
return ps;
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class TrackedEntityCriteriaMapper 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, Map<String, TrackedEntityAttribute> attributes) {
String[] split = item.split(DimensionalObject.DIMENSION_NAME_SEP);
if (split.length % 2 != 1) {
throw new IllegalQueryException("Query item or filter is invalid: " + item);
}
QueryItem queryItem = getItem(split[0], attributes);
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;
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class TrackedEntityCriteriaMapper method validateProgram.
private Program validateProgram(TrackedEntityInstanceCriteria criteria) {
Function<String, Program> getProgram = uid -> {
if (isNotEmpty(uid)) {
return programService.getProgram(uid);
}
return null;
};
final Program program = getProgram.apply(criteria.getProgram());
if (isNotEmpty(criteria.getProgram()) && program == null) {
throw new IllegalQueryException("Program does not exist: " + criteria.getProgram());
}
return program;
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class TrackedEntityCriteriaMapper method validateTrackedEntityType.
private TrackedEntityType validateTrackedEntityType(TrackedEntityInstanceCriteria criteria) {
Function<String, TrackedEntityType> getTeiType = uid -> {
if (isNotEmpty(uid)) {
return trackedEntityTypeService.getTrackedEntityType(uid);
}
return null;
};
final TrackedEntityType trackedEntityType = getTeiType.apply(criteria.getTrackedEntityType());
if (isNotEmpty(criteria.getTrackedEntityType()) && trackedEntityType == null) {
throw new IllegalQueryException("Tracked entity type does not exist: " + criteria.getTrackedEntityType());
}
return trackedEntityType;
}
Aggregations