use of org.geotools.filter.FilterFactoryImpl in project ddf by codice.
the class CswQueryFactory method buildFilter.
private CswRecordMapperFilterVisitor buildFilter(QueryConstraintType constraint) throws CswException {
CswRecordMapperFilterVisitor visitor = new CswRecordMapperFilterVisitor(metacardType, metacardTypes);
Filter filter = null;
if (constraint != null) {
if (constraint.isSetCqlText()) {
try {
filter = CQL.toFilter(constraint.getCqlText());
} catch (CQLException e) {
throw new CswException("Unable to parse CQL Constraint: " + e.getMessage(), e);
}
} else if (constraint.isSetFilter()) {
FilterType constraintFilter = constraint.getFilter();
filter = parseFilter(constraintFilter);
}
} else {
// not supported by catalog:
//filter = Filter.INCLUDE;
filter = builder.attribute(Core.ID).is().like().text(FilterDelegate.WILDCARD_CHAR);
}
if (filter == null) {
throw new CswException("Invalid Filter Expression", CswConstants.NO_APPLICABLE_CODE, null);
}
filter = transformCustomFunctionToFilter(filter);
try {
visitor.setVisitedFilter((Filter) filter.accept(visitor, new FilterFactoryImpl()));
} catch (UnsupportedOperationException ose) {
throw new CswException(ose.getMessage(), CswConstants.INVALID_PARAMETER_VALUE, null);
}
return visitor;
}
use of org.geotools.filter.FilterFactoryImpl in project ddf by codice.
the class SolrProviderTest method testContextualSimpleWithLogicOperators.
@Test
public void testContextualSimpleWithLogicOperators() throws Exception {
deleteAllIn(provider);
MockMetacard m = new MockMetacard(Library.getTampaRecord());
m.setTitle("Tampa");
List<Metacard> list = Arrays.asList((Metacard) new MockMetacard(Library.getFlagstaffRecord()), m);
assertEquals(2, create(list).getCreatedMetacards().size());
/** CONTEXTUAL QUERY - AND negative **/
String wildcard = DEFAULT_TEST_WILDCARD;
String singleChar = DEFAULT_TEST_SINGLE_WILDCARD;
String escape = DEFAULT_TEST_ESCAPE;
FilterFactory filterFactory = new FilterFactoryImpl();
Filter filter = filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.like(filterFactory.property(Metacard.METADATA), TAMPA_QUERY_PHRASE, wildcard, singleChar, escape, false));
SourceResponse sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Flagstaff and Tampa", 0, sourceResponse.getResults().size());
/** CONTEXTUAL QUERY - AND positive **/
filter = filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.like(filterFactory.property(Metacard.METADATA), AIRPORT_QUERY_PHRASE, wildcard, singleChar, escape, false));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Flagstaff and Airport", ONE_HIT, sourceResponse.getResults().size());
/** CONTEXTUAL QUERY - OR positive **/
filter = filterFactory.or(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.like(filterFactory.property(Metacard.METADATA), TAMPA_QUERY_PHRASE, wildcard, singleChar, escape, false));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Flagstaff OR Tampa", 2, sourceResponse.getResults().size());
/** CONTEXTUAL QUERY - AND / OR positive **/
filter = filterFactory.or(filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), AIRPORT_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.like(filterFactory.property(Metacard.METADATA), "AZ", wildcard, singleChar, escape, false)), filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Failed: (Airport AND AZ) or Flagstaff", ONE_HIT, sourceResponse.getResults().size());
/** COMPLEX CONTEXTUAL QUERY **/
filter = filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), AIRPORT_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), "AZ", wildcard, singleChar, escape, false), filterFactory.or(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.like(filterFactory.property(Metacard.METADATA), TAMPA_QUERY_PHRASE, wildcard, singleChar, escape, false))));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("(Airport AND (AZ AND (Flagstaff OR TAMPA)))", ONE_HIT, sourceResponse.getResults().size());
/** CONTEXTUAL QUERY - NOT positive **/
filter = filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.not(filterFactory.like(filterFactory.property(Metacard.METADATA), TAMPA_QUERY_PHRASE, wildcard, singleChar, escape, false)));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Did not find Flagstaff NOT Tampa", ONE_HIT, sourceResponse.getResults().size());
/** CONTEXTUAL QUERY - NOT negative **/
filter = filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.not(filterFactory.like(filterFactory.property(Metacard.METADATA), AIRPORT_QUERY_PHRASE, wildcard, singleChar, escape, false)));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Wrongly found Flagstaff NOT Airport", 0, sourceResponse.getResults().size());
/** CONTEXTUAL QUERY - Single NOT positive **/
filter = filterFactory.not(filterFactory.like(filterFactory.property(Metacard.METADATA), TAMPA_QUERY_PHRASE, wildcard, singleChar, escape, false));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Did not find Flagstaff", ONE_HIT, sourceResponse.getResults().size());
assertTrue(sourceResponse.getResults().get(0).getMetacard().getMetadata().contains(FLAGSTAFF_QUERY_PHRASE));
/** CONTEXTUAL QUERY - NOT multi **/
LinkedList<Filter> filters = new LinkedList<Filter>();
filters.add(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false));
filters.add(filterFactory.not(filterFactory.like(filterFactory.property(Metacard.METADATA), TAMPA_QUERY_PHRASE, wildcard, singleChar, escape, false)));
filters.add(filterFactory.not(filterFactory.like(filterFactory.property(Metacard.METADATA), "Pennsylvania", wildcard, singleChar, escape, false)));
filter = filterFactory.and(filters);
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Did not find Flagstaff NOT Tampa", ONE_HIT, sourceResponse.getResults().size());
/** CONTEXTUAL QUERY - AND / OR **/
filter = filterFactory.or(filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), AIRPORT_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.like(filterFactory.property(Metacard.METADATA), "AZ", wildcard, singleChar, escape, false)), filterFactory.or(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.like(filterFactory.property(Metacard.METADATA), "AZ", wildcard, singleChar, escape, false)));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Failed: ( Airport ) AND ( AZ ) OR ( Flagstaff ) OR ( AZ ) ", ONE_HIT, sourceResponse.getResults().size());
/** CONTEXTUAL QUERY - OR Then NOT **/
filter = filterFactory.or(filterFactory.like(filterFactory.property(Metacard.METADATA), FLAGSTAFF_QUERY_PHRASE, wildcard, singleChar, escape, false), filterFactory.and(filterFactory.like(filterFactory.property(Metacard.METADATA), "AZ", wildcard, singleChar, escape, false), filterFactory.not(filterFactory.like(filterFactory.property(Metacard.METADATA), TAMPA_QUERY_PHRASE, wildcard, singleChar, escape, false))));
sourceResponse = provider.query(new QueryRequestImpl(new QueryImpl(filter)));
assertEquals("Failed: ( Flagstaff ) OR ( AZ ) NOT ( ( Tampa ) ) ", ONE_HIT, sourceResponse.getResults().size());
}
use of org.geotools.filter.FilterFactoryImpl in project ddf by codice.
the class SolrProviderTest method testCreateMultivaluedAttribute.
/**
* Tests that multivalued attributes are stored and returned
*
* @throws UnsupportedQueryException
* @throws IngestException
*/
@Test
public void testCreateMultivaluedAttribute() throws UnsupportedQueryException, IngestException {
deleteAllIn(provider);
FilterFactory filterFactory = new FilterFactoryImpl();
MockMetacard metacard = new MockMetacard(Library.getFlagstaffRecord());
List<Serializable> a = new ArrayList<>();
a.add("sample-validator");
a.add("sample-validator2");
AttributeImpl attribute = new AttributeImpl(Validation.VALIDATION_WARNINGS, a);
metacard.setAttribute(attribute);
create(metacard);
Filter filter = filterFactory.like(filterFactory.property(Metacard.TITLE), MockMetacard.DEFAULT_TITLE, DEFAULT_TEST_WILDCARD, DEFAULT_TEST_SINGLE_WILDCARD, DEFAULT_TEST_ESCAPE, false);
QueryImpl query = new QueryImpl(filter);
query.setStartIndex(1);
SourceResponse sourceResponse = provider.query(new QueryRequestImpl(query));
List<Result> results = sourceResponse.getResults();
Metacard mResult = results.get(0).getMetacard();
assertThat(mResult.getAttribute(Validation.VALIDATION_WARNINGS).getValues().size(), is(2));
}
use of org.geotools.filter.FilterFactoryImpl 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);
}
use of org.geotools.filter.FilterFactoryImpl in project ddf by codice.
the class TestPubSubOgcFilter method testContextualEvaluate.
@Test
@Ignore
public void testContextualEvaluate() throws TransformerException {
FilterFactory filterFactory = new FilterFactoryImpl();
Filter filter = filterFactory.like(filterFactory.literal("abcdef"), "abcdef");
printFilter(filter);
assertTrue(filter.evaluate(null));
Filter filter2 = filterFactory.like(filterFactory.literal("123456"), "123456abc");
assertFalse(filter2.evaluate(null));
}
Aggregations