Search in sources :

Example 1 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction in project ddf by codice.

the class CswEndpoint method updateRecords.

private int updateRecords(Subject subject, UpdateAction updateAction) throws CswException, FederationException, IngestException, SourceUnavailableException, UnsupportedQueryException {
    updateAction = transformUpdateAction(updateAction);
    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, updateAction.getTypeName());
        queryRequest = queryFactory.updateQueryRequestTags(queryRequest, schemaTransformerManager.getTransformerSchemaForId(updateAction.getTypeName()));
        Map<String, Serializable> recordProperties = updateAction.getRecordProperties();
        Iterable<List<Result>> resultList = Iterables.partition(ResultIterable.resultIterable(framework, queryRequest), DEFAULT_BATCH);
        int batchCount = 1;
        int updatedCount = 0;
        CompletionService<Integer> completionService = new ExecutorCompletionService<>(queryExecutor);
        final UpdateAction callableUpdateAction = updateAction;
        for (List<Result> results : resultList) {
            final int batch = batchCount;
            Callable<Integer> callable = () -> {
                try {
                    return updateResultList(recordProperties, batch, results);
                } catch (IngestException | SourceUnavailableException e) {
                    LOGGER.debug(UNABLE_TO_UPDATE_MSG, e);
                    throw new CswException(UNABLE_TO_UPDATE_MSG, CswConstants.TRANSACTION_FAILED, callableUpdateAction.getHandle());
                }
            };
            batchCount++;
            Callable<Integer> updateCallable = subject.associateWith(callable);
            completionService.submit(updateCallable);
        }
        for (int i = 0; i < batchCount - 1; i++) {
            try {
                Future<Integer> completedFuture = completionService.take();
                try {
                    updatedCount += completedFuture.get();
                } catch (ExecutionException | CancellationException e) {
                    LOGGER.debug("Error updating", e);
                    throw new CswException(UNABLE_TO_UPDATE_MSG, CswConstants.TRANSACTION_FAILED, updateAction.getHandle());
                }
            } catch (InterruptedException e) {
                LOGGER.debug("Metacard update interrupted", e);
                Thread.currentThread().interrupt();
                break;
            }
        }
        return updatedCount;
    }
    return 0;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) QueryRequest(ddf.catalog.operation.QueryRequest) ResultIterable(ddf.catalog.util.impl.ResultIterable) UpdateRequest(ddf.catalog.operation.UpdateRequest) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Callable(java.util.concurrent.Callable) Result(ddf.catalog.data.Result) UpdateResponse(ddf.catalog.operation.UpdateResponse) Metacard(ddf.catalog.data.Metacard) CompletionService(java.util.concurrent.CompletionService) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) IngestException(ddf.catalog.source.IngestException) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction 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;
}
Also used : CreateResponse(ddf.catalog.operation.CreateResponse) CreateRequest(ddf.catalog.operation.CreateRequest) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) TransactionSummaryType(net.opengis.cat.csw.v_2_0_2.TransactionSummaryType) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) ExecutionException(java.util.concurrent.ExecutionException) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) Subject(org.apache.shiro.subject.Subject) CancellationException(java.util.concurrent.CancellationException) ResourceNotFoundException(ddf.catalog.resource.ResourceNotFoundException) MimeTypeParseException(javax.activation.MimeTypeParseException) ParseException(org.locationtech.jts.io.ParseException) IngestException(ddf.catalog.source.IngestException) IOException(java.io.IOException) FederationException(ddf.catalog.federation.FederationException) ExecutionException(java.util.concurrent.ExecutionException) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogQueryException(ddf.catalog.util.impl.CatalogQueryException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) ResourceNotSupportedException(ddf.catalog.resource.ResourceNotSupportedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BigInteger(java.math.BigInteger) Metacard(ddf.catalog.data.Metacard) CancellationException(java.util.concurrent.CancellationException) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction) CreateRequestImpl(ddf.catalog.operation.impl.CreateRequestImpl) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 3 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction 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.actions.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) InsertAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction)

Example 4 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction in project ddf by codice.

the class TransactionMessageBodyReaderTest method testReadUpdateByConstraintFrom.

