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