public DeviceTypeWithUsersAndDevicesVO(DeviceTypeVO vo) { setId(vo.getId()); setName(vo.getName()); setDescription(vo.getDescription()); setEntityVersion(vo.getEntityVersion()); }
public static DeviceTypeVO convertDeviceType(DeviceType deviceType) { if (deviceType != null) { DeviceTypeVO vo = new DeviceTypeVO(); vo.setId(deviceType.getId()); vo.setName(deviceType.getName()); vo.setDescription(deviceType.getDescription()); vo.setEntityVersion(deviceType.getEntityVersion()); return vo; } return null; }
public DeviceTypeVO convertTo() { DeviceTypeVO deviceTypeVO = new DeviceTypeVO(); deviceTypeVO.setName(name); deviceTypeVO.setDescription(description); return deviceTypeVO; } }
@Transactional public DeviceTypeVO verifyDeviceType(Optional<DeviceTypeVO> deviceTypeNullable) { //case device type is not defined if (deviceTypeNullable == null || deviceTypeNullable.orElse(null) == null) { return null; } DeviceTypeVO deviceType = deviceTypeNullable.get(); Optional<DeviceTypeVO> storedOpt = findDeviceTypeByIdOrName(deviceType); if (storedOpt.isPresent()) { return storedOpt.get(); } throw new NoSuchElementException(String.format(Messages.DEVICE_TYPE_NOT_FOUND, deviceType.getId())); }
@Transactional public DeviceTypeVO update(@NotNull Long deviceTypeId, DeviceTypeUpdate deviceTypeUpdate) { DeviceTypeVO existing = deviceTypeDao.find(deviceTypeId); if (existing == null) { throw new NoSuchElementException(String.format(Messages.DEVICE_TYPE_NOT_FOUND, deviceTypeId)); } if (deviceTypeUpdate.getName().isPresent()) { existing.setName(deviceTypeUpdate.getName().get()); } if (deviceTypeUpdate.getDescription().isPresent()) { existing.setDescription(deviceTypeUpdate.getDescription().get()); } hiveValidator.validate(existing); return deviceTypeDao.merge(existing); }
@Transactional public DeviceTypeVO createOrUpdateDeviceTypeByUser(Optional<DeviceTypeVO> deviceTypeNullable, UserVO user) { //case device type is not defined if (deviceTypeNullable == null || deviceTypeNullable.orElse(null) == null) { return null; } DeviceTypeVO deviceType = deviceTypeNullable.orElse(null); Optional<DeviceTypeVO> storedOpt = findDeviceTypeByIdOrName(deviceType); if (storedOpt.isPresent()) { return storedOpt.get(); } else { if (deviceType.getId() != null) { throw new IllegalParametersException(Messages.INVALID_REQUEST_PARAMETERS); } if (user.isAdmin()) { DeviceTypeWithUsersAndDevicesVO newDeviceType = new DeviceTypeWithUsersAndDevicesVO(deviceType); deviceTypeDao.persist(newDeviceType); deviceType.setId(newDeviceType.getId()); } else { throw new ActionNotAllowedException(Messages.DEVICE_TYPE_CREATION_NOT_ALLOWED); } return deviceType; } }
@Override public void persist(DeviceTypeVO newDeviceType) { DeviceType deviceType = DeviceType.convert(newDeviceType); super.persist(deviceType); newDeviceType.setId(deviceType.getId()); }
@Transactional public DeviceTypeVO createOrUpdateDeviceTypeByUser(UserVO user) { DeviceTypeVO deviceTypeVO = new DeviceTypeVO(); deviceTypeVO.setName(user.getLogin()); deviceTypeVO.setDescription(String.format("User %s default device type", user.getLogin())); return createOrUpdateDeviceTypeByUser(Optional.ofNullable(deviceTypeVO), user); }
@Transactional public DeviceTypeVO create(DeviceTypeVO newDeviceType) { hiveValidator.validate(newDeviceType); logger.debug("Creating device type {}", newDeviceType); if (newDeviceType.getId() != null) { logger.error("Can't create device type entity with id={} specified", newDeviceType.getId()); throw new IllegalParametersException(Messages.ID_NOT_ALLOWED); } List<DeviceTypeVO> existing = deviceTypeDao.findByName(newDeviceType.getName()); if (!existing.isEmpty()) { logger.error("Device type with name {} already exists", newDeviceType.getName()); throw new ActionNotAllowedException(Messages.DUPLICATE_DEVICE_TYPE); } deviceTypeDao.persist(newDeviceType); logger.info("Entity {} created successfully", newDeviceType); return newDeviceType; }
@Override public void assignToDeviceType(DeviceTypeVO deviceType, UserVO user) { assert deviceType != null && deviceType.getId() != null; assert user != null && user.getId() != null; DeviceType existing = find(DeviceType.class, deviceType.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 DeviceTypeVO merge(DeviceTypeVO existing) { DeviceType deviceType = find(DeviceType.class, existing.getId()); deviceType.setName(existing.getName()); deviceType.setDescription(existing.getDescription()); deviceType.setEntityVersion(existing.getEntityVersion()); super.merge(deviceType); return existing; }
@HiveWebsocketAuth @PreAuthorize("isAuthenticated() and hasPermission(null, 'GET_DEVICE_TYPE')") public void processUserGetDeviceType(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 deviceTypeId = gson.fromJson(request.get(DEVICE_TYPE_ID), Long.class); if (deviceTypeId == null) { logger.error(Messages.DEVICE_TYPE_ID_REQUIRED); throw new HiveException(Messages.DEVICE_TYPE_ID_REQUIRED, BAD_REQUEST.getStatusCode()); } UserWithDeviceTypeVO existingUser = userService.findUserWithDeviceType(userId); if (existingUser == null) { logger.error("Can't get device type with id {}: user {} not found", deviceTypeId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } for (DeviceTypeVO deviceType : existingUser.getDeviceTypes()) { if (deviceTypeId.equals(deviceType.getId())) { WebSocketResponse response = new WebSocketResponse(); response.addValue(DEVICE_TYPE, UserDeviceTypeResponse.fromDeviceType(deviceType), DEVICE_TYPES_LISTED); clientHandler.sendMessage(request, response, session); return; } } clientHandler.sendErrorResponse(request, NOT_FOUND.getStatusCode(), String.format(Messages.USER_DEVICE_TYPE_NOT_FOUND, deviceTypeId, userId), session); }
public static DeviceType convert(DeviceTypeVO vo) { if (vo != null) { DeviceType deviceType = new DeviceType(); deviceType.setId(vo.getId()); deviceType.setName(vo.getName()); deviceType.setDescription(vo.getDescription()); deviceType.setEntityVersion(vo.getEntityVersion()); return deviceType; } return null; } }
/** * {@inheritDoc} */ @Override public Response getDeviceType(long id, long deviceTypeId) { UserWithDeviceTypeVO existingUser = userService.findUserWithDeviceType(id); if (existingUser == null) { logger.error("Can't get device type with id {}: user {} not found", deviceTypeId, id); ErrorResponse errorResponseEntity = new ErrorResponse(NOT_FOUND.getStatusCode(), String.format(Messages.USER_NOT_FOUND, id)); return ResponseFactory.response(NOT_FOUND, errorResponseEntity); } if (existingUser.getAllDeviceTypesAvailable()) { DeviceTypeVO deviceTypeVO = deviceTypeService.getWithDevices(deviceTypeId); if (deviceTypeVO != null) { return ResponseFactory.response(OK, UserDeviceTypeResponse.fromDeviceType(deviceTypeVO), JsonPolicyDef.Policy.DEVICE_TYPES_LISTED); } } for (DeviceTypeVO deviceType : existingUser.getDeviceTypes()) { if (deviceType.getId() == deviceTypeId) { return ResponseFactory.response(OK, UserDeviceTypeResponse.fromDeviceType(deviceType), JsonPolicyDef.Policy.DEVICE_TYPES_LISTED); } } ErrorResponse errorResponseEntity = new ErrorResponse(NOT_FOUND.getStatusCode(), String.format(Messages.USER_DEVICE_TYPE_NOT_FOUND, deviceTypeId, id)); return ResponseFactory.response(NOT_FOUND, errorResponseEntity); }
if (!deviceTypes.isEmpty()) { deviceTypes.forEach(deviceType -> { deviceTypeIds.add(deviceType.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; }