Search in sources :

Example 6 with OperationTransaction

use of ddf.catalog.operation.OperationTransaction in project ddf by codice.

the class IdentificationPluginTest method testUpdateMetacardWithModifiedTimeSameAsCurrentMetacard.

@Test
public void testUpdateMetacardWithModifiedTimeSameAsCurrentMetacard() throws Exception {
    String xml = convert("/registry-both-extid.xml");
    sampleData.setAttribute(Metacard.METADATA, xml);
    sampleData.setAttribute(RegistryObjectMetacardType.REMOTE_REGISTRY_ID, "remoteRegistryId");
    OperationTransaction operationTransaction = new OperationTransactionImpl(null, Collections.singletonList(sampleData));
    Map<String, Serializable> properties = new HashMap<>();
    properties.put(Constants.OPERATION_TRANSACTION_KEY, operationTransaction);
    List<Map.Entry<Serializable, Metacard>> updatedEntries = new ArrayList<>();
    Metacard updateMetacard = sampleData;
    updatedEntries.add(new AbstractMap.SimpleEntry<>(updateMetacard.getId(), updateMetacard));
    UpdateRequest updateRequest = new UpdateRequestImpl(updatedEntries, Metacard.ID, properties);
    UpdateRequest processedUpdateRequest = identificationPlugin.process(updateRequest);
    assertThat(processedUpdateRequest.getUpdates().size(), is(1));
}
Also used : Serializable(java.io.Serializable) OperationTransactionImpl(ddf.catalog.operation.impl.OperationTransactionImpl) HashMap(java.util.HashMap) UpdateRequest(ddf.catalog.operation.UpdateRequest) ArrayList(java.util.ArrayList) OperationTransaction(ddf.catalog.operation.OperationTransaction) AbstractMap(java.util.AbstractMap) Metacard(ddf.catalog.data.Metacard) UpdateRequestImpl(ddf.catalog.operation.impl.UpdateRequestImpl) Test(org.junit.Test)

Example 7 with OperationTransaction

use of ddf.catalog.operation.OperationTransaction in project ddf by codice.

the class IdentificationPlugin method process.

/**
     * For registry metacards verifies the update should take place by checking that the update
     * metacard is at least as up to date as the existing one. Also updates the tags, identifiers,
     * and transient attributes of the updated metacard.
     *
     * @param input the {@link UpdateRequest} to process
     * @return
     * @throws PluginExecutionException
     * @throws StopProcessingException
     */
@Override
public UpdateRequest process(UpdateRequest input) throws PluginExecutionException, StopProcessingException {
    if (!Requests.isLocal(input)) {
        return input;
    }
    OperationTransaction operationTransaction = (OperationTransaction) input.getProperties().get(Constants.OPERATION_TRANSACTION_KEY);
    List<Metacard> previousMetacards = operationTransaction.getPreviousStateMetacards();
    Map<String, Metacard> previousMetacardsMap = previousMetacards.stream().filter(e -> RegistryUtility.isRegistryMetacard(e) || RegistryUtility.isInternalRegistryMetacard(e)).collect(Collectors.toMap(RegistryUtility::getRegistryId, Function.identity()));
    List<Map.Entry<Serializable, Metacard>> entriesToRemove = new ArrayList<>();
    List<Map.Entry<Serializable, Metacard>> registryUpdates = input.getUpdates().stream().filter(e -> RegistryUtility.isRegistryMetacard(e.getValue())).collect(Collectors.toList());
    for (Map.Entry<Serializable, Metacard> entry : registryUpdates) {
        Metacard updateMetacard = entry.getValue();
        Metacard existingMetacard = previousMetacardsMap.get(RegistryUtility.getRegistryId(updateMetacard));
        if (existingMetacard == null) {
            continue;
        }
        if (updateMetacard.getMetadata() != null && !updateMetacard.getModifiedDate().before(existingMetacard.getModifiedDate())) {
            updateMetacard.setAttribute(new AttributeImpl(Metacard.ID, existingMetacard.getId()));
            copyTransientAttributes(updateMetacard, existingMetacard);
            updateTags(updateMetacard);
            if (isInternal(updateMetacard)) {
                updateMetacard.setAttribute(existingMetacard.getAttribute(RegistryObjectMetacardType.REMOTE_METACARD_ID));
                updateMetacard.setAttribute(existingMetacard.getAttribute(RegistryObjectMetacardType.REMOTE_REGISTRY_ID));
            }
            updateIdentifiers(updateMetacard, false);
        } else {
            entriesToRemove.add(entry);
        }
    }
    input.getUpdates().removeAll(entriesToRemove);
    return input;
}
Also used : PreIngestPlugin(ddf.catalog.plugin.PreIngestPlugin) CreateRequest(ddf.catalog.operation.CreateRequest) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) OperationTransaction(ddf.catalog.operation.OperationTransaction) Function(java.util.function.Function) ArrayList(java.util.ArrayList) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) MetacardMarshaller(org.codice.ddf.registry.schemabindings.helper.MetacardMarshaller) Requests(ddf.catalog.util.impl.Requests) Constants(ddf.catalog.Constants) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map) UpdateRequest(ddf.catalog.operation.UpdateRequest) RegistryConstants(org.codice.ddf.registry.common.RegistryConstants) RegistryPackageType(oasis.names.tc.ebxml_regrep.xsd.rim._3.RegistryPackageType) RegistryUtility(org.codice.ddf.registry.common.metacard.RegistryUtility) StopProcessingException(ddf.catalog.plugin.StopProcessingException) ParserException(org.codice.ddf.parser.ParserException) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) DeleteRequest(ddf.catalog.operation.DeleteRequest) List(java.util.List) Attribute(ddf.catalog.data.Attribute) ExternalIdentifierType(oasis.names.tc.ebxml_regrep.xsd.rim._3.ExternalIdentifierType) RegistryObjectMetacardType(org.codice.ddf.registry.common.metacard.RegistryObjectMetacardType) UuidGenerator(org.codice.ddf.platform.util.uuidgenerator.UuidGenerator) Serializable(java.io.Serializable) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) OperationTransaction(ddf.catalog.operation.OperationTransaction) Metacard(ddf.catalog.data.Metacard) Map(java.util.Map)

