Search in sources :

Example 11 with CQLException

use of org.geotools.filter.text.cql2.CQLException in project incubator-rya by apache.

the class GeoWaveGeoIndexer method getIteratorWrapper.

private CloseableIteration<Statement, QueryEvaluationException> getIteratorWrapper(final String filterString) {
    return new CloseableIteration<Statement, QueryEvaluationException>() {

        private CloseableIterator<SimpleFeature> featureIterator = null;

        CloseableIterator<SimpleFeature> getIterator() throws QueryEvaluationException {
            if (featureIterator == null) {
                Filter cqlFilter;
                try {
                    cqlFilter = ECQL.toFilter(filterString);
                } catch (final CQLException e) {
                    logger.error("Error parsing query: " + LogUtils.clean(filterString), e);
                    throw new QueryEvaluationException(e);
                }
                final CQLQuery cqlQuery = new CQLQuery(null, cqlFilter, featureDataAdapter);
                final QueryOptions queryOptions = new QueryOptions(featureDataAdapter, index);
                try {
                    featureIterator = geoWaveDataStore.query(queryOptions, cqlQuery);
                } catch (final Exception e) {
                    logger.error("Error performing query: " + filterString, e);
                    throw new QueryEvaluationException(e);
                }
            }
            return featureIterator;
        }

        @Override
        public boolean hasNext() throws QueryEvaluationException {
            return getIterator().hasNext();
        }

        @Override
        public Statement next() throws QueryEvaluationException {
            final SimpleFeature feature = getIterator().next();
            final String subjectString = feature.getAttribute(SUBJECT_ATTRIBUTE).toString();
            final String predicateString = feature.getAttribute(PREDICATE_ATTRIBUTE).toString();
            final String objectString = feature.getAttribute(OBJECT_ATTRIBUTE).toString();
            final Object context = feature.getAttribute(CONTEXT_ATTRIBUTE);
            final String contextString = context != null ? context.toString() : "";
            final Statement statement = StatementSerializer.readStatement(subjectString, predicateString, objectString, contextString);
            return statement;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("Remove not implemented");
        }

        @Override
        public void close() throws QueryEvaluationException {
            try {
                getIterator().close();
            } catch (final IOException e) {
                throw new QueryEvaluationException(e);
            }
        }
    };
}
Also used : CloseableIterator(mil.nga.giat.geowave.core.store.CloseableIterator) RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.openrdf.model.Statement) CQLQuery(mil.nga.giat.geowave.adapter.vector.query.cql.CQLQuery) IOException(java.io.IOException) QueryOptions(mil.nga.giat.geowave.core.store.query.QueryOptions) SimpleFeature(org.opengis.feature.simple.SimpleFeature) GeoWavePluginException(mil.nga.giat.geowave.adapter.vector.plugin.GeoWavePluginException) ParseException(com.vividsolutions.jts.io.ParseException) SchemaException(org.geotools.feature.SchemaException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) CQLException(org.geotools.filter.text.cql2.CQLException) IOException(java.io.IOException) CloseableIteration(info.aduna.iteration.CloseableIteration) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Filter(org.opengis.filter.Filter) CQLException(org.geotools.filter.text.cql2.CQLException)

Example 12 with CQLException

use of org.geotools.filter.text.cql2.CQLException in project incubator-rya by apache.

the class GeoWaveGeoIndexer method deleteStatements.

private void deleteStatements(final Collection<RyaStatement> ryaStatements) throws IOException {
    // create a feature collection
    final DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
    for (final RyaStatement ryaStatement : ryaStatements) {
        final Statement statement = RyaToRdfConversions.convertStatement(ryaStatement);
        // if the predicate list is empty, accept all predicates.
        // Otherwise, make sure the predicate is on the "valid" list
        final boolean isValidPredicate = validPredicates.isEmpty() || validPredicates.contains(statement.getPredicate());
        if (isValidPredicate && (statement.getObject() instanceof Literal)) {
            try {
                final SimpleFeature feature = createFeature(featureType, statement);
                featureCollection.add(feature);
            } catch (final ParseException e) {
                logger.warn("Error getting geo from statement: " + statement.toString(), e);
            }
        }
    }
    // remove this feature collection from the store
    if (!featureCollection.isEmpty()) {
        final Set<Identifier> featureIds = new HashSet<Identifier>();
        final FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null);
        final Set<String> stringIds = DataUtilities.fidSet(featureCollection);
        for (final String id : stringIds) {
            featureIds.add(filterFactory.featureId(id));
        }
        final String filterString = stringIds.stream().collect(Collectors.joining("','", "'", "'"));
        Filter filter = null;
        try {
            filter = ECQL.toFilter(GEO_ID_ATTRIBUTE + " IN (" + filterString + ")", filterFactory);
        } catch (final CQLException e) {
            logger.error("Unable to generate filter for deleting the statement.", e);
        }
        featureStore.removeFeatures(filter);
    }
}
Also used : RyaStatement(org.apache.rya.api.domain.RyaStatement) Statement(org.openrdf.model.Statement) RyaStatement(org.apache.rya.api.domain.RyaStatement) SimpleFeature(org.opengis.feature.simple.SimpleFeature) FilterFactory(org.opengis.filter.FilterFactory) Identifier(org.opengis.filter.identity.Identifier) Filter(org.opengis.filter.Filter) Literal(org.openrdf.model.Literal) ParseException(com.vividsolutions.jts.io.ParseException) CQLException(org.geotools.filter.text.cql2.CQLException) DefaultFeatureCollection(org.geotools.feature.DefaultFeatureCollection) HashSet(java.util.HashSet)

