Search in sources :

Example 11 with SortByType

use of net.opengis.filter.v_1_1_0.SortByType in project ddf by codice.

the class WfsSourceTest method testSortingAscendingSortingSupported.

/**
 * Verify that the SortBy is set with the mapped Feature Property and a ASC sort order. In this
 * case, the incoming sort property of TEMPORAL is mapped to myTemporalFeatureProperty.
 *
 * <p><?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns5:GetFeature startIndex="1"
 * count="1" service="WFS" version="2.0.0" xmlns:ns2="http://www.opengis.net/ows/1.1"
 * xmlns="http://www.opengis.net/fes/2.0" xmlns:ns4="http://www.opengis.net/gml"
 * xmlns:ns3="http://www.w3.org/1999/xlink" xmlns:ns5="http://www.opengis.net/wfs/2.0"> <ns5:Query
 * typeNames="SampleFeature0" handle="SampleFeature0"> <Filter> <PropertyIsLike wildCard="*"
 * singleChar="?" escapeChar="!"> <Literal>*</Literal> <ValueReference>title</ValueReference>
 * </PropertyIsLike> </Filter> <SortBy> <SortProperty>
 * <ValueReference>myTemporalFeatureProperty</ValueReference> <SortOrder>ASC</SortOrder>
 * </SortProperty> </SortBy> </ns5:Query> </ns5:GetFeature>
 */
@Test
public void testSortingAscendingSortingSupported() throws Exception {
    // Setup
    final String searchPhrase = "*";
    final String mockTemporalFeatureProperty = "myTemporalFeatureProperty";
    final String mockFeatureType = "{http://example.com}" + SAMPLE_FEATURE_NAME + 0;
    final int pageSize = 1;
    WfsSource source = getWfsSource(ONE_TEXT_PROPERTY_SCHEMA, MockWfsServer.getFilterCapabilities(), 1, false, false, 0);
    MetacardMapper mockMetacardMapper = mock(MetacardMapper.class);
    when(mockMetacardMapper.getFeatureType()).thenReturn(mockFeatureType);
    when(mockMetacardMapper.getSortByTemporalFeatureProperty()).thenReturn(mockTemporalFeatureProperty);
    List<MetacardMapper> mappers = new ArrayList<MetacardMapper>(1);
    mappers.add(mockMetacardMapper);
    source.setMetacardToFeatureMapper(mappers);
    QueryImpl query = new QueryImpl(builder.attribute(Metacard.ANY_TEXT).is().like().text(searchPhrase));
    query.setPageSize(pageSize);
    SortBy sortBy = new SortByImpl(Result.TEMPORAL, SortOrder.ASCENDING);
    query.setSortBy(sortBy);
    // Perform Test
    GetFeatureType featureType = source.buildGetFeatureRequest(query);
    // Verify
    QueryType queryType = (QueryType) featureType.getAbstractQueryExpression().get(0).getValue();
    JAXBElement<?> abstractSortingClause = queryType.getAbstractSortingClause();
    SortByType sortByType = (SortByType) abstractSortingClause.getValue();
    assertThat(sortByType.getSortProperty().get(0).getValueReference(), is(mockTemporalFeatureProperty));
    assertThat(sortByType.getSortProperty().get(0).getSortOrder().name(), is(SortOrderType.ASC.value()));
}
Also used : SortBy(org.opengis.filter.sort.SortBy) ArrayList(java.util.ArrayList) SortByType(net.opengis.filter.v_2_0_0.SortByType) Matchers.containsString(org.hamcrest.Matchers.containsString) QueryImpl(ddf.catalog.operation.impl.QueryImpl) SortByImpl(ddf.catalog.filter.impl.SortByImpl) QueryType(net.opengis.wfs.v_2_0_0.QueryType) MetacardMapper(org.codice.ddf.spatial.ogc.wfs.catalog.mapper.MetacardMapper) GetFeatureType(net.opengis.wfs.v_2_0_0.GetFeatureType) Test(org.junit.Test)

Example 12 with SortByType

use of net.opengis.filter.v_1_1_0.SortByType in project ddf by codice.

the class GetRecordsRequest method get202RecordsType.

/**
 * Convert the KVP values into a GetRecordsType, validates format of fields and enumeration
 * constraints required to meet the schema requirements of the GetRecordsType. No further
 * validation is done at this point
 *
 * @return GetRecordsType representation of this key-value representation
 * @throws CswException An exception when some field cannot be converted to the equivalent
 *     GetRecordsType value
 */
