use of org.geotools.filter.SortByImpl in project ddf by codice.
the class SolrProviderTest method testStartIndexWithSorting.
@Test
public void testStartIndexWithSorting() throws Exception {
deleteAllIn(provider);
List<Metacard> metacards = new ArrayList<Metacard>();
DateTime dt = new DateTime(1985, 1, 1, 1, 1, 1, 1, DateTimeZone.UTC);
TreeSet<Date> calculatedDates = new TreeSet<Date>();
for (int j = 0; j < 10; j++) {
for (int i = 0; i < 100; i = i + 10) {
MetacardImpl metacard = new MockMetacard(Library.getFlagstaffRecord());
// ingest sporadically the effective dates so the default return
// order won't be ordered
Date calculatedDate = dt.plusDays(100 - i + 10 - j).toDate();
calculatedDates.add(calculatedDate);
metacard.setEffectiveDate(calculatedDate);
metacards.add(metacard);
}
}
// The TreeSet will sort them, the array will give me access to everyone
// without an iterator
Date[] dates = new Date[calculatedDates.size()];
calculatedDates.toArray(dates);
/** CREATE **/
CreateResponse response = create(metacards);
LOGGER.info("CREATED {} records.", response.getCreatedMetacards().size());
CommonQueryBuilder queryBuilder = new CommonQueryBuilder();
QueryImpl query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
int maxSize = 20;
int startIndex = 2;
// STARTINDEX=2, MAXSIZE=20
query.setPageSize(maxSize);
query.setStartIndex(startIndex);
SortByImpl sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
query.setSortBy(sortBy);
SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(maxSize, sourceResponse.getResults().size());
for (int i = 0; i < sourceResponse.getResults().size(); i++) {
Result r = sourceResponse.getResults().get(i);
Date effectiveDate = r.getMetacard().getEffectiveDate();
DateTime currentDate = new DateTime(effectiveDate.getTime());
LOGGER.debug("Testing current index: {}", startIndex + i);
assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
}
// STARTINDEX=20, MAXSIZE=5
// a match-all queryByProperty
query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
maxSize = 5;
startIndex = 20;
query.setPageSize(maxSize);
query.setStartIndex(startIndex);
sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
query.setSortBy(sortBy);
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(maxSize, sourceResponse.getResults().size());
for (int i = 0; i < sourceResponse.getResults().size(); i++) {
Result r = sourceResponse.getResults().get(i);
Date effectiveDate = r.getMetacard().getEffectiveDate();
DateTime currentDate = new DateTime(effectiveDate.getTime());
LOGGER.debug("Testing current index: {}", startIndex + i);
assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
}
// STARTINDEX=80, MAXSIZE=20
// a match-all queryByProperty
query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
maxSize = 20;
startIndex = 80;
query.setPageSize(maxSize);
query.setStartIndex(startIndex);
sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
query.setSortBy(sortBy);
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(maxSize, sourceResponse.getResults().size());
for (int i = 0; i < sourceResponse.getResults().size(); i++) {
Result r = sourceResponse.getResults().get(i);
Date effectiveDate = r.getMetacard().getEffectiveDate();
DateTime currentDate = new DateTime(effectiveDate.getTime());
LOGGER.debug("Testing current index: {}", startIndex + i);
assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
}
// STARTINDEX=1, MAXSIZE=100
// a match-all queryByProperty
query = queryBuilder.queryByProperty(Metacard.CONTENT_TYPE, MockMetacard.DEFAULT_TYPE);
maxSize = 100;
startIndex = 1;
query.setPageSize(maxSize);
query.setStartIndex(startIndex);
sortBy = new SortByImpl(queryBuilder.filterFactory.property(Metacard.EFFECTIVE), org.opengis.filter.sort.SortOrder.ASCENDING);
query.setSortBy(sortBy);
sourceResponse = provider.query(new QueryRequestImpl(query));
assertEquals(maxSize, sourceResponse.getResults().size());
for (int i = 0; i < sourceResponse.getResults().size(); i++) {
Result r = sourceResponse.getResults().get(i);
Date effectiveDate = r.getMetacard().getEffectiveDate();
DateTime currentDate = new DateTime(effectiveDate.getTime());
LOGGER.debug("Testing current index: {}", startIndex + i);
assertEquals(new DateTime(dates[startIndex - 1 + i].getTime()).getDayOfYear(), currentDate.getDayOfYear());
}
}
use of org.geotools.filter.SortByImpl in project ddf by codice.
the class FederationAdminServiceImpl method getRegistryMetacardsByFilter.
private List<Metacard> getRegistryMetacardsByFilter(Filter filter, Set<String> sourceIds) throws FederationAdminException {
if (filter == null) {
throw new FederationAdminException("Error getting registry metacards. Null filter provided.");
}
PropertyName propertyName = new PropertyNameImpl(Metacard.MODIFIED);
SortBy sortBy = new SortByImpl(propertyName, SortOrder.ASCENDING);
QueryImpl query = new QueryImpl(filter);
query.setSortBy(sortBy);
query.setPageSize(PAGE_SIZE);
QueryRequest queryRequest = new QueryRequestImpl(query, sourceIds);
try {
QueryResponse queryResponse = security.runWithSubjectOrElevate(() -> catalogFramework.query(queryRequest));
return queryResponse.getResults().stream().map(Result::getMetacard).filter(Objects::nonNull).collect(Collectors.toList());
} catch (SecurityServiceException | InvocationTargetException e) {
String message = "Error querying for registry metacards.";
LOGGER.debug("{} For Filter: {}", message, filter);
throw new FederationAdminException(message, e);
}
}
use of org.geotools.filter.SortByImpl in project ddf by codice.
the class CommonQueryBuilder method nn.
// public QueryImpl within(double x, double y) {
// double[] coords = {x, y} ;
//
// QueryImpl query = new QueryImpl( filterFactory.within(Metacard.ANY_GEO, new PointImpl(new
// DirectPositionImpl(coords), DefaultGeographicCRS.WGS84)));
//
// query.setStartIndex(1) ;
//
// query.setRequestsTotalResultsCount(true);
//
// return query;
// }
/**
* Creates a {@link QueryImpl} that sorts by distance, startIndex = 1, and nearest neighbor at
* the coordinates given with the WGS84 CRS.
*
* @param x
* - the x coordinate
* @param y
* - the y coordinate
* @return {@link QueryImpl}
*/
// public QueryImpl nn(double x, double y) {
//
// double[] coords = {x, y} ;
//
// QueryImpl query = new QueryImpl(
// filterFactory.beyond(
// Metacard.ANY_GEO,
// new PointImpl(new DirectPositionImpl(coords), DefaultGeographicCRS.WGS84),
// 0.0,
// UomOgcMapping.METRE.name()));
//
// query.setStartIndex(1) ;
//
// SortByImpl sortby = new SortByImpl(filterFactory.property(Result.DISTANCE),
// org.opengis.filter.sort.SortOrder.ASCENDING);
// query.setSortBy(sortby) ;
//
// return query;
// }
/**
* Creates a {@link QueryImpl} that sorts by distance, startIndex = 1, and nearest neighbor at
* the coordinates given with the WGS84 CRS.
*
* @param x
* - the x coordinate
* @param y
* - the y coordinate
* @return {@link QueryImpl}
*/
public QueryImpl nn(Geometry geometry) {
QueryImpl query = new QueryImpl(filterFactory.beyond(Metacard.ANY_GEO, geometry, 0.0, UomOgcMapping.METRE.name()));
query.setStartIndex(1);
SortByImpl sortby = new SortByImpl(filterFactory.property(Result.DISTANCE), org.opengis.filter.sort.SortOrder.ASCENDING);
query.setSortBy(sortby);
return query;
}
use of org.geotools.filter.SortByImpl in project ddf by codice.
the class CommonQueryBuilder method pointRadius.
/**
* Creates a point radius {@link QueryImpl} with units of measurement of meters.
*
* @param x
* @param y
* @param distance
* @return
*/
// public QueryImpl pointRadius(double x, double y, double distance) {
//
// double[] coords = {x, y} ;
//
// QueryImpl query = new QueryImpl(
// filterFactory.dwithin(
// Metacard.ANY_GEO,
// new PointImpl(new DirectPositionImpl(coords), DefaultGeographicCRS.WGS84),
// distance,
// UomOgcMapping.METRE.name()));
//
// query.setStartIndex(1) ;
//
// SortByImpl sortby = new SortByImpl(filterFactory.property(Result.DISTANCE),
// org.opengis.filter.sort.SortOrder.ASCENDING);
// query.setSortBy(sortby) ;
//
// return query;
// }
// public QueryImpl intersects(double x, double y) {
//
// double[] coords = {x, y} ;
//
// QueryImpl query = new QueryImpl( filterFactory.intersects(Metacard.ANY_GEO, new PointImpl(new
// DirectPositionImpl(coords), DefaultGeographicCRS.WGS84)));
//
// query.setStartIndex(1) ;
//
// query.setRequestsTotalResultsCount(true);
//
// return query;
//
// }
/**
* Creates a point radius {@link QueryImpl} with units of measurement of meters.
*
* @param x
* @param y
* @param distance
* @return
*/
public QueryImpl pointRadius(double x, double y, double distance) {
double[] coords = { x, y };
QueryImpl query = new QueryImpl(filterFactory.dwithin(Metacard.ANY_GEO, new PointImpl(new DirectPositionImpl(coords), DefaultGeographicCRS.WGS84), distance, UomOgcMapping.METRE.name()));
query.setStartIndex(1);
SortByImpl sortby = new SortByImpl(filterFactory.property(Result.DISTANCE), org.opengis.filter.sort.SortOrder.ASCENDING);
query.setSortBy(sortby);
return query;
}
use of org.geotools.filter.SortByImpl in project ddf by codice.
the class RefreshRegistryEntries method getBasicRegistryQuery.
private Query getBasicRegistryQuery() {
List<Filter> filters = new ArrayList<>();
filters.add(filterBuilder.attribute(Metacard.TAGS).is().equalTo().text(RegistryConstants.REGISTRY_TAG));
PropertyName propertyName = new PropertyNameImpl(Metacard.MODIFIED);
SortBy sortBy = new SortByImpl(propertyName, SortOrder.ASCENDING);
QueryImpl query = new QueryImpl(filterBuilder.allOf(filters));
query.setSortBy(sortBy);
query.setPageSize(PAGE_SIZE);
return query;
}
Aggregations