Search in sources :

Example 71 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class ResourceOperations method getResourceOptions.

public Map<String, Set<String>> getResourceOptions(String metacardId, String sourceId, boolean fanoutEnabled) throws ResourceNotFoundException {
    LOGGER.trace(ENTERING_STR, "getResourceOptions");
    Map<String, Set<String>> optionsMap;
    try {
        LOGGER.debug("source id to get options from: {}", sourceId);
        QueryRequest queryRequest = new QueryRequestImpl(createMetacardIdQuery(metacardId), false, Collections.singletonList(sourceId == null ? this.getId() : sourceId), null);
        QueryResponse queryResponse = queryOperations.query(queryRequest, null, false, fanoutEnabled);
        List<Result> results = queryResponse.getResults();
        if (!results.isEmpty()) {
            Metacard metacard = results.get(0).getMetacard();
            // or the local provider.
            if (StringUtils.isEmpty(sourceId) || sourceId.equals(getId())) {
                optionsMap = Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, getOptionsFromLocalProvider(metacard));
            } else {
                optionsMap = Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, getOptionsFromFederatedSource(metacard, sourceId));
            }
        } else {
            String message = "Could not find metacard " + metacardId + " on source " + sourceId;
            throw new ResourceNotFoundException(message);
        }
    } catch (UnsupportedQueryException e) {
        LOGGER.debug(ERR_FINDING_MC, metacardId, e);
        throw new ResourceNotFoundException(ERR_FINDING_MC_UNSUPPORTED, e);
    } catch (FederationException e) {
        LOGGER.debug(ERR_FEDERATING_QUERY, metacardId, e);
        throw new ResourceNotFoundException(ERR_FINDING_MC_FEDERATION, e);
    } catch (IllegalArgumentException e) {
        LOGGER.debug(NO_MC_FOUND, metacardId, e);
        throw new ResourceNotFoundException(QUERY_RETURNED_NULL, e);
    } finally {
        LOGGER.trace(EXITING_STR, "getResourceOptions");
    }
    return optionsMap;
}
Also used : Set(java.util.Set) QueryRequest(ddf.catalog.operation.QueryRequest) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 72 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class ResourceOperations method getLocalResourceOptions.

public Map<String, Set<String>> getLocalResourceOptions(String metacardId, boolean fanoutEnabled) throws ResourceNotFoundException {
    LOGGER.trace(ENTERING_STR, GET_LOCAL_RESOURCE_OPTIONS);
    Map<String, Set<String>> optionsMap;
    try {
        QueryRequest queryRequest = new QueryRequestImpl(createMetacardIdQuery(metacardId), false, Collections.singletonList(getId()), null);
        QueryResponse queryResponse = queryOperations.query(queryRequest, null, false, fanoutEnabled);
        List<Result> results = queryResponse.getResults();
        if (!results.isEmpty()) {
            Metacard metacard = results.get(0).getMetacard();
            optionsMap = Collections.singletonMap(ResourceRequest.OPTION_ARGUMENT, getOptionsFromLocalProvider(metacard));
        } else {
            String message = "Could not find metacard " + metacardId + " on local source";
            ResourceNotFoundException resourceNotFoundException = new ResourceNotFoundException(message);
            LOGGER.trace(EXITING_STR, GET_LOCAL_RESOURCE_OPTIONS);
            throw resourceNotFoundException;
        }
    } catch (UnsupportedQueryException e) {
        LOGGER.debug(ERR_FINDING_MC, metacardId, e);
        LOGGER.trace(EXITING_STR, GET_LOCAL_RESOURCE_OPTIONS);
        throw new ResourceNotFoundException(ERR_FINDING_MC_UNSUPPORTED, e);
    } catch (FederationException e) {
        LOGGER.debug(ERR_FEDERATING_QUERY, metacardId, e);
        LOGGER.trace(EXITING_STR, GET_LOCAL_RESOURCE_OPTIONS);
        throw new ResourceNotFoundException(ERR_FINDING_MC_FEDERATION, e);
    } catch (IllegalArgumentException e) {
        LOGGER.debug(NO_MC_FOUND, metacardId, e);
        LOGGER.trace(EXITING_STR, GET_LOCAL_RESOURCE_OPTIONS);
        throw new ResourceNotFoundException(QUERY_RETURNED_NULL, e);
    }
    LOGGER.trace(EXITING_STR, GET_LOCAL_RESOURCE_OPTIONS);
    return optionsMap;
}
Also used : Set(java.util.Set) QueryRequest(ddf.catalog.operation.QueryRequest) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException)