@Test
public void testReadUpdateByConstraintFrom() throws IOException, ParseException {
    TransactionMessageBodyReader reader = new TransactionMessageBodyReader(mock(Converter.class), CswQueryFactoryTest.getCswMetacardType(), registry);
    CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(UPDATE_REQUEST_BY_CONSTRAINT_XML, StandardCharsets.UTF_8));
    assertThat(request, notNullValue());
    assertThat(request.getInsertActions().size(), is(0));
    assertThat(request.getDeleteActions().size(), is(0));
    assertThat(request.getUpdateActions().size(), is(1));
    UpdateAction updateAction = request.getUpdateActions().get(0);
    assertThat(updateAction, notNullValue());
    assertThat(updateAction.getMetacard(), nullValue());
    Map<String, Serializable> recordProperties = updateAction.getRecordProperties();
    assertThat(recordProperties, notNullValue());
    assertThat(recordProperties.size(), is(4));
    Serializable newSubjectValue = recordProperties.get(Topic.CATEGORY);
    assertThat(newSubjectValue, notNullValue());
    assertThat(newSubjectValue, is("Foo"));
    Serializable newDateValue = recordProperties.get("modified");
    assertThat(newDateValue, notNullValue());
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = simpleDateFormat.parse("2015-07-21");
    assertThat(newDateValue, is(date));
    Serializable newLocationValue = recordProperties.get("location");
    assertThat(newLocationValue, notNullValue());
    assertThat(newLocationValue, is("POLYGON ((1.0 2.0, 3.0 2.0, 3.0 4.0, 1.0 4.0, 1.0 2.0))"));
    Serializable newFormatValue = recordProperties.get("media.format");
    // No <Value> was specified in the request.
    assertThat(newFormatValue, nullValue());
    QueryConstraintType constraint = updateAction.getConstraint();
    assertThat(constraint, notNullValue());
    FilterType filter = constraint.getFilter();
    assertThat(filter, notNullValue());
    assertThat(filter.isSetComparisonOps(), is(true));
    assertThat(filter.isSetLogicOps(), is(false));
    assertThat(filter.isSetSpatialOps(), is(false));
    assertThat(request.getService(), is(CswConstants.CSW));
    assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
    assertThat(request.isVerbose(), is(false));
}
Also used : Serializable(java.io.Serializable) FilterType(net.opengis.filter.v_1_1_0.FilterType) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) Converter(com.thoughtworks.xstream.converters.Converter) CswRecordConverter(org.codice.ddf.spatial.ogc.csw.catalog.converter.CswRecordConverter) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Example 5 with UpdateAction

use of org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction in project ddf by codice.

the class TransactionMessageBodyReaderTest method testReadUpdateByNewRecordFrom.

@Test
public void testReadUpdateByNewRecordFrom() throws IOException, ParseException {
    TransactionMessageBodyReader reader = new TransactionMessageBodyReader(cswRecordConverter, CswQueryFactoryTest.getCswMetacardType(), registry);
    CswTransactionRequest request = reader.readFrom(CswTransactionRequest.class, null, null, null, null, IOUtils.toInputStream(UPDATE_REQUEST_BY_RECORD_XML, StandardCharsets.UTF_8));
    assertThat(request, notNullValue());
    assertThat(request.getInsertActions().size(), is(0));
    assertThat(request.getDeleteActions().size(), is(0));
    assertThat(request.getUpdateActions().size(), is(1));
    UpdateAction updateAction = request.getUpdateActions().get(0);
    assertThat(updateAction, notNullValue());
    assertThat(updateAction.getMetacard(), notNullValue());
    Metacard metacard = updateAction.getMetacard();
    assertThat(metacard.getId(), is("123"));
    assertThat(metacard.getTitle(), is("Aliquam fermentum purus quis arcu"));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = simpleDateFormat.parse("2008-08-10");
    assertThat(metacard.getModifiedDate(), is(date));
    assertThat(metacard.getLocation(), is("POLYGON ((1.0 2.0, 3.0 2.0, 3.0 4.0, 1.0 4.0, 1.0 2.0))"));
    assertThat(request.getService(), is(CswConstants.CSW));
    assertThat(request.getVersion(), is(CswConstants.VERSION_2_0_2));
    assertThat(request.isVerbose(), is(false));
}
Also used : Metacard(ddf.catalog.data.Metacard) UpdateAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Aggregations

UpdateAction (org.codice.ddf.spatial.ogc.csw.catalog.actions.UpdateAction)14 CswTransactionRequest (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest)11 Test (org.junit.Test)9 Metacard (ddf.catalog.data.Metacard)8 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)6 QueryConstraintType (net.opengis.cat.csw.v_2_0_2.QueryConstraintType)6 ArrayList (java.util.ArrayList)5 DeleteAction (org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction)5 InsertAction (org.codice.ddf.spatial.ogc.csw.catalog.actions.InsertAction)5 UpdateActionImpl (org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateActionImpl)5 Serializable (java.io.Serializable)4 UpdateRequest (ddf.catalog.operation.UpdateRequest)3 UpdateResponse (ddf.catalog.operation.UpdateResponse)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 TransactionResponseType (net.opengis.cat.csw.v_2_0_2.TransactionResponseType)3 TransactionSummaryType (net.opengis.cat.csw.v_2_0_2.TransactionSummaryType)3 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)3 Result (ddf.catalog.data.Result)2