public static NetworkWithUsersAndDevicesVO convertWithDevicesAndUsers(Network network) { if (network != null) { NetworkVO vo1 = convertNetwork(network); NetworkWithUsersAndDevicesVO vo = new NetworkWithUsersAndDevicesVO(vo1); if (network.getUsers() != null) { vo.setUsers(network.getUsers().stream().map(User::convertToVo).collect(Collectors.toSet())); } else { vo.setUsers(Collections.emptySet()); } if (network.getDevices() != null) { Set<DeviceVO> deviceList = network.getDevices().stream().map(Device::convertToVo).collect(Collectors.toSet()); vo.setDevices(deviceList); } else { vo.setDevices(Collections.emptySet()); } return vo; } return null; }
@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); }