Search in sources :

Example 1 with TransactionSummaryType

use of net.opengis.cat.csw.v_2_0_2.TransactionSummaryType 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 2 with TransactionSummaryType

use of net.opengis.cat.csw.v_2_0_2.TransactionSummaryType 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 3 with TransactionSummaryType

use of net.opengis.cat.csw.v_2_0_2.TransactionSummaryType in project ddf by codice.

the class TestCswEndpoint method testUpdateTransactionWithNewRecord.

@Test
public void testUpdateTransactionWithNewRecord() throws CswException, FederationException, IngestException, SourceUnavailableException, UnsupportedQueryException {
    List<Update> updatedMetacards = new ArrayList<>();
    updatedMetacards.add(new UpdateImpl(new MetacardImpl(), new MetacardImpl()));
    UpdateResponse updateResponse = new UpdateResponseImpl(null, null, updatedMetacards);
    doReturn(updateResponse).when(catalogFramework).update(any(UpdateRequest.class));
    MetacardImpl updatedMetacard = new MetacardImpl();
    updatedMetacard.setId("123");
    UpdateAction updateAction = new UpdateAction(updatedMetacard, CswConstants.CSW_RECORD, "");
    CswTransactionRequest transactionRequest = new CswTransactionRequest();
    transactionRequest.getUpdateActions().add(updateAction);
    transactionRequest.setVersion(CswConstants.VERSION_2_0_2);
    transactionRequest.setService(CswConstants.CSW);
    transactionRequest.setVerbose(false);
    TransactionResponseType response = csw.transaction(transactionRequest);
    assertThat(response, notNullValue());
    TransactionSummaryType summary = response.getTransactionSummary();
    assertThat(summary, notNullValue());
    assertThat(summary.getTotalDeleted().intValue(), is(0));
    assertThat(summary.getTotalInserted().intValue(), is(0));
    assertThat(summary.getTotalUpdated().intValue(), is(1));
    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);
    ArgumentCaptor<UpdateRequest> updateRequestArgumentCaptor = ArgumentCaptor.forClass(UpdateRequest.class);
    verify(catalogFramework, times(1)).update(updateRequestArgumentCaptor.capture());
    UpdateRequest actualUpdateRequest = updateRequestArgumentCaptor.getValue();
    assertThat(actualUpdateRequest.getUpdates().size(), is(1));
    assertThat(actualUpdateRequest.getUpdates().get(0).getValue().getId(), is("123"));
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) UpdateRequest(ddf.catalog.operation.UpdateRequest) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) ArrayList(java.util.ArrayList) Update(ddf.catalog.operation.Update) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) UpdateResponse(ddf.catalog.operation.UpdateResponse) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) Test(org.junit.Test)

Example 4 with TransactionSummaryType

use of net.opengis.cat.csw.v_2_0_2.TransactionSummaryType in project ddf by codice.

the class TestCswEndpoint method testIngestTransaction.

@Test
public void testIngestTransaction() throws CswException, SourceUnavailableException, FederationException, IngestException {
    CswTransactionRequest request = new CswTransactionRequest();
    request.getInsertActions().add(new InsertAction(CswConstants.CSW_TYPE, null, Arrays.asList(new MetacardImpl())));
    TransactionResponseType response = csw.transaction(request);
    assertThat(response, notNullValue());
    assertThat(response.getInsertResult().isEmpty(), is(true));
    assertThat(response.getTransactionSummary(), notNullValue());
    TransactionSummaryType summary = response.getTransactionSummary();
    assertThat(summary.getTotalDeleted().intValue(), is(0));
    assertThat(summary.getTotalUpdated().intValue(), is(0));
    assertThat(summary.getTotalInserted().intValue(), is(1));
    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 : CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) Test(org.junit.Test)

Example 5 with TransactionSummaryType

use of net.opengis.cat.csw.v_2_0_2.TransactionSummaryType in project ddf by codice.

the class TestCswEndpoint method testUpdateTransactionWithConstraint.

