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 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; } }