Search in sources :

Example 1 with Disjunction

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;
}
Also used : Disjunction(org.hisp.dhis.query.Disjunction) Query(org.hisp.dhis.query.Query) Collection(java.util.Collection) Interpretation(org.hisp.dhis.interpretation.Interpretation)

Example 2 with Disjunction

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;
}
Also used : Restriction(org.hisp.dhis.query.Restriction) Disjunction(org.hisp.dhis.query.Disjunction) Criterion(org.hisp.dhis.query.Criterion) Conjunction(org.hisp.dhis.query.Conjunction) Criterion(org.hisp.dhis.query.Criterion) Junction(org.hisp.dhis.query.Junction)

Example 3 with Disjunction

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());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Disjunction(org.hisp.dhis.query.Disjunction) User(org.hisp.dhis.user.User) Query(org.hisp.dhis.query.Query) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with Disjunction

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;
}
Also used : Disjunction(org.hisp.dhis.query.Disjunction) ArrayList(java.util.ArrayList)

Aggregations

Disjunction (org.hisp.dhis.query.Disjunction)4 Query (org.hisp.dhis.query.Query)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)1 Interpretation (org.hisp.dhis.interpretation.Interpretation)1 Conjunction (org.hisp.dhis.query.Conjunction)1 Criterion (org.hisp.dhis.query.Criterion)1 Junction (org.hisp.dhis.query.Junction)1 Restriction (org.hisp.dhis.query.Restriction)1 User (org.hisp.dhis.user.User)1 Test (org.junit.jupiter.api.Test)1