@Test
public void testUpdateTransactionWithConstraint() throws CswException, FederationException, IngestException, SourceUnavailableException, UnsupportedQueryException {
    List<Result> results = new ArrayList<>();
    MetacardImpl firstResult = new MetacardImpl();
    firstResult.setId("123");
    firstResult.setTitle("Title one");
    firstResult.setAttribute("subject", "Subject one");
    results.add(new ResultImpl(firstResult));
    MetacardImpl secondResult = new MetacardImpl();
    secondResult.setId("789");
    secondResult.setTitle("Title two");
    secondResult.setAttribute("subject", "Subject two");
    results.add(new ResultImpl(secondResult));
    QueryResponse queryResponse = new QueryResponseImpl(null, results, results.size());
    doReturn(queryResponse).when(catalogFramework).query(any(QueryRequest.class));
    List<Update> updatedMetacards = new ArrayList<>();
    updatedMetacards.add(new UpdateImpl(new MetacardImpl(), new MetacardImpl()));
    updatedMetacards.add(new UpdateImpl(new MetacardImpl(), new MetacardImpl()));
    UpdateResponse updateResponse = new UpdateResponseImpl(null, null, updatedMetacards);
    doReturn(updateResponse).when(catalogFramework).update(any(UpdateRequest.class));
    Map<String, Serializable> recordProperties = new HashMap<>();
    recordProperties.put("title", "foo");
    recordProperties.put("subject", "bar");
    QueryConstraintType constraint = new QueryConstraintType();
    constraint.setCqlText("title = 'fake'");
    UpdateAction updateAction = new UpdateAction(recordProperties, CswConstants.CSW_RECORD, "", constraint, DefaultCswRecordMap.getDefaultCswRecordMap().getPrefixToUriMapping());
    CswTransactionRequest updateRequest = new CswTransactionRequest();
    updateRequest.getUpdateActions().add(updateAction);
    updateRequest.setVersion(CswConstants.VERSION_2_0_2);
    updateRequest.setService(CswConstants.CSW);
    updateRequest.setVerbose(false);
    TransactionResponseType response = csw.transaction(updateRequest);
    assertThat(response, notNullValue());
    TransactionSummaryType summary = response.getTransactionSummary();
    assertThat(summary, notNullValue());
    assertThat(summary.getTotalDeleted().intValue(), is(0));
    assertThat(summary.getTotalInserted().intValue(), is(0));
    assertThat(summary.getTotalUpdated().intValue(), is(2));
    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);
    ArgumentCaptor<UpdateRequest> updateRequestArgumentCaptor = ArgumentCaptor.forClass(UpdateRequest.class);
    verify(catalogFramework, times(1)).update(updateRequestArgumentCaptor.capture());
    UpdateRequest actualUpdateRequest = updateRequestArgumentCaptor.getValue();
    List<Map.Entry<Serializable, Metacard>> updates = actualUpdateRequest.getUpdates();
    assertThat(updates.size(), is(2));
    Metacard firstUpdate = updates.get(0).getValue();
    assertThat(firstUpdate.getId(), is("123"));
    assertThat(firstUpdate.getTitle(), is("foo"));
    assertThat(firstUpdate.getAttribute("subject").getValue(), is("bar"));
    Metacard secondUpdate = updates.get(1).getValue();
    assertThat(secondUpdate.getId(), is("789"));
    assertThat(secondUpdate.getTitle(), is("foo"));
    assertThat(secondUpdate.getAttribute("subject").getValue(), is("bar"));
}
Also used : UpdateImpl(ddf.catalog.operation.impl.UpdateImpl) Serializable(java.io.Serializable) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) Update(ddf.catalog.operation.Update) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Result(ddf.catalog.data.Result) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) UpdateResponse(ddf.catalog.operation.UpdateResponse) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) QueryRequest(ddf.catalog.operation.QueryRequest) UpdateRequest(ddf.catalog.operation.UpdateRequest) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) Metacard(ddf.catalog.data.Metacard) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) QueryResponse(ddf.catalog.operation.QueryResponse) Test(org.junit.Test)

Aggregations

TransactionResponseType (net.opengis.cat.csw.v_2_0_2.TransactionResponseType)8 TransactionSummaryType (net.opengis.cat.csw.v_2_0_2.TransactionSummaryType)8 Test (org.junit.Test)7 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)6 CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)6 ResultImpl (ddf.catalog.data.impl.ResultImpl)3 ArrayList (java.util.ArrayList)3 InsertAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.InsertAction)3 UpdateAction (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateAction)3 Metacard (ddf.catalog.data.Metacard)2 Result (ddf.catalog.data.Result)2 QueryRequest (ddf.catalog.operation.QueryRequest)2 QueryResponse (ddf.catalog.operation.QueryResponse)2 Update (ddf.catalog.operation.Update)2 UpdateRequest (ddf.catalog.operation.UpdateRequest)2 UpdateResponse (ddf.catalog.operation.UpdateResponse)2 OperationTransactionImpl (ddf.catalog.operation.impl.OperationTransactionImpl)2 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)2 UpdateImpl (ddf.catalog.operation.impl.UpdateImpl)2 UpdateResponseImpl (ddf.catalog.operation.impl.UpdateResponseImpl)2