@Test
 public void testFindAll() {
   Query q = new QueryImpl();
   q.eq(OWNER, "flup");
   when(dataService.findAll(ENTITY_NAME)).thenReturn(Stream.of(mappingProjectEntity));
   when(userService.getUser("flup")).thenReturn(owner);
   when(mappingTargetRepository.toMappingTargets(mappingTargetEntities))
       .thenReturn(asList(mappingTarget1, mappingTarget2));
   List<MappingProject> result = mappingProjectRepositoryImpl.getAllMappingProjects();
   mappingProject.setIdentifier("mappingProjectID");
   assertEquals(result, asList(mappingProject));
 }
  @Override
  public Stream<Entity> findAll(Query q) {
    if (q.getRules().isEmpty()) {
      return getEntities().stream();
    }

    if ((q.getRules().size() != 1) || (q.getRules().get(0).getOperator() != Operator.EQUALS)) {
      throw new MolgenisDataException("The only query allowed on this Repository is gene EQUALS");
    }

    String geneSymbol = (String) q.getRules().get(0).getValue();
    List<Entity> entities = getEntitiesByGeneSymbol().get(geneSymbol);

    return entities != null ? entities.stream() : Stream.empty();
  }
 @Override
 protected List<Entity> getBatch(Query<Entity> q) {
   Consumer<SearchRequestBuilder> searchRequestBuilderConsumer =
       searchRequestBuilder ->
           searchRequestGenerator.buildSearchRequest(
               searchRequestBuilder,
               type,
               SearchType.QUERY_AND_FETCH,
               q,
               null,
               null,
               null,
               entityType);
   return elasticsearchFacade
       .searchForIds(searchRequestBuilderConsumer, q.toString(), type, indexName)
       .map(idString -> convert(idString, entityType.getIdAttribute()))
       .map(idObject -> elasticsearchEntityFactory.getReference(entityType, idObject))
       .collect(toList());
 }
Esempio n. 4
0
 /**
  * Validates query based on the given entity type, converts query values to the expected type if
  * necessary.
  *
  * @param query query
  * @param entityType entity type
  * @throws MolgenisValidationException if query is invalid
  */
 public void validate(Query<? extends Entity> query, EntityType entityType) {
   query.getRules().forEach(queryRule -> validateQueryRule(queryRule, entityType));
 }
  private EntityCollectionResponseV2 createEntityCollectionResponse(
      String entityName, EntityCollectionRequestV2 request, HttpServletRequest httpRequest) {
    EntityMetaData meta = dataService.getEntityMetaData(entityName);

    Query q = request.getQ() != null ? request.getQ().createQuery(meta) : new QueryImpl();
    q.pageSize(request.getNum()).offset(request.getStart()).sort(request.getSort());
    Fetch fetch =
        AttributeFilterToFetchConverter.convert(
            request.getAttrs(), meta, languageService.getCurrentUserLanguageCode());
    if (fetch != null) {
      q.fetch(fetch);
    }

    if (request.getAggs() != null) {
      // return aggregates for aggregate query
      AggregateQuery aggsQ = request.getAggs().createAggregateQuery(meta, q);
      AttributeMetaData xAttr = aggsQ.getAttributeX();
      AttributeMetaData yAttr = aggsQ.getAttributeY();
      if (xAttr == null && yAttr == null) {
        throw new MolgenisQueryException("Aggregate query is missing 'x' or 'y' attribute");
      }
      AggregateResult aggs = dataService.aggregate(entityName, aggsQ);
      AttributeMetaDataResponseV2 xAttrResponse =
          xAttr != null
              ? new AttributeMetaDataResponseV2(
                  entityName, meta, xAttr, fetch, permissionService, dataService, languageService)
              : null;
      AttributeMetaDataResponseV2 yAttrResponse =
          yAttr != null
              ? new AttributeMetaDataResponseV2(
                  entityName, meta, yAttr, fetch, permissionService, dataService, languageService)
              : null;
      return new EntityAggregatesResponse(
          aggs, xAttrResponse, yAttrResponse, BASE_URI + '/' + entityName);
    } else {
      Long count = dataService.count(entityName, q);
      Iterable<Entity> it;
      if (count > 0) {
        it =
            new Iterable<Entity>() {
              @Override
              public Iterator<Entity> iterator() {
                return dataService.findAll(entityName, q).iterator();
              }
            };
      } else {
        it = Collections.emptyList();
      }
      EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it);

      List<Map<String, Object>> entities = new ArrayList<>();
      for (Entity entity : it) {
        Map<String, Object> responseData = new LinkedHashMap<String, Object>();
        createEntityValuesResponse(entity, fetch, responseData);
        entities.add(responseData);
      }

      UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getFullURL(httpRequest));

      String prevHref = null;
      if (pager.getPrevStart() != null) {
        builder.replaceQueryParam("start", pager.getPrevStart());
        prevHref = builder.build(false).toUriString();
      }

      String nextHref = null;
      if (pager.getNextStart() != null) {
        builder.replaceQueryParam("start", pager.getNextStart());
        nextHref = builder.build(false).toUriString();
      }

      return new EntityCollectionResponseV2(
          pager,
          entities,
          fetch,
          BASE_URI + '/' + entityName,
          meta,
          permissionService,
          dataService,
          languageService,
          prevHref,
          nextHref);
    }
  }