Search in sources :

Example 6 with CQL

use of org.geotools.filter.text.cql2.CQL 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 7 with CQL

use of org.geotools.filter.text.cql2.CQL 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)

Aggregations

CQLException (org.geotools.filter.text.cql2.CQLException)7 Filter (org.opengis.filter.Filter)6 IOException (java.io.IOException)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 PersistenceException (org.codice.ddf.persistence.PersistenceException)2 SolrQueryFilterVisitor (org.codice.solr.query.SolrQueryFilterVisitor)2 Query (ddf.catalog.operation.Query)1 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)1 Filter (eu.esdihumboldt.hale.common.instance.model.Filter)1 Instance (eu.esdihumboldt.hale.common.instance.model.Instance)1 MutableInstance (eu.esdihumboldt.hale.common.instance.model.MutableInstance)1 DefaultInstance (eu.esdihumboldt.hale.common.instance.model.impl.DefaultInstance)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1