private Optional<NetworkVO> findNetworkByIdOrName(NetworkVO network) { return ofNullable(network.getId()) .map(id -> ofNullable(networkDao.find(id))) .orElseGet(() -> networkDao.findFirstByName(network.getName())); } }
/** * Allows user access to given network * * @param userId id of user * @param networkId id of network */ @Transactional(propagation = Propagation.REQUIRED) public void assignNetwork(@NotNull long userId, @NotNull long networkId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't assign network with id {}: user {} not found", networkId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } NetworkWithUsersAndDevicesVO existingNetwork = networkDao.findWithUsers(networkId).orElse(null); if (Objects.isNull(existingNetwork)) { throw new HiveException(String.format(Messages.NETWORK_NOT_FOUND, networkId), NOT_FOUND.getStatusCode()); } networkDao.assignToNetwork(existingNetwork, existingUser); }
@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 public NetworkVO update(@NotNull Long networkId, NetworkUpdate networkUpdate) { NetworkVO existing = networkDao.find(networkId); if (existing == null) { throw new NoSuchElementException(String.format(Messages.NETWORK_NOT_FOUND, networkId)); } if (networkUpdate.getName().isPresent()){ existing.setName(networkUpdate.getName().get()); } if (networkUpdate.getDescription().isPresent()){ existing.setDescription(networkUpdate.getDescription().get()); } hiveValidator.validate(existing); return networkDao.merge(existing); }
@Transactional public NetworkVO create(NetworkVO newNetwork) { hiveValidator.validate(newNetwork); logger.debug("Creating network {}", newNetwork); if (newNetwork.getId() != null) { logger.error("Can't create network entity with id={} specified", newNetwork.getId()); throw new IllegalParametersException(Messages.ID_NOT_ALLOWED); } List<NetworkVO> existing = networkDao.findByName(newNetwork.getName()); if (!existing.isEmpty()) { logger.error("Network with name {} already exists", newNetwork.getName()); throw new ActionNotAllowedException(Messages.DUPLICATE_NETWORK); } networkDao.persist(newNetwork); logger.info("Entity {} created successfully", newNetwork); return newNetwork; }
public boolean isNetworkExists(Long networkId) { return ofNullable(networkId) .map(id -> networkDao.find(id) != null) .orElse(false); }
@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; } }
@Transactional public Long findDefaultNetworkByUserId(Long userId) { return networkDao.findDefaultByUser(userId) .map(NetworkVO::getId) .orElseThrow(() -> new ActionNotAllowedException(Messages.NO_ACCESS_TO_NETWORK)); }
@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); }
@Override public Response handle(Request request) { final CountNetworkRequest req = (CountNetworkRequest) request.getBody(); final long count = networkDao.count(req.getName(), req.getNamePattern(), req.getPrincipal()); final CountResponse countResponse = new CountResponse(count); return Response.newBuilder() .withBody(countResponse) .buildSuccess(); } }
/** * Revokes user access to given network * * @param userId id of user * @param networkId id of network */ @Transactional(propagation = Propagation.REQUIRED) public void unassignNetwork(@NotNull long userId, @NotNull long networkId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't unassign network with id {}: user {} not found", networkId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } NetworkVO existingNetwork = networkDao.find(networkId); if (existingNetwork == null) { logger.error("Can't unassign user with id {}: network {} not found", userId, networkId); throw new HiveException(String.format(Messages.NETWORK_NOT_FOUND, networkId), NOT_FOUND.getStatusCode()); } userDao.unassignNetwork(existingUser, networkId); }
private Optional<NetworkVO> findNetworkByIdOrName(NetworkVO network) { return ofNullable(network.getId()) .map(id -> ofNullable(networkDao.find(id))) .orElseGet(() -> networkDao.findFirstByName(network.getName())); } }