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 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; }
public NetworkWithUsersAndDevicesVO(NetworkVO vo) { setId(vo.getId()); setName(vo.getName()); setDescription(vo.getDescription()); setEntityVersion(vo.getEntityVersion()); }
@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); }
@Transactional public NetworkVO createOrUpdateNetworkByUser(Optional<NetworkVO> networkNullable, UserVO user) { //case network is not defined if (networkNullable == null || networkNullable.orElse(null) == null) { return null; } NetworkVO network = networkNullable.orElse(null); Optional<NetworkVO> storedOpt = findNetworkByIdOrName(network); if (storedOpt.isPresent()) { return storedOpt.get(); } else { if (network.getId() != null) { throw new IllegalParametersException(Messages.INVALID_REQUEST_PARAMETERS); } if (user.isAdmin()) { NetworkWithUsersAndDevicesVO newNetwork = new NetworkWithUsersAndDevicesVO(network); networkDao.persist(newNetwork); network.setId(newNetwork.getId()); baseUserService.assignNetwork(user.getId(), network.getId()); // Assign created network to user } else { throw new ActionNotAllowedException(Messages.NETWORK_CREATION_NOT_ALLOWED); } return network; } }