|
|
|
@ -20,7 +20,10 @@ import lombok.AllArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.hibernate.exception.ConstraintViolationException; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.event.TransactionalEventListener; |
|
|
|
import org.thingsboard.server.cache.resourceinfo.ResourceInfoEvictEvent; |
|
|
|
import org.thingsboard.server.common.data.EntityType; |
|
|
|
import org.thingsboard.server.cache.resourceinfo.ResourceInfoCacheKey; |
|
|
|
import org.thingsboard.server.common.data.ResourceType; |
|
|
|
import org.thingsboard.server.common.data.TbResource; |
|
|
|
import org.thingsboard.server.common.data.TbResourceInfo; |
|
|
|
@ -31,6 +34,7 @@ import org.thingsboard.server.common.data.id.TbResourceId; |
|
|
|
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.dao.entity.AbstractCachedEntityService; |
|
|
|
import org.thingsboard.server.dao.exception.DataValidationException; |
|
|
|
import org.thingsboard.server.dao.service.DataValidator; |
|
|
|
import org.thingsboard.server.dao.service.PaginatedRemover; |
|
|
|
@ -45,7 +49,7 @@ import static org.thingsboard.server.dao.service.Validator.validateId; |
|
|
|
@Service("TbResourceDaoService") |
|
|
|
@Slf4j |
|
|
|
@AllArgsConstructor |
|
|
|
public class BaseResourceService implements ResourceService { |
|
|
|
public class BaseResourceService extends AbstractCachedEntityService<ResourceInfoCacheKey, TbResourceInfo, ResourceInfoEvictEvent> implements ResourceService { |
|
|
|
|
|
|
|
public static final String INCORRECT_RESOURCE_ID = "Incorrect resourceId "; |
|
|
|
private final TbResourceDao resourceDao; |
|
|
|
@ -55,10 +59,12 @@ public class BaseResourceService implements ResourceService { |
|
|
|
@Override |
|
|
|
public TbResource saveResource(TbResource resource) { |
|
|
|
resourceValidator.validate(resource, TbResourceInfo::getTenantId); |
|
|
|
|
|
|
|
try { |
|
|
|
return resourceDao.save(resource.getTenantId(), resource); |
|
|
|
TbResource saved = resourceDao.save(resource.getTenantId(), resource); |
|
|
|
publishEvictEvent(new ResourceInfoEvictEvent(new ResourceInfoCacheKey(resource.getTenantId(), resource.getId()))); |
|
|
|
return saved; |
|
|
|
} catch (Exception t) { |
|
|
|
publishEvictEvent(new ResourceInfoEvictEvent(new ResourceInfoCacheKey(resource.getTenantId(), resource.getId()))); |
|
|
|
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null); |
|
|
|
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("resource_unq_key")) { |
|
|
|
String field = ResourceType.LWM2M_MODEL.equals(resource.getResourceType()) ? "resourceKey" : "fileName"; |
|
|
|
@ -86,7 +92,9 @@ public class BaseResourceService implements ResourceService { |
|
|
|
public TbResourceInfo findResourceInfoById(TenantId tenantId, TbResourceId resourceId) { |
|
|
|
log.trace("Executing findResourceInfoById [{}] [{}]", tenantId, resourceId); |
|
|
|
Validator.validateId(resourceId, INCORRECT_RESOURCE_ID + resourceId); |
|
|
|
return resourceInfoDao.findById(tenantId, resourceId.getId()); |
|
|
|
|
|
|
|
return cache.getAndPutInTransaction(new ResourceInfoCacheKey(tenantId, resourceId), |
|
|
|
() -> resourceInfoDao.findById(tenantId, resourceId.getId()), true); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ -169,13 +177,9 @@ public class BaseResourceService implements ResourceService { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
protected Optional<ConstraintViolationException> extractConstraintViolationException(Exception t) { |
|
|
|
if (t instanceof ConstraintViolationException) { |
|
|
|
return Optional.of((ConstraintViolationException) t); |
|
|
|
} else if (t.getCause() instanceof ConstraintViolationException) { |
|
|
|
return Optional.of((ConstraintViolationException) (t.getCause())); |
|
|
|
} else { |
|
|
|
return Optional.empty(); |
|
|
|
} |
|
|
|
@TransactionalEventListener(classes = ResourceInfoCacheKey.class) |
|
|
|
@Override |
|
|
|
public void handleEvictEvent(ResourceInfoEvictEvent event) { |
|
|
|
cache.evict(event.getKey()); |
|
|
|
} |
|
|
|
} |
|
|
|
|