public boolean isDeviceTypeExists(Long deviceTypeId) { return ofNullable(deviceTypeId) .map(id -> deviceTypeDao.find(id) != null) .orElse(false); }
private Optional<DeviceTypeVO> findDeviceTypeByIdOrName(DeviceTypeVO deviceType) { return ofNullable(deviceType.getId()) .map(id -> ofNullable(deviceTypeDao.find(id))) .orElseGet(() -> deviceTypeDao.findFirstByName(deviceType.getName())); } }
/** * Revokes user access to given device type * * @param userId id of user * @param deviceTypeId id of device type */ @Transactional(propagation = Propagation.REQUIRED) public void unassignDeviceType(@NotNull long userId, @NotNull long deviceTypeId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't unassign device type with id {}: user {} not found", deviceTypeId, userId); throw new HiveException(String.format(Messages.USER_NOT_FOUND, userId), NOT_FOUND.getStatusCode()); } if (existingUser.getAllDeviceTypesAvailable()) { throw new HiveException(String.format(Messages.DEVICE_TYPE_ASSIGNMENT_NOT_ALLOWED, userId), FORBIDDEN.getStatusCode()); } DeviceTypeVO existingDeviceType = deviceTypeDao.find(deviceTypeId); if (existingDeviceType == null) { logger.error("Can't unassign user with id {}: device type {} not found", userId, deviceTypeId); throw new HiveException(String.format(Messages.DEVICE_TYPE_NOT_FOUND, deviceTypeId), NOT_FOUND.getStatusCode()); } userDao.unassignDeviceType(existingUser, deviceTypeId); }
@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); }