Example 8 with OperationTransaction

use of ddf.catalog.operation.OperationTransaction in project ddf by codice.

the class ResourceUriPolicyTest method getMockProperties.

private Map<String, Serializable> getMockProperties(String previousResourceUri) throws URISyntaxException {
    Metacard previousMetacard = getMockMetacard(previousResourceUri);
    OperationTransaction trx = mock(OperationTransaction.class);
    when(trx.getPreviousStateMetacards()).thenReturn(Collections.singletonList(previousMetacard));
    Map<String, Serializable> properties = new HashMap<>();
    properties.put(OPERATION_TRANSACTION_KEY, trx);
    properties.put(LOCAL_DESTINATION_KEY, true);
    return properties;
}
Also used : OperationTransaction(ddf.catalog.operation.OperationTransaction) Metacard(ddf.catalog.data.Metacard) Serializable(java.io.Serializable) HashMap(java.util.HashMap)

Example 9 with OperationTransaction

use of ddf.catalog.operation.OperationTransaction in project ddf by codice.

the class AbstractCswStore method delete.

@Override
public DeleteResponse delete(DeleteRequest deleteRequest) throws IngestException {
    Map<String, Serializable> properties = new HashMap<>();
    validateOperation();
    Subject subject = (Subject) deleteRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
    Csw csw = factory.getClientForSubject(subject);
    CswTransactionRequest transactionRequest = getTransactionRequest();
    OperationTransaction opTrans = (OperationTransaction) deleteRequest.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY);
    String typeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
    if (typeName == null) {
        typeName = CswConstants.CSW_RECORD;
    }
    for (Serializable itemToDelete : deleteRequest.getAttributeValues()) {
        try {
            DeleteType deleteType = new DeleteType();
            deleteType.setTypeName(typeName);
            QueryConstraintType queryConstraintType = new QueryConstraintType();
            Filter filter;
            FilterType filterType;
            filter = filterBuilder.attribute(deleteRequest.getAttributeName()).is().equalTo().text(itemToDelete.toString());
            filterType = filterAdapter.adapt(filter, cswFilterDelegate);
            queryConstraintType.setCqlText(CswCqlTextFilter.getInstance().getCqlText(filterType));
            deleteType.setConstraint(queryConstraintType);
            DeleteAction deleteAction = new DeleteActionImpl(deleteType, DefaultCswRecordMap.getPrefixToUriMapping());
            transactionRequest.getDeleteActions().add(deleteAction);
        } catch (UnsupportedQueryException e) {
            throw new IngestException("Unsupported Query.", e);
        }
    }
    try {
        TransactionResponseType response = csw.transaction(transactionRequest);
        if (response.getTransactionSummary().getTotalDeleted().intValue() != deleteRequest.getAttributeValues().size()) {
            throw new IngestException("Csw Transaction Failed. Number of metacards deleted did not match number requested.");
        }
    } catch (CswException e) {
        throw new IngestException("Csw Transaction Failed", e);
    }
    return new DeleteResponseImpl(deleteRequest, properties, new ArrayList(opTrans.getPreviousStateMetacards()));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ArrayList(java.util.ArrayList) Subject(ddf.security.Subject) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) OperationTransaction(ddf.catalog.operation.OperationTransaction) FilterType(net.opengis.filter.v_1_1_0.FilterType) DeleteResponseImpl(ddf.catalog.operation.impl.DeleteResponseImpl) Filter(org.opengis.filter.Filter) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) DeleteAction(org.codice.ddf.spatial.ogc.csw.catalog.actions.DeleteAction) DeleteType(net.opengis.cat.csw.v_2_0_2.DeleteType) IngestException(ddf.catalog.source.IngestException) DeleteActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.DeleteActionImpl)

