|
|
|
@ -15,8 +15,8 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.dao.device; |
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.hibernate.exception.ConstraintViolationException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
@ -42,14 +42,14 @@ import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.msg.EncryptionUtil; |
|
|
|
import org.thingsboard.server.dao.entity.AbstractCachedEntityService; |
|
|
|
import org.thingsboard.server.dao.entity.CachedVersionedEntityService; |
|
|
|
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent; |
|
|
|
import org.thingsboard.server.dao.eventsourcing.SaveEntityEvent; |
|
|
|
import org.thingsboard.server.dao.exception.DataValidationException; |
|
|
|
import org.thingsboard.server.dao.resource.ImageService; |
|
|
|
import org.thingsboard.server.dao.service.DataValidator; |
|
|
|
import org.thingsboard.server.dao.service.PaginatedRemover; |
|
|
|
import org.thingsboard.server.dao.service.Validator; |
|
|
|
import org.thingsboard.server.dao.service.validator.DeviceProfileDataValidator; |
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.security.cert.Certificate; |
|
|
|
@ -69,7 +69,8 @@ import static org.thingsboard.server.dao.service.Validator.validateString; |
|
|
|
|
|
|
|
@Service("DeviceProfileDaoService") |
|
|
|
@Slf4j |
|
|
|
public class DeviceProfileServiceImpl extends AbstractCachedEntityService<DeviceProfileCacheKey, DeviceProfile, DeviceProfileEvictEvent> implements DeviceProfileService { |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class DeviceProfileServiceImpl extends CachedVersionedEntityService<DeviceProfileCacheKey, DeviceProfile, DeviceProfileEvictEvent> implements DeviceProfileService { |
|
|
|
|
|
|
|
private static final String INCORRECT_TENANT_ID = "Incorrect tenantId "; |
|
|
|
private static final String INCORRECT_DEVICE_PROFILE_ID = "Incorrect deviceProfileId "; |
|
|
|
@ -87,7 +88,7 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
private DeviceService deviceService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private DataValidator<DeviceProfile> deviceProfileValidator; |
|
|
|
private DeviceProfileDataValidator deviceProfileValidator; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private ImageService imageService; |
|
|
|
@ -95,21 +96,23 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
@TransactionalEventListener(classes = DeviceProfileEvictEvent.class) |
|
|
|
@Override |
|
|
|
public void handleEvictEvent(DeviceProfileEvictEvent event) { |
|
|
|
List<DeviceProfileCacheKey> keys = new ArrayList<>(2); |
|
|
|
keys.add(DeviceProfileCacheKey.fromName(event.getTenantId(), event.getNewName())); |
|
|
|
if (event.getDeviceProfileId() != null) { |
|
|
|
keys.add(DeviceProfileCacheKey.fromId(event.getDeviceProfileId())); |
|
|
|
List<DeviceProfileCacheKey> toEvict = new ArrayList<>(2); |
|
|
|
toEvict.add(DeviceProfileCacheKey.forName(event.getTenantId(), event.getNewName())); |
|
|
|
if (event.getSavedDeviceProfile() != null) { |
|
|
|
cache.put(DeviceProfileCacheKey.forId(event.getSavedDeviceProfile().getId()), event.getSavedDeviceProfile()); |
|
|
|
} else if (event.getDeviceProfileId() != null) { |
|
|
|
toEvict.add(DeviceProfileCacheKey.forId(event.getDeviceProfileId())); |
|
|
|
} |
|
|
|
if (event.isDefaultProfile()) { |
|
|
|
keys.add(DeviceProfileCacheKey.defaultProfile(event.getTenantId())); |
|
|
|
toEvict.add(DeviceProfileCacheKey.forDefaultProfile(event.getTenantId())); |
|
|
|
} |
|
|
|
if (StringUtils.isNotEmpty(event.getOldName()) && !event.getOldName().equals(event.getNewName())) { |
|
|
|
keys.add(DeviceProfileCacheKey.fromName(event.getTenantId(), event.getOldName())); |
|
|
|
toEvict.add(DeviceProfileCacheKey.forName(event.getTenantId(), event.getOldName())); |
|
|
|
} |
|
|
|
if (StringUtils.isNotEmpty(event.getProvisionDeviceKey())) { |
|
|
|
keys.add(DeviceProfileCacheKey.fromProvisionDeviceKey(event.getProvisionDeviceKey())); |
|
|
|
toEvict.add(DeviceProfileCacheKey.forProvisionKey(event.getProvisionDeviceKey())); |
|
|
|
} |
|
|
|
cache.evict(keys); |
|
|
|
cache.evict(toEvict); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -121,8 +124,8 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
public DeviceProfile findDeviceProfileById(TenantId tenantId, DeviceProfileId deviceProfileId, boolean putInCache) { |
|
|
|
log.trace("Executing findDeviceProfileById [{}]", deviceProfileId); |
|
|
|
validateId(deviceProfileId, id -> INCORRECT_DEVICE_PROFILE_ID + id); |
|
|
|
return cache.getOrFetchFromDB(DeviceProfileCacheKey.fromId(deviceProfileId), |
|
|
|
() -> deviceProfileDao.findById(tenantId, deviceProfileId.getId()), true, putInCache); |
|
|
|
return cache.get(DeviceProfileCacheKey.forId(deviceProfileId), |
|
|
|
() -> deviceProfileDao.findById(tenantId, deviceProfileId.getId()), putInCache); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -134,7 +137,7 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
public DeviceProfile findDeviceProfileByName(TenantId tenantId, String profileName, boolean putInCache) { |
|
|
|
log.trace("Executing findDeviceProfileByName [{}][{}]", tenantId, profileName); |
|
|
|
validateString(profileName, pn -> INCORRECT_DEVICE_PROFILE_NAME + pn); |
|
|
|
return cache.getOrFetchFromDB(DeviceProfileCacheKey.fromName(tenantId, profileName), |
|
|
|
return cache.getOrFetchFromDB(DeviceProfileCacheKey.forName(tenantId, profileName), |
|
|
|
() -> deviceProfileDao.findByName(tenantId, profileName), true, putInCache); |
|
|
|
} |
|
|
|
|
|
|
|
@ -142,7 +145,7 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
public DeviceProfile findDeviceProfileByProvisionDeviceKey(String provisionDeviceKey) { |
|
|
|
log.trace("Executing findDeviceProfileByProvisionDeviceKey provisionKey [{}]", provisionDeviceKey); |
|
|
|
validateString(provisionDeviceKey, dk -> INCORRECT_PROVISION_DEVICE_KEY + dk); |
|
|
|
return cache.getAndPutInTransaction(DeviceProfileCacheKey.fromProvisionDeviceKey(provisionDeviceKey), |
|
|
|
return cache.getAndPutInTransaction(DeviceProfileCacheKey.forProvisionKey(provisionDeviceKey), |
|
|
|
() -> deviceProfileDao.findByProvisionDeviceKey(provisionDeviceKey), false); |
|
|
|
} |
|
|
|
|
|
|
|
@ -179,7 +182,7 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
savedDeviceProfile = deviceProfileDao.saveAndFlush(deviceProfile.getTenantId(), deviceProfile); |
|
|
|
publishEvictEvent(new DeviceProfileEvictEvent(savedDeviceProfile.getTenantId(), savedDeviceProfile.getName(), |
|
|
|
oldDeviceProfile != null ? oldDeviceProfile.getName() : null, savedDeviceProfile.getId(), savedDeviceProfile.isDefault(), |
|
|
|
oldDeviceProfile != null ? oldDeviceProfile.getProvisionDeviceKey() : null)); |
|
|
|
oldDeviceProfile != null ? oldDeviceProfile.getProvisionDeviceKey() : null, savedDeviceProfile)); |
|
|
|
if (publishSaveEvent) { |
|
|
|
eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(savedDeviceProfile.getTenantId()).entityId(savedDeviceProfile.getId()) |
|
|
|
.entity(savedDeviceProfile).oldEntity(oldDeviceProfile).created(oldDeviceProfile == null).build()); |
|
|
|
@ -241,13 +244,9 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
null, deviceProfile.getId(), deviceProfile.isDefault(), |
|
|
|
deviceProfile.getProvisionDeviceKey())); |
|
|
|
eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId).entityId(deviceProfileId).entity(deviceProfile).build()); |
|
|
|
} catch (Exception t) { |
|
|
|
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); |
|
|
|
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_device_profile")) { |
|
|
|
throw new DataValidationException("The device profile referenced by the devices cannot be deleted!"); |
|
|
|
} else { |
|
|
|
throw t; |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
checkConstraintViolation(e, "fk_device_profile", "The device profile referenced by the devices cannot be deleted!"); |
|
|
|
throw e; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -316,7 +315,7 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device |
|
|
|
public DeviceProfile findDefaultDeviceProfile(TenantId tenantId) { |
|
|
|
log.trace("Executing findDefaultDeviceProfile tenantId [{}]", tenantId); |
|
|
|
validateId(tenantId, id -> INCORRECT_TENANT_ID + id); |
|
|
|
return cache.getAndPutInTransaction(DeviceProfileCacheKey.defaultProfile(tenantId), |
|
|
|
return cache.getAndPutInTransaction(DeviceProfileCacheKey.forDefaultProfile(tenantId), |
|
|
|
() -> deviceProfileDao.findDefaultDeviceProfile(tenantId), true); |
|
|
|
} |
|
|
|
|
|
|
|
|