use of org.codice.ddf.registry.common.metacard.RegistryUtility in project ddf by codice.
the class FederationAdminServiceImpl method addRegistryEntries.
@Override
public List<String> addRegistryEntries(List<Metacard> metacards, Set<String> destinations) throws FederationAdminException {
validateRegistryMetacards(metacards);
List<String> registryIds;
Map<String, Serializable> properties = new HashMap<>();
CreateRequest createRequest = new CreateRequestImpl(metacards, properties, destinations);
try {
CreateResponse createResponse = security.runWithSubjectOrElevate(() -> catalogFramework.create(createRequest));
//loop through to get id's
if (!createResponse.getProcessingErrors().isEmpty()) {
throw new FederationAdminException("Processing error occurred while creating registry entry. Details:" + System.lineSeparator() + stringifyProcessingErrors(createResponse.getProcessingErrors()));
}
registryIds = createResponse.getCreatedMetacards().stream().filter(RegistryUtility::isRegistryMetacard).map(RegistryUtility::getRegistryId).collect(Collectors.toList());
} catch (SecurityServiceException | InvocationTargetException e) {
throw new FederationAdminException("Error adding local registry entry.", e);
}
return registryIds;
}
use of org.codice.ddf.registry.common.metacard.RegistryUtility in project ddf by codice.
the class RegistryStoreImpl method update.
@Override
public UpdateResponse update(UpdateRequest request) throws IngestException {
if (request.getUpdates().stream().map(e -> RegistryUtility.getRegistryId(e.getValue())).anyMatch(Objects::isNull)) {
throw new IngestException("One or more of the metacards is not a registry metacard");
}
Map<String, Metacard> updatedMetacards = request.getUpdates().stream().collect(Collectors.toMap(e -> RegistryUtility.getRegistryId(e.getValue()), Map.Entry::getValue));
Map<String, Metacard> origMetacards = ((OperationTransaction) request.getPropertyValue(Constants.OPERATION_TRANSACTION_KEY)).getPreviousStateMetacards().stream().collect(Collectors.toMap(RegistryUtility::getRegistryId, e -> e));
//update the new metacards with the id from the orig so that they can be found on the remote system
try {
for (Map.Entry<String, Metacard> entry : updatedMetacards.entrySet()) {
setMetacardExtID(entry.getValue(), origMetacards.get(entry.getKey()).getId());
}
} catch (ParserException e) {
throw new IngestException("Could not update metacards id", e);
}
return super.update(request);
}
use of org.codice.ddf.registry.common.metacard.RegistryUtility in project ddf by codice.
the class RegistryStoreImpl method create.
@Override
public CreateResponse create(CreateRequest request) throws IngestException {
if (request.getMetacards().stream().map(RegistryUtility::getRegistryId).anyMatch(Objects::isNull)) {
throw new IngestException("One or more of the metacards is not a registry metacard");
}
validateOperation();
List<Filter> regIdFilters = request.getMetacards().stream().map(e -> filterBuilder.attribute(RegistryObjectMetacardType.REMOTE_METACARD_ID).is().equalTo().text(e.getId())).collect(Collectors.toList());
Filter tagFilter = filterBuilder.attribute(Metacard.TAGS).is().equalTo().text(RegistryConstants.REGISTRY_TAG_INTERNAL);
Map<String, Serializable> queryProps = new HashMap<>();
queryProps.put(SecurityConstants.SECURITY_SUBJECT, request.getPropertyValue(SecurityConstants.SECURITY_SUBJECT));
QueryImpl query = new QueryImpl(filterBuilder.allOf(tagFilter, filterBuilder.attribute(RegistryObjectMetacardType.REGISTRY_LOCAL_NODE).empty(), filterBuilder.anyOf(regIdFilters)));
QueryRequest queryRequest = new QueryRequestImpl(query, queryProps);
try {
SourceResponse queryResponse = super.query(queryRequest);
Map<String, Metacard> responseMap = queryResponse.getResults().stream().collect(Collectors.toMap(e -> RegistryUtility.getRegistryId(e.getMetacard()), Result::getMetacard));
List<Metacard> metacardsToCreate = request.getMetacards().stream().filter(e -> !responseMap.containsKey(RegistryUtility.getRegistryId(e))).collect(Collectors.toList());
List<Metacard> allMetacards = new ArrayList<>(responseMap.values());
if (CollectionUtils.isNotEmpty(metacardsToCreate)) {
CreateResponse createResponse = super.create(new CreateRequestImpl(metacardsToCreate, request.getProperties()));
allMetacards.addAll(createResponse.getCreatedMetacards());
}
return new CreateResponseImpl(request, request.getProperties(), allMetacards);
} catch (UnsupportedQueryException e) {
LOGGER.warn("Unable to perform pre-create remote query. Proceeding with original query. Error was {}", e.getMessage());
}
return super.create(request);
}
use of org.codice.ddf.registry.common.metacard.RegistryUtility 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;
}
Aggregations