Search in sources :

Example 61 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class PostgreSqlQueryGeneratorTest method getSqlSortOnUnselectedMref.

@Test
public void getSqlSortOnUnselectedMref() {
    Package package_ = when(mock(Package.class).getId()).thenReturn("org_molgenis").getMock();
    Attribute ref1IdAttr = when(mock(Attribute.class).getName()).thenReturn("ref1Id").getMock();
    when(ref1IdAttr.getIdentifier()).thenReturn("ref1IdAttrId");
    EntityType ref1Meta = when(mock(EntityType.class).getId()).thenReturn("Ref1").getMock();
    when(ref1Meta.getId()).thenReturn("ref1Id");
    when(ref1Meta.getIdAttribute()).thenReturn(ref1IdAttr);
    Attribute ref2IdAttr = when(mock(Attribute.class).getName()).thenReturn("ref2Id").getMock();
    when(ref2IdAttr.getIdentifier()).thenReturn("ref2IdAttrId");
    EntityType ref2Meta = when(mock(EntityType.class).getId()).thenReturn("Ref2").getMock();
    when(ref2Meta.getId()).thenReturn("ref2Id");
    when(ref2Meta.getIdAttribute()).thenReturn(ref2IdAttr);
    Attribute masterIdAttr = when(mock(Attribute.class).getName()).thenReturn("masterId").getMock();
    when(masterIdAttr.getIdentifier()).thenReturn("masterIdAttrId");
    when(masterIdAttr.getDataType()).thenReturn(STRING);
    Attribute mref1Attr = when(mock(Attribute.class).getName()).thenReturn("mref1").getMock();
    when(mref1Attr.getIdentifier()).thenReturn("mref1AttrId");
    when(mref1Attr.getDataType()).thenReturn(MREF);
    when(mref1Attr.getRefEntity()).thenReturn(ref1Meta);
    EntityType entityType = when(mock(EntityType.class).getId()).thenReturn("org_molgenis_MasterEntity").getMock();
    when(entityType.getId()).thenReturn("entityTypeId");
    when(entityType.getPackage()).thenReturn(package_);
    when(entityType.getIdAttribute()).thenReturn(masterIdAttr);
    when(entityType.getAttribute("masterId")).thenReturn(masterIdAttr);
    when(entityType.getAttribute("mref1")).thenReturn(mref1Attr);
    when(entityType.getAtomicAttributes()).thenReturn(asList(masterIdAttr, mref1Attr));
    Fetch fetch = new Fetch().field("masterId");
    Sort sort = new Sort("mref1");
    QueryImpl<Entity> q = new QueryImpl<>();
    q.setFetch(fetch);
    q.setSort(sort);
    List<Object> parameters = Lists.newArrayList();
    String sqlSelect = PostgreSqlQueryGenerator.getSqlSelect(entityType, q, parameters, true);
    assertEquals(sqlSelect, "SELECT this.\"masterId\", (SELECT array_agg(DISTINCT ARRAY[\"mref1\".\"order\"::TEXT,\"mref1\".\"mref1\"::TEXT]) FROM \"entityTypeId#c34894ba_mref1\" AS \"mref1\" WHERE this.\"masterId\" = \"mref1\".\"masterId\") AS \"mref1\" FROM \"entityTypeId#c34894ba\" AS this ORDER BY \"mref1\" ASC, \"masterId\" ASC");
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) Attribute(org.molgenis.data.meta.model.Attribute) Package(org.molgenis.data.meta.model.Package) Test(org.testng.annotations.Test)

Example 62 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class MappingServiceController method validateAttributeMapping.

@PostMapping("/validateAttrMapping")
@ResponseBody
public AttributeMappingValidationReport validateAttributeMapping(@Valid @RequestBody MappingServiceRequest mappingServiceRequest) {
    String targetEntityName = mappingServiceRequest.getTargetEntityName();
    EntityType targetEntityType = dataService.getEntityType(targetEntityName);
    String targetAttributeName = mappingServiceRequest.getTargetAttributeName();
    Attribute targetAttr = targetEntityType.getAttribute(targetAttributeName);
    if (targetAttr == null) {
        throw new UnknownAttributeException(targetEntityType, targetAttributeName);
    }
    String algorithm = mappingServiceRequest.getAlgorithm();
    Long offset = mappingServiceRequest.getOffset();
    Long num = mappingServiceRequest.getNum();
    Query<Entity> query = new QueryImpl<>().offset(offset.intValue()).pageSize(num.intValue());
    String sourceEntityName = mappingServiceRequest.getSourceEntityName();
    Iterable<Entity> sourceEntities = () -> dataService.findAll(sourceEntityName, query).iterator();
    long total = dataService.count(sourceEntityName, new QueryImpl<>());
    long nrSuccess = 0, nrErrors = 0;
    Map<String, String> errorMessages = new LinkedHashMap<>();
    for (AlgorithmEvaluation evaluation : algorithmService.applyAlgorithm(targetAttr, algorithm, sourceEntities)) {
        if (evaluation.hasError()) {
            errorMessages.put(evaluation.getEntity().getIdValue().toString(), evaluation.getErrorMessage());
            ++nrErrors;
        } else {
            ++nrSuccess;
        }
    }
    return new AttributeMappingValidationReport(total, nrSuccess, nrErrors, errorMessages);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) EntityType(org.molgenis.data.meta.model.EntityType) AlgorithmEvaluation(org.molgenis.semanticmapper.service.impl.AlgorithmEvaluation) AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) QueryImpl(org.molgenis.data.support.QueryImpl)

