Search in sources :

Example 36 with FilterFactoryImpl

use of org.geotools.filter.FilterFactoryImpl in project ddf by codice.

the class SolrProviderTest method testCreateOperationWithSourceId.

@Test
public void testCreateOperationWithSourceId() throws IngestException, UnsupportedQueryException {
    deleteAllIn(provider);
    MockMetacard metacard = new MockMetacard(Library.getFlagstaffRecord());
    String id = UUID.randomUUID().toString();
    metacard.setId(id);
    metacard.setSourceId("ddfChild");
    Date oneDayAgo = new DateTime().minusDays(1).toDate();
    metacard.setCreatedDate(oneDayAgo);
    metacard.setExpirationDate(oneDayAgo);
    metacard.setEffectiveDate(oneDayAgo);
    metacard.setModifiedDate(oneDayAgo);
    CreateResponse createResponse = create(metacard);
    Metacard createdMetacard = createResponse.getCreatedMetacards().get(0);
    assertNotNull(createdMetacard.getId());
    assertEquals(MockMetacard.DEFAULT_TITLE, createdMetacard.getTitle());
    assertEquals(MockMetacard.DEFAULT_LOCATION, createdMetacard.getLocation());
    assertEquals(MockMetacard.DEFAULT_TYPE, createdMetacard.getContentTypeName());
    assertEquals(MockMetacard.DEFAULT_VERSION, createdMetacard.getContentTypeVersion());
    assertNotNull(createdMetacard.getMetadata());
    assertThat(createdMetacard.getMetadata(), containsString("<title>Flagstaff Chamber of Commerce</title>"));
    assertThat(createdMetacard.getMetadata().isEmpty(), is(not(true)));
    assertThat(createdMetacard.getCreatedDate(), is(oneDayAgo));
    assertThat(createdMetacard.getModifiedDate(), is(oneDayAgo));
    assertThat(createdMetacard.getEffectiveDate(), is(oneDayAgo));
    assertThat(createdMetacard.getExpirationDate(), is(oneDayAgo));
    assertTrue(Arrays.equals(metacard.getThumbnail(), createdMetacard.getThumbnail()));
    assertEquals(metacard.getLocation(), createdMetacard.getLocation());
    assertThat(createdMetacard.getSourceId(), is(metacard.getSourceId()));
    // --------------------
    FilterFactory filterFactory = new FilterFactoryImpl();
    // SIMPLE TITLE SEARCH
    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();
    assertEquals(1, results.size());
    assertNotNull(mResult.getId());
    assertEquals(MockMetacard.DEFAULT_TITLE, mResult.getTitle());
    assertEquals(MockMetacard.DEFAULT_LOCATION, mResult.getLocation());
    assertEquals(MockMetacard.DEFAULT_TYPE, mResult.getContentTypeName());
    assertEquals(MockMetacard.DEFAULT_VERSION, mResult.getContentTypeVersion());
    assertNotNull(mResult.getMetadata());
    assertThat(mResult.getMetadata(), containsString("<title>Flagstaff Chamber of Commerce</title>"));
    assertThat(mResult.getMetadata().isEmpty(), is(not(true)));
    assertThat(mResult.getCreatedDate(), is(oneDayAgo));
    assertThat(mResult.getModifiedDate(), is(oneDayAgo));
    assertThat(mResult.getEffectiveDate(), is(oneDayAgo));
    assertThat(mResult.getExpirationDate(), is(oneDayAgo));
    assertTrue(Arrays.equals(metacard.getThumbnail(), mResult.getThumbnail()));
    assertEquals(metacard.getLocation(), mResult.getLocation());
// assertThat(mResult.getSourceId(), is("ddf"));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) CreateResponse(ddf.catalog.operation.CreateResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) Date(java.util.Date) DateTime(org.joda.time.DateTime) FilterFactory(org.opengis.filter.FilterFactory) Result(ddf.catalog.data.Result) Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 37 with FilterFactoryImpl

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;
}
Also used : FilterType(net.opengis.filter.v_1_1_0.FilterType) Filter(org.opengis.filter.Filter) CswRecordMapperFilterVisitor(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.mappings.CswRecordMapperFilterVisitor) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) CQLException(org.geotools.filter.text.cql2.CQLException)

Example 38 with FilterFactoryImpl

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());
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SourceResponse(ddf.catalog.operation.SourceResponse) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Matchers.containsString(org.hamcrest.Matchers.containsString) FilterFactory(org.opengis.filter.FilterFactory) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 39 with FilterFactoryImpl

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));
}
Also used : Serializable(java.io.Serializable) SourceResponse(ddf.catalog.operation.SourceResponse) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) FilterFactory(org.opengis.filter.FilterFactory) Result(ddf.catalog.data.Result) QueryImpl(ddf.catalog.operation.impl.QueryImpl) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) Test(org.junit.Test)

Example 40 with FilterFactoryImpl

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);
}
Also used : TemporalFilter(ddf.catalog.impl.filter.TemporalFilter) BBoxSpatialFilter(org.codice.ddf.opensearch.query.filter.BBoxSpatialFilter) PolygonSpatialFilter(org.codice.ddf.opensearch.query.filter.PolygonSpatialFilter) Filter(org.opengis.filter.Filter) Calendar(java.util.Calendar) FilterFactoryImpl(org.geotools.filter.FilterFactoryImpl) SimpleDateFormat(java.text.SimpleDateFormat) FilterFactory(org.opengis.filter.FilterFactory) Date(java.util.Date) FilterTransformer(org.geotools.filter.FilterTransformer) Test(org.junit.Test)

Aggregations

FilterFactoryImpl (org.geotools.filter.FilterFactoryImpl)58 Test (org.junit.Test)50 FilterFactory (org.opengis.filter.FilterFactory)42 Filter (org.opengis.filter.Filter)39 QueryImpl (ddf.catalog.operation.impl.QueryImpl)35 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)33 SourceResponse (ddf.catalog.operation.SourceResponse)21 Metacard (ddf.catalog.data.Metacard)18 SolrProviderTest (ddf.catalog.source.solr.SolrProviderTest)17 Date (java.util.Date)12 ArrayList (java.util.ArrayList)11 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)9 QueryResponse (ddf.catalog.operation.QueryResponse)9 Result (ddf.catalog.data.Result)8 CreateResponse (ddf.catalog.operation.CreateResponse)8 QueryRequest (ddf.catalog.operation.QueryRequest)8 CreateRequestImpl (ddf.catalog.operation.impl.CreateRequestImpl)8 Calendar (java.util.Calendar)6 DateTime (org.joda.time.DateTime)6 Ignore (org.junit.Ignore)6