use of org.graylog2.contentpacks.facades.EntityFacade in project graylog2-server by Graylog2.
the class ContentPackService method uninstallContentPack.
private ContentPackUninstallation uninstallContentPack(ContentPackInstallation installation, ContentPackV1 contentPack) {
final Entity rootEntity = EntityV1.createRoot(contentPack);
final ImmutableMap<String, ValueReference> parameters = installation.parameters();
final ImmutableGraph<Entity> dependencyGraph = buildEntityGraph(rootEntity, contentPack.entities(), parameters);
final Traverser<Entity> entityTraverser = Traverser.forGraph(dependencyGraph);
final Iterable<Entity> entitiesInOrder = entityTraverser.breadthFirst(rootEntity);
final Set<NativeEntityDescriptor> removedEntities = new HashSet<>();
final Set<NativeEntityDescriptor> failedEntities = new HashSet<>();
final Set<NativeEntityDescriptor> skippedEntities = new HashSet<>();
try {
for (Entity entity : entitiesInOrder) {
if (entity.equals(rootEntity)) {
continue;
}
final Optional<NativeEntityDescriptor> nativeEntityDescriptorOptional = installation.entities().stream().filter(descriptor -> entity.id().equals(descriptor.contentPackEntityId())).findFirst();
final EntityWithExcerptFacade facade = entityFacades.getOrDefault(entity.type(), UnsupportedEntityFacade.INSTANCE);
if (nativeEntityDescriptorOptional.isPresent()) {
final NativeEntityDescriptor nativeEntityDescriptor = nativeEntityDescriptorOptional.get();
final Optional nativeEntityOptional = facade.loadNativeEntity(nativeEntityDescriptor);
final ModelId entityId = nativeEntityDescriptor.id();
final long installCount = contentPackInstallationPersistenceService.countInstallationOfEntityById(entityId);
final long systemFoundCount = contentPackInstallationPersistenceService.countInstallationOfEntityByIdAndFoundOnSystem(entityId);
if (installCount > 1 || (installCount == 1 && systemFoundCount >= 1)) {
skippedEntities.add(nativeEntityDescriptor);
LOG.debug("Did not remove entity since other content pack installations still use them: {}", nativeEntityDescriptor);
} else if (nativeEntityOptional.isPresent()) {
final Object nativeEntity = nativeEntityOptional.get();
LOG.trace("Removing existing native entity for {} ({})", nativeEntityDescriptor);
try {
// The EntityFacade#delete() method expects the actual entity object
// noinspection unchecked
facade.delete(((NativeEntity) nativeEntity).entity());
removedEntities.add(nativeEntityDescriptor);
} catch (Exception e) {
LOG.warn("Couldn't remove native entity {}", nativeEntity);
failedEntities.add(nativeEntityDescriptor);
}
} else {
LOG.trace("Couldn't find existing native entity for {} ({})", nativeEntityDescriptor);
}
}
}
} catch (Exception e) {
throw new ContentPackException("Failed to remove content pack <" + contentPack.id() + "/" + contentPack.revision() + ">", e);
}
final int deletedInstallations = contentPackInstallationPersistenceService.deleteById(installation.id());
LOG.debug("Deleted {} installation(s) of content pack {}", deletedInstallations, contentPack.id());
return ContentPackUninstallation.builder().entities(ImmutableSet.copyOf(removedEntities)).skippedEntities(ImmutableSet.copyOf(skippedEntities)).failedEntities(ImmutableSet.copyOf(failedEntities)).build();
}
Aggregations