use of ddf.catalog.core.versioning.MetacardVersion.Action in project ddf by codice.
the class Historian method version.
/**
* Versions updated {@link Metacard}s and {@link ContentItem}s.
*
* @param streamUpdateRequest Needed to pass {@link ddf.catalog.core.versioning.MetacardVersion#SKIP_VERSIONING}
* flag into downstream update
* @param updateStorageResponse Versions this response's updated items
* @return the update response originally passed in
* @throws UnsupportedQueryException
* @throws SourceUnavailableException
* @throws IngestException
*/
public UpdateStorageResponse version(UpdateStorageRequest streamUpdateRequest, UpdateStorageResponse updateStorageResponse, UpdateResponse updateResponse) throws UnsupportedQueryException, SourceUnavailableException, IngestException {
if (doSkip(updateStorageResponse)) {
return updateStorageResponse;
}
setSkipFlag(streamUpdateRequest);
setSkipFlag(updateStorageResponse);
List<Metacard> updatedMetacards = updateStorageResponse.getUpdatedContentItems().stream().filter(ci -> StringUtils.isBlank(ci.getQualifier())).map(ContentItem::getMetacard).filter(Objects::nonNull).filter(isNotVersionNorDeleted).collect(Collectors.toList());
Map<String, Metacard> originalMetacards = query(forIds(updatedMetacards.stream().map(Metacard::getId).collect(Collectors.toList())));
Collection<ReadStorageRequest> ids = getReadStorageRequests(updatedMetacards);
Map<String, List<ContentItem>> content = getContent(ids);
Function<String, Action> getAction = (id) -> content.containsKey(id) ? Action.VERSIONED_CONTENT : Action.VERSIONED;
Map<String, Metacard> versionMetacards = getVersionMetacards(originalMetacards.values(), getAction, (Subject) updateResponse.getProperties().get(SecurityConstants.SECURITY_SUBJECT));
CreateStorageResponse createStorageResponse = versionContentItems(content, versionMetacards);
if (createStorageResponse == null) {
LOGGER.debug("Could not version content items.");
return updateStorageResponse;
}
setResourceUriForContent(/*mutable*/
versionMetacards, createStorageResponse);
storeVersionMetacards(versionMetacards);
return updateStorageResponse;
}
use of ddf.catalog.core.versioning.MetacardVersion.Action in project ddf by codice.
the class Historian method version.
/**
* Versions deleted {@link Metacard}s.
*
* @param deleteResponse Versions this responses deleted metacards
*/
public DeleteResponse version(DeleteResponse deleteResponse) throws SourceUnavailableException, IngestException {
if (doSkip(deleteResponse)) {
return deleteResponse;
}
setSkipFlag(deleteResponse);
List<Metacard> deletedMetacards = deleteResponse.getDeletedMetacards().stream().filter(isNotVersionNorDeleted).collect(Collectors.toList());
// [ContentItem.getId: content items]
Map<String, List<ContentItem>> contentItems = getContent(getReadStorageRequests(deletedMetacards));
Function<String, Action> getAction = (id) -> contentItems.containsKey(id) ? Action.DELETED_CONTENT : Action.DELETED;
// [MetacardVersion.VERSION_OF_ID: versioned metacard]
Map<String, Metacard> versionedMap = getVersionMetacards(deletedMetacards, getAction, (Subject) deleteResponse.getRequest().getProperties().get(SecurityConstants.SECURITY_SUBJECT));
CreateStorageResponse createStorageResponse = versionContentItems(contentItems, versionedMap);
if (createStorageResponse != null) {
setResourceUriForContent(/*Mutable*/
versionedMap, createStorageResponse);
}
executeAsSystem(() -> catalogProvider().create(new CreateRequestImpl(new ArrayList<>(versionedMap.values()))));
String emailAddress = SubjectUtils.getEmailAddress((Subject) deleteResponse.getProperties().get(SecurityConstants.SECURITY_SUBJECT));
List<Metacard> deletionMetacards = versionedMap.entrySet().stream().map(s -> new DeletedMetacardImpl(uuidGenerator.generateUuid(), s.getKey(), emailAddress, s.getValue().getId(), MetacardVersionImpl.toMetacard(s.getValue(), metacardTypes))).collect(Collectors.toList());
executeAsSystem(() -> catalogProvider().create(new CreateRequestImpl(deletionMetacards, new HashMap<>())));
return deleteResponse;
}
use of ddf.catalog.core.versioning.MetacardVersion.Action in project ddf by codice.
the class MetacardApplication method revertMetacard.
private void revertMetacard(Metacard versionMetacard, String id, boolean alreadyCreated) throws SourceUnavailableException, IngestException, FederationException, UnsupportedQueryException {
LOGGER.trace("Reverting metacard [{}] to version [{}]", id, versionMetacard.getId());
Metacard revertMetacard = MetacardVersionImpl.toMetacard(versionMetacard, types);
Action action = Action.fromKey((String) versionMetacard.getAttribute(MetacardVersion.ACTION).getValue());
if (DELETE_ACTIONS.contains(action)) {
attemptDeleteDeletedMetacard(id);
if (!alreadyCreated) {
catalogFramework.create(new CreateRequestImpl(revertMetacard));
}
} else {
tryUpdate(4, () -> {
catalogFramework.update(new UpdateRequestImpl(id, revertMetacard));
return true;
});
}
}
use of ddf.catalog.core.versioning.MetacardVersion.Action in project ddf by codice.
the class MetacardApplication method revertContentandMetacard.
private void revertContentandMetacard(Metacard latestContent, Metacard versionMetacard, String id) throws SourceUnavailableException, IngestException, ResourceNotFoundException, IOException, ResourceNotSupportedException, FederationException, UnsupportedQueryException {
LOGGER.trace("Reverting content and metacard for metacard [{}]. \nLatest content: [{}] \nVersion metacard: [{}]", id, latestContent.getId(), versionMetacard.getId());
Map<String, Serializable> properties = new HashMap<>();
properties.put("no-default-tags", true);
ResourceResponse latestResource = catalogFramework.getLocalResource(new ResourceRequestById(latestContent.getId(), properties));
ContentItemImpl contentItem = new ContentItemImpl(id, new ByteSourceWrapper(() -> latestResource.getResource().getInputStream()), latestResource.getResource().getMimeTypeValue(), latestResource.getResource().getName(), latestResource.getResource().getSize(), MetacardVersionImpl.toMetacard(versionMetacard, types));
// Try to delete the "deleted metacard" marker first.
boolean alreadyCreated = false;
Action action = Action.fromKey((String) versionMetacard.getAttribute(MetacardVersion.ACTION).getValue());
if (DELETE_ACTIONS.contains(action)) {
alreadyCreated = true;
catalogFramework.create(new CreateStorageRequestImpl(Collections.singletonList(contentItem), id, new HashMap<>()));
} else {
// Currently we can't guarantee the metacard will exist yet because of the 1 second
// soft commit in solr. this busy wait loop should be fixed when alternate solution
// is found.
tryUpdate(4, () -> {
catalogFramework.update(new UpdateStorageRequestImpl(Collections.singletonList(contentItem), id, new HashMap<>()));
return true;
});
}
LOGGER.trace("Successfully reverted metacard content for [{}]", id);
revertMetacard(versionMetacard, id, alreadyCreated);
}
Aggregations