Search in sources :

Example 56 with CacheEvict

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;
}
Also used : BasicMqttCredentials(org.thingsboard.server.common.data.device.credentials.BasicMqttCredentials) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) ProvisionFailedException(org.thingsboard.server.dao.device.provision.ProvisionFailedException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ExecutionException(java.util.concurrent.ExecutionException) CacheEvict(org.springframework.cache.annotation.CacheEvict) Transactional(org.springframework.transaction.annotation.Transactional)

Example 57 with CacheEvict

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;
        }
    }
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) Edge(org.thingsboard.server.common.data.edge.Edge) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Example 58 with CacheEvict

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);
}
Also used : EntityView(org.thingsboard.server.common.data.EntityView) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Example 59 with CacheEvict

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() + ")");
        }
    }
}
Also used : DocumentType(org.kuali.kfs.kew.doctype.bo.DocumentType) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Example 60 with CacheEvict

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;
}
Also used : QDept(com.github.liuweijw.business.admin.domain.QDept) CacheEvict(org.springframework.cache.annotation.CacheEvict) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CacheEvict (org.springframework.cache.annotation.CacheEvict)64 Transactional (org.springframework.transaction.annotation.Transactional)17 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)14 Date (java.util.Date)8 ArrayList (java.util.ArrayList)6 CacheInfoEvict (org.entando.entando.aps.system.services.cache.CacheInfoEvict)5 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)5 EntityView (org.thingsboard.server.common.data.EntityView)4 QUser (com.github.liuweijw.business.admin.domain.QUser)3 MetacatRequestContext (com.netflix.metacat.common.MetacatRequestContext)3 ConnectorRequestContext (com.netflix.metacat.common.server.connectors.ConnectorRequestContext)3 ConnectorTableService (com.netflix.metacat.common.server.connectors.ConnectorTableService)3 ExecutionException (java.util.concurrent.ExecutionException)3 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)3 QMenu (com.github.liuweijw.business.admin.domain.QMenu)2 QRole (com.github.liuweijw.business.admin.domain.QRole)2 QUserRole (com.github.liuweijw.business.admin.domain.QUserRole)2 Role (com.github.liuweijw.business.admin.domain.Role)2 User (com.github.liuweijw.business.admin.domain.User)2 UserRole (com.github.liuweijw.business.admin.domain.UserRole)2