Example 63 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class MappingServiceController method advancedMappingEditor.

/**
 * Returns a view that allows the user to edit mappings involving xrefs / categoricals / strings
 */
@PostMapping("/advancedmappingeditor")
public String advancedMappingEditor(@RequestParam() String mappingProjectId, @RequestParam() String target, @RequestParam() String source, @RequestParam() String targetAttribute, @RequestParam() String sourceAttribute, @RequestParam String algorithm, Model model) {
    MappingProject project = mappingService.getMappingProject(mappingProjectId);
    MappingTarget mappingTarget = project.getMappingTarget(target);
    EntityMapping entityMapping = mappingTarget.getMappingForSource(source);
    AttributeMapping attributeMapping = entityMapping.getAttributeMapping(targetAttribute);
    model.addAttribute("mappingProject", project);
    model.addAttribute("entityMapping", entityMapping);
    model.addAttribute("attributeMapping", attributeMapping);
    // set variables for the target column in the mapping editor
    Attribute targetAttr = dataService.getEntityType(target).getAttribute(targetAttribute);
    Stream<Entity> targetAttributeEntities;
    String targetAttributeIdAttribute = null;
    String targetAttributeLabelAttribute = null;
    if (EntityTypeUtils.isReferenceType(targetAttr)) {
        targetAttributeEntities = dataService.findAll(dataService.getEntityType(target).getAttribute(targetAttribute).getRefEntity().getId());
        targetAttributeIdAttribute = dataService.getEntityType(target).getAttribute(targetAttribute).getRefEntity().getIdAttribute().getName();
        targetAttributeLabelAttribute = dataService.getEntityType(target).getAttribute(targetAttribute).getRefEntity().getLabelAttribute().getName();
    } else {
        targetAttributeEntities = dataService.findAll(dataService.getEntityType(target).getId());
        targetAttributeIdAttribute = dataService.getEntityType(target).getIdAttribute().getName();
        targetAttributeLabelAttribute = dataService.getEntityType(target).getLabelAttribute().getName();
    }
    model.addAttribute("targetAttributeEntities", (Iterable<Entity>) targetAttributeEntities::iterator);
    model.addAttribute("targetAttributeIdAttribute", targetAttributeIdAttribute);
    model.addAttribute("targetAttributeLabelAttribute", targetAttributeLabelAttribute);
    // set variables for the source column in the mapping editor
    Attribute sourceAttr = dataService.getEntityType(source).getAttribute(sourceAttribute);
    Stream<Entity> sourceAttributeEntities;
    String sourceAttributeIdAttribute = null;
    String sourceAttributeLabelAttribute = null;
    if (EntityTypeUtils.isReferenceType(sourceAttr)) {
        sourceAttributeEntities = dataService.findAll(dataService.getEntityType(source).getAttribute(sourceAttribute).getRefEntity().getId());
        sourceAttributeIdAttribute = dataService.getEntityType(source).getAttribute(sourceAttribute).getRefEntity().getIdAttribute().getName();
        sourceAttributeLabelAttribute = dataService.getEntityType(source).getAttribute(sourceAttribute).getRefEntity().getLabelAttribute().getName();
    } else {
        sourceAttributeEntities = dataService.findAll(dataService.getEntityType(source).getId());
        sourceAttributeIdAttribute = dataService.getEntityType(source).getIdAttribute().getName();
        sourceAttributeLabelAttribute = dataService.getEntityType(source).getLabelAttribute().getName();
    }
    List<Entity> sourceAttributeEntityList = sourceAttributeEntities.collect(toList());
    model.addAttribute("sourceAttributeEntities", sourceAttributeEntityList);
    model.addAttribute("numberOfSourceAttributes", sourceAttributeEntityList.size());
    model.addAttribute("sourceAttributeIdAttribute", sourceAttributeIdAttribute);
    model.addAttribute("sourceAttributeLabelAttribute", sourceAttributeLabelAttribute);
    // Check if the selected source attribute is isAggregatable
    Attribute sourceAttributeAttribute = dataService.getEntityType(source).getAttribute(sourceAttribute);
    if (sourceAttributeAttribute.isAggregatable()) {
        AggregateResult aggregate = dataService.aggregate(source, new AggregateQueryImpl().attrX(sourceAttributeAttribute).query(new QueryImpl<>()));
        List<Long> aggregateCounts = new ArrayList<>();
        for (List<Long> count : aggregate.getMatrix()) {
            aggregateCounts.add(count.get(0));
        }
        model.addAttribute("aggregates", aggregateCounts);
    }
    model.addAttribute("target", target);
    model.addAttribute("source", source);
    model.addAttribute("targetAttribute", dataService.getEntityType(target).getAttribute(targetAttribute));
    model.addAttribute("sourceAttribute", dataService.getEntityType(source).getAttribute(sourceAttribute));
    model.addAttribute("hasWritePermission", hasWritePermission(project, false));
    CategoryMapping<String, String> categoryMapping = null;
    if (algorithm == null) {
        algorithm = attributeMapping.getAlgorithm();
    }
    try {
        categoryMapping = create(algorithm);
    } catch (Exception ignore) {
    }
    if (categoryMapping == null) {
        categoryMapping = createEmpty(sourceAttribute);
    }
    model.addAttribute("categoryMapping", categoryMapping);
    return VIEW_CATEGORY_MAPPING_EDITOR;
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Attribute(org.molgenis.data.meta.model.Attribute) ExplainedAttribute(org.molgenis.semanticsearch.explain.bean.ExplainedAttribute) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) URISyntaxException(java.net.URISyntaxException) AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) QueryImpl(org.molgenis.data.support.QueryImpl) AggregateResult(org.molgenis.data.aggregation.AggregateResult)