Example 10 with OperationTransaction

use of ddf.catalog.operation.OperationTransaction in project ddf by codice.

the class AbstractCswStore method update.

@Override
public UpdateResponse update(UpdateRequest updateRequest) throws IngestException {
    Map<String, Serializable> properties = new HashMap<>();
    validateOperation();
    Subject subject = (Subject) updateRequest.getPropertyValue(SecurityConstants.SECURITY_SUBJECT);
    Csw csw = factory.getClientForSubject(subject);
    CswTransactionRequest transactionRequest = getTransactionRequest();
    OperationTransaction opTrans = (OperationTransaction) updateRequest.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY);
    String insertTypeName = schemaTransformerManager.getTransformerIdForSchema(cswSourceConfiguration.getOutputSchema());
    HashSet<ProcessingDetails> errors = new HashSet<>();
    if (insertTypeName == null) {
        insertTypeName = CswConstants.CSW_RECORD;
    }
    ArrayList<Metacard> updatedMetacards = new ArrayList<>(updateRequest.getUpdates().size());
    ArrayList<Filter> updatedMetacardFilters = new ArrayList<>(updateRequest.getUpdates().size());
    for (Map.Entry<Serializable, Metacard> update : updateRequest.getUpdates()) {
        Metacard metacard = update.getValue();
        properties.put(metacard.getId(), metacard);
        updatedMetacardFilters.add(filterBuilder.attribute(updateRequest.getAttributeName()).is().equalTo().text(update.getKey().toString()));
        transactionRequest.getUpdateActions().add(new UpdateActionImpl(metacard, insertTypeName, null));
    }
    try {
        TransactionResponseType response = csw.transaction(transactionRequest);
        if (response.getTransactionSummary().getTotalUpdated().longValue() != updateRequest.getUpdates().size()) {
            errors.add(new ProcessingDetailsImpl(this.getId(), null, "One or more updates failed"));
        }
    } catch (CswException e) {
        throw new IngestException("Csw Transaction Failed.", e);
    }
    try {
        updatedMetacards.addAll(transactionQuery(updatedMetacardFilters, subject));
    } catch (UnsupportedQueryException e) {
        errors.add(new ProcessingDetailsImpl(this.getId(), e, "Failed to retrieve updated metacards"));
    }
    return new UpdateResponseImpl(updateRequest, properties, updatedMetacards, new ArrayList(opTrans.getPreviousStateMetacards()), errors);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) ArrayList(java.util.ArrayList) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) ProcessingDetailsImpl(ddf.catalog.operation.impl.ProcessingDetailsImpl) TransactionResponseType(net.opengis.cat.csw.v_2_0_2.TransactionResponseType) OperationTransaction(ddf.catalog.operation.OperationTransaction) ProcessingDetails(ddf.catalog.operation.ProcessingDetails) UpdateResponseImpl(ddf.catalog.operation.impl.UpdateResponseImpl) IngestException(ddf.catalog.source.IngestException) HashSet(java.util.HashSet) UpdateActionImpl(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.UpdateActionImpl) Csw(org.codice.ddf.spatial.ogc.csw.catalog.common.Csw) Subject(ddf.security.Subject) Metacard(ddf.catalog.data.Metacard) Filter(org.opengis.filter.Filter) CswTransactionRequest(org.codice.ddf.spatial.ogc.csw.catalog.common.transaction.CswTransactionRequest) Map(java.util.Map) DefaultCswRecordMap(org.codice.ddf.spatial.ogc.csw.catalog.common.converter.DefaultCswRecordMap) HashMap(java.util.HashMap)

Aggregations

OperationTransaction (ddf.catalog.operation.OperationTransaction)10 Metacard (ddf.catalog.data.Metacard)8 Serializable (java.io.Serializable)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 UpdateRequest (ddf.catalog.operation.UpdateRequest)4 Map (java.util.Map)4 OperationTransactionImpl (ddf.catalog.operation.impl.OperationTransactionImpl)3 UpdateRequestImpl (ddf.catalog.operation.impl.UpdateRequestImpl)3 IngestException (ddf.catalog.source.IngestException)3 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)3 List (java.util.List)3 Constants (ddf.catalog.Constants)2 Attribute (ddf.catalog.data.Attribute)2 Result (ddf.catalog.data.Result)2 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)2 CreateRequest (ddf.catalog.operation.CreateRequest)2 DeleteRequest (ddf.catalog.operation.DeleteRequest)2 Query (ddf.catalog.operation.Query)2 StopProcessingException (ddf.catalog.plugin.StopProcessingException)2