Example 13 with CQLException

use of org.geotools.filter.text.cql2.CQLException in project ddf by codice.

the class PersistentStoreImpl method get.

@Override
public List<Map<String, Object>> get(String type, String cql, int startIndex, int pageSize) throws PersistenceException {
    if (StringUtils.isBlank(type)) {
        throw new PersistenceException("The type of object(s) to retrieve must be non-null and not blank, e.g., notification, metacard, etc.");
    }
    if (startIndex < 0) {
        throw new IllegalArgumentException("The start index must be nonnegative.");
    }
    if (pageSize <= 0 || pageSize > MAX_PAGE_SIZE) {
        throw new IllegalArgumentException(String.format("The page size must be greater than 0 and less than or equal to %d.", MAX_PAGE_SIZE));
    }
    // Set Solr Core name to type and create/connect to Solr Core
    SolrClient solrClient = getSolrClient(type);
    SolrQueryFilterVisitor visitor = new SolrQueryFilterVisitor(solrClient, type);
    try {
        SolrQuery solrQuery;
        // If not cql specified, then return all items
        if (StringUtils.isBlank(cql)) {
            solrQuery = new SolrQuery("*:*");
        } else {
            Filter filter = ECQL.toFilter(cql);
            solrQuery = (SolrQuery) filter.accept(visitor, null);
        }
        if (solrQuery == null) {
            throw new PersistenceException("Unsupported query " + cql);
        }
        solrQuery.setRows(pageSize);
        solrQuery.setStart(startIndex);
        solrQuery.addSort(PersistentItem.ID, SolrQuery.ORDER.asc);
        QueryResponse solrResponse = solrClient.query(solrQuery, METHOD.POST);
        long numResults = solrResponse.getResults().getNumFound();
        LOGGER.debug("numResults = {}", numResults);
        final SolrDocumentList docs = solrResponse.getResults();
        return documentListToResultList(docs);
    } catch (CQLException e) {
        throw new PersistenceException("CQLException while getting Solr data with cql statement " + cql, e);
    } catch (SolrServerException | SolrException | IOException e) {
        throw new PersistenceException("Exception while getting Solr data with cql statement " + cql, e);
    }
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrQueryFilterVisitor(org.codice.solr.query.SolrQueryFilterVisitor) SolrDocumentList(org.apache.solr.common.SolrDocumentList) IOException(java.io.IOException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) SolrClient(org.codice.solr.client.solrj.SolrClient) Filter(org.opengis.filter.Filter) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) PersistenceException(org.codice.ddf.persistence.PersistenceException) CQLException(org.geotools.filter.text.cql2.CQLException) SolrException(org.apache.solr.common.SolrException)

Example 14 with CQLException

use of org.geotools.filter.text.cql2.CQLException in project hale by halestudio.

the class FilterTest method testComplexInstancesECQL.

