Search in sources :

Example 6 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class TestQueryImpl method testSetFilter.

/**
     * Test method for {@link ddf.catalog.operation.impl.QueryImpl#setFilter(org.opengis.filter.Filter)}
     * .
     */
@Test
public void testSetFilter() {
    QueryImpl qi = new QueryImpl(filter1);
    assertEquals(filter1, qi.getFilter());
    qi.setFilter(filter2);
    assertEquals(filter2, qi.getFilter());
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Test(org.junit.Test)

Example 7 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class TestQueryImpl method testQueryImpleFilterWithNullFilter.

@Test(expected = IllegalArgumentException.class)
public void testQueryImpleFilterWithNullFilter() {
    Filter filter = null;
    QueryImpl qi = new QueryImpl(filter);
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) Test(org.junit.Test)

Example 8 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class RemoveCommand method executeRemoveFromStore.

private Object executeRemoveFromStore() throws Exception {
    CatalogFacade catalogProvider = getCatalog();
    if (hasFilter()) {
        QueryImpl query = new QueryImpl(getFilter());
        query.setRequestsTotalResultsCount(true);
        query.setPageSize(-1);
        Map<String, Serializable> properties = new HashMap<>();
        properties.put("mode", "native");
        SourceResponse queryResponse = catalogProvider.query(new QueryRequestImpl(query, properties));
        final List<String> idsFromFilteredQuery = queryResponse.getResults().stream().map(result -> result.getMetacard().getId()).collect(Collectors.toList());
        if (ids == null) {
            ids = idsFromFilteredQuery;
        } else {
            ids = ids.stream().filter(id -> idsFromFilteredQuery.contains(id)).collect(Collectors.toList());
        }
    }
    final int numberOfMetacardsToRemove = ids.size();
    if (numberOfMetacardsToRemove > 0) {
        printSuccessMessage("Found " + numberOfMetacardsToRemove + " metacards to remove.");
    } else {
        printErrorMessage("No records found meeting filter criteria.");
        return null;
    }
    DeleteRequestImpl request = new DeleteRequestImpl(ids.toArray(new String[numberOfMetacardsToRemove]));
    DeleteResponse response = catalogProvider.delete(request);
    if (response.getDeletedMetacards().size() > 0) {
        printSuccessMessage(ids + " successfully deleted.");
        LOGGER.info(ids + " removed using catalog:remove command");
    } else {
        printErrorMessage(ids + " could not be deleted.");
        LOGGER.info(ids + " could not be deleted using catalog:remove command");
    }
    return null;
}
Also used : QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Arrays(java.util.Arrays) QueryImpl(ddf.catalog.operation.impl.QueryImpl) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Argument(org.apache.karaf.shell.api.action.Argument) DeleteResponse(ddf.catalog.operation.DeleteResponse) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) Command(org.apache.karaf.shell.api.action.Command) List(java.util.List) SourceResponse(ddf.catalog.operation.SourceResponse) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) Service(org.apache.karaf.shell.api.action.lifecycle.Service) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) Option(org.apache.karaf.shell.api.action.Option) Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) HashMap(java.util.HashMap) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DeleteResponse(ddf.catalog.operation.DeleteResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade)

Example 9 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class SearchCommand method executeSearchStore.

