Search in sources :

Example 41 with SortByImpl

use of ddf.catalog.filter.impl.SortByImpl in project ddf by codice.

the class SearchService method createQuery.

/**
     * Creates a new query from the incoming parameters
     *
     * @param filter
     *            - Filter to query
     * @param startIndexLng
     *            - Start index for the query
     * @param countLng
     *            - number of results for the query
     * @param sortStr
     *            - How to sort the query results
     * @param maxTimeoutLng
     *            - timeout value on the query execution
     * @return - the new query
     */
private Query createQuery(Filter filter, Long startIndexLng, Long countLng, String sortStr, Long maxTimeoutLng) {
    // default values
    String sortField = Result.TEMPORAL;
    String sortOrder = DEFAULT_SORT_ORDER;
    Long startIndex = startIndexLng == null ? Long.valueOf(DEFAULT_START_INDEX) : startIndexLng;
    Long count = countLng == null ? Long.valueOf(DEFAULT_COUNT) : countLng;
    long maxTimeout = maxTimeoutLng == null ? DEFAULT_TIMEOUT : maxTimeoutLng;
    // is specified
    if (!(StringUtils.isEmpty(sortStr))) {
        String[] sortAry = sortStr.split(":");
        if (sortAry.length > 1) {
            sortField = sortAry[0];
            sortOrder = sortAry[1];
        }
    }
    // Query must specify a valid sort order if a sort field was specified, i.e., query
    // cannot specify just "date:", must specify "date:asc"
    SortBy sort;
    if ("asc".equalsIgnoreCase(sortOrder)) {
        sort = new SortByImpl(sortField, SortOrder.ASCENDING);
    } else if ("desc".equalsIgnoreCase(sortOrder)) {
        sort = new SortByImpl(sortField, SortOrder.DESCENDING);
    } else {
        throw new IllegalArgumentException("Incorrect sort order received, must be 'asc' or 'desc'");
    }
    LOGGER.debug("Retrieved query settings: \n sortField: {} \nsortOrder: {}", sortField, sortOrder);
    if (filter == null) {
        LOGGER.debug("Received an empty filter. Using a wildcard contextual filter instead.");
        filter = filterBuilder.attribute(Metacard.ANY_TEXT).is().like().text(FilterDelegate.WILDCARD_CHAR);
    }
    return new QueryImpl(filter, startIndex.intValue(), count.intValue(), sort, true, maxTimeout);
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(ddf.catalog.filter.impl.SortByImpl) SortBy(org.opengis.filter.sort.SortBy)

Example 42 with SortByImpl

use of ddf.catalog.filter.impl.SortByImpl in project ddf by codice.

the class TestCswSource method testQueryWithSortByDistance.

@Test
public void testQueryWithSortByDistance() throws JAXBException, UnsupportedQueryException, DatatypeConfigurationException, SAXException, IOException, SecurityServiceException {
    // Setup
    final String searchPhrase = "*";
    final int pageSize = 1;
    final int numRecordsReturned = 1;
    final long numRecordsMatched = 1;
    setupMockContextForMetacardTypeRegistrationAndUnregistration(getDefaultContentTypes());
    try {
        configureMockCsw(numRecordsReturned, numRecordsMatched, CswConstants.VERSION_2_0_2);
    } catch (CswException e) {
        fail("Could not configure Mock Remote CSW: " + e.getMessage());
    }
    QueryImpl query = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase));
    query.setPageSize(pageSize);
    SortBy sortBy = new SortByImpl(Result.DISTANCE, SortOrder.DESCENDING);
    query.setSortBy(sortBy);
    AbstractCswSource cswSource = getCswSource(mockCsw, mockContext);
    cswSource.setCswUrl(URL);
    cswSource.setId(ID);
    // Perform test
    SourceResponse response = cswSource.query(new QueryRequestImpl(query));
    // Verify
    Assert.assertNotNull(response);
    assertThat(response.getResults().size(), is(numRecordsReturned));
    assertThat(response.getHits(), is(numRecordsMatched));
    ArgumentCaptor<GetRecordsType> captor = ArgumentCaptor.forClass(GetRecordsType.class);
    try {
        verify(mockCsw, atLeastOnce()).getRecords(captor.capture());
    } catch (CswException e) {
        fail("Could not verify mock CSW record count: " + e.getMessage());
    }
    GetRecordsType getRecordsType = captor.getValue();
    QueryType cswQuery = (QueryType) getRecordsType.getAbstractQuery().getValue();
    assertThat(cswQuery.getSortBy(), nullValue());
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) SortBy(org.opengis.filter.sort.SortBy) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(ddf.catalog.filter.impl.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) Test(org.junit.Test)

