use of org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction in project ddf by codice.
the class CswEndpoint method transaction.
@Override
@POST
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public TransactionResponseType transaction(CswTransactionRequest request) throws CswException {
if (request == null) {
throw new CswException("TransactionRequest request is null");
}
TransactionResponseType response = new TransactionResponseType();
TransactionSummaryType summary = new TransactionSummaryType();
summary.setTotalInserted(BigInteger.valueOf(0));
summary.setTotalUpdated(BigInteger.valueOf(0));
summary.setTotalDeleted(BigInteger.valueOf(0));
response.setTransactionSummary(summary);
response.setVersion(CswConstants.VERSION_2_0_2);
int numInserted = 0;
final Subject subject = SecurityUtils.getSubject();
for (InsertAction insertAction : request.getInsertActions()) {
final InsertAction transformInsertAction = transformInsertAction(insertAction);
List<Metacard> metacards = transformInsertAction.getRecords();
CompletionService<CreateResponse> completionService = new ExecutorCompletionService<>(queryExecutor);
for (Metacard record : metacards) {
CreateRequest createRequest = new CreateRequestImpl(record);
Callable<CreateResponse> callable = () -> {
try {
return framework.create(createRequest);
} catch (IngestException | SourceUnavailableException e) {
LOGGER.debug("Unable to insert record(s)", e);
throw new CswException("Unable to insert record(s).", CswConstants.TRANSACTION_FAILED, transformInsertAction.getHandle());
}
};
Callable<CreateResponse> createCallable = subject.associateWith(callable);
completionService.submit(createCallable);
}
for (int i = 0; i < metacards.size(); i++) {
try {
Future<CreateResponse> completedFuture = completionService.take();
try {
CreateResponse futureResponse = completedFuture.get();
numInserted += futureResponse.getCreatedMetacards().size();
if (request.isVerbose()) {
response.getInsertResult().add(getInsertResultFromResponse(futureResponse));
}
} catch (ExecutionException | CancellationException e) {
LOGGER.debug("Error ingesting Metacard", e);
throw new CswException("Unable to insert record(s).", CswConstants.TRANSACTION_FAILED, insertAction.getHandle());
}
} catch (InterruptedException e) {
LOGGER.debug("Metacard ingest interrupted", e);
Thread.currentThread().interrupt();
break;
}
}
}
LOGGER.debug("{} records inserted.", numInserted);
response.getTransactionSummary().setTotalInserted(BigInteger.valueOf(numInserted));
int numUpdated = 0;
List<UpdateAction> updateActions = request.getUpdateActions();
CompletionService<Integer> updateCompletionService = new ExecutorCompletionService<>(queryExecutor);
for (final UpdateAction updateAction : updateActions) {
Callable<Integer> callable = () -> {
try {
return updateRecords(subject, updateAction);
} catch (CswException | FederationException | IngestException | SourceUnavailableException | UnsupportedQueryException | CatalogQueryException e) {
LOGGER.debug(UNABLE_TO_UPDATE_MSG, e);
throw new CswException(UNABLE_TO_UPDATE_MSG, CswConstants.TRANSACTION_FAILED, updateAction.getHandle());
}
};
Callable<Integer> updateCallable = subject.associateWith(callable);
updateCompletionService.submit(updateCallable);
}
for (int i = 0; i < updateActions.size(); i++) {
try {
Future<Integer> completedFuture = updateCompletionService.take();
try {
numUpdated += completedFuture.get();
} catch (ExecutionException | CancellationException e) {
LOGGER.debug("Error updating Metacard", e);
throw new CswException(UNABLE_TO_UPDATE_MSG, CswConstants.TRANSACTION_FAILED, "Update");
}
} catch (InterruptedException e) {
LOGGER.debug("Metacard update interrupted", e);
Thread.currentThread().interrupt();
break;
}
}
LOGGER.debug("{} records updated.", numUpdated);
response.getTransactionSummary().setTotalUpdated(BigInteger.valueOf(numUpdated));
int numDeleted = 0;
for (DeleteAction deleteAction : request.getDeleteActions()) {
try {
numDeleted += deleteRecords(deleteAction);
} catch (Exception e) {
LOGGER.debug(UNABLE_TO_DELETE_MSG, e);
throw new CswException(UNABLE_TO_DELETE_MSG, CswConstants.TRANSACTION_FAILED, deleteAction.getHandle());
}
}
LOGGER.debug("{} records deleted.", numDeleted);
response.getTransactionSummary().setTotalDeleted(BigInteger.valueOf(numDeleted));
return response;
}
use of org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction in project ddf by codice.
the class TransactionRequestConverter method marshal.
@Override
public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
if (o == null || !CswTransactionRequest.class.isAssignableFrom(o.getClass())) {
return;
}
CswTransactionRequest request = (CswTransactionRequest) o;
writer.addAttribute(CswConstants.SERVICE, request.getService());
writer.addAttribute(CswConstants.VERSION, request.getVersion());
writer.addAttribute(CswConstants.VERBOSE_RESPONSE, String.valueOf(request.isVerbose()));
writer.addAttribute(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.CSW_NAMESPACE_PREFIX, CswConstants.CSW_OUTPUT_SCHEMA);
writer.addAttribute(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.OGC_NAMESPACE_PREFIX, CswConstants.OGC_SCHEMA);
for (InsertAction insertAction : request.getInsertActions()) {
writer.startNode(CswConstants.CSW_TRANSACTION_INSERT_NODE);
writer.addAttribute(CswConstants.TYPE_NAME_PARAMETER, insertAction.getTypeName());
marshallingContext.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
marshallingContext.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, insertAction.getTypeName());
for (Metacard metacard : insertAction.getRecords()) {
marshallingContext.convertAnother(metacard, delegatingTransformer);
}
writer.endNode();
}
for (UpdateAction updateAction : request.getUpdateActions()) {
writer.startNode(CswConstants.CSW_TRANSACTION_UPDATE_NODE);
writer.addAttribute(CswConstants.TYPE_NAME_PARAMETER, updateAction.getTypeName());
marshallingContext.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.ID);
marshallingContext.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, updateAction.getTypeName());
marshallingContext.convertAnother(updateAction.getMetacard(), delegatingTransformer);
writer.endNode();
}
for (DeleteAction deleteAction : request.getDeleteActions()) {
writer.startNode(CswConstants.CSW_TRANSACTION_DELETE_NODE);
writer.addAttribute(CswConstants.TYPE_NAME_PARAMETER, deleteAction.getTypeName());
writer.startNode(CswConstants.CSW_CONSTRAINT);
writer.addAttribute(CswConstants.VERSION, CswConstants.CONSTRAINT_VERSION);
writer.startNode(CswConstants.CSW_CQL_TEXT);
writer.setValue(deleteAction.getConstraint().getCqlText());
writer.endNode();
writer.endNode();
writer.endNode();
}
}
use of org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction in project ddf by codice.
the class TransactionMessageBodyReaderTest method testReadInsertAndDeleteFrom.
@Test
public void testReadInsertAndDeleteFrom() throws IOException {
Converter mockConverter = mock(Converter.class);
when(mockConverter.canConvert(any(Class.class))).thenReturn(true);
when(mockConverter.unmarshal(any(HierarchicalStreamReader.class), any(UnmarshallingContext.class))).thenReturn(mock(Metacard.class));
TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mockConverter, CswQueryFactoryTest.getCswMetacardType(), registry);
CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(INSERT_AND_DELETE_REQUEST_XML, StandardCharsets.UTF_8));
assertThat(request, notNullValue());
assertThat(request.getDeleteActions().size(), is(1));
assertThat(request.getInsertActions().size(), is(1));
assertThat(request.getUpdateActions().size(), is(0));
DeleteAction deleteAction = request.getDeleteActions().get(0);
assertThat(deleteAction, notNullValue());
assertThat(deleteAction.getTypeName(), is(CswConstants.CSW_RECORD));
assertThat(deleteAction.getHandle(), is("something"));
assertThat(deleteAction.getConstraint(), notNullValue());
assertThat(deleteAction.getConstraint().getCqlText().trim(), is("title = 'foo'"));
InsertAction insertAction = request.getInsertActions().get(0);
assertThat(insertAction, notNullValue());
assertThat(insertAction.getRecords().size(), is(1));
assertThat(request.getService(), is(CswConstants.CSW));
assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
assertThat(request.isVerbose(), is(true));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction in project ddf by codice.
the class TransactionMessageBodyReaderTest method testReadInsertFrom.
@Test
public void testReadInsertFrom() throws Exception {
Converter mockConverter = mock(Converter.class);
when(mockConverter.canConvert(any(Class.class))).thenReturn(true);
when(mockConverter.unmarshal(any(HierarchicalStreamReader.class), any(UnmarshallingContext.class))).thenReturn(mock(Metacard.class));
TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mockConverter, CswQueryFactoryTest.getCswMetacardType(), registry);
CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(getInsertRequest(COUNT), StandardCharsets.UTF_8));
assertThat(request, notNullValue());
assertThat(request.getInsertActions().size(), is(1));
assertThat(request.getDeleteActions().size(), is(0));
assertThat(request.getUpdateActions().size(), is(0));
InsertAction insertAction = request.getInsertActions().get(0);
assertThat(insertAction, notNullValue());
assertThat(insertAction.getRecords().size(), is(COUNT));
assertThat(request.getService(), is(CswConstants.CSW));
assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
assertThat(request.isVerbose(), is(true));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction in project ddf by codice.
the class TransactionRequestConverterTest method testValidInsertMarshal.
@Test
public void testValidInsertMarshal() throws SAXException, IOException, XpathException {
CswTransactionRequest transactionRequest = new CswTransactionRequest();
MetacardImpl metacard = new MetacardImpl();
metacard.setId(METACARD_ID);
InsertAction insertAction = new InsertActionImpl(CswConstants.CSW_METACARD_TYPE_NAME, null, Arrays.asList(metacard));
transactionRequest.getInsertActions().add(insertAction);
transactionRequest.setService(CswConstants.CSW);
transactionRequest.setVerbose(true);
transactionRequest.setVersion(CswConstants.VERSION_2_0_2);
String xml = xStream.toXML(transactionRequest);
System.out.println("xml = " + xml);
System.out.println("EXPECTED_INSERT_XML = " + EXPECTED_INSERT_XML);
Diff diff = XMLUnit.compareXML(xml, EXPECTED_INSERT_XML);
assertThat(diff.similar(), is(true));
}
Aggregations