Freedom9339 commented on code in PR #7734: URL: https://github.com/apache/nifi/pull/7734#discussion_r1513373492
########## nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/StandardNiFiServiceFacade.java: ########## @@ -2939,6 +2939,115 @@ public ControllerServiceEntity updateControllerService(final Revision revision, return entityFactory.createControllerServiceEntity(snapshot.getComponent(), dtoFactory.createRevisionDTO(snapshot.getLastModification()), permissions, operatePermissions, bulletinEntities); } + @Override + public ControllerServiceEntity moveControllerService(final Revision revision, final ControllerServiceDTO controllerServiceDTO, final String newProcessGroupID) { + // get the component, ensure we have access to it, and perform the move request + final ControllerServiceNode controllerService = controllerServiceDAO.getControllerService(controllerServiceDTO.getId()); + final RevisionUpdate<ControllerServiceDTO> snapshot = updateComponent(revision, + controllerService, + () -> moveControllerServiceWork(controllerService, newProcessGroupID), + cs -> { + awaitValidationCompletion(cs); + final ControllerServiceDTO dto = dtoFactory.createControllerServiceDto(cs); + final ControllerServiceReference ref = controllerService.getReferences(); + final ControllerServiceReferencingComponentsEntity referencingComponentsEntity = createControllerServiceReferencingComponentsEntity(ref); + dto.setReferencingComponents(referencingComponentsEntity.getControllerServiceReferencingComponents()); + return dto; + }); + + final PermissionsDTO permissions = dtoFactory.createPermissionsDto(controllerService); + final PermissionsDTO operatePermissions = dtoFactory.createPermissionsDto(new OperationAuthorizable(controllerService)); + final List<BulletinDTO> bulletins = dtoFactory.createBulletinDtos(bulletinRepository.findBulletinsForSource(controllerServiceDTO.getId())); + final List<BulletinEntity> bulletinEntities = bulletins.stream().map(bulletin -> entityFactory.createBulletinEntity(bulletin, permissions.getCanRead())).collect(Collectors.toList()); + return entityFactory.createControllerServiceEntity(snapshot.getComponent(), dtoFactory.createRevisionDTO(snapshot.getLastModification()), permissions, operatePermissions, bulletinEntities); + } + + private ControllerServiceNode moveControllerServiceWork(final ControllerServiceNode controllerService, final String newProcessGroupID) { + final ProcessGroup oldParentGroup = controllerService.getProcessGroup(); + Set<ComponentNode> referencedComponents = controllerService.getReferences().getReferencingComponents(); + isReferencesDisabled(referencedComponents); + oldParentGroup.removeControllerService(controllerService); + ProcessGroup newParent; + if (!oldParentGroup.isRootGroup() && oldParentGroup.getParent().getIdentifier().equals(newProcessGroupID)) { + // move to parent process group + newParent = oldParentGroup.getParent(); + newParent.addControllerService(controllerService); + + // unset any references the controller services has to other controller services that are now out of scope + Map<String, String> updatedProps = new HashMap<>(); + Set<Map.Entry<PropertyDescriptor, PropertyConfiguration>> properties = controllerService.getProperties().entrySet(); + for (var prop : properties) { + var value = prop.getValue(); + if (value !=null) { + ControllerServiceNode controller; + try { + controller = controllerServiceDAO.getControllerService((value.getRawValue())); + } catch (Exception e){ + continue; + } + if (controller != null) { + if (!hasProcessGroup(controller.getProcessGroup(), newParent.getIdentifier())) { + controller.removeReference(controllerService, prop.getKey()); + updatedProps.put(prop.getKey().getName(), null); + } + } + } + } + if (!updatedProps.isEmpty()) + controllerService.setProperties(updatedProps, true, Collections.emptySet()); + + } else { + // move to child process group + newParent = oldParentGroup.getProcessGroup(newProcessGroupID); + newParent.addControllerService(controllerService); + + // unset any references for processors that are outside the new scope + for (ComponentNode node : referencedComponents) { + if (!hasProcessGroup(newParent, node.getProcessGroupIdentifier())) { + Set<Map.Entry<PropertyDescriptor, PropertyConfiguration>> properties = node.getProperties().entrySet(); + Map<String, String> updatedProps = new HashMap<>(); + for (Map.Entry<PropertyDescriptor, PropertyConfiguration> prop : properties) { + final PropertyConfiguration value = prop.getValue(); + if (value != null && value.getRawValue().equals(controllerService.getIdentifier())) { + controllerService.removeReference(node, prop.getKey()); + node.getComponent().onPropertyModified(prop.getKey(), controllerService.getIdentifier(), null); + updatedProps.put(prop.getKey().getName(), null); + } + } + node.setProperties(updatedProps, true, Collections.emptySet()); + } + } + } + + return controllerService; + } + private boolean hasProcessGroup(ProcessGroup root, String id){ Review Comment: Removed method. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org