Example 43 with SortByImpl

use of ddf.catalog.filter.impl.SortByImpl in project ddf by codice.

the class ReplicateCommand 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.EFFECTIVE, SortOrder.DESCENDING));
    QueryRequest queryRequest = new QueryRequestImpl(query, Arrays.asList(sourceId));
    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 %s: %s%n", sourceId, e.getMessage()));
        return null;
    }
    if (response.getProcessingDetails() != null && !response.getProcessingDetails().isEmpty()) {
        for (SourceProcessingDetails details : response.getProcessingDetails()) {
            LOGGER.debug("Got Issues: {}", details.getWarnings());
        }
        return null;
    }
    final long totalHits = response.getHits();
    if (totalHits == 0) {
        console.println("No records were found to replicate.");
        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)

Example 44 with SortByImpl

use of ddf.catalog.filter.impl.SortByImpl in project alliance by codice.

the class NsiliSourceTest method testQuerySupportedDescendingSorting.

@Test
public void testQuerySupportedDescendingSorting() throws Exception {
    QueryImpl propertyIsLikeQuery = new QueryImpl(builder.attribute(Metacard.CONTENT_TYPE).is().equalTo().text(GMTI));
    SortBy sortBy = new SortByImpl(Metacard.MODIFIED, SortOrder.DESCENDING);
    propertyIsLikeQuery.setSortBy(sortBy);
    source.query(new QueryRequestImpl(propertyIsLikeQuery));
    ArgumentCaptor<SortAttribute[]> argumentCaptor = ArgumentCaptor.forClass(SortAttribute[].class);
    verify(catalogMgr).submit_query(any(Query.class), any(String[].class), argumentCaptor.capture(), any(NameValue[].class));
    String sortAttr = NsiliConstants.NSIL_CARD + "." + NsiliConstants.DATE_TIME_MODIFIED;
    assertThat(argumentCaptor.getValue()[0].attribute_name, is(sortAttr));
    assertThat(argumentCaptor.getValue()[0].sort_polarity, is(Polarity.DESCENDING));
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortAttribute(org.codice.alliance.nsili.common.GIAS.SortAttribute) Query(org.codice.alliance.nsili.common.GIAS.Query) SortByImpl(ddf.catalog.filter.impl.SortByImpl) SortBy(org.opengis.filter.sort.SortBy) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 45 with SortByImpl

use of ddf.catalog.filter.impl.SortByImpl in project ddf by codice.

the class DumpCommand method executeWithSubject.

@Override
protected final Object executeWithSubject() throws Exception {
    if (signer == null) {
        signer = new DigitalSignature(security);
    }
    if (FilenameUtils.getExtension(dirPath).equals("") && !dirPath.endsWith(File.separator)) {
        dirPath += File.separator;
    }
    final File dumpDir = new File(dirPath);
    if (!dumpDir.exists()) {
        printErrorMessage("Directory [" + dirPath + "] must exist.");
        console.println("If the directory does indeed exist, try putting the path in quotes.");
        return null;
    }
    if (!dumpDir.isDirectory()) {
        printErrorMessage("Path [" + dirPath + "] must be a directory.");
        return null;
    }
    if (!SERIALIZED_OBJECT_ID.matches(transformerId)) {
        transformers = getTransformers();
        if (transformers == null) {
            console.println(transformerId + " is an invalid metacard transformer.");
            return null;
        }
    }
    if (StringUtils.isNotBlank(zipFileName) && new File(dirPath + zipFileName).exists()) {
        console.println("Cannot dump Catalog.  Zip file " + zipFileName + " already exists.");
        return null;
    }
    if (StringUtils.isNotBlank(zipFileName) && !zipFileName.endsWith(".zip")) {
        zipFileName = zipFileName + ".zip";
    }
    securityLogger.audit("Called catalog:dump command with path : {}", dirPath);
    CatalogFacade catalog = getCatalog();
    SortBy sort = new SortByImpl(Core.ID, SortOrder.ASCENDING);
    QueryImpl query = new QueryImpl(getFilter());
    query.setRequestsTotalResultsCount(true);
    query.setPageSize(pageSize);
    query.setSortBy(sort);
    final AtomicLong resultCount = new AtomicLong(0);
    long start = System.currentTimeMillis();
    BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(multithreaded);
    RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();
    final ExecutorService executorService = new ThreadPoolExecutor(multithreaded, multithreaded, 0L, TimeUnit.MILLISECONDS, blockingQueue, StandardThreadFactoryBuilder.newThreadFactory("dumpCommandThread"), rejectedExecutionHandler);
    QueryRequest queryRequest = new QueryRequestImpl(query);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Hits for Search: {}", catalog.query(queryRequest).getHits());
    }
    if (StringUtils.isNotBlank(zipFileName)) {
        File outputFile = new File(dirPath + zipFileName);
        createZip(catalog, queryRequest, outputFile, resultCount);
        String alias = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(SystemBaseUrl.EXTERNAL_HOST));
        String password = AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty("javax.net.ssl.keyStorePassword"));
        try (InputStream inputStream = new FileInputStream(outputFile)) {
            byte[] signature = signer.createDigitalSignature(inputStream, alias, password);
            if (signature != null) {
                String epoch = Long.toString(Instant.now().getEpochSecond());
                String signatureFilepath = String.format("%sdump_%s.sig", dirPath, epoch);
                FileUtils.writeByteArrayToFile(new File(signatureFilepath), signature);
            }
        }
    } else {
        ResultIterable.resultIterable(catalog::query, queryRequest).stream().map(Collections::singletonList).map(result -> new SourceResponseImpl(queryRequest, result)).forEach(response -> handleResult(response, executorService, dumpDir, resultCount));
    }
    executorService.shutdown();
    boolean interrupted = false;
    try {
        while (!executorService.isTerminated()) {
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                interrupted = true;
            }
        }
    } finally {
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
    }
    long end = System.currentTimeMillis();
    String elapsedTime = timeFormatter.print(new Period(start, end).withMillis(0));
    console.printf(" %d file(s) dumped in %s\t%n", resultCount.get(), elapsedTime);
    LOGGER.debug("{} file(s) dumped in {}", resultCount.get(), elapsedTime);
    console.println();
    securityLogger.audit("Exported {} files to {}", resultCount.get(), dirPath);
    return null;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) BinaryContent(ddf.catalog.data.BinaryContent) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) Command(org.apache.karaf.shell.api.action.Command) MetacardTransformer(ddf.catalog.transform.MetacardTransformer) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) Map(java.util.Map) SortByImpl(ddf.catalog.filter.impl.SortByImpl) URI(java.net.URI) ZipEntry(java.util.zip.ZipEntry) ServiceReference(org.osgi.framework.ServiceReference) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) StandardThreadFactoryBuilder(org.codice.ddf.platform.util.StandardThreadFactoryBuilder) ResourceRequestById(ddf.catalog.operation.impl.ResourceRequestById) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) BlockingQueue(java.util.concurrent.BlockingQueue) PrivilegedAction(java.security.PrivilegedAction) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) Serializable(java.io.Serializable) Objects(java.util.Objects) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Attribute(ddf.catalog.data.Attribute) PeriodFormatterBuilder(org.joda.time.format.PeriodFormatterBuilder) AccessController(java.security.AccessController) FilenameUtils(org.apache.commons.io.FilenameUtils) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) ResourceResponse(ddf.catalog.operation.ResourceResponse) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ZipOutputStream(java.util.zip.ZipOutputStream) ResultIterable(ddf.catalog.util.impl.ResultIterable) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) HashMap(java.util.HashMap) PeriodFormatter(org.joda.time.format.PeriodFormatter) Resource(ddf.catalog.resource.Resource) ArrayList(java.util.ArrayList) Reference(org.apache.karaf.shell.api.action.lifecycle.Reference) SortBy(org.opengis.filter.sort.SortBy) ContentItem(ddf.catalog.content.data.ContentItem) Constants(ddf.catalog.Constants) Metacard(ddf.catalog.data.Metacard) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) ResourceRequest(ddf.catalog.operation.ResourceRequest) QueryRequest(ddf.catalog.operation.QueryRequest) ObjectOutputStream(java.io.ObjectOutputStream) Result(ddf.catalog.data.Result) SimpleEntry(java.util.AbstractMap.SimpleEntry) Core(ddf.catalog.data.types.Core) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) SortOrder(org.opengis.filter.sort.SortOrder) Period(org.joda.time.Period) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Logger(org.slf4j.Logger) SystemBaseUrl(org.codice.ddf.configuration.SystemBaseUrl) SecurityLogger(ddf.security.audit.SecurityLogger) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Argument(org.apache.karaf.shell.api.action.Argument) FileInputStream(java.io.FileInputStream) File(java.io.File) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) SourceResponse(ddf.catalog.operation.SourceResponse) Service(org.apache.karaf.shell.api.action.lifecycle.Service) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) Option(org.apache.karaf.shell.api.action.Option) Collections(java.util.Collections) InputStream(java.io.InputStream) DigitalSignature(org.codice.ddf.commands.util.DigitalSignature) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) SortBy(org.opengis.filter.sort.SortBy) QueryImpl(ddf.catalog.operation.impl.QueryImpl) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CatalogFacade(org.codice.ddf.commands.catalog.facade.CatalogFacade) Collections(java.util.Collections) QueryRequest(ddf.catalog.operation.QueryRequest) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DigitalSignature(org.codice.ddf.commands.util.DigitalSignature) Period(org.joda.time.Period) FileInputStream(java.io.FileInputStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) SortByImpl(ddf.catalog.filter.impl.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) File(java.io.File)

Aggregations

SortByImpl (ddf.catalog.filter.impl.SortByImpl)68 QueryImpl (ddf.catalog.operation.impl.QueryImpl)65 Test (org.junit.Test)56 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)47 SortBy (org.opengis.filter.sort.SortBy)45 SourceResponse (ddf.catalog.operation.SourceResponse)28 QueryRequest (ddf.catalog.operation.QueryRequest)23 Matchers.containsString (org.hamcrest.Matchers.containsString)21 Filter (org.opengis.filter.Filter)13 ArrayList (java.util.ArrayList)12 GetFeatureType (net.opengis.wfs.v_2_0_0.GetFeatureType)12 QueryType (net.opengis.wfs.v_2_0_0.QueryType)12 Query (ddf.catalog.operation.Query)9 TemporalFilter (ddf.catalog.impl.filter.TemporalFilter)8 InputStream (java.io.InputStream)8 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)8 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)8 CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)8 MetacardMapper (org.codice.ddf.spatial.ogc.wfs.catalog.mapper.MetacardMapper)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7