Search in sources :

Example 1 with DeleteAction

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));
}
Also used : UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) Test(org.junit.Test)

Example 2 with DeleteAction

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));
}
Also used : UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) Diff(org.custommonkey.xmlunit.Diff) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Test(org.junit.Test)

Example 3 with DeleteAction

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;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) CreateRequest(ddf.catalog.operation.CreateRequest) CreateResponse(ddf.catalog.operation.CreateResponse) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) FederationException(ddf.catalog.federation.FederationException) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) IngestException(ddf.catalog.source.IngestException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 4 with DeleteAction

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);
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Metacard(ddf.catalog.data.Metacard) DeleteResponse(ddf.catalog.operation.DeleteResponse) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) QueryResponse(ddf.catalog.operation.QueryResponse) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) DeleteRequest(ddf.catalog.operation.DeleteRequest) Test(org.junit.Test)

Example 5 with DeleteAction

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();
    }
}
Also used : Metacard(ddf.catalog.data.Metacard) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction)

Aggregations

DeleteAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteAction)11 CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)10 Test (org.junit.Test)7 InsertAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction)6 DeleteType (net.opengis.cat.csw.v_2_0_2.DeleteType)5 UpdateAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction)5 Metacard (ddf.catalog.data.Metacard)4 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)4 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)4 Converter (com.thoughtworks.xstream.converters.Converter)3 ArrayList (java.util.ArrayList)3 TransactionResponseType (net.opengis.cat.csw.v_2_0_2.TransactionResponseType)3 CswRecordConverter (org.codice.ddf.spatial.ogc.csw.catalog.converter.CswRecordConverter)3 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)3 DeleteResponseImpl (ddf.catalog.operation.impl.DeleteResponseImpl)2 IngestException (ddf.catalog.source.IngestException)2 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)2 HashMap (java.util.HashMap)2 TransactionSummaryType (net.opengis.cat.csw.v_2_0_2.TransactionSummaryType)2 CswException (org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)2