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));
}
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;
}
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;
}
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()));
}
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);
}
Aggregations