Search in sources :

Example 1 with SortByType

use of org.geotoolkit.ogc.xml.v110.SortByType in project ddf by codice.

the class AbstractCswSource method createSortBy.

private SortByType createSortBy(QueryRequest queryRequest) {
    Query query = queryRequest.getQuery();
    SortByType cswSortBy = null;
    if (query != null && query.getSortBy() != null && query.getSortBy().getPropertyName() != null) {
        List<SortBy> sortBys = new ArrayList<>();
        sortBys.add(query.getSortBy());
        Serializable extSortBySer = queryRequest.getPropertyValue(ADDITIONAL_SORT_BYS);
        if (extSortBySer instanceof SortBy[]) {
            SortBy[] extSortBys = (SortBy[]) extSortBySer;
            if (extSortBys.length > 0) {
                sortBys.addAll(Arrays.asList(extSortBys));
            }
        }
        for (SortBy sortBy : sortBys) {
            SortPropertyType sortProperty = new SortPropertyType();
            PropertyNameType propertyName = new PropertyNameType();
            if (sortBy.getPropertyName() != null) {
                String propName = sortBy.getPropertyName().getPropertyName();
                if (propName != null) {
                    if (Result.TEMPORAL.equals(propName) || Metacard.ANY_DATE.equals(propName)) {
                        propName = Core.MODIFIED;
                    } else if (Result.RELEVANCE.equals(propName) || Metacard.ANY_TEXT.equals(propName)) {
                        propName = Core.TITLE;
                    } else if (Result.DISTANCE.equals(propName) || Metacard.ANY_GEO.equals(propName)) {
                        continue;
                    }
                    if (cswSortBy == null) {
                        cswSortBy = new SortByType();
                    }
                    propertyName.setContent(Arrays.asList((Object) cswFilterDelegate.mapPropertyName(propName)));
                    sortProperty.setPropertyName(propertyName);
                    if (SortOrder.DESCENDING.equals(query.getSortBy().getSortOrder())) {
                        sortProperty.setSortOrder(SortOrderType.DESC);
                    } else {
                        sortProperty.setSortOrder(SortOrderType.ASC);
                    }
                    cswSortBy.getSortProperty().add(sortProperty);
                }
            }
        }
    } else {
        return null;
    }
    return cswSortBy;
}
Also used : Serializable(java.io.Serializable) Query(ddf.catalog.operation.Query) SortBy(org.opengis.filter.sort.SortBy) ArrayList(java.util.ArrayList) SortPropertyType(net.opengis.filter.v_1_1_0.SortPropertyType) SortByType(net.opengis.filter.v_1_1_0.SortByType) PropertyNameType(net.opengis.filter.v_1_1_0.PropertyNameType)

Example 2 with SortByType

use of org.geotoolkit.ogc.xml.v110.SortByType in project ddf by codice.

the class AbstractCswSource method createQuery.

private JAXBElement<QueryType> createQuery(QueryRequest queryRequest, ElementSetType elementSetType, List<QName> elementNames) throws UnsupportedQueryException {
    QueryType queryType = new QueryType();
    QName queryTypeQName = null;
    try {
        if (StringUtils.isNotBlank(cswSourceConfiguration.getQueryTypeName())) {
            String[] parts = cswSourceConfiguration.getQueryTypeName().split(":");
            if (parts.length > 1) {
                queryTypeQName = new QName(cswSourceConfiguration.getQueryTypeNamespace(), parts[1], parts[0]);
            } else {
                queryTypeQName = new QName(cswSourceConfiguration.getQueryTypeNamespace(), cswSourceConfiguration.getQueryTypeName());
            }
        }
    } catch (IllegalArgumentException e) {
        LOGGER.debug("Unable to parse query type QName of {}.  Defaulting to CSW Record", cswSourceConfiguration.getQueryTypeName());
    }
    if (queryTypeQName == null) {
        queryTypeQName = new QName(CswConstants.CSW_OUTPUT_SCHEMA, CswConstants.CSW_RECORD_LOCAL_NAME, CswConstants.CSW_NAMESPACE_PREFIX);
    }
    QueryRequest transformedQueryRequest = cswQueryFilterTransformerProvider != null ? cswQueryFilterTransformerProvider.getTransformer(cswSourceConfiguration.getQueryTypeName()).map(it -> it.transform(queryRequest, null)).orElse(queryRequest) : queryRequest;
    queryType.setTypeNames(Arrays.asList(queryTypeQName));
    if (null != elementSetType) {
        queryType.setElementSetName(createElementSetName(elementSetType));
    } else if (!CollectionUtils.isEmpty(elementNames)) {
        queryType.setElementName(elementNames);
    } else {
        queryType.setElementSetName(createElementSetName(ElementSetType.FULL));
    }
    SortByType sortBy = createSortBy(transformedQueryRequest);
    if (sortBy != null) {
        queryType.setSortBy(sortBy);
    }
    QueryConstraintType constraint = createQueryConstraint(transformedQueryRequest);
    if (null != constraint) {
        queryType.setConstraint(constraint);
    }
    ObjectFactory objectFactory = new ObjectFactory();
    return objectFactory.createQuery(queryType);
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) ObjectFactory(net.opengis.cat.csw.v_2_0_2.ObjectFactory) QName(javax.xml.namespace.QName) SortByType(net.opengis.filter.v_1_1_0.SortByType) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType)

