use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction in project ddf by codice.
the class TestTransactionRequestConverter method testUnmarshalDelete.
@Test
public void testUnmarshalDelete() throws Exception {
String deleteRequest = IOUtils.toString(TestTransactionRequestConverter.class.getResourceAsStream("/deleteRequest.xml"));
CswTransactionRequest request = (CswTransactionRequest) xStream.fromXML(deleteRequest);
assertThat(request.getDeleteActions(), hasSize(1));
assertThat(request.getUpdateActions(), emptyCollectionOf(UpdateAction.class));
assertThat(request.getInsertActions(), emptyCollectionOf(InsertAction.class));
DeleteAction action = request.getDeleteActions().get(0);
assertThat(action.getTypeName(), is(CswConstants.CSW_RECORD));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction in project ddf by codice.
the class TestTransactionRequestConverter method testMultipleOperations.
@Test
public void testMultipleOperations() throws Exception {
CswTransactionRequest transactionRequest = new CswTransactionRequest();
MetacardImpl metacard = new MetacardImpl();
metacard.setId(METACARD_ID);
transactionRequest.setService(CswConstants.CSW);
transactionRequest.setVerbose(true);
transactionRequest.setVersion(CswConstants.VERSION_2_0_2);
InsertAction insertAction = new InsertAction(CswConstants.CSW_METACARD_TYPE_NAME, null, Arrays.asList(metacard));
transactionRequest.getInsertActions().add(insertAction);
UpdateAction updateAction = new UpdateAction(metacard, CswConstants.CSW_METACARD_TYPE_NAME, null);
transactionRequest.getUpdateActions().add(updateAction);
DeleteType deleteType = new DeleteType();
QueryConstraintType queryConstraintType = new QueryConstraintType();
queryConstraintType.setCqlText("identifier = " + METACARD_ID);
deleteType.setConstraint(queryConstraintType);
DeleteAction deleteAction = new DeleteAction(deleteType, null);
transactionRequest.getDeleteActions().add(deleteAction);
String xml = xStream.toXML(transactionRequest);
Diff diff = XMLUnit.compareXML(xml, EXPECTED_MULTI_OP_XML);
assertThat(diff.similar(), is(true));
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction 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;
for (InsertAction insertAction : request.getInsertActions()) {
CreateRequest createRequest = new CreateRequestImpl(insertAction.getRecords());
try {
CreateResponse createResponse = framework.create(createRequest);
if (request.isVerbose()) {
response.getInsertResult().add(getInsertResultFromResponse(createResponse));
}
numInserted += createResponse.getCreatedMetacards().size();
} catch (IngestException | SourceUnavailableException e) {
throw new CswException("Unable to insert record(s).", CswConstants.TRANSACTION_FAILED, insertAction.getHandle());
}
}
LOGGER.debug("{} records inserted.", numInserted);
response.getTransactionSummary().setTotalInserted(BigInteger.valueOf(numInserted));
int numUpdated = 0;
for (UpdateAction updateAction : request.getUpdateActions()) {
try {
numUpdated += updateRecords(updateAction);
} catch (CswException | FederationException | IngestException | SourceUnavailableException | UnsupportedQueryException e) {
throw new CswException("Unable to update record(s).", CswConstants.TRANSACTION_FAILED, updateAction.getHandle());
}
}
LOGGER.debug("{} records updated.", numUpdated);
response.getTransactionSummary().setTotalUpdated(BigInteger.valueOf(numUpdated));
int numDeleted = 0;
for (DeleteAction deleteAction : request.getDeleteActions()) {
try {
numDeleted += deleteRecords(deleteAction);
} catch (CswException | FederationException | IngestException | SourceUnavailableException | UnsupportedQueryException e) {
throw new CswException("Unable to delete record(s).", 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.common.transaction.DeleteAction in project ddf by codice.
the class TestCswEndpoint method testDeleteTransaction.
@Test
public void testDeleteTransaction() throws CswException, UnsupportedQueryException, SourceUnavailableException, FederationException, IngestException {
DeleteType deleteType = mock(DeleteType.class);
doReturn(CswConstants.CSW_RECORD).when(deleteType).getTypeName();
doReturn("").when(deleteType).getHandle();
QueryConstraintType queryConstraintType = new QueryConstraintType();
queryConstraintType.setCqlText("title = \"foo\"");
doReturn(queryConstraintType).when(deleteType).getConstraint();
List<Result> results = new ArrayList<>();
results.add(new ResultImpl(new MetacardImpl()));
results.add(new ResultImpl(new MetacardImpl()));
QueryResponse queryResponse = new QueryResponseImpl(null, results, results.size());
doReturn(queryResponse).when(catalogFramework).query(any(QueryRequest.class));
List<Metacard> deletedMetacards = new ArrayList<>();
deletedMetacards.add(new MetacardImpl());
deletedMetacards.add(new MetacardImpl());
DeleteResponse deleteResponse = new DeleteResponseImpl(null, null, deletedMetacards);
doReturn(deleteResponse).when(catalogFramework).delete(any(DeleteRequest.class));
DeleteAction deleteAction = new DeleteAction(deleteType, DefaultCswRecordMap.getDefaultCswRecordMap().getPrefixToUriMapping());
CswTransactionRequest deleteRequest = new CswTransactionRequest();
deleteRequest.getDeleteActions().add(deleteAction);
deleteRequest.setVersion(CswConstants.VERSION_2_0_2);
deleteRequest.setService(CswConstants.CSW);
deleteRequest.setVerbose(false);
TransactionResponseType response = csw.transaction(deleteRequest);
assertThat(response, notNullValue());
TransactionSummaryType summary = response.getTransactionSummary();
assertThat(summary, notNullValue());
assertThat(summary.getTotalDeleted().intValue(), is(2));
assertThat(summary.getTotalInserted().intValue(), is(0));
assertThat(summary.getTotalUpdated().intValue(), is(0));
verifyMarshalResponse(response, "net.opengis.cat.csw.v_2_0_2:net.opengis.filter.v_1_1_0:net.opengis.gml.v_3_1_1", cswQnameOutPutSchema);
}
use of org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction 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();
}
}
Aggregations