Search in sources :

Example 31 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class CswEndpoint method queryCsw.

private CswRecordCollection queryCsw(GetRecordsType request) throws CswException {
    if (LOGGER.isDebugEnabled()) {
        try {
            Writer writer = new StringWriter();
            try {
                Marshaller marshaller = CswQueryFactory.getJaxBContext().createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                JAXBElement<GetRecordsType> jaxbElement = new ObjectFactory().createGetRecords(request);
                marshaller.marshal(jaxbElement, writer);
            } catch (JAXBException e) {
                LOGGER.debug("Unable to marshall {} to XML.  Exception {}", GetRecordsType.class, e);
            }
            LOGGER.debug(writer.toString());
        } catch (Exception e) {
            LOGGER.debug("Unable to create debug message for getRecordsType: {}", e);
        }
    }
    QueryType query = (QueryType) request.getAbstractQuery().getValue();
    CswRecordCollection response = new CswRecordCollection();
    response.setRequest(request);
    response.setOutputSchema(request.getOutputSchema());
    response.setMimeType(request.getOutputFormat());
    response.setElementName(query.getElementName());
    response.setElementSetType((query.getElementSetName() != null) ? query.getElementSetName().getValue() : null);
    response.setResultType((ResultType) ObjectUtils.defaultIfNull(request.getResultType(), ResultType.HITS));
    if (ResultType.HITS.equals(request.getResultType()) || ResultType.RESULTS.equals(request.getResultType())) {
        QueryRequest queryRequest = queryFactory.getQuery(request);
        try {
            queryRequest = queryFactory.updateQueryRequestTags(queryRequest, request.getOutputSchema());
            LOGGER.debug("Attempting to execute query: {}", queryRequest);
            QueryResponse queryResponse = framework.query(queryRequest);
            response.setSourceResponse(queryResponse);
        } catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
            LOGGER.debug("Unable to query", e);
            throw new CswException(e);
        }
    }
    return response;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Marshaller(javax.xml.bind.Marshaller) QueryRequest(ddf.catalog.operation.QueryRequest) JAXBException(javax.xml.bind.JAXBException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) FederationException(ddf.catalog.federation.FederationException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) MimeTypeParseException(javax.activation.MimeTypeParseException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) FederationException(ddf.catalog.federation.FederationException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ParseException(com.vividsolutions.jts.io.ParseException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringWriter(java.io.StringWriter) ObjectFactory(net.opengis.cat.csw.v_2_0_2.ObjectFactory) QueryResponse(ddf.catalog.operation.QueryResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 32 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class CswEndpoint method updateRecords.

private int updateRecords(UpdateAction updateAction) throws CswException, FederationException, IngestException, SourceUnavailableException, UnsupportedQueryException {
    if (updateAction.getMetacard() != null) {
        Metacard newRecord = updateAction.getMetacard();
        if (newRecord.getId() != null) {
            UpdateRequest updateRequest = new UpdateRequestImpl(newRecord.getId(), newRecord);
            LOGGER.debug("Attempting to update {} ", newRecord.getId());
            UpdateResponse updateResponse = framework.update(updateRequest);
            return updateResponse.getUpdatedMetacards().size();
        } else {
            throw new CswException("Unable to update record.  No ID was specified in the request.", CswConstants.MISSING_PARAMETER_VALUE, updateAction.getHandle());
        }
    } else if (updateAction.getConstraint() != null) {
        QueryConstraintType constraint = updateAction.getConstraint();
        QueryRequest queryRequest = queryFactory.getQuery(constraint);
        queryRequest = queryFactory.updateQueryRequestTags(queryRequest, schemaTransformerManager.getTransformerSchemaForId(updateAction.getTypeName()));
        QueryResponse response = framework.query(queryRequest);
        if (response.getHits() > 0) {
            Map<String, Serializable> recordProperties = updateAction.getRecordProperties();
            List<String> updatedMetacardIdsList = new ArrayList<>();
            List<Metacard> updatedMetacards = new ArrayList<>();
            for (Result result : response.getResults()) {
                Metacard metacard = result.getMetacard();
                if (metacard != null) {
                    for (Entry<String, Serializable> recordProperty : recordProperties.entrySet()) {
                        Attribute attribute = new AttributeImpl(recordProperty.getKey(), recordProperty.getValue());
                        metacard.setAttribute(attribute);
                    }
                    updatedMetacardIdsList.add(metacard.getId());
                    updatedMetacards.add(metacard);
                }
            }
            if (updatedMetacardIdsList.size() > 0) {
                String[] updatedMetacardIds = updatedMetacardIdsList.toArray(new String[updatedMetacardIdsList.size()]);
                UpdateRequest updateRequest = new UpdateRequestImpl(updatedMetacardIds, updatedMetacards);
                LOGGER.debug("Attempting to update {} metacards.", updatedMetacardIdsList.size());
                UpdateResponse updateResponse = framework.update(updateRequest);
                return updateResponse.getUpdatedMetacards().size();
            }
        }
    }
    return 0;
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) UpdateRequest(ddf.catalog.operation.UpdateRequest) Attribute(ddf.catalog.data.Attribute) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Result(ddf.catalog.data.Result) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) Entry(java.util.Map.Entry) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Map(java.util.Map) HashMap(java.util.HashMap)

Example 33 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class CswEndpoint method queryById.

private CswRecordCollection queryById(List<String> ids, String outputSchema) throws CswException {
    QueryRequest queryRequest = queryFactory.getQueryById(ids);
    try {
        CswRecordCollection response = new CswRecordCollection();
        response.setById(true);
        queryRequest = queryFactory.updateQueryRequestTags(queryRequest, outputSchema);
        QueryResponse queryResponse = framework.query(queryRequest);
        response.setSourceResponse(queryResponse);
        List<Metacard> metacards = new LinkedList<>();
        for (Result result : queryResponse.getResults()) {
            metacards.add(result.getMetacard());
        }
        response.setCswRecords(metacards);
        return response;
    } catch (FederationException | SourceUnavailableException | UnsupportedQueryException e) {
        throw new CswException(e);
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Metacard(ddf.catalog.data.Metacard) QueryRequest(ddf.catalog.operation.QueryRequest) QueryResponse(ddf.catalog.operation.QueryResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) FederationException(ddf.catalog.federation.FederationException) LinkedList(java.util.LinkedList) Result(ddf.catalog.data.Result)

Example 34 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class SendEvent method sendEvent.

private void sendEvent(String operation, Metacard... metacards) {
    if (subject == null) {
        return;
    }
    try {
        List<Result> results = Arrays.asList(metacards).stream().map(ResultImpl::new).collect(Collectors.toList());
        QueryResponse queryResponse = new QueryResponseImpl(query, results, true, metacards.length);
        CswRecordCollection recordCollection = new CswRecordCollection();
        recordCollection.setElementName(elementName);
        recordCollection.setElementSetType(elementSetType);
        recordCollection.setById(false);
        recordCollection.setRequest(request);
        recordCollection.setResultType(resultType);
        recordCollection.setDoWriteNamespaces(false);
        recordCollection.setMimeType(mimeType);
        recordCollection.setOutputSchema(outputSchema);
        queryResponse.getRequest().getProperties().put(SecurityConstants.SECURITY_SUBJECT, subject);
        for (AccessPlugin plugin : getAccessPlugins()) {
            queryResponse = plugin.processPostQuery(queryResponse);
        }
        if (queryResponse.getResults().isEmpty()) {
            return;
        }
        recordCollection.setSourceResponse(queryResponse);
        send(operation, recordCollection);
    } catch (StopProcessingException | InvalidSyntaxException e) {
        LOGGER.debug("Unable to send event error running AccessPlugin processPostQuery. ", e);
    }
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) AccessPlugin(ddf.catalog.plugin.AccessPlugin) QueryResponse(ddf.catalog.operation.QueryResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) StopProcessingException(ddf.catalog.plugin.StopProcessingException) Result(ddf.catalog.data.Result)

Example 35 with QueryResponse

use of ddf.catalog.operation.QueryResponse in project ddf by codice.

the class CswEndpoint method deleteRecords.

private int deleteRecords(DeleteAction deleteAction) throws CswException, FederationException, IngestException, SourceUnavailableException, UnsupportedQueryException {
    QueryRequest queryRequest = queryFactory.getQuery(deleteAction.getConstraint());
    queryRequest = queryFactory.updateQueryRequestTags(queryRequest, schemaTransformerManager.getTransformerSchemaForId(deleteAction.getTypeName()));
    QueryResponse response = framework.query(queryRequest);
    List<String> ids = new ArrayList<>();
    for (Result result : response.getResults()) {
        if (result != null && result.getMetacard() != null) {
            ids.add(result.getMetacard().getId());
        }
    }
    if (ids.size() > 0) {
        DeleteRequestImpl deleteRequest = new DeleteRequestImpl(ids.toArray(new String[ids.size()]));
        LOGGER.debug("Attempting to delete {} metacards. ", ids.size());
        DeleteResponse deleteResponse = framework.delete(deleteRequest);
        return deleteResponse.getDeletedMetacards().size();
    }
    return 0;
}
Also used : DeleteResponse(ddf.catalog.operation.DeleteResponse) QueryRequest(ddf.catalog.operation.QueryRequest) QueryResponse(ddf.catalog.operation.QueryResponse) DeleteRequestImpl(ddf.catalog.operation.impl.DeleteRequestImpl) ArrayList(java.util.ArrayList) Result(ddf.catalog.data.Result)

Aggregations

QueryResponse (ddf.catalog.operation.QueryResponse)120 QueryRequest (ddf.catalog.operation.QueryRequest)87 Test (org.junit.Test)74 Metacard (ddf.catalog.data.Metacard)67 Result (ddf.catalog.data.Result)54 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)52 ArrayList (java.util.ArrayList)51 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)34 QueryImpl (ddf.catalog.operation.impl.QueryImpl)34 FederationException (ddf.catalog.federation.FederationException)33 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)30 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)24 HashSet (java.util.HashSet)24 Filter (org.opengis.filter.Filter)24 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)23 Source (ddf.catalog.source.Source)22 ResultImpl (ddf.catalog.data.impl.ResultImpl)21 Serializable (java.io.Serializable)21 HashMap (java.util.HashMap)21 InputStream (java.io.InputStream)19