@Transactional public boolean delete(long id, boolean force) { logger.trace("About to execute named query \"Network.deleteById\" for "); NetworkWithUsersAndDevicesVO network = getWithDevices(id); if (!force && network != null && !network.getDevices().isEmpty()) { logger.warn("Failed to delete non-empty network with id {}", id); String deviceIds = network.getDevices().stream().map(DeviceVO::getDeviceId).collect(Collectors.joining(", ")); throw new HiveException(String.format(Messages.NETWORK_DELETION_NOT_ALLOWED, deviceIds), SC_BAD_REQUEST); } int result = networkDao.deleteById(id); logger.debug("Deleted {} rows from Network table", result); return result > 0; }
@Transactional(propagation = Propagation.NOT_SUPPORTED) public NetworkWithUsersAndDevicesVO getWithDevices(@NotNull Long networkId) { HivePrincipal principal = (HivePrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); Set<Long> permittedNetworks = principal.getNetworkIds(); Set<Long> permittedDeviceTypes = principal.getDeviceTypeIds(); Optional<NetworkWithUsersAndDevicesVO> result = of(principal) .flatMap(pr -> { if (pr.getUser() != null) { return of(pr.getUser()); } else { return empty(); } }).flatMap(user -> { Long idForFiltering = user.isAdmin() ? null : user.getId(); List<NetworkWithUsersAndDevicesVO> found = networkDao.getNetworksByIdsAndUsers(idForFiltering, Collections.singleton(networkId), permittedNetworks); return found.stream().findFirst(); }).map(network -> { if (permittedDeviceTypes != null && !permittedDeviceTypes.isEmpty()) { Set<DeviceVO> allowed = network.getDevices().stream() .filter(device -> permittedDeviceTypes.contains(device.getDeviceTypeId())) .collect(Collectors.toSet()); network.setDevices(allowed); } return network; }); return result.orElse(null); }