Example 64 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class QueryGeneratorTest method generateOneQueryRuleNotEqualsCategoricalNull.

// FIXME add test for ref entity where id attribute is int
// FIXME add test where value is entity
@Test
public void generateOneQueryRuleNotEqualsCategoricalNull() {
    String value = null;
    Query<Entity> q = new QueryImpl<>().not().eq(categoricalAttrName, value);
    QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType);
    QueryBuilder expectedQuery = boolQuery().mustNot(constantScoreQuery(boolQuery().mustNot(nestedQuery(categoricalAttrName, existsQuery(categoricalAttrName + ".xid"), ScoreMode.Avg))));
    assertQueryBuilderEquals(query, expectedQuery);
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) QueryImpl(org.molgenis.data.support.QueryImpl) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.testng.annotations.Test)

Example 65 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class QueryGeneratorTest method generateOneQueryRuleNotEqualsXref.

// FIXME add test for ref entity where id attribute is int
// FIXME add test where value is entity
@Test
public void generateOneQueryRuleNotEqualsXref() {
    String value = "id";
    Query<Entity> q = new QueryImpl<>().not().eq(xrefAttrName, value);
    QueryBuilder query = queryGenerator.createQueryBuilder(q, entityType);
    QueryBuilder expectedQuery = boolQuery().mustNot(constantScoreQuery(nestedQuery(xrefAttrName, termQuery(xrefAttrName + ".xid." + FIELD_NOT_ANALYZED, value), ScoreMode.Avg)));
    assertQueryBuilderEquals(query, expectedQuery);
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) QueryImpl(org.molgenis.data.support.QueryImpl) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.testng.annotations.Test)

Aggregations

QueryImpl (org.molgenis.data.support.QueryImpl)98 Test (org.testng.annotations.Test)70 DynamicEntity (org.molgenis.data.support.DynamicEntity)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)36 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)36 EntityType (org.molgenis.data.meta.model.EntityType)28 Attribute (org.molgenis.data.meta.model.Attribute)25 Entity (org.molgenis.data.Entity)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 Stream (java.util.stream.Stream)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)7 AggregateQueryImpl (org.molgenis.data.support.AggregateQueryImpl)7 Objects.requireNonNull (java.util.Objects.requireNonNull)6 QueryRule (org.molgenis.data.QueryRule)6 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)6 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 Instant (java.time.Instant)5 LocalDate (java.time.LocalDate)5 Operator (org.molgenis.data.QueryRule.Operator)5