@Test
public void testComplexInstancesECQL() throws Exception {
    /*
		 * SchemaReader reader = new XmlSchemaReader();
		 * reader.setSharedTypes(null); reader.setSource(new
		 * DefaultInputSupplier
		 * ((getClass().getResource("/testdata/inspire3/HydroPhysicalWaters.xsd"
		 * ).toURI()))); IOReport report = reader.execute(null);
		 * assertTrue(report.isSuccess()); Schema schema = reader.getSchema();
		 * 
		 * StreamGmlReader instanceReader = new GmlInstanceReader();
		 * instanceReader.setSource(new
		 * DefaultInputSupplier(getClass().getResource
		 * ("/testdata/out/transformWrite_ERM_HPW.gml").toURI()));
		 * instanceReader.setSourceSchema(schema);
		 * 
		 * instanceReader.validate(); report = instanceReader.execute(null);
		 * assertTrue(report.isSuccess());
		 * 
		 * InstanceCollection instances = instanceReader.getInstances();
		 * assertFalse(instances.isEmpty());
		 */
    ResourceIterator<Instance> ri = FilterTest.complexinstances.iterator();
    try {
        boolean foundIt = false;
        boolean stayFalse = false;
        boolean stayFalseToo = false;
        boolean foundIt11 = false;
        boolean foundIt1 = false;
        boolean foundIt2 = false;
        boolean foundIt3 = false;
        Filter ecqlfilter = new FilterGeoECqlImpl("\"geometry.Polygon.srsName\" = 'EPSG:4326'");
        Filter ecqlfilter11 = new FilterGeoECqlImpl("width.WidthRange.upper = 15.0");
        Filter ecqlfilter1 = new FilterGeoECqlImpl("\"width.WidthRange.upper\" = 15.0");
        Filter foulfilter1 = new FilterGeoECqlImpl("\"location.AbstractSolid.id\" = 'HURR'");
        Filter foulfilter = new FilterGeoECqlImpl("HERP = 'DERP'");
        Filter ecqlfilter2 = new FilterGeoECqlImpl("\"id\" = '_00000000-7953-b57f-0000-00000010cb14'");
        Filter ecqlfilter3 = new FilterGeoECqlImpl("'_00000000-7953-b57f-0000-00000010cb14' = \"id\"");
        // this should throw a CQL Exception
        try {
            new FilterGeoECqlImpl("id = '_00000000-7953-b57f-0000-00000010cb14'");
            fail("Expected exception!");
        } catch (CQLException e) {
            System.out.println("CQL Exception thrown because \"id\" is reserved");
        }
        while (ri.hasNext()) {
            Instance inst = ri.next();
            assertNotNull(inst);
            if (ecqlfilter.match(inst)) {
                foundIt = true;
            }
            if (ecqlfilter1.match(inst)) {
                foundIt1 = true;
            }
            if (ecqlfilter11.match(inst)) {
                foundIt11 = true;
            }
            if (foulfilter.match(inst)) {
                stayFalse = true;
            }
            if (foulfilter1.match(inst)) {
                stayFalseToo = true;
            }
            if (ecqlfilter2.match(inst)) {
                foundIt2 = true;
            }
            if (ecqlfilter3.match(inst)) {
                foundIt3 = true;
            }
        }
        assertTrue(foundIt);
        assertTrue(foundIt1);
        assertTrue(foundIt11);
        assertTrue(foundIt2);
        assertTrue(foundIt3);
        assertFalse(stayFalse);
        assertFalse(stayFalseToo);
    // TODO
    } finally {
        ri.close();
    }
}
Also used : MutableInstance(eu.esdihumboldt.hale.common.instance.model.MutableInstance) Instance(eu.esdihumboldt.hale.common.instance.model.Instance) DefaultInstance(eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance) Filter(eu.esdihumboldt.hale.common.instance.model.Filter) CQLException(org.geotools.filter.text.cql2.CQLException) Test(org.junit.Test)

Example 15 with CQLException

use of org.geotools.filter.text.cql2.CQLException in project hale by halestudio.

the class AbstractGeotoolsFilter method migrateFilter.

@Override
public Optional<eu.esdihumboldt.hale.common.instance.model.Filter> migrateFilter(EntityDefinition context, AlignmentMigration migration, SimpleLog log) {
    EntityReplacementVisitor visitor = new EntityReplacementVisitor(migration, name -> resolveProperty(name, context, log), log);
    Object extraData = null;
    Filter copy = (Filter) internFilter.accept(visitor, extraData);
    try {
        String filterString = toFilterTerm(copy);
        return Optional.of(buildFilter(filterString));
    } catch (CQLException e) {
        log.error("Filter could not be automatically migrated", e);
        return Optional.empty();
    }
}
Also used : EntityReplacementVisitor(eu.esdihumboldt.hale.common.filter.internal.EntityReplacementVisitor) Filter(org.opengis.filter.Filter) EntityAwareFilter(eu.esdihumboldt.hale.common.align.instance.EntityAwareFilter) CQLException(org.geotools.filter.text.cql2.CQLException)

Aggregations

CQLException (org.geotools.filter.text.cql2.CQLException)19 Filter (org.opengis.filter.Filter)12 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)3 RyaStatement (org.apache.rya.api.domain.RyaStatement)3 SimpleFeature (org.opengis.feature.simple.SimpleFeature)3 Statement (org.openrdf.model.Statement)3 ParseException (com.vividsolutions.jts.io.ParseException)2 QueryImpl (ddf.catalog.operation.impl.QueryImpl)2 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)2 Filter (eu.esdihumboldt.hale.common.instance.model.Filter)2 CloseableIteration (info.aduna.iteration.CloseableIteration)2 ParseException (java.text.ParseException)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 Expression (org.opengis.filter.expression.Expression)2 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)2