Search in sources :

Example 1 with SortByImpl

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());
    }
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) CreateResponse(ddf.catalog.operation.CreateResponse) ArrayList(java.util.ArrayList) DateTime(org.joda.time.DateTime) Date(java.util.Date) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(org.geotools.filter.SortByImpl) TreeSet(java.util.TreeSet) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) Test(org.junit.Test)

Example 2 with SortByImpl

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);
    }
}
Also used : FederationAdminException(org.codice.ddf.registry.federationadmin.service.internal.FederationAdminException) PropertyName(org.opengis.filter.expression.PropertyName) SecurityServiceException(ddf.security.service.SecurityServiceException) QueryRequest(ddf.catalog.operation.QueryRequest) SortBy(org.opengis.filter.sort.SortBy) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(org.geotools.filter.SortByImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) PropertyNameImpl(ddf.catalog.filter.impl.PropertyNameImpl)

Example 3 with SortByImpl

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;
}
Also used : QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(org.geotools.filter.SortByImpl)

Example 4 with SortByImpl

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;
}
Also used : DirectPositionImpl(org.geotools.geometry.jts.spatialschema.geometry.DirectPositionImpl) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(org.geotools.filter.SortByImpl) PointImpl(org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl)

Example 5 with SortByImpl

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;
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) SortByImpl(org.geotools.filter.SortByImpl) SortBy(org.opengis.filter.sort.SortBy) ArrayList(java.util.ArrayList) PropertyNameImpl(ddf.catalog.filter.impl.PropertyNameImpl)

Aggregations

QueryImpl (ddf.catalog.operation.impl.QueryImpl)6 SortByImpl (org.geotools.filter.SortByImpl)6 Result (ddf.catalog.data.Result)3 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)3 ArrayList (java.util.ArrayList)3 Metacard (ddf.catalog.data.Metacard)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 PropertyNameImpl (ddf.catalog.filter.impl.PropertyNameImpl)2 SourceResponse (ddf.catalog.operation.SourceResponse)2 DirectPositionImpl (org.geotools.geometry.jts.spatialschema.geometry.DirectPositionImpl)2 PointImpl (org.geotools.geometry.jts.spatialschema.geometry.primitive.PointImpl)2 Test (org.junit.Test)2 Filter (org.opengis.filter.Filter)2 PropertyName (org.opengis.filter.expression.PropertyName)2 SortBy (org.opengis.filter.sort.SortBy)2 CreateResponse (ddf.catalog.operation.CreateResponse)1 QueryRequest (ddf.catalog.operation.QueryRequest)1 QueryResponse (ddf.catalog.operation.QueryResponse)1 SecurityServiceException (ddf.security.service.SecurityServiceException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1