@Transactional public boolean delete(long id, boolean force) { logger.trace("About to execute named query \"DeviceType.deleteById\" for "); DeviceTypeWithUsersAndDevicesVO deviceType = getWithDevices(id); if (!force && deviceType != null && !deviceType.getDevices().isEmpty()) { logger.warn("Failed to delete non-empty device type with id {}", id); String deviceIds = deviceType.getDevices().stream().map(DeviceVO::getDeviceId).collect(Collectors.joining(", ")); throw new HiveException(String.format(Messages.DEVICE_TYPE_DELETION_NOT_ALLOWED, deviceIds), SC_BAD_REQUEST); } int result = deviceTypeDao.deleteById(id); logger.debug("Deleted {} rows from DeviceType table", result); return result > 0; }
private Optional<DeviceTypeVO> findDeviceTypeByIdOrName(DeviceTypeVO deviceType) { return ofNullable(deviceType.getId()) .map(id -> ofNullable(deviceTypeDao.find(id))) .orElseGet(() -> deviceTypeDao.findFirstByName(deviceType.getName())); } }
@Transactional public Long findDefaultDeviceType(Set<Long> deviceTypeIds) { return deviceTypeDao.findDefault(deviceTypeIds) .map(DeviceTypeVO::getId) .orElseThrow(() -> new ActionNotAllowedException(Messages.NO_ACCESS_TO_DEVICE_TYPE)); }
/** * Allows user access to given device type * * @param userId id of user * @param deviceTypeId id of device type */ @Transactional(propagation = Propagation.REQUIRED) public void assignDeviceType(@NotNull long userId, @NotNull long deviceTypeId) { UserVO existingUser = userDao.find(userId); if (existingUser == null) { logger.error("Can't assign 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()); } DeviceTypeWithUsersAndDevicesVO existingDeviceType = deviceTypeDao.findWithUsers(deviceTypeId).orElse(null); if (Objects.isNull(existingDeviceType)) { throw new HiveException(String.format(Messages.DEVICE_TYPE_NOT_FOUND, deviceTypeId), NOT_FOUND.getStatusCode()); } deviceTypeDao.assignToDeviceType(existingDeviceType, existingUser); }
@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 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; }
public boolean isDeviceTypeExists(Long deviceTypeId) { return ofNullable(deviceTypeId) .map(id -> deviceTypeDao.find(id) != null) .orElse(false); }
idForFiltering = null; List<DeviceTypeWithUsersAndDevicesVO> found = deviceTypeDao.getDeviceTypesByIdsAndUsers(idForFiltering, Collections.singleton(deviceTypeId), permittedDeviceTypes); return found.stream().findFirst();
@Override public Response handle(Request request) { final CountDeviceTypeRequest req = (CountDeviceTypeRequest) request.getBody(); final long count = deviceTypeDao.count(req.getName(), req.getNamePattern(), req.getPrincipal()); final CountResponse countResponse = new CountResponse(count); return Response.newBuilder() .withBody(countResponse) .buildSuccess(); } }
@Override public Response handle(Request request) { final ListDeviceTypeRequest req = (ListDeviceTypeRequest) request.getBody(); final List<DeviceTypeVO> deviceTypes = deviceTypeDao.list(req.getName(), req.getNamePattern(), req.getSortField(), req.isSortOrderAsc(), req.getTake(), req.getSkip(), req.getPrincipal()); return Response.newBuilder() .withBody(new ListDeviceTypeResponse(deviceTypes)) .buildSuccess(); } }
/** * 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); }