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