private Object executeSearchStore(Filter filter) throws Exception {
    String formatString = "%1$-33s %2$-26s %3$-" + TITLE_MAX_LENGTH + "s %4$-" + EXCERPT_MAX_LENGTH + "s%n";
    CatalogFacade catalogProvider = getCatalog();
    QueryImpl query = new QueryImpl(filter);
    query.setRequestsTotalResultsCount(true);
    if (numberOfItems > -1) {
        query.setPageSize(numberOfItems);
    }
    long start = System.currentTimeMillis();
    SourceResponse response = catalogProvider.query(new QueryRequestImpl(query));
    long end = System.currentTimeMillis();
    int size = 0;
    if (response.getResults() != null) {
        size = response.getResults().size();
    }
    console.println();
    console.printf(" %d result(s) out of %s%d%s in %3.3f seconds", (size), Ansi.ansi().fg(Ansi.Color.CYAN).toString(), response.getHits(), Ansi.ansi().reset().toString(), (end - start) / MS_PER_SECOND);
    console.printf(formatString, "", "", "", "");
    printHeaderMessage(String.format(formatString, ID, DATE, TITLE, EXCERPT));
    for (Result result : response.getResults()) {
        Metacard metacard = result.getMetacard();
        String title = (metacard.getTitle() != null ? metacard.getTitle() : "N/A");
        String excerpt = "N/A";
        String modifiedDate = "";
        if (searchPhrase != null) {
            if (metacard.getMetadata() != null) {
                XPathHelper helper = new XPathHelper(metacard.getMetadata());
                String indexedText = helper.getDocument().getDocumentElement().getTextContent();
                indexedText = indexedText.replaceAll("\\r\\n|\\r|\\n", " ");
                String normalizedSearchPhrase = searchPhrase.replaceAll("\\*", "");
                int index = -1;
                if (caseSensitive) {
                    index = indexedText.indexOf(normalizedSearchPhrase);
                } else {
                    index = indexedText.toLowerCase().indexOf(normalizedSearchPhrase.toLowerCase());
                }
                if (index != -1) {
                    int contextLength = (EXCERPT_MAX_LENGTH - normalizedSearchPhrase.length() - 8) / 2;
                    excerpt = "..." + indexedText.substring(Math.max(index - contextLength, 0), index);
                    excerpt = excerpt + Ansi.ansi().fg(Ansi.Color.GREEN).toString();
                    excerpt = excerpt + indexedText.substring(index, index + normalizedSearchPhrase.length());
                    excerpt = excerpt + Ansi.ansi().reset().toString();
                    excerpt = excerpt + indexedText.substring(index + normalizedSearchPhrase.length(), Math.min(indexedText.length(), index + normalizedSearchPhrase.length() + contextLength)) + "...";
                }
            }
        }
        if (metacard.getModifiedDate() != null) {
            modifiedDate = new DateTime(metacard.getModifiedDate().getTime()).toString(DATETIME_FORMATTER);
        }
        console.printf(formatString, metacard.getId(), modifiedDate, title.substring(0, Math.min(title.length(), TITLE_MAX_LENGTH)), excerpt);
    }
    return null;
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) SourceResponse(ddf.catalog.operation.SourceResponse) XPathHelper(ddf.util.XPathHelper) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) DateTime(org.joda.time.DateTime) Result(ddf.catalog.data.Result)

Example 10 with QueryImpl

use of ddf.catalog.operation.impl.QueryImpl in project ddf by codice.

the class MigrateCommand method query.

@Override
protected SourceResponse query(CatalogFacade framework, Filter filter, int startIndex, long querySize) {
    QueryImpl query = new QueryImpl(filter);
    query.setRequestsTotalResultsCount(true);
    query.setPageSize((int) querySize);
    query.setSortBy(new SortByImpl(Metacard.MODIFIED, SortOrder.DESCENDING));
    QueryRequest queryRequest = new QueryRequestImpl(query);
    query.setStartIndex(startIndex);
    SourceResponse response;
    try {
        LOGGER.debug("Querying with startIndex: {}", startIndex);
        response = framework.query(queryRequest);
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        printErrorMessage(String.format("Received error from Frameworks: %s%n", e.getMessage()));
        return null;
    }
    if (response.getProcessingDetails() != null && !response.getProcessingDetails().isEmpty()) {
        for (SourceProcessingDetails details : response.getProcessingDetails()) {
            LOGGER.debug("Got Issues: {}", details.getWarnings());
        }
        return null;
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryImpl(ddf.catalog.operation.impl.QueryImpl) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) SortByImpl(ddf.catalog.filter.impl.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) FederationException(ddf.catalog.federation.FederationException) SourceProcessingDetails(ddf.catalog.operation.SourceProcessingDetails)

Aggregations

QueryImpl (ddf.catalog.operation.impl.QueryImpl)232 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)186 Test (org.junit.Test)149 Filter (org.opengis.filter.Filter)117 SourceResponse (ddf.catalog.operation.SourceResponse)95 QueryRequest (ddf.catalog.operation.QueryRequest)66 Metacard (ddf.catalog.data.Metacard)61 ArrayList (java.util.ArrayList)50 Result (ddf.catalog.data.Result)49 Matchers.containsString (org.hamcrest.Matchers.containsString)30 Query (ddf.catalog.operation.Query)29 QueryResponse (ddf.catalog.operation.QueryResponse)28 SortByImpl (ddf.catalog.filter.impl.SortByImpl)27 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)25 SortBy (org.opengis.filter.sort.SortBy)25 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)24 Serializable (java.io.Serializable)23 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)22 HashMap (java.util.HashMap)20 Matchers.anyString (org.mockito.Matchers.anyString)20