Fix caching for deviceProfile provision key: no need to safe if null value cuz its crashes
This commit is contained in:
		
							parent
							
								
									38ee1e01e0
								
							
						
					
					
						commit
						94b9e43aae
					
				@ -16,6 +16,7 @@
 | 
			
		||||
package org.thingsboard.server.dao.device;
 | 
			
		||||
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.thingsboard.server.common.data.StringUtils;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceProfileId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
 | 
			
		||||
@ -65,7 +66,7 @@ public class DeviceProfileCacheKey implements Serializable {
 | 
			
		||||
            return deviceProfileId.toString();
 | 
			
		||||
        } else if (defaultProfile) {
 | 
			
		||||
            return tenantId.toString();
 | 
			
		||||
        } else if (provisionDeviceKey != null) {
 | 
			
		||||
        } else if (StringUtils.isNotEmpty(provisionDeviceKey)) {
 | 
			
		||||
            return provisionDeviceKey;
 | 
			
		||||
        }
 | 
			
		||||
        return tenantId + "_" + name;
 | 
			
		||||
 | 
			
		||||
@ -27,6 +27,7 @@ public class DeviceProfileEvictEvent {
 | 
			
		||||
    private final String oldName;
 | 
			
		||||
    private final DeviceProfileId deviceProfileId;
 | 
			
		||||
    private final boolean defaultProfile;
 | 
			
		||||
    private final String provisionDeviceKey;
 | 
			
		||||
    private final String newProvisionDeviceKey;
 | 
			
		||||
    private final String oldProvisionDeviceKey;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -103,8 +103,8 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
 | 
			
		||||
        if (StringUtils.isNotEmpty(event.getOldName()) && !event.getOldName().equals(event.getNewName())) {
 | 
			
		||||
            keys.add(DeviceProfileCacheKey.fromName(event.getTenantId(), event.getOldName()));
 | 
			
		||||
        }
 | 
			
		||||
        if (event.getProvisionDeviceKey() != null) {
 | 
			
		||||
            keys.add(DeviceProfileCacheKey.fromProvisionDeviceKey(event.getProvisionDeviceKey()));
 | 
			
		||||
        if (StringUtils.isNotEmpty(event.getOldProvisionDeviceKey()) && !event.getOldProvisionDeviceKey().equals(event.getNewProvisionDeviceKey())) {
 | 
			
		||||
            keys.add(DeviceProfileCacheKey.fromProvisionDeviceKey(event.getOldProvisionDeviceKey()));
 | 
			
		||||
        }
 | 
			
		||||
        cache.evict(keys);
 | 
			
		||||
    }
 | 
			
		||||
@ -125,6 +125,14 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
 | 
			
		||||
                () -> deviceProfileDao.findByName(tenantId, profileName), true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public DeviceProfile findDeviceProfileByProvisionDeviceKey(String provisionDeviceKey) {
 | 
			
		||||
        log.trace("Executing findDeviceProfileByProvisionDeviceKey provisionKey [{}]", provisionDeviceKey);
 | 
			
		||||
        validateString(provisionDeviceKey, INCORRECT_PROVISION_DEVICE_KEY + provisionDeviceKey);
 | 
			
		||||
        return cache.getAndPutInTransaction(DeviceProfileCacheKey.fromProvisionDeviceKey(provisionDeviceKey),
 | 
			
		||||
                () -> deviceProfileDao.findByProvisionDeviceKey(provisionDeviceKey), false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public DeviceProfileInfo findDeviceProfileInfoById(TenantId tenantId, DeviceProfileId deviceProfileId) {
 | 
			
		||||
        log.trace("Executing findDeviceProfileById [{}]", deviceProfileId);
 | 
			
		||||
@ -147,11 +155,11 @@ 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(),
 | 
			
		||||
                    deviceProfile.getProvisionDeviceKey() != null ? deviceProfile.getProvisionDeviceKey() : null));
 | 
			
		||||
                    savedDeviceProfile.getProvisionDeviceKey(), oldDeviceProfile != null ? oldDeviceProfile.getProvisionDeviceKey() : null));
 | 
			
		||||
        } catch (Exception t) {
 | 
			
		||||
            handleEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(),
 | 
			
		||||
                    oldDeviceProfile != null ? oldDeviceProfile.getName() : null, null, deviceProfile.isDefault(),
 | 
			
		||||
                    deviceProfile.getProvisionDeviceKey() != null ? deviceProfile.getProvisionDeviceKey() : null));
 | 
			
		||||
                    deviceProfile.getProvisionDeviceKey(), oldDeviceProfile != null ? oldDeviceProfile.getProvisionDeviceKey() : null));
 | 
			
		||||
            checkConstraintViolation(t,
 | 
			
		||||
                    Map.of("device_profile_name_unq_key", DEVICE_PROFILE_WITH_SUCH_NAME_ALREADY_EXISTS,
 | 
			
		||||
                            "device_provision_key_unq_key", "Device profile with such provision device key already exists!",
 | 
			
		||||
@ -192,7 +200,7 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
 | 
			
		||||
            deviceProfileDao.removeById(tenantId, deviceProfileId.getId());
 | 
			
		||||
            publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(),
 | 
			
		||||
                    null, deviceProfile.getId(), deviceProfile.isDefault(),
 | 
			
		||||
                    deviceProfile.getProvisionDeviceKey() != null ? deviceProfile.getProvisionDeviceKey() : null));
 | 
			
		||||
                    deviceProfile.getProvisionDeviceKey(), null));
 | 
			
		||||
        } catch (Exception t) {
 | 
			
		||||
            ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
 | 
			
		||||
            if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("fk_device_profile")) {
 | 
			
		||||
@ -219,14 +227,6 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
 | 
			
		||||
        return deviceProfileDao.findDeviceProfileInfos(tenantId, pageLink, transportType);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public DeviceProfile findDeviceProfileByProvisionDeviceKey(String provisionDeviceKey) {
 | 
			
		||||
        log.trace("Executing findDeviceProfileByProvisionDeviceKey provisionKey [{}]", provisionDeviceKey);
 | 
			
		||||
        validateString(provisionDeviceKey, INCORRECT_PROVISION_DEVICE_KEY + provisionDeviceKey);
 | 
			
		||||
        return cache.getAndPutInTransaction(DeviceProfileCacheKey.fromProvisionDeviceKey(provisionDeviceKey),
 | 
			
		||||
                () -> deviceProfileDao.findByProvisionDeviceKey(provisionDeviceKey), true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public DeviceProfile findOrCreateDeviceProfile(TenantId tenantId, String name) {
 | 
			
		||||
        log.trace("Executing findOrCreateDefaultDeviceProfile");
 | 
			
		||||
@ -298,14 +298,14 @@ public class DeviceProfileServiceImpl extends AbstractCachedEntityService<Device
 | 
			
		||||
            boolean changed = false;
 | 
			
		||||
            if (previousDefaultDeviceProfile == null) {
 | 
			
		||||
                deviceProfileDao.save(tenantId, deviceProfile);
 | 
			
		||||
                publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), null, deviceProfile.getId(), true, null));
 | 
			
		||||
                publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), null, deviceProfile.getId(), true, deviceProfile.getProvisionDeviceKey(), null));
 | 
			
		||||
                changed = true;
 | 
			
		||||
            } else if (!previousDefaultDeviceProfile.getId().equals(deviceProfile.getId())) {
 | 
			
		||||
                previousDefaultDeviceProfile.setDefault(false);
 | 
			
		||||
                deviceProfileDao.save(tenantId, previousDefaultDeviceProfile);
 | 
			
		||||
                deviceProfileDao.save(tenantId, deviceProfile);
 | 
			
		||||
                publishEvictEvent(new DeviceProfileEvictEvent(previousDefaultDeviceProfile.getTenantId(), previousDefaultDeviceProfile.getName(), null, previousDefaultDeviceProfile.getId(), false, null));
 | 
			
		||||
                publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), null, deviceProfile.getId(), true, null));
 | 
			
		||||
                publishEvictEvent(new DeviceProfileEvictEvent(previousDefaultDeviceProfile.getTenantId(), previousDefaultDeviceProfile.getName(), null, previousDefaultDeviceProfile.getId(), false, deviceProfile.getProvisionDeviceKey(), null));
 | 
			
		||||
                publishEvictEvent(new DeviceProfileEvictEvent(deviceProfile.getTenantId(), deviceProfile.getName(), null, deviceProfile.getId(), true, deviceProfile.getProvisionDeviceKey(), null));
 | 
			
		||||
                changed = true;
 | 
			
		||||
            }
 | 
			
		||||
            return changed;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user