Example 3 with SortByType

use of org.geotoolkit.ogc.xml.v110.SortByType in project ddf by codice.

the class CswQueryFactoryTest method testPostGetRecordsValidSort.

@Test
public void testPostGetRecordsValidSort() throws CswException, UnsupportedQueryException, SourceUnavailableException, FederationException {
    GetRecordsType grr = createDefaultPostRecordsRequest();
    grr.setResultType(ResultType.RESULTS);
    QueryType query = new QueryType();
    SortByType incomingSort = new SortByType();
    SortPropertyType propType = new SortPropertyType();
    PropertyNameType propName = new PropertyNameType();
    propName.setContent(Collections.singletonList(TITLE_TEST_ATTRIBUTE));
    propType.setPropertyName(propName);
    incomingSort.getSortProperty().add(propType);
    query.setSortBy(incomingSort);
    JAXBElement<QueryType> jaxbQuery = new JAXBElement<>(cswQnameOutPutSchema, QueryType.class, query);
    grr.setAbstractQuery(jaxbQuery);
    QueryRequest queryRequest = queryFactory.getQuery(grr);
    SortBy resultSort = queryRequest.getQuery().getSortBy();
    assertThat(resultSort.getPropertyName().getPropertyName(), is(CQL_FRAMEWORK_TEST_ATTRIBUTE));
    assertThat(resultSort.getSortOrder(), is(SortOrder.ASCENDING));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) SortBy(org.opengis.filter.sort.SortBy) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) SortPropertyType(net.opengis.filter.v_1_1_0.SortPropertyType) SortByType(net.opengis.filter.v_1_1_0.SortByType) JAXBElement(javax.xml.bind.JAXBElement) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) PropertyNameType(net.opengis.filter.v_1_1_0.PropertyNameType) Test(org.junit.Test)

Example 4 with SortByType

use of org.geotoolkit.ogc.xml.v110.SortByType in project ddf by codice.

the class WfsSource method buildGetFeatureRequest.

