use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class PredicateTest method testMultipleCriteria.
@Test
public void testMultipleCriteria() throws Exception {
String methodName = "testMultipleCriteria";
LOGGER.debug("*************** START: {} *****************", methodName);
// test with temporal, spatial, and entry
MockQuery query = new MockQuery();
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar start = df.newXMLGregorianCalendarDate(2011, 10, 25, 0);
XMLGregorianCalendar end = df.newXMLGregorianCalendarDate(2011, 10, 27, 0);
query.addTemporalFilter(start, end, Metacard.MODIFIED);
String wkt = "POLYGON((0 10, 0 0, 10 0, 10 10, 0 10))";
query.addSpatialFilter(wkt, 0.0, "Meter", "CONTAINS");
// create entry criteria
String catalogId = "ABC123";
query.addEntryFilter(catalogId);
MetacardImpl metacard = new MetacardImpl();
metacard.setLocation("POINT(5 5)");
metacard.setId(catalogId);
metacard.setCreatedDate(new Date());
metacard.setExpirationDate(new Date());
metacard.setEffectiveDate(new Date());
metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
Date modifiedDate = cal.toGregorianCalendar().getTime();
metacard.setModifiedDate(modifiedDate);
HashMap<String, Object> properties = new HashMap<>();
properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
Map<String, Object> contextualMap = constructContextualMap(metacard);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
// Above Pulled from PubSubProviderImpl
Event testEvent = new Event("topic", properties);
// input passes temporal, id, and geo
SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
LOGGER.debug("Resulting Predicate: {}", pred);
Filter filter = query.getFilter();
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
String filterXml = transform.transform(filter);
LOGGER.debug(filterXml);
assertTrue(pred.matches(testEvent));
// input passes temporal, id, but fails geo
// geo out of range
metacard.setLocation("POINT(5 50)");
properties.clear();
properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
// Below Pulled from PubSubProviderImpl
contextualMap = constructContextualMap(metacard);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
// Above Pulled from PubSubProviderImpl
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
// input passes geo, and id, but fails temporal
metacard.setLocation("POINT(5 5)");
XMLGregorianCalendar cal1 = df.newXMLGregorianCalendarDate(2011, 10, 28, 0);
Date modifiedDate1 = cal1.toGregorianCalendar().getTime();
// date out of range
metacard.setModifiedDate(modifiedDate1);
properties.clear();
properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
// Below Pulled from PubSubProviderImpl
contextualMap = constructContextualMap(metacard);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
// Above Pulled from PubSubProviderImpl
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
// input passes temporal, geo, but fails id
XMLGregorianCalendar cal2 = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
Date modifiedDate2 = cal2.toGregorianCalendar().getTime();
metacard.setModifiedDate(modifiedDate2);
// bad id
metacard.setId("invalid_id");
properties.clear();
properties.put(PubSubConstants.HEADER_ID_KEY, metacard.getId());
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
// Below Pulled from PubSubProviderImpl
contextualMap = constructContextualMap(metacard);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
// Above Pulled from PubSubProviderImpl
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
LOGGER.debug("*************** END: {} *****************", methodName);
}
use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class PredicateTest method testTemporal.
@Test
public void testTemporal() throws Exception {
String methodName = "testTemporal";
LOGGER.debug("*************** START: {} *****************", methodName);
MockQuery query = new MockQuery();
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar start = df.newXMLGregorianCalendarDate(2011, 10, 25, 0);
XMLGregorianCalendar end = df.newXMLGregorianCalendarDate(2011, 10, 27, 0);
query.addTemporalFilter(start, end, Metacard.EFFECTIVE);
SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
LOGGER.debug("Resulting Predicate: {}", pred);
Filter filter = query.getFilter();
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
String filterXml = transform.transform(filter);
LOGGER.debug(filterXml);
// input that passes temporal
LOGGER.debug("\npass temporal.\n");
MetacardImpl metacard = new MetacardImpl();
metacard.setCreatedDate(new Date());
metacard.setExpirationDate(new Date());
metacard.setModifiedDate(new Date());
metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
XMLGregorianCalendar cal = df.newXMLGregorianCalendarDate(2011, 10, 26, 0);
Date effectiveDate = cal.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate);
HashMap<String, Object> properties = new HashMap<>();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
Map<String, Object> contextualMap = constructContextualMap(metacard);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
// Above Pulled from PubSubProviderImpl
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
Event testEvent = new Event("topic", properties);
boolean b = pred.matches(testEvent);
assertTrue(b);
// input that fails temporal
LOGGER.debug("\nfail temporal. fail content type.\n");
// time out of
XMLGregorianCalendar cal1 = df.newXMLGregorianCalendarDate(2012, 10, 30, 0);
// range
Date effectiveDate1 = cal1.toGregorianCalendar().getTime();
metacard.setEffectiveDate(effectiveDate1);
LOGGER.debug("metacard date: {}", metacard.getEffectiveDate());
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_ENTRY_KEY, metacard);
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
LOGGER.debug("*************** END: {} *****************", methodName);
}
use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class OpenSearchQueryTest method testOgcFilterEvaluateContextualLike.
@Test
@Ignore
public void testOgcFilterEvaluateContextualLike() throws Exception {
// String input = "abc_cat_dog_xyz";
String input = "<ns1:thing xmlns:ns1=\"http://ddf.codice.org/mynamespace\">cat</ns1:thing>";
// String searchTerm = "cat";
// List<Filter> filters = new ArrayList<Filter>();
FilterFactory filterFactory = new FilterFactoryImpl();
// Filter filter = filterFactory.like( filterFactory.property( "AnyText" ), searchTerm );
// Filter filter = filterFactory.equal( filterFactory.property( "thing" ),
// filterFactory.literal( searchTerm ), false );
// Filter filter = filterFactory.like( filterFactory.property( Query.ANY_TEXT ), searchTerm,
// "*", "?", "\\" );
// Filter filter = filterFactory.equal( filterFactory.property( Query.ANY_TEXT ),
// searchTerm, false );
// Filter filter = filterFactory.like( filterFactory.property( Query.ANY_TEXT ), searchTerm,
// "*", "?", "\\" );
Calendar.getInstance().getTime();
String startDate = "2011-10-8T05:48:27.891-07:00";
String endDate = "2011-10-10T06:18:27.581-07:00";
TemporalFilter temporalFilter = new TemporalFilter(startDate, endDate);
// WORKS Filter filter = filterFactory.between( filterFactory.literal( new Date() ),
// filterFactory.literal( temporalFilter.getStartDate() ), filterFactory.literal(
// temporalFilter.getEndDate() ) );
Filter filter = filterFactory.between(filterFactory.literal(new Date()), filterFactory.literal(temporalFilter.getStartDate()), filterFactory.literal(temporalFilter.getEndDate()));
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
LOGGER.debug(transform.transform(filter));
// VisualizingVisitor vv = new VisualizingVisitor() ;
// filter.accept(vv, null) ;
// vv.getMap();
boolean result = filter.evaluate(input);
LOGGER.debug("result = {}", result);
// filters.add( filter );
}
use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class OpenSearchSource method setOpenSearchParameters.
// Refactored from query() and made protected so JUnit tests could be written for this logic
protected boolean setOpenSearchParameters(Query query, Subject subject, WebClient client) {
if (LOGGER.isDebugEnabled()) {
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
try {
LOGGER.debug(transform.transform(query));
} catch (TransformerException e) {
LOGGER.debug("Error transforming query to XML", e);
}
}
OpenSearchFilterVisitor visitor = new OpenSearchFilterVisitor();
query.accept(visitor, null);
ContextualSearch contextualFilter = visitor.getContextualSearch();
//TODO fix this so we aren't just triggering off of a contextual query
if (contextualFilter != null && StringUtils.isNotEmpty(contextualFilter.getSearchPhrase())) {
// All queries must have at least a search phrase to be valid, hence this check
// for a contextual filter with a non-empty search phrase
OpenSearchSiteUtil.populateSearchOptions(client, query, subject, parameters);
OpenSearchSiteUtil.populateContextual(client, contextualFilter.getSearchPhrase(), parameters);
applyFilters(visitor, client);
return true;
// ensure that there is no search phrase - we will add our own
} else if ((visitor.getSpatialSearch() != null && contextualFilter != null && StringUtils.isEmpty(contextualFilter.getSearchPhrase())) || (visitor.getSpatialSearch() != null && contextualFilter == null)) {
OpenSearchSiteUtil.populateSearchOptions(client, query, subject, parameters);
// add a wildcard search term - this query came in with no search phrase and a search phrase is necessary
OpenSearchSiteUtil.populateContextual(client, "*", parameters);
applyFilters(visitor, client);
return true;
}
return false;
}
use of org.geotools.filter.FilterTransformer in project ddf by codice.
the class OpenSearchQueryTest method testOgcFilterEvaluateTemporalBetween.
@Test
public // @Ignore
void testOgcFilterEvaluateTemporalBetween() throws Exception {
FilterFactory filterFactory = new FilterFactoryImpl();
// get a calendar instance, which defaults to "now"
Calendar calendar = Calendar.getInstance();
// get a date to represent "today"
Date now = calendar.getTime();
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZ");
String dateInRange = dateFormatter.format(now);
// set calendar time in past to create start date for temporal filter's criteria
calendar.add(Calendar.DAY_OF_YEAR, -1);
Date start = calendar.getTime();
String startDate = dateFormatter.format(start);
LOGGER.debug("startDate = {}", startDate);
// set calendar time in future to create end date for temporal filter's criteria
calendar.add(Calendar.DAY_OF_YEAR, +3);
Date end = calendar.getTime();
String endDate = dateFormatter.format(end);
LOGGER.debug("endDate = {}", endDate);
// Test date between start and end dates
Filter filter = filterFactory.between(filterFactory.literal(dateInRange), filterFactory.literal(startDate), filterFactory.literal(endDate));
FilterTransformer transform = new FilterTransformer();
transform.setIndentation(2);
LOGGER.debug(transform.transform(filter));
boolean result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertTrue(result);
// Test date that is after end date
calendar.add(Calendar.DAY_OF_YEAR, +3);
Date outOfRange = calendar.getTime();
String outOfRangeDate = dateFormatter.format(outOfRange);
filter = filterFactory.between(filterFactory.literal(outOfRangeDate), filterFactory.literal(startDate), filterFactory.literal(endDate));
LOGGER.debug(transform.transform(filter));
result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertFalse(result);
// Test date that is before start date
calendar.add(Calendar.DAY_OF_YEAR, -20);
Date outOfRange2 = calendar.getTime();
String outOfRangeDate2 = dateFormatter.format(outOfRange2);
filter = filterFactory.between(filterFactory.literal(outOfRangeDate2), filterFactory.literal(startDate), filterFactory.literal(endDate));
LOGGER.debug(transform.transform(filter));
result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertFalse(result);
// Test date that is equal to start date
filter = filterFactory.between(filterFactory.literal(startDate), filterFactory.literal(startDate), filterFactory.literal(endDate));
LOGGER.debug(transform.transform(filter));
result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertTrue(result);
// Test date that is equal to end date
filter = filterFactory.between(filterFactory.literal(endDate), filterFactory.literal(startDate), filterFactory.literal(endDate));
LOGGER.debug(transform.transform(filter));
result = filter.evaluate(null);
LOGGER.debug("result = {}", result);
assertTrue(result);
}
Aggregations