|
|
|
@ -23,7 +23,9 @@ import org.apache.commons.lang3.RandomStringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.thingsboard.server.common.data.Customer; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.Tenant; |
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
@ -33,9 +35,7 @@ import org.thingsboard.server.common.data.security.DeviceCredentials; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|
|
|
import org.thingsboard.server.dao.customer.CustomerDao; |
|
|
|
import org.thingsboard.server.dao.exception.DataValidationException; |
|
|
|
import org.thingsboard.server.dao.model.CustomerEntity; |
|
|
|
import org.thingsboard.server.dao.model.DeviceEntity; |
|
|
|
import org.thingsboard.server.dao.model.TenantEntity; |
|
|
|
import org.thingsboard.server.dao.service.DataValidator; |
|
|
|
import org.thingsboard.server.dao.service.PaginatedRemover; |
|
|
|
import org.thingsboard.server.dao.tenant.TenantDao; |
|
|
|
@ -43,9 +43,13 @@ import org.thingsboard.server.dao.tenant.TenantDao; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
import static org.thingsboard.server.dao.DaoUtil.*; |
|
|
|
import static org.thingsboard.server.dao.DaoUtil.convertDataList; |
|
|
|
import static org.thingsboard.server.dao.DaoUtil.getData; |
|
|
|
import static org.thingsboard.server.dao.DaoUtil.toUUIDs; |
|
|
|
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
|
|
|
import static org.thingsboard.server.dao.service.Validator.*; |
|
|
|
import static org.thingsboard.server.dao.service.Validator.validateId; |
|
|
|
import static org.thingsboard.server.dao.service.Validator.validateIds; |
|
|
|
import static org.thingsboard.server.dao.service.Validator.validatePageLink; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
@ -74,17 +78,16 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
public ListenableFuture<Device> findDeviceByIdAsync(DeviceId deviceId) { |
|
|
|
log.trace("Executing findDeviceById [{}]", deviceId); |
|
|
|
validateId(deviceId, "Incorrect deviceId " + deviceId); |
|
|
|
ListenableFuture<Device> deviceEntity = deviceDao.findByIdAsync(deviceId.getId()); |
|
|
|
return Futures.transform(deviceEntity, (Function<? super DeviceEntity, ? extends Device>) input -> getData(input)); |
|
|
|
return deviceDao.findByIdAsync(deviceId.getId()); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public Optional<Device> findDeviceByTenantIdAndName(TenantId tenantId, String name) { |
|
|
|
log.trace("Executing findDeviceByTenantIdAndName [{}][{}]", tenantId, name); |
|
|
|
validateId(tenantId, "Incorrect tenantId " + tenantId); |
|
|
|
Optional<Device> deviceEntityOpt = deviceDao.findDevicesByTenantIdAndName(tenantId.getId(), name); |
|
|
|
if (deviceEntityOpt.isPresent()) { |
|
|
|
return Optional.of(deviceEntityOpt.get()); |
|
|
|
Optional<Device> deviceOpt = deviceDao.findDevicesByTenantIdAndName(tenantId.getId(), name); |
|
|
|
if (deviceOpt.isPresent()) { |
|
|
|
return Optional.of(deviceOpt.get()); |
|
|
|
} else { |
|
|
|
return Optional.empty(); |
|
|
|
} |
|
|
|
@ -136,7 +139,7 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
validateId(tenantId, "Incorrect tenantId " + tenantId); |
|
|
|
validatePageLink(pageLink, "Incorrect page link " + pageLink); |
|
|
|
List<Device> devices = deviceDao.findDevicesByTenantId(tenantId.getId(), pageLink); |
|
|
|
return new TextPageData<Device>(devices, pageLink); |
|
|
|
return new TextPageData<>(devices, pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -144,8 +147,7 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
log.trace("Executing findDevicesByTenantIdAndIdsAsync, tenantId [{}], deviceIds [{}]", tenantId, deviceIds); |
|
|
|
validateId(tenantId, "Incorrect tenantId " + tenantId); |
|
|
|
validateIds(deviceIds, "Incorrect deviceIds " + deviceIds); |
|
|
|
ListenableFuture<List<Device>> devices = deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId.getId(), toUUIDs(deviceIds)); |
|
|
|
return Futures.transform(deviceEntities, (Function<List<DeviceEntity>, List<Device>>) input -> convertDataList(input)); |
|
|
|
return deviceDao.findDevicesByTenantIdAndIdsAsync(tenantId.getId(), toUUIDs(deviceIds)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -163,7 +165,7 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
validateId(customerId, "Incorrect customerId " + customerId); |
|
|
|
validatePageLink(pageLink, "Incorrect page link " + pageLink); |
|
|
|
List<Device> devices = deviceDao.findDevicesByTenantIdAndCustomerId(tenantId.getId(), customerId.getId(), pageLink); |
|
|
|
return new TextPageData<Device>(devices, pageLink); |
|
|
|
return new TextPageData<>(devices, pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -172,9 +174,8 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
validateId(tenantId, "Incorrect tenantId " + tenantId); |
|
|
|
validateId(customerId, "Incorrect customerId " + customerId); |
|
|
|
validateIds(deviceIds, "Incorrect deviceIds " + deviceIds); |
|
|
|
ListenableFuture<List<DeviceEntity>> deviceEntities = deviceDao.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId.getId(), |
|
|
|
return deviceDao.findDevicesByTenantIdCustomerIdAndIdsAsync(tenantId.getId(), |
|
|
|
customerId.getId(), toUUIDs(deviceIds)); |
|
|
|
return Futures.transform(deviceEntities, (Function<List<DeviceEntity>, List<Device>>) input -> convertDataList(input)); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -201,7 +202,7 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
protected void validateUpdate(Device device) { |
|
|
|
deviceDao.findDevicesByTenantIdAndName(device.getTenantId().getId(), device.getName()).ifPresent( |
|
|
|
d -> { |
|
|
|
if (!d.getId().equals(device.getUuidId())) { |
|
|
|
if (!d.getUuidId().equals(device.getUuidId())) { |
|
|
|
throw new DataValidationException("Device with such name already exists!"); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -216,7 +217,7 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
if (device.getTenantId() == null) { |
|
|
|
throw new DataValidationException("Device should be assigned to tenant!"); |
|
|
|
} else { |
|
|
|
TenantEntity tenant = tenantDao.findById(device.getTenantId().getId()); |
|
|
|
Tenant tenant = tenantDao.findById(device.getTenantId().getId()); |
|
|
|
if (tenant == null) { |
|
|
|
throw new DataValidationException("Device is referencing to non-existent tenant!"); |
|
|
|
} |
|
|
|
@ -224,32 +225,32 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
if (device.getCustomerId() == null) { |
|
|
|
device.setCustomerId(new CustomerId(NULL_UUID)); |
|
|
|
} else if (!device.getCustomerId().getId().equals(NULL_UUID)) { |
|
|
|
CustomerEntity customer = customerDao.findById(device.getCustomerId().getId()); |
|
|
|
Customer customer = customerDao.findById(device.getCustomerId().getId()); |
|
|
|
if (customer == null) { |
|
|
|
throw new DataValidationException("Can't assign device to non-existent customer!"); |
|
|
|
} |
|
|
|
if (!customer.getTenantId().equals(device.getTenantId().getId())) { |
|
|
|
if (!customer.getTenantId().getId().equals(device.getTenantId().getId())) { |
|
|
|
throw new DataValidationException("Can't assign device to customer from different tenant!"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
private PaginatedRemover<TenantId, DeviceEntity> tenantDevicesRemover = |
|
|
|
new PaginatedRemover<TenantId, DeviceEntity>() { |
|
|
|
private PaginatedRemover<TenantId, Device> tenantDevicesRemover = |
|
|
|
new PaginatedRemover<TenantId, Device>() { |
|
|
|
|
|
|
|
@Override |
|
|
|
protected List<DeviceEntity> findEntities(TenantId id, TextPageLink pageLink) { |
|
|
|
protected List<Device> findEntities(TenantId id, TextPageLink pageLink) { |
|
|
|
return deviceDao.findDevicesByTenantId(id.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void removeEntity(DeviceEntity entity) { |
|
|
|
deleteDevice(new DeviceId(entity.getId())); |
|
|
|
protected void removeEntity(Device entity) { |
|
|
|
deleteDevice(new DeviceId(entity.getUuidId())); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
class CustomerDevicesUnassigner extends PaginatedRemover<CustomerId, DeviceEntity> { |
|
|
|
private class CustomerDevicesUnassigner extends PaginatedRemover<CustomerId, Device> { |
|
|
|
|
|
|
|
private TenantId tenantId; |
|
|
|
|
|
|
|
@ -258,13 +259,13 @@ public class DeviceServiceImpl implements DeviceService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected List<DeviceEntity> findEntities(CustomerId id, TextPageLink pageLink) { |
|
|
|
protected List<Device> findEntities(CustomerId id, TextPageLink pageLink) { |
|
|
|
return deviceDao.findDevicesByTenantIdAndCustomerId(tenantId.getId(), id.getId(), pageLink); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void removeEntity(DeviceEntity entity) { |
|
|
|
unassignDeviceFromCustomer(new DeviceId(entity.getId())); |
|
|
|
protected void removeEntity(Device entity) { |
|
|
|
unassignDeviceFromCustomer(new DeviceId(entity.getUuidId())); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|