public NetworkWithUsersAndDevicesVO(NetworkVO vo) { setId(vo.getId()); setName(vo.getName()); setDescription(vo.getDescription()); setEntityVersion(vo.getEntityVersion()); }
public static NetworkVO convertNetwork(Network network) { if (network != null) { NetworkVO vo = new NetworkVO(); vo.setId(network.getId()); vo.setName(network.getName()); vo.setDescription(network.getDescription()); vo.setEntityVersion(network.getEntityVersion()); return vo; } return null; }
public NetworkVO convertTo() { NetworkVO networkVO = new NetworkVO(); networkVO.setName(name); networkVO.setDescription(description); return networkVO; } }
@Transactional public NetworkVO verifyNetwork(Optional<NetworkVO> networkNullable) { //case network is not defined if (networkNullable == null || networkNullable.orElse(null) == null) { return null; } NetworkVO network = networkNullable.get(); Optional<NetworkVO> storedOpt = findNetworkByIdOrName(network); if (storedOpt.isPresent()) { return storedOpt.get(); } throw new NoSuchElementException(String.format(Messages.NETWORK_NOT_FOUND, network.getId())); }
@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 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; } }
@Override public void persist(NetworkVO newNetwork) { Network network = Network.convert(newNetwork); super.persist(network); newNetwork.setId(network.getId()); }
@Transactional public NetworkVO createOrUpdateNetworkByUser(UserVO user) { NetworkVO networkVO = new NetworkVO(); networkVO.setName(user.getLogin()); networkVO.setDescription(String.format("User %s default network", user.getLogin())); return createOrUpdateNetworkByUser(Optional.of(networkVO), user); }
@Override public long hasAccessToNetwork(UserVO user, NetworkVO network) { Network nw = reference(Network.class, network.getId()); return createNamedQuery(Long.class, "User.hasAccessToNetwork", empty()) .setParameter("user", user.getId()) .setParameter("network", nw) .getSingleResult(); }
public static Network convert(NetworkVO vo) { if (vo != null) { Network network = new Network(); network.setId(vo.getId()); network.setName(vo.getName()); network.setDescription(vo.getDescription()); network.setEntityVersion(vo.getEntityVersion()); return network; } return null; } }
@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; }
@Override public void assignToNetwork(NetworkVO network, UserVO user) { assert network != null && network.getId() != null; assert user != null && user.getId() != null; Network existing = find(Network.class, network.getId()); User userReference = reference(User.class, user.getId()); if (existing.getUsers() == null) { existing.setUsers(new HashSet<>()); } existing.getUsers().add(userReference); super.merge(existing); }
@Override public NetworkVO merge(NetworkVO existing) { Network network = find(Network.class, existing.getId()); network.setName(existing.getName()); network.setDescription(existing.getDescription()); network.setEntityVersion(existing.getEntityVersion()); super.merge(network); return existing; }
@HiveWebsocketAuth @PreAuthorize("isAuthenticated() and hasPermission(null, 'GET_NETWORK')") public void processUserGetNetwork(JsonObject request, WebSocketSession session) { Long userId = gson.fromJson(request.get(USER_ID), Long.class); if (userId == null) { logger.error(Messages.USER_ID_REQUIRED); throw new HiveException(Messages.USER_ID_REQUIRED, BAD_REQUEST.getStatusCode()); } Long networkId = gson.fromJson(request.get(NETWORK_ID), Long.class); if (networkId == null) { logger.error(Messages.NETWORK_ID_REQUIRED); throw new HiveException(Messages.NETWORK_ID_REQUIRED, BAD_REQUEST.getStatusCode()); } UserWithNetworkVO existingUser = userService.findUserWithNetworks(userId); if (existingUser == null) { logger.error("Can't get network with id {}: user {} not found", networkId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } for (NetworkVO network : existingUser.getNetworks()) { if (networkId.equals(network.getId())) { WebSocketResponse response = new WebSocketResponse(); response.addValue(NETWORK, UserNetworkResponse.fromNetwork(network), NETWORKS_LISTED); clientHandler.sendMessage(request, response, session); return; } } clientHandler.sendErrorResponse(request, NOT_FOUND.getStatusCode(), String.format(Messages.USER_NETWORK_NOT_FOUND, networkId, userId), session); }
/** * {@inheritDoc} */ @Override public Response getNetwork(long id, long networkId) { UserWithNetworkVO existingUser = userService.findUserWithNetworks(id); if (existingUser == null) { logger.error("Can't get network with id {}: user {} not found", networkId, id); ErrorResponse errorResponseEntity = new ErrorResponse(NOT_FOUND.getStatusCode(), String.format(Messages.USER_NOT_FOUND, id)); return ResponseFactory.response(NOT_FOUND, errorResponseEntity); } for (NetworkVO network : existingUser.getNetworks()) { if (network.getId() == networkId) { return ResponseFactory.response(OK, UserNetworkResponse.fromNetwork(network), JsonPolicyDef.Policy.NETWORKS_LISTED); } } ErrorResponse errorResponseEntity = new ErrorResponse(NOT_FOUND.getStatusCode(), String.format(Messages.USER_NETWORK_NOT_FOUND, networkId, id)); return ResponseFactory.response(NOT_FOUND, errorResponseEntity); }
if (!networks.isEmpty()) { networks.forEach(network -> { networkIds.add(network.getId().toString()); });
public static DeviceVO convertToVo(Device dc) { DeviceVO vo = null; if (dc != null) { vo = new DeviceVO(); vo.setId(dc.getId()); vo.setDeviceId(dc.getDeviceId()); vo.setName(dc.getName()); vo.setData(dc.getData()); vo.setBlocked(dc.getBlocked()); NetworkVO networkVO = Network.convertNetwork(dc.getNetwork()); vo.setNetworkId(networkVO.getId()); DeviceTypeVO deviceTypeVO = DeviceType.convertDeviceType(dc.getDeviceType()); vo.setDeviceTypeId(deviceTypeVO.getId()); } return vo; }