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