use of org.hisp.dhis.common.QueryItem in project dhis2-core by dhis2.
the class DefaultTrackedEntityAttributeService method validateScope.
@Override
public String validateScope(TrackedEntityAttribute trackedEntityAttribute, String value, TrackedEntityInstance trackedEntityInstance, OrganisationUnit organisationUnit, Program program) {
Assert.notNull(trackedEntityAttribute, "tracked entity attribute is required.");
if (!trackedEntityAttribute.isUnique() || value == null) {
return null;
}
TrackedEntityInstanceQueryParams params = new TrackedEntityInstanceQueryParams();
params.addAttribute(new QueryItem(trackedEntityAttribute, QueryOperator.EQ, value, trackedEntityAttribute.getValueType(), trackedEntityAttribute.getAggregationType(), trackedEntityAttribute.getOptionSet()));
if (trackedEntityAttribute.getOrgunitScope() && trackedEntityAttribute.getProgramScope()) {
Assert.notNull(program, "program is required for program scope");
Assert.notNull(organisationUnit, "organisationUnit is required for org unit scope");
if (!program.getOrganisationUnits().contains(organisationUnit)) {
return "Organisation unit is not assigned to program " + program.getUid();
}
params.setProgram(program);
params.addOrganisationUnit(organisationUnit);
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.SELECTED);
} else if (trackedEntityAttribute.getOrgunitScope()) {
Assert.notNull(organisationUnit, "organisation unit is required for org unit scope");
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.SELECTED);
params.addOrganisationUnit(organisationUnit);
} else if (trackedEntityAttribute.getProgramScope()) {
Assert.notNull(program, "program is required for program scope");
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
params.setProgram(program);
} else {
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
}
// TODO re-factor to avoid circular dependency
TrackedEntityInstanceService trackedEntityInstanceService = (TrackedEntityInstanceService) applicationContext.getBean(TrackedEntityInstanceService.class);
Grid instances = trackedEntityInstanceService.getTrackedEntityInstancesGrid(params);
if (!(instances.getHeight() == 0)) {
if (trackedEntityInstance == null || (instances.getHeight() == 1 && !instances.getRow(0).contains(trackedEntityInstance.getUid()))) {
return "Non-unique attribute value '" + value + "' for attribute " + trackedEntityAttribute.getUid();
}
}
return null;
}
use of org.hisp.dhis.common.QueryItem 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;
}
use of org.hisp.dhis.common.QueryItem 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;
}
use of org.hisp.dhis.common.QueryItem in project dhis2-core by dhis2.
the class DefaultTrackedEntityInstanceService method getTrackedEntityInstancesGrid.
// TODO lower index on attribute value?
@Override
public Grid getTrackedEntityInstancesGrid(TrackedEntityInstanceQueryParams params) {
decideAccess(params);
validate(params);
handleAttributes(params);
params.setUser(currentUserService.getCurrentUser());
// ---------------------------------------------------------------------
// Conform parameters
// ---------------------------------------------------------------------
params.conform();
// ---------------------------------------------------------------------
// Grid headers
// ---------------------------------------------------------------------
Grid grid = new ListGrid();
grid.addHeader(new GridHeader(TRACKED_ENTITY_INSTANCE_ID, "Instance"));
grid.addHeader(new GridHeader(CREATED_ID, "Created"));
grid.addHeader(new GridHeader(LAST_UPDATED_ID, "Last updated"));
grid.addHeader(new GridHeader(ORG_UNIT_ID, "Organisation unit"));
grid.addHeader(new GridHeader(ORG_UNIT_NAME, "Organisation unit name"));
grid.addHeader(new GridHeader(TRACKED_ENTITY_ID, "Tracked entity"));
grid.addHeader(new GridHeader(INACTIVE_ID, "Inactive"));
if (params.isIncludeDeleted()) {
grid.addHeader(new GridHeader(DELETED, "Deleted", ValueType.BOOLEAN, "boolean", false, false));
}
for (QueryItem item : params.getAttributes()) {
grid.addHeader(new GridHeader(item.getItem().getUid(), item.getItem().getName()));
}
List<Map<String, String>> entities = trackedEntityInstanceStore.getTrackedEntityInstancesGrid(params);
// ---------------------------------------------------------------------
// Grid rows
// ---------------------------------------------------------------------
Set<String> tes = new HashSet<>();
for (Map<String, String> entity : entities) {
grid.addRow();
grid.addValue(entity.get(TRACKED_ENTITY_INSTANCE_ID));
grid.addValue(entity.get(CREATED_ID));
grid.addValue(entity.get(LAST_UPDATED_ID));
grid.addValue(entity.get(ORG_UNIT_ID));
grid.addValue(entity.get(ORG_UNIT_NAME));
grid.addValue(entity.get(TRACKED_ENTITY_ID));
grid.addValue(entity.get(INACTIVE_ID));
if (params.isIncludeDeleted()) {
grid.addValue(entity.get(DELETED));
}
tes.add(entity.get(TRACKED_ENTITY_ID));
for (QueryItem item : params.getAttributes()) {
grid.addValue(entity.get(item.getItemId()));
}
}
Map<String, Object> metaData = new HashMap<>();
if (params.isPaging()) {
int count = 0;
if (params.isTotalPages()) {
count = trackedEntityInstanceStore.getTrackedEntityInstanceCount(params);
}
Pager pager = new Pager(params.getPageWithDefault(), count, params.getPageSizeWithDefault());
metaData.put(PAGER_META_KEY, pager);
}
if (!params.isSkipMeta()) {
Map<String, String> names = new HashMap<>();
for (String te : tes) {
TrackedEntity entity = trackedEntityService.getTrackedEntity(te);
names.put(te, entity != null ? entity.getDisplayName() : null);
}
metaData.put(META_DATA_NAMES_KEY, names);
}
grid.setMetaData(metaData);
return grid;
}
use of org.hisp.dhis.common.QueryItem 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;
}
Aggregations