Example 73 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class CatalogFrameworkQueryTest method testBeforeQuery.

@Test
public void testBeforeQuery() {
    Calendar beforeCal = Calendar.getInstance();
    beforeCal.add(Calendar.YEAR, 4);
    Calendar card1Exp = Calendar.getInstance();
    card1Exp.add(Calendar.YEAR, 1);
    Calendar card2Exp = Calendar.getInstance();
    card2Exp.add(Calendar.YEAR, 3);
    List<Metacard> metacards = new ArrayList<Metacard>();
    MetacardImpl newCard1 = new MetacardImpl();
    newCard1.setId(null);
    newCard1.setExpirationDate(card1Exp.getTime());
    metacards.add(newCard1);
    MetacardImpl newCard2 = new MetacardImpl();
    newCard2.setId(null);
    newCard2.setExpirationDate(card2Exp.getTime());
    metacards.add(newCard2);
    String mcId1 = null;
    String mcId2 = null;
    CreateResponse createResponse = null;
    try {
        createResponse = framework.create(new CreateRequestImpl(metacards, null));
    } catch (IngestException e1) {
        fail();
    } catch (SourceUnavailableException e1) {
        fail();
    }
    assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
    for (Metacard curCard : createResponse.getCreatedMetacards()) {
        if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
            mcId1 = curCard.getId();
        } else {
            mcId2 = curCard.getId();
        }
        assertNotNull(curCard.getId());
    }
    FilterFactory filterFactory = new FilterFactoryImpl();
    Instant beforeInstant = new DefaultInstant(new DefaultPosition(beforeCal.getTime()));
    QueryImpl query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
    QueryRequest queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Expecting return 2 results.", 2, response.getHits());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    beforeInstant = new DefaultInstant(new DefaultPosition(card2Exp.getTime()));
    query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Before filter should return 1 result", 1, response.getHits());
        assertEquals("Before filter should return metacard[" + mcId1 + "]", mcId1, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    beforeInstant = new DefaultInstant(new DefaultPosition(card1Exp.getTime()));
    query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Before filter should return 0 results.", 0, response.getHits());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Calendar(java.util.Calendar) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Instant(org.opengis.temporal.Instant) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) DefaultInstant(org.geotools.temporal.object.DefaultInstant) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DefaultPosition(org.geotools.temporal.object.DefaultPosition) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 74 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class CatalogFrameworkQueryTest method testBeginsQuery.

@Test
public void testBeginsQuery() {
    Calendar beginsStart = Calendar.getInstance();
    Calendar card1Exp = Calendar.getInstance();
    if (beginsStart.equals(card1Exp)) {
        card1Exp.add(Calendar.MILLISECOND, 1);
    }
    Calendar card2Exp = Calendar.getInstance();
    card2Exp.add(Calendar.YEAR, 3);
    Calendar beginsEnd = Calendar.getInstance();
    beginsEnd.add(Calendar.YEAR, 4);
    List<Metacard> metacards = new ArrayList<Metacard>();
    MetacardImpl newCard1 = new MetacardImpl();
    newCard1.setId(null);
    newCard1.setExpirationDate(card1Exp.getTime());
    metacards.add(newCard1);
    MetacardImpl newCard2 = new MetacardImpl();
    newCard2.setId(null);
    newCard2.setExpirationDate(card2Exp.getTime());
    metacards.add(newCard2);
    String mcId1 = null;
    String mcId2 = null;
    CreateResponse createResponse = null;
    try {
        createResponse = framework.create(new CreateRequestImpl(metacards, null));
    } catch (IngestException e1) {
        LOGGER.error("Failure", e1);
        fail();
    } catch (SourceUnavailableException e1) {
        LOGGER.error("Failure", e1);
        fail();
    }
    assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
    for (Metacard curCard : createResponse.getCreatedMetacards()) {
        if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
            mcId1 = curCard.getId();
        } else {
            mcId2 = curCard.getId();
        }
        assertNotNull(curCard.getId());
    }
    FilterFactory filterFactory = new FilterFactoryImpl();
    Period beginsPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(beginsStart.getTime())), new DefaultInstant(new DefaultPosition(beginsEnd.getTime())));
    QueryImpl query = new QueryImpl(filterFactory.begins(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beginsPeriod)));
    QueryRequest queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Expecting return 0 results.", 0, response.getHits());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    beginsPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(card1Exp.getTime())), new DefaultInstant(new DefaultPosition(beginsEnd.getTime())));
    query = new QueryImpl(filterFactory.begins(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beginsPeriod)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Begins filter should return 1 result", 1, response.getHits());
        assertEquals("Begins filter should return metacard[" + mcId1 + "]", mcId1, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    beginsPeriod = new DefaultPeriod(new DefaultInstant(new DefaultPosition(card2Exp.getTime())), new DefaultInstant(new DefaultPosition(beginsEnd.getTime())));
    query = new QueryImpl(filterFactory.begins(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beginsPeriod)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Begins filter should return 1 result", 1, response.getHits());
        assertEquals("Begins filter should return metacard[" + mcId2 + "]", mcId2, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Calendar(java.util.Calendar) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) Period(org.opengis.temporal.Period) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultInstant(org.geotools.temporal.object.DefaultInstant) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultPosition(org.geotools.temporal.object.DefaultPosition) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 75 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class SortedFederationStrategyTest method testStartIndexGreaterThanMaxStartIndex.

@Test
public void testStartIndexGreaterThanMaxStartIndex() throws Exception {
    SortedQueryMonitorFactory mockSortedQueryMonitorFactory = mock(SortedQueryMonitorFactory.class);
    when(mockSortedQueryMonitorFactory.createMonitor(any(CompletionService.class), any(Map.class), responseArgumentCaptor.capture(), requestArgumentCaptor.capture(), any(List.class))).thenReturn(mockSortedQueryMonitor);
    SortedFederationStrategy federateStrategy = new SortedFederationStrategy(queryExecutor, Arrays.asList(preQueryPlugin), new ArrayList<>(), mockSortedQueryMonitorFactory);
    // startIndex and pageSize must be > 1
    Query mockQ = new QueryImpl(mock(NullFilterImpl.class), SortedFederationStrategy.DEFAULT_MAX_START_INDEX + 5, 1, mock(SortBy.class), true, LONG_TIMEOUT);
    QueryRequest fedQueryRequest = new QueryRequestImpl(mockQ, properties);
    List<Source> sources = new ArrayList<>();
    // Multiple sources needed for OffsetResultHandler to be used
    for (int i = 0; i < 2; i++) {
        Source mockSource = mock(Source.class);
        when(mockSource.getId()).thenReturn("mock source " + i);
        sources.add(mockSource);
    }
    QueryResponse federateResponse = federateStrategy.federate(sources, fedQueryRequest);
    assertThat(federateResponse.getRequest().getQuery().getStartIndex(), is(SortedFederationStrategy.DEFAULT_MAX_START_INDEX + 5));
    // Modified offset is set to 1
    verify(mockSortedQueryMonitorFactory).createMonitor(any(CompletionService.class), any(Map.class), responseArgumentCaptor.capture(), requestArgumentCaptor.capture(), any(List.class));
    assertThat(requestArgumentCaptor.getValue().getQuery().getStartIndex(), is(1));
}
Also used : NullFilterImpl(org.geotools.filter.NullFilterImpl) Query(ddf.catalog.operation.Query) QueryRequest(ddf.catalog.operation.QueryRequest) SortBy(org.opengis.filter.sort.SortBy) ArrayList(java.util.ArrayList) Source(ddf.catalog.source.Source) QueryImpl(ddf.catalog.operation.impl.QueryImpl) CompletionService(java.util.concurrent.CompletionService) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

QueryResponse (ddf.catalog.operation.QueryResponse)187 QueryRequest (ddf.catalog.operation.QueryRequest)130 Test (org.junit.Test)113 Metacard (ddf.catalog.data.Metacard)91 Result (ddf.catalog.data.Result)85 ArrayList (java.util.ArrayList)73 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)70 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)55 QueryImpl (ddf.catalog.operation.impl.QueryImpl)48 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)43 FederationException (ddf.catalog.federation.FederationException)40 ResultImpl (ddf.catalog.data.impl.ResultImpl)39 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)39 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)33 Filter (org.opengis.filter.Filter)33 HashMap (java.util.HashMap)31 Serializable (java.io.Serializable)30 Source (ddf.catalog.source.Source)28 HashSet (java.util.HashSet)28 CatalogFramework (ddf.catalog.CatalogFramework)26