Search in sources :

Example 6 with Instant

use of org.opengis.temporal.Instant in project ddf by codice.

the class CatalogFrameworkQueryTest method testBeforeQuery.

@Test
public void testBeforeQuery() {
    Calendar beforeCal = Calendar.getInstance();
    beforeCal.add(Calendar.YEAR, 4);
    Calendar card1Exp = Calendar.getInstance();
    card1Exp.add(Calendar.YEAR, 1);
    Calendar card2Exp = Calendar.getInstance();
    card2Exp.add(Calendar.YEAR, 3);
    List<Metacard> metacards = new ArrayList<Metacard>();
    MetacardImpl newCard1 = new MetacardImpl();
    newCard1.setId(null);
    newCard1.setExpirationDate(card1Exp.getTime());
    metacards.add(newCard1);
    MetacardImpl newCard2 = new MetacardImpl();
    newCard2.setId(null);
    newCard2.setExpirationDate(card2Exp.getTime());
    metacards.add(newCard2);
    String mcId1 = null;
    String mcId2 = null;
    CreateResponse createResponse = null;
    try {
        createResponse = framework.create(new CreateRequestImpl(metacards, null));
    } catch (IngestException e1) {
        fail();
    } catch (SourceUnavailableException e1) {
        fail();
    }
    assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
    for (Metacard curCard : createResponse.getCreatedMetacards()) {
        if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
            mcId1 = curCard.getId();
        } else {
            mcId2 = curCard.getId();
        }
        assertNotNull(curCard.getId());
    }
    FilterFactory filterFactory = new FilterFactoryImpl();
    Instant beforeInstant = new DefaultInstant(new DefaultPosition(beforeCal.getTime()));
    QueryImpl query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
    QueryRequest queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Expecting return 2 results.", 2, response.getHits());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    beforeInstant = new DefaultInstant(new DefaultPosition(card2Exp.getTime()));
    query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Before filter should return 1 result", 1, response.getHits());
        assertEquals("Before filter should return metacard[" + mcId1 + "]", mcId1, response.getResults().get(0).getMetacard().getId());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
    beforeInstant = new DefaultInstant(new DefaultPosition(card1Exp.getTime()));
    query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
    queryReq = new QueryRequestImpl(query, false);
    try {
        QueryResponse response = framework.query(queryReq);
        assertEquals("Before filter should return 0 results.", 0, response.getHits());
    } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
        LOGGER.error("Failure", e);
        fail();
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) CreateResponse(ddf.catalog.operation.CreateResponse) Calendar(java.util.Calendar) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Instant(org.opengis.temporal.Instant) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) DefaultInstant(org.geotools.temporal.object.DefaultInstant) FederationException(ddf.catalog.federation.FederationException) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) FilterFactory(org.opengis.filter.FilterFactory) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) DefaultPosition(org.geotools.temporal.object.DefaultPosition) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) IngestException(ddf.catalog.source.IngestException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 7 with Instant

use of org.opengis.temporal.Instant in project ddf by codice.

the class GeotoolsFilterAdapterImpl method visit.

@Override
public Object visit(PropertyIsEqualTo filter, Object delegate) {
    ExpressionValues filterValues = getExpressions(filter, delegate);
    String propertyName = filterValues.propertyName;
    Object literal = filterValues.literal;
    String functionName = filterValues.functionName;
    List<Object> functionArgs = filterValues.functionArgs;
    // Special case to handle relative temporal queries
    if (literal instanceof String && RELATIVE_TEMPORAL_REGEX.matcher((String) literal).matches()) {
        DateTime currentDateTime = new DateTime();
        org.joda.time.Period period = PeriodParser.parse((String) literal, RELATIVE_TEMPORAL_REGEX);
        DateTime pastDateTime = currentDateTime.minus(period);
        return ((FilterDelegate<?>) delegate).propertyIsBetween(propertyName, pastDateTime.toDate(), currentDateTime.toDate());
    }
    if (functionName != null) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(functionName, functionArgs, literal);
    } else if (literal instanceof String) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, (String) literal, filter.isMatchingCase());
    } else if (literal instanceof Date) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, (Date) literal);
    } else if (literal instanceof Instant) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, ((Instant) literal).getPosition().getDate());
    } else if (literal instanceof Period) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, ((Period) literal).getBeginning().getPosition().getDate(), ((Period) literal).getEnding().getPosition().getDate());
    } else if (literal instanceof Integer) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, ((Integer) literal).intValue());
    } else if (literal instanceof Short) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, ((Short) literal).shortValue());
    } else if (literal instanceof Long) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, ((Long) literal).longValue());
    } else if (literal instanceof Float) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, ((Float) literal).floatValue());
    } else if (literal instanceof Double) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, ((Double) literal).doubleValue());
    } else if (literal instanceof Boolean) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, ((Boolean) literal).booleanValue());
    } else if (literal instanceof byte[]) {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, (byte[]) literal);
    } else {
        return ((FilterDelegate<?>) delegate).propertyIsEqualTo(propertyName, literal);
    }
}
Also used : FilterDelegate(ddf.catalog.filter.FilterDelegate) Instant(org.opengis.temporal.Instant) Period(org.opengis.temporal.Period) DateTime(org.joda.time.DateTime) Date(java.util.Date)

Example 8 with Instant

use of org.opengis.temporal.Instant in project ddf by codice.

the class FilterBuilderTest method afterDate.