protected GetFeatureType buildGetFeatureRequest(Query query) 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();
            String typeName = null;
            if (StringUtils.isEmpty(filterDelegateEntry.getKey().getPrefix())) {
                typeName = filterDelegateEntry.getKey().getLocalPart();
            } else {
                typeName = filterDelegateEntry.getKey().getPrefix() + ":" + filterDelegateEntry.getKey().getLocalPart();
            }
            if (StringUtils.isNotBlank(srsName)) {
                wfsQuery.setSrsName(srsName);
            }
            wfsQuery.setTypeNames(Arrays.asList(typeName));
            wfsQuery.setHandle(filterDelegateEntry.getKey().getLocalPart());
            FilterType filter = filterAdapter.adapt(query, filterDelegateEntry.getValue());
            if (filter != null) {
                if (areAnyFiltersSet(filter)) {
                    wfsQuery.setAbstractSelectionClause(new net.opengis.filter.v_2_0_0.ObjectFactory().createFilter(filter));
                }
                if (!this.disableSorting) {
                    if (query.getSortBy() != null) {
                        SortOrder sortOrder = query.getSortBy().getSortOrder();
                        if (filterDelegateEntry.getValue().isSortingSupported() && filterDelegateEntry.getValue().getAllowedSortOrders().contains(sortOrder)) {
                            JAXBElement<SortByType> sortBy = buildSortBy(filterDelegateEntry.getKey(), query.getSortBy());
                            if (sortBy != null) {
                                if (LOGGER.isDebugEnabled()) {
                                    LOGGER.debug("Sorting using sort order of [{}].", sortOrder.identifier());
                                }
                                wfsQuery.setAbstractSortingClause(sortBy);
                            }
                        } else if (filterDelegateEntry.getValue().isSortingSupported() && CollectionUtils.isEmpty(filterDelegateEntry.getValue().getAllowedSortOrders())) {
                            JAXBElement<SortByType> sortBy = buildSortBy(filterDelegateEntry.getKey(), query.getSortBy());
                            if (sortBy != null) {
                                if (LOGGER.isDebugEnabled()) {
                                    LOGGER.debug("No sort orders defined in getCapabilities.  Attempting to sort using sort order of [{}].", sortOrder.identifier());
                                }
                                wfsQuery.setAbstractSortingClause(sortBy);
                            }
                        } else if (filterDelegateEntry.getValue().isSortingSupported() && !filterDelegateEntry.getValue().getAllowedSortOrders().contains(sortOrder)) {
                            LOGGER.debug("Unsupported sort order of [{}]. Supported sort orders are {}.", sortOrder, filterDelegateEntry.getValue().getAllowedSortOrders());
                        } else if (!filterDelegateEntry.getValue().isSortingSupported()) {
                            LOGGER.debug("Sorting is not supported.");
                        }
                    }
                } else {
                    LOGGER.debug("Sorting is disabled.");
                }
                queries.add(wfsQuery);
            } else {
                LOGGER.debug("WFS Source {}: {} has an invalid filter.", getId(), filterDelegateEntry.getKey());
            }
        }
    }
    if (!queries.isEmpty()) {
        GetFeatureType getFeatureType = new GetFeatureType();
        int pageSize = query.getPageSize();
        if (pageSize < 0) {
            LOGGER.debug("Page size has a negative value");
            throw new UnsupportedQueryException("Unable to build query. Page size has a negative value.");
        }
        int startIndex = query.getStartIndex();
        if (startIndex < 0) {
            LOGGER.debug("Start index has a negative value");
            throw new UnsupportedQueryException("Unable to build query. Start index has a negative value.");
        } else if (startIndex != 0) {
            // Convert DDF index of 1 back to index of 0 for WFS 2.0
            startIndex = query.getStartIndex() - 1;
        } else {
            LOGGER.debug("Query already has a start index of 0");
        }
        getFeatureType.setCount(BigInteger.valueOf(query.getPageSize()));
        getFeatureType.setStartIndex(BigInteger.valueOf(startIndex));
        List<JAXBElement<?>> incomingQueries = getFeatureType.getAbstractQueryExpression();
        for (QueryType queryType : queries) {
            incomingQueries.add(new net.opengis.wfs.v_2_0_0.ObjectFactory().createQuery(queryType));
        }
        logMessage(getFeatureType);
        return getFeatureType;
    } else {
        throw new UnsupportedQueryException("Unable to build query. No filters could be created from query criteria.");
    }
}
Also used : ContentType(ddf.catalog.data.ContentType) QName(javax.xml.namespace.QName) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) SortOrder(org.opengis.filter.sort.SortOrder) SortByType(net.opengis.filter.v_2_0_0.SortByType) JAXBElement(javax.xml.bind.JAXBElement) FilterType(net.opengis.filter.v_2_0_0.FilterType) QueryType(net.opengis.wfs.v_2_0_0.QueryType) GetFeatureType(net.opengis.wfs.v_2_0_0.GetFeatureType)

Example 5 with SortByType

use of org.geotoolkit.ogc.xml.v110.SortByType in project ddf by codice.

the class WfsSource method buildSortBy.

private JAXBElement<SortByType> buildSortBy(QName featureType, SortBy incomingSortBy) throws UnsupportedQueryException {
    net.opengis.filter.v_2_0_0.ObjectFactory filterObjectFactory = new net.opengis.filter.v_2_0_0.ObjectFactory();
    String propertyName = mapSortByPropertyName(featureType, incomingSortBy.getPropertyName().getPropertyName(), metacardToFeatureMappers);
    if (propertyName != null) {
        SortOrder sortOrder = incomingSortBy.getSortOrder();
        SortPropertyType sortPropertyType = filterObjectFactory.createSortPropertyType();
        sortPropertyType.setValueReference(propertyName);
        if (SortOrder.ASCENDING.equals(sortOrder)) {
            sortPropertyType.setSortOrder(SortOrderType.ASC);
        } else if (SortOrder.DESCENDING.equals(sortOrder)) {
            sortPropertyType.setSortOrder(SortOrderType.DESC);
        } else {
            throw new UnsupportedQueryException("Unable to build query. Unknown sort order of [" + sortOrder.identifier() + "].");
        }
        SortByType sortByType = filterObjectFactory.createSortByType();
        sortByType.getSortProperty().add(sortPropertyType);
        return filterObjectFactory.createSortBy(sortByType);
    } else {
        return null;
    }
}
Also used : UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) SortOrder(org.opengis.filter.sort.SortOrder) SortPropertyType(net.opengis.filter.v_2_0_0.SortPropertyType) SortByType(net.opengis.filter.v_2_0_0.SortByType)

Aggregations

ArrayList (java.util.ArrayList)9 SortByType (net.opengis.filter.v_1_1_0.SortByType)8 QName (javax.xml.namespace.QName)6 SortByType (net.opengis.filter.v_2_0_0.SortByType)6 SortBy (org.opengis.filter.sort.SortBy)6 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 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)3 JAXBElement (javax.xml.bind.JAXBElement)3 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)3 SortPropertyType (org.geotoolkit.ogc.xml.v110.SortPropertyType)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ContentType (ddf.catalog.data.ContentType)2