public GetRecordsType get202RecordsType() throws CswException {
    GetRecordsType getRecords = new GetRecordsType();
    getRecords.setOutputSchema(getOutputSchema());
    getRecords.setRequestId(getRequestId());
    if (getMaxRecords() != null) {
        getRecords.setMaxRecords(getMaxRecords());
    }
    if (getStartPosition() != null) {
        getRecords.setStartPosition(getStartPosition());
    }
    if (getOutputFormat() != null) {
        getRecords.setOutputFormat(getOutputFormat());
    }
    if (getResponseHandler() != null) {
        getRecords.setResponseHandler(Arrays.asList(getResponseHandler()));
    }
    if (getResultType() != null) {
        try {
            getRecords.setResultType(ResultType.fromValue(getResultType()));
        } catch (IllegalArgumentException iae) {
            LOGGER.debug("Failed to find \"{}\" as a valid ResultType", LogSanitizer.sanitize(getResultType()), iae);
            throw new CswException("A CSW getRecords request ResultType must be \"hits\", \"results\", or \"validate\"");
        }
    }
    if (getDistributedSearch() != null && getDistributedSearch()) {
        DistributedSearchType disSearch = new DistributedSearchType();
        disSearch.setHopCount(getHopCount());
        getRecords.setDistributedSearch(disSearch);
    }
    QueryType query = new QueryType();
    Map<String, String> namespaces = parseNamespaces(getNamespace());
    List<QName> typeNames = typeStringToQNames(getTypeNames(), namespaces);
    query.setTypeNames(typeNames);
    if (getElementName() != null && getElementSetName() != null) {
        LOGGER.debug("CSW getRecords request received with mutually exclusive ElementName and SetElementName set");
        throw new CswException("A CSW getRecords request can only have an \"ElementName\" or an \"ElementSetName\"");
    }
    if (getElementName() != null) {
        query.setElementName(typeStringToQNames(getElementName(), namespaces));
    }
    if (getElementSetName() != null) {
        try {
            ElementSetNameType eleSetName = new ElementSetNameType();
            eleSetName.setTypeNames(typeNames);
            eleSetName.setValue(ElementSetType.fromValue(getElementSetName()));
            query.setElementSetName(eleSetName);
        } catch (IllegalArgumentException iae) {
            LOGGER.debug("Failed to find \"{}\" as a valid elementSetType", LogSanitizer.sanitize(getElementSetName()), iae);
            throw new CswException("A CSW getRecords request ElementSetType must be \"brief\", \"summary\", or \"full\"");
        }
    }
    if (getSortBy() != null) {
        SortByType sort = new SortByType();
        List<SortPropertyType> sortProps = new LinkedList<SortPropertyType>();
        String[] sortOptions = getSortBy().split(",");
        for (String sortOption : sortOptions) {
            if (sortOption.lastIndexOf(':') < 1) {
                throw new CswException("Invalid Sort Order format: " + getSortBy());
            }
            SortPropertyType sortProperty = new SortPropertyType();
            PropertyNameType propertyName = new PropertyNameType();
            String propName = StringUtils.substringBeforeLast(sortOption, ":");
            String direction = StringUtils.substringAfterLast(sortOption, ":");
            propertyName.setContent(Arrays.asList((Object) propName));
            SortOrderType sortOrder;
            if (direction.equals("A")) {
                sortOrder = SortOrderType.ASC;
            } else if (direction.equals("D")) {
                sortOrder = SortOrderType.DESC;
            } else {
                throw new CswException("Invalid Sort Order format: " + getSortBy());
            }
            sortProperty.setPropertyName(propertyName);
            sortProperty.setSortOrder(sortOrder);
            sortProps.add(sortProperty);
        }
        sort.setSortProperty(sortProps);
        query.setElementName(typeStringToQNames(getElementName(), namespaces));
        query.setSortBy(sort);
    }
    if (getConstraint() != null) {
        QueryConstraintType queryConstraint = new QueryConstraintType();
        if (getConstraintLanguage().equalsIgnoreCase(CswConstants.CONSTRAINT_LANGUAGE_CQL)) {
            queryConstraint.setCqlText(getConstraint());
        } else if (getConstraintLanguage().equalsIgnoreCase(CswConstants.CONSTRAINT_LANGUAGE_FILTER)) {
            try {
                XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
                xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
                xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
                XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(constraint));
                Unmarshaller unmarshaller = JAX_BCONTEXT.createUnmarshaller();
                @SuppressWarnings("unchecked") JAXBElement<FilterType> jaxbFilter = (JAXBElement<FilterType>) unmarshaller.unmarshal(xmlStreamReader);
                queryConstraint.setFilter(jaxbFilter.getValue());
            } catch (JAXBException e) {
                LOGGER.debug("JAXBException parsing OGC Filter:", e);
                throw new CswException("JAXBException parsing OGC Filter:" + getConstraint());
            } catch (Exception e) {
                LOGGER.debug("Unable to parse OGC Filter:", e);
                throw new CswException("Unable to parse OGC Filter:" + getConstraint());
            }
        } else {
            throw new CswException("Invalid Constraint Language defined: " + getConstraintLanguage());
        }
        query.setConstraint(queryConstraint);
    }
    JAXBElement<QueryType> jaxbQuery = new JAXBElement<QueryType>(new QName(CswConstants.CSW_OUTPUT_SCHEMA), QueryType.class, query);
    getRecords.setAbstractQuery(jaxbQuery);
    return getRecords;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SortByType(net.opengis.filter.v_1_1_0.SortByType) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) StringReader(java.io.StringReader) ElementSetNameType(net.opengis.cat.csw.v_2_0_2.ElementSetNameType) SortPropertyType(net.opengis.filter.v_1_1_0.SortPropertyType) DistributedSearchType(net.opengis.cat.csw.v_2_0_2.DistributedSearchType) Unmarshaller(javax.xml.bind.Unmarshaller) PropertyNameType(net.opengis.filter.v_1_1_0.PropertyNameType) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) JAXBElement(javax.xml.bind.JAXBElement) LinkedList(java.util.LinkedList) JAXBException(javax.xml.bind.JAXBException) SortOrderType(net.opengis.filter.v_1_1_0.SortOrderType) FilterType(net.opengis.filter.v_1_1_0.FilterType) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 13 with SortByType

