use of org.springframework.cache.annotation.CacheEvict in project thingsboard by thingsboard.
the class DeviceServiceImpl method saveDevice.
@Override
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#profile.tenantId, #provisionRequest.deviceName}")
@Transactional
public Device saveDevice(ProvisionRequest provisionRequest, DeviceProfile profile) {
Device device = new Device();
device.setName(provisionRequest.getDeviceName());
device.setType(profile.getName());
device.setTenantId(profile.getTenantId());
Device savedDevice = saveDevice(device);
if (!StringUtils.isEmpty(provisionRequest.getCredentialsData().getToken()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getX509CertHash()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getUsername()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getPassword()) || !StringUtils.isEmpty(provisionRequest.getCredentialsData().getClientId())) {
DeviceCredentials deviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getTenantId(), savedDevice.getId());
if (deviceCredentials == null) {
deviceCredentials = new DeviceCredentials();
}
deviceCredentials.setDeviceId(savedDevice.getId());
deviceCredentials.setCredentialsType(provisionRequest.getCredentialsType());
switch(provisionRequest.getCredentialsType()) {
case ACCESS_TOKEN:
deviceCredentials.setCredentialsId(provisionRequest.getCredentialsData().getToken());
break;
case MQTT_BASIC:
BasicMqttCredentials mqttCredentials = new BasicMqttCredentials();
mqttCredentials.setClientId(provisionRequest.getCredentialsData().getClientId());
mqttCredentials.setUserName(provisionRequest.getCredentialsData().getUsername());
mqttCredentials.setPassword(provisionRequest.getCredentialsData().getPassword());
deviceCredentials.setCredentialsValue(JacksonUtil.toString(mqttCredentials));
break;
case X509_CERTIFICATE:
deviceCredentials.setCredentialsValue(provisionRequest.getCredentialsData().getX509CertHash());
break;
case LWM2M_CREDENTIALS:
break;
}
try {
deviceCredentialsService.updateDeviceCredentials(savedDevice.getTenantId(), deviceCredentials);
} catch (Exception e) {
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
}
}
// eviction by name is described as annotation @CacheEvict above
cacheManager.removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId());
return savedDevice;
}
use of org.springframework.cache.annotation.CacheEvict in project thingsboard by thingsboard.
the class EdgeServiceImpl method saveEdge.
@CacheEvict(cacheNames = EDGE_CACHE, key = "{#edge.tenantId, #edge.name}")
@Override
public Edge saveEdge(Edge edge) {
log.trace("Executing saveEdge [{}]", edge);
edgeValidator.validate(edge, Edge::getTenantId);
try {
return edgeDao.save(edge.getTenantId(), edge);
} catch (Exception t) {
ConstraintViolationException e = extractConstraintViolationException(t).orElse(null);
if (e != null && e.getConstraintName() != null && e.getConstraintName().equalsIgnoreCase("edge_name_unq_key")) {
throw new DataValidationException("Edge with such name already exists!");
} else {
throw t;
}
}
}
use of org.springframework.cache.annotation.CacheEvict in project thingsboard by thingsboard.
the class EntityViewServiceImpl method assignEntityViewToCustomer.
@CacheEvict(cacheNames = ENTITY_VIEW_CACHE, key = "{#entityViewId}")
@Override
public EntityView assignEntityViewToCustomer(TenantId tenantId, EntityViewId entityViewId, CustomerId customerId) {
EntityView entityView = findEntityViewById(tenantId, entityViewId);
entityView.setCustomerId(customerId);
return saveEntityView(entityView);
}
use of org.springframework.cache.annotation.CacheEvict in project cu-kfs by CU-CommunityApps.
the class DocumentTypeServiceImpl method versionAndSave.
@CacheEvict(value = { DocumentType.CACHE_NAME, Permission.CACHE_NAME }, allEntries = true)
public void versionAndSave(DocumentType documentType) {
// is going to be an update and not a create just throw and exception to be on the safe side
if (documentType.getDocumentTypeId() != null && documentType.getVersionNumber() != null) {
throw new RuntimeException("DocumentType configured for update and not versioning which we support");
}
// grab the old document. Don't Use Cached Version!
DocumentType oldDocumentType = findByName(documentType.getName());
// reset the children on the oldDocumentType
// oldDocumentType.resetChildren();
String existingDocTypeId = null;
if (oldDocumentType != null) {
existingDocTypeId = oldDocumentType.getDocumentTypeId();
// set version number on the new doc type using the max version from the database
Integer maxVersionNumber = documentTypeDAO.getMaxVersionNumber(documentType.getName());
documentType.setVersion(maxVersionNumber != null ? Integer.valueOf(maxVersionNumber + 1) : Integer.valueOf(0));
oldDocumentType.setCurrentInd(Boolean.FALSE);
if (LOG.isInfoEnabled()) {
LOG.info("Saving old document type Id " + oldDocumentType.getDocumentTypeId() + " name '" + oldDocumentType.getName() + "' (current = " + oldDocumentType.getCurrentInd() + ")");
}
save(oldDocumentType);
}
// check to see that no current documents exist in database
if (!CollectionUtils.isEmpty(documentTypeDAO.findAllCurrentByName(documentType.getName()))) {
String errorMsg = "Found invalid 'current' document with name '" + documentType.getName() + "'. None should exist.";
LOG.error(errorMsg);
throw new RuntimeException(errorMsg);
}
// set up the previous current doc type on the new doc type
documentType.setPreviousVersionId(existingDocTypeId);
documentType.setCurrentInd(Boolean.TRUE);
save(documentType);
if (LOG.isInfoEnabled()) {
LOG.info("Saved current document type Id " + documentType.getDocumentTypeId() + " name '" + documentType.getName() + "' (current = " + documentType.getCurrentInd() + ")");
}
// attach the children to this new parent. cloning the children would probably be a better way to go here...
if (ObjectUtils.isNotNull(existingDocTypeId)) {
// documentType.getPreviousVersion() should not be null at this point
for (DocumentType child : getChildDocumentTypes(existingDocTypeId)) {
child.setDocTypeParentId(documentType.getDocumentTypeId());
save(child);
if (LOG.isInfoEnabled()) {
LOG.info("Saved child document type Id " + child.getDocumentTypeId() + " name '" + child.getName() + "' (parent = " + child.getDocTypeParentId() + ", current = " + child.getCurrentInd() + ")");
}
}
}
// the db and not from the cache
if (documentType.getDocTypeParentId() != null) {
DocumentType parent = getDocumentTypeDAO().findById(documentType.getDocTypeParentId());
save(parent);
if (LOG.isInfoEnabled()) {
LOG.info("Saved parent document type Id " + parent.getDocumentTypeId() + " name '" + parent.getName() + "' (current = " + parent.getCurrentInd() + ")");
}
}
}
use of org.springframework.cache.annotation.CacheEvict in project fw-cloud-framework by liuweijw.
the class DeptServiceImpl method delById.
@Override
@Transactional
@CacheEvict(allEntries = true)
public boolean delById(Integer id) {
if (null == id || id < 0)
return false;
QDept qDept = QDept.dept;
Long num = this.queryFactory.update(qDept).set(qDept.statu, 1).where(qDept.deptId.eq(id)).execute();
return null != num && num > 0;
}
Aggregations