use of org.hisp.dhis.query.Disjunction in project dhis2-core by dhis2.
the class InterpretationController method getEntityList.
@Override
@SuppressWarnings("unchecked")
protected List<Interpretation> getEntityList(WebMetadata metadata, WebOptions options, List<String> filters, List<Order> orders) throws QueryParserException {
// If custom filter (mentions:in:[username]) in filters -> Remove from
// filters
List<String> mentionsFromCustomFilters = MentionUtils.removeCustomFilters(filters);
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, orders, getPaginationData(options), options.getRootJunction());
query.setDefaultOrder();
query.setDefaults(Defaults.valueOf(options.get("defaults", DEFAULTS)));
// disjunction including interpretation mentions and comments mentions
for (Disjunction disjunction : (Collection<Disjunction>) getDisjunctionsFromCustomMentions(mentionsFromCustomFilters, query.getSchema())) {
query.add(disjunction);
}
List<Interpretation> entityList;
if (options.getOptions().containsKey("query")) {
entityList = Lists.newArrayList(manager.filter(getEntityClass(), options.getOptions().get("query")));
} else {
entityList = (List<Interpretation>) queryService.query(query);
}
return entityList;
}
use of org.hisp.dhis.query.Disjunction in project dhis2-core by dhis2.
the class DefaultQueryPlanner method handleJunction.
private Junction handleJunction(Query query, Junction queryJunction, boolean persistedOnly) {
Iterator<org.hisp.dhis.query.Criterion> iterator = queryJunction.getCriterions().iterator();
Junction criteriaJunction = Disjunction.class.isInstance(queryJunction) ? new Disjunction(query.getSchema()) : new Conjunction(query.getSchema());
while (iterator.hasNext()) {
org.hisp.dhis.query.Criterion criterion = iterator.next();
if (Junction.class.isInstance(criterion)) {
Junction junction = handleJunction(query, (Junction) criterion, persistedOnly);
if (!junction.getCriterions().isEmpty()) {
criteriaJunction.getAliases().addAll(junction.getAliases());
criteriaJunction.add(junction);
}
if (((Junction) criterion).getCriterions().isEmpty()) {
iterator.remove();
}
} else if (Restriction.class.isInstance(criterion)) {
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));
if (restriction.getQueryPath().isPersisted() && !restriction.getQueryPath().haveAlias(1)) {
criteriaJunction.getAliases().addAll(Arrays.asList(((Restriction) criterion).getQueryPath().getAlias()));
criteriaJunction.getCriterions().add(criterion);
iterator.remove();
} else if (persistedOnly) {
throw new RuntimeException("Path " + restriction.getQueryPath().getPath() + " is not fully persisted, unable to build persisted only query plan.");
}
}
}
return criteriaJunction;
}
use of org.hisp.dhis.query.Disjunction in project dhis2-core by dhis2.
the class MetadataExportServiceTest method testMetadataExportWithCustomQueries.
@Test
void testMetadataExportWithCustomQueries() {
DataElementGroup deg1 = createDataElementGroup('A');
DataElement de1 = createDataElement('A');
DataElement de2 = createDataElement('B');
DataElement de3 = createDataElement('C');
manager.save(de1);
manager.save(de2);
manager.save(de3);
User user = createUser('A');
manager.save(user);
deg1.addDataElement(de1);
deg1.addDataElement(de2);
deg1.addDataElement(de3);
deg1.setCreatedBy(user);
manager.save(deg1);
Query deQuery = Query.from(schemaService.getDynamicSchema(DataElement.class));
Disjunction disjunction = deQuery.disjunction();
disjunction.add(Restrictions.eq("id", de1.getUid()));
disjunction.add(Restrictions.eq("id", de2.getUid()));
deQuery.add(disjunction);
Query degQuery = Query.from(schemaService.getDynamicSchema(DataElementGroup.class));
degQuery.add(Restrictions.eq("id", "INVALID UID"));
MetadataExportParams params = new MetadataExportParams();
params.addQuery(deQuery);
params.addQuery(degQuery);
Map<Class<? extends IdentifiableObject>, List<? extends IdentifiableObject>> metadata = metadataExportService.getMetadata(params);
assertFalse(metadata.containsKey(User.class));
assertFalse(metadata.containsKey(DataElementGroup.class));
assertTrue(metadata.containsKey(DataElement.class));
assertEquals(2, metadata.get(DataElement.class).size());
}
use of org.hisp.dhis.query.Disjunction in project dhis2-core by dhis2.
the class InterpretationController method getDisjunctionsFromCustomMentions.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private Collection<Disjunction> getDisjunctionsFromCustomMentions(List<String> mentions, Schema schema) {
Collection<Disjunction> disjunctions = new ArrayList<Disjunction>();
for (String m : mentions) {
Disjunction disjunction = new Disjunction(schema);
String[] split = m.substring(1, m.length() - 1).split(",");
List<String> items = Lists.newArrayList(split);
disjunction.add(Restrictions.in("mentions.username", items));
disjunction.add(Restrictions.in("comments.mentions.username", items));
disjunctions.add(disjunction);
}
return disjunctions;
}
Aggregations