use of net.opengis.filter.v_1_1_0.SortByType in project ddf by codice.

the class WfsSource method buildGetFeatureRequest.

private ExtendedGetFeatureType buildGetFeatureRequest(Query query, ResultTypeType resultType, BigInteger maxFeatures) throws UnsupportedQueryException {
    List<ContentType> contentTypes = getContentTypesFromQuery(query);
    List<QueryType> queries = new ArrayList<>();
    for (Entry<QName, WfsFilterDelegate> filterDelegateEntry : featureTypeFilters.entrySet()) {
        if (contentTypes.isEmpty() || isFeatureTypeInQuery(contentTypes, filterDelegateEntry.getKey().getLocalPart())) {
            QueryType wfsQuery = new QueryType();
            wfsQuery.setTypeName(Collections.singletonList(filterDelegateEntry.getKey()));
            if (StringUtils.isNotBlank(srsName)) {
                wfsQuery.setSrsName(srsName);
            }
            FilterType filter = filterAdapter.adapt(query, filterDelegateEntry.getValue());
            if (filter != null) {
                if (areAnyFiltersSet(filter)) {
                    wfsQuery.setFilter(filter);
                }
                if (!this.disableSorting && query.getSortBy() != null && query.getSortBy().getPropertyName() != null && query.getSortBy().getPropertyName().getPropertyName() != null) {
                    SortByType sortByType = buildSortBy(filterDelegateEntry.getKey(), query.getSortBy());
                    if (sortByType != null && sortByType.getSortProperty() != null && sortByType.getSortProperty().size() > 0) {
                        LOGGER.debug("Sorting using sort property [{}] and sort order [{}].", sortByType.getSortProperty().get(0).getPropertyName(), sortByType.getSortProperty().get(0).getSortOrder());
                        wfsQuery.setSortBy(sortByType);
                    } else {
                        throw new UnsupportedQueryException("Source " + this.getId() + " does not support specified sort property " + query.getSortBy().getPropertyName().getPropertyName());
                    }
                } else {
                    LOGGER.debug("Sorting is disabled or sort not specified.");
                }
                queries.add(wfsQuery);
            } else {
                LOGGER.debug("WFS Source {}: {} has an invalid filter.", getId(), filterDelegateEntry.getKey());
            }
        }
    }
    if (!queries.isEmpty()) {
        ExtendedGetFeatureType getFeatureType = new ExtendedGetFeatureType();
        getFeatureType.setMaxFeatures(maxFeatures);
        getFeatureType.getQuery().addAll(queries);
        getFeatureType.setService(Wfs11Constants.WFS);
        getFeatureType.setVersion(Wfs11Constants.VERSION_1_1_0);
        getFeatureType.setResultType(resultType);
        if (supportsStartIndex && resultType.equals(ResultTypeType.RESULTS)) {
            // Convert DDF index of 1 back to index of 0 for WFS
            getFeatureType.setStartIndex(BigInteger.valueOf(query.getStartIndex() - 1));
        }
        logMessage(getFeatureType);
        return getFeatureType;
    } else {
        throw new UnsupportedQueryException("Unable to build query. No filters could be created from query criteria.");
    }
}
Also used : FilterType(net.opengis.filter.v_1_1_0.FilterType) ContentType(ddf.catalog.data.ContentType) QName(javax.xml.namespace.QName) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) SortByType(net.opengis.filter.v_1_1_0.SortByType) QueryType(net.opengis.wfs.v_1_1_0.QueryType)