@Test
public void afterDate() {
    final Date date = new Date();
    FilterVisitor visitor = spy(new DefaultFilterVisitor() {
    });
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Filter filter = builder.attribute(FOO_ATTRIBUTE).is().after().date(date);
    filter.accept(visitor, null);
    ArgumentCaptor<After> expressionArgument = ArgumentCaptor.forClass(After.class);
    verify(visitor).visit(expressionArgument.capture(), any());
    ExpressionVisitor expVisitor = spy(new DefaultExpressionVisitor() {
    });
    expressionArgument.getValue().getExpression1().accept(expVisitor, null);
    ArgumentCaptor<PropertyName> propertyNameArgument = ArgumentCaptor.forClass(PropertyName.class);
    verify(expVisitor).visit(propertyNameArgument.capture(), any());
    assertEquals(FOO_ATTRIBUTE, propertyNameArgument.getValue().getPropertyName());
    expressionArgument.getValue().getExpression2().accept(expVisitor, null);
    ArgumentCaptor<Literal> literalArgument = ArgumentCaptor.forClass(Literal.class);
    verify(expVisitor).visit(literalArgument.capture(), any());
    assertEquals(date, ((Instant) literalArgument.getValue().getValue()).getPosition().getDate());
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) Instant(org.opengis.temporal.Instant) DefaultExpressionVisitor(org.geotools.filter.visitor.DefaultExpressionVisitor) ExpressionVisitor(org.opengis.filter.expression.ExpressionVisitor) Date(java.util.Date) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) FilterVisitor(org.opengis.filter.FilterVisitor) Filter(org.opengis.filter.Filter) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) Literal(org.opengis.filter.expression.Literal) After(org.opengis.filter.temporal.After) DefaultExpressionVisitor(org.geotools.filter.visitor.DefaultExpressionVisitor) Test(org.junit.Test)

Example 9 with Instant

use of org.opengis.temporal.Instant in project ddf by codice.

the class SolrFilterBuilder method makePeriod.

private Period makePeriod(Date start, Date end) {
    DefaultPosition defaultPosition = new DefaultPosition(start);
    Instant startInstant = new DefaultInstant(defaultPosition);
    Instant endInstant = new DefaultInstant(new DefaultPosition(end));
    Period period = new DefaultPeriod(startInstant, endInstant);
    return period;
}
Also used : DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) DefaultPosition(org.geotools.temporal.object.DefaultPosition) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Instant(org.opengis.temporal.Instant) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Period(org.opengis.temporal.Period) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod)

Example 10 with Instant

use of org.opengis.temporal.Instant in project ddf by codice.

the class CswRecordMapperFilterVisitor method visit.

@Override
public Object visit(PropertyIsGreaterThanOrEqualTo filter, Object extraData) {
    Expression expr1 = visit(filter.getExpression1(), extraData);
    Expression expr2 = visit(filter.getExpression2(), expr1);
    // work around since Solr Provider doesn't support greaterOrEqual on temporal (DDF-311)
    if (isTemporalQuery(expr1, expr2)) {
        // also not supported by provider (DDF-311)
        // TEquals tEquals = getFactory(extraData).tequals(expr1, expr2);
        // After after = getFactory(extraData).after(expr1, expr2);
        // return getFactory(extraData).or(tEquals, after);
        Object val = null;
        Expression other = null;
        if (expr2 instanceof Literal) {
            val = ((Literal) expr2).getValue();
            other = expr1;
        } else if (expr1 instanceof Literal) {
            val = ((Literal) expr1).getValue();
            other = expr2;
        }
        if (val != null) {
            Date orig = (Date) val;
            Instant start = new DefaultInstant(new DefaultPosition(orig));
            Instant end = new DefaultInstant(new DefaultPosition(new Date()));
            DefaultPeriod period = new DefaultPeriod(start, end);
            Literal literal = getFactory(extraData).literal(period);
            return getFactory(extraData).during(other, literal);
        }
    }
    return getFactory(extraData).greaterOrEqual(expr1, expr2);
}
Also used : Expression(org.opengis.filter.expression.Expression) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) Literal(org.opengis.filter.expression.Literal) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Instant(org.opengis.temporal.Instant) DefaultPosition(org.geotools.temporal.object.DefaultPosition) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Date(java.util.Date)

Aggregations

Instant (org.opengis.temporal.Instant)17 DefaultInstant (org.geotools.temporal.object.DefaultInstant)14 DefaultPosition (org.geotools.temporal.object.DefaultPosition)14 DefaultPeriod (org.geotools.temporal.object.DefaultPeriod)13 Period (org.opengis.temporal.Period)10 Filter (org.opengis.filter.Filter)7 Date (java.util.Date)6 SpatialFilter (ddf.catalog.impl.filter.SpatialFilter)4 Expression (org.opengis.filter.expression.Expression)4 Literal (org.opengis.filter.expression.Literal)4 SpatialDistanceFilter (ddf.catalog.impl.filter.SpatialDistanceFilter)3 TemporalFilter (ddf.catalog.impl.filter.TemporalFilter)3 QueryImpl (ddf.catalog.operation.impl.QueryImpl)3 Test (org.junit.Test)3 AttributeType (ddf.catalog.data.AttributeType)2 Metacard (ddf.catalog.data.Metacard)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)2 FederationException (ddf.catalog.federation.FederationException)2 CreateResponse (ddf.catalog.operation.CreateResponse)2 QueryRequest (ddf.catalog.operation.QueryRequest)2