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;
}
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;
}
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();
}
}
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();
}
}
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));
}
Aggregations