Example 14 with SortByType

use of net.opengis.filter.v_1_1_0.SortByType in project ddf by codice.

the class WfsSource method buildSortBy.

private SortByType buildSortBy(QName featureType, SortBy incomingSortBy) {
    net.opengis.filter.v_1_1_0.ObjectFactory filterObjectFactory = new net.opengis.filter.v_1_1_0.ObjectFactory();
    String propertyName = mapSortByPropertyName(featureType, incomingSortBy.getPropertyName().getPropertyName(), metacardMappers);
    if (propertyName != null) {
        SortOrder sortOrder = incomingSortBy.getSortOrder();
        SortPropertyType sortPropertyType = filterObjectFactory.createSortPropertyType();
        PropertyNameType propertyNameType = filterObjectFactory.createPropertyNameType();
        List<Serializable> props = Arrays.asList(propertyName);
        propertyNameType.setContent(props);
        sortPropertyType.setPropertyName(propertyNameType);
        if (SortOrder.ASCENDING.equals(sortOrder)) {
            sortPropertyType.setSortOrder(SortOrderType.ASC);
        } else if (SortOrder.DESCENDING.equals(sortOrder)) {
            sortPropertyType.setSortOrder(SortOrderType.DESC);
        } else {
            LOGGER.debug("Unable to build query. Unknown sort order of [{}].", sortOrder == null ? null : sortOrder.identifier());
            return null;
        }
        SortByType sortByType = filterObjectFactory.createSortByType();
        sortByType.getSortProperty().add(sortPropertyType);
        return sortByType;
    }
    return null;
}
Also used : Serializable(java.io.Serializable) SortOrder(org.opengis.filter.sort.SortOrder) SortByType(net.opengis.filter.v_1_1_0.SortByType) ObjectFactory(net.opengis.wfs.v_1_1_0.ObjectFactory) SortPropertyType(net.opengis.filter.v_1_1_0.SortPropertyType) PropertyNameType(net.opengis.filter.v_1_1_0.PropertyNameType)

Aggregations

SortByType (net.opengis.filter.v_1_1_0.SortByType)8 ArrayList (java.util.ArrayList)7 SortByType (net.opengis.filter.v_2_0_0.SortByType)6 SortBy (org.opengis.filter.sort.SortBy)6 QName (javax.xml.namespace.QName)5 PropertyNameType (net.opengis.filter.v_1_1_0.PropertyNameType)5 SortPropertyType (net.opengis.filter.v_1_1_0.SortPropertyType)5 GetFeatureType (net.opengis.wfs.v_2_0_0.GetFeatureType)5 QueryType (net.opengis.wfs.v_2_0_0.QueryType)5 Test (org.junit.Test)5 SortByImpl (ddf.catalog.filter.impl.SortByImpl)4 QueryImpl (ddf.catalog.operation.impl.QueryImpl)4 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)4 MetacardMapper (org.codice.ddf.spatial.ogc.wfs.catalog.mapper.MetacardMapper)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)3 JAXBElement (javax.xml.bind.JAXBElement)3 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)3 SortOrder (org.opengis.filter.sort.SortOrder)3 ContentType (ddf.catalog.data.ContentType)2