use of org.geotools.filter.FilterFactoryImpl in project ddf by codice.
the class TestCswSource method testAbsoluteTemporalSearchTwoRanges.
@Test
public void testAbsoluteTemporalSearchTwoRanges() throws JAXBException, UnsupportedQueryException, DatatypeConfigurationException, SAXException, IOException, SecurityServiceException {
// Setup
String expectedXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" + "<GetRecords resultType=\"results\" outputFormat=\"application/xml\"\r\n" + " outputSchema=\"http://www.opengis.net/cat/csw/2.0.2\" startPosition=\"1\"\r\n" + " maxRecords=\"10\" service=\"CSW\" version=\"2.0.2\"" + " xmlns=\"http://www.opengis.net/cat/csw/2.0.2\"" + " xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\"" + " xmlns:ogc=\"http://www.opengis.net/ogc\">\r\n" + " <Query typeNames=\"csw:Record\">\r\n" + " <ElementSetName>full</ElementSetName>\r\n" + " <Constraint version=\"1.1.0\">\r\n" + " <ogc:Filter>\r\n" + " <ogc:Or>\r\n" + " <ogc:PropertyIsBetween>\r\n" + " <ogc:PropertyName>effective</ogc:PropertyName>\r\n" + " <ogc:LowerBoundary>\r\n" + " <ogc:Literal>START1_DATE_TIME</ogc:Literal>\r\n" + " </ogc:LowerBoundary>\r\n" + " <ogc:UpperBoundary>\r\n" + " <ogc:Literal>END1_DATE_TIME</ogc:Literal>\r\n" + " </ogc:UpperBoundary>\r\n" + " </ogc:PropertyIsBetween>\r\n" + " <ogc:PropertyIsBetween>\r\n" + " <ogc:PropertyName>effective</ogc:PropertyName>\r\n" + " <ogc:LowerBoundary>\r\n" + " <ogc:Literal>START2_DATE_TIME</ogc:Literal>\r\n" + " </ogc:LowerBoundary>\r\n" + " <ogc:UpperBoundary>\r\n" + " <ogc:Literal>END2_DATE_TIME</ogc:Literal>\r\n" + " </ogc:UpperBoundary>\r\n" + " </ogc:PropertyIsBetween>\r\n" + " </ogc:Or>\r\n" + " </ogc:Filter>\r\n" + " </Constraint>\r\n" + " </Query>\r\n" + "</GetRecords>\r\n";
final int pageSize = 10;
final int numRecordsReturned = 1;
final long numRecordsMatched = 10;
setupMockContextForMetacardTypeRegistrationAndUnregistration(getDefaultContentTypes());
try {
configureMockCsw(numRecordsReturned, numRecordsMatched, CswConstants.VERSION_2_0_2);
} catch (CswException e) {
fail("Could not configure Mock Remote CSW: " + e.getMessage());
}
DateTime startDate = new DateTime(2012, 5, 1, 0, 0, 0, 0);
DateTime endDate = new DateTime(2012, 12, 31, 0, 0, 0, 0);
DateTime startDate2 = new DateTime(2013, 5, 1, 0, 0, 0, 0);
DateTime endDate2 = new DateTime(2013, 12, 31, 0, 0, 0, 0);
DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
// Load the expected start and end date time into the excepted result
// XML
expectedXml = expectedXml.replace("START1_DATE_TIME", fmt.print(startDate));
expectedXml = expectedXml.replace("END1_DATE_TIME", fmt.print(endDate));
expectedXml = expectedXml.replace("START2_DATE_TIME", fmt.print(startDate2));
expectedXml = expectedXml.replace("END2_DATE_TIME", fmt.print(endDate2));
// Single absolute time range to search across
FilterFactory filterFactory = new FilterFactoryImpl();
Filter temporalFilter1 = builder.attribute(Metacard.EFFECTIVE).is().during().dates(startDate.toDate(), endDate.toDate());
Filter temporalFilter2 = builder.attribute(Metacard.EFFECTIVE).is().during().dates(startDate2.toDate(), endDate2.toDate());
Filter temporalFilter = filterFactory.or(temporalFilter1, temporalFilter2);
QueryImpl temporalQuery = new QueryImpl(temporalFilter);
temporalQuery.setPageSize(pageSize);
AbstractCswSource cswSource = getCswSource(mockCsw, mockContext);
cswSource.setCswUrl(URL);
cswSource.setId(ID);
// Perform test
cswSource.query(new QueryRequestImpl(temporalQuery));
// Verify
ArgumentCaptor<GetRecordsType> captor = ArgumentCaptor.forClass(GetRecordsType.class);
try {
verify(mockCsw, atLeastOnce()).getRecords(captor.capture());
} catch (CswException e) {
fail("Could not verify Mock CSW record count " + e.getMessage());
}
GetRecordsType getRecordsType = captor.getValue();
String xml = getGetRecordsTypeAsXml(getRecordsType);
Diff xmlDiff = new Diff(expectedXml, xml);
if (!xmlDiff.similar()) {
LOGGER.error("Unexpected XML request sent");
LOGGER.error("Expected: {}", expectedXml);
LOGGER.error("Actual: {}", xml);
}
assertXMLEqual(expectedXml, xml);
}
use of org.geotools.filter.FilterFactoryImpl 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.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));
}
Aggregations