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);
}
}
};
}
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);
}
}
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);
}
}
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();
}
}
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();
}
}
Aggregations