'Create' method for dao (used for audit logs, notifications, alarm comments)
This commit is contained in:
		
							parent
							
								
									a8866ba387
								
							
						
					
					
						commit
						e6d08e6528
					
				@ -39,6 +39,8 @@ public interface Dao<T> {
 | 
			
		||||
 | 
			
		||||
    T saveAndFlush(TenantId tenantId, T t);
 | 
			
		||||
 | 
			
		||||
    T create(TenantId tenantId, T t);
 | 
			
		||||
 | 
			
		||||
    boolean removeById(TenantId tenantId, UUID id);
 | 
			
		||||
 | 
			
		||||
    void removeAllByIds(Collection<UUID> ids);
 | 
			
		||||
 | 
			
		||||
@ -19,14 +19,19 @@ import com.datastax.oss.driver.api.core.uuid.Uuids;
 | 
			
		||||
import com.google.common.collect.Lists;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.data.jpa.repository.JpaRepository;
 | 
			
		||||
import org.springframework.transaction.annotation.Transactional;
 | 
			
		||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
 | 
			
		||||
import org.springframework.transaction.support.TransactionTemplate;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.dao.Dao;
 | 
			
		||||
import org.thingsboard.server.dao.DaoUtil;
 | 
			
		||||
import org.thingsboard.server.dao.model.BaseEntity;
 | 
			
		||||
import org.thingsboard.server.dao.util.SqlDao;
 | 
			
		||||
 | 
			
		||||
import javax.persistence.EntityManager;
 | 
			
		||||
import javax.persistence.PersistenceContext;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
@ -41,6 +46,12 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
 | 
			
		||||
        extends JpaAbstractDaoListeningExecutorService
 | 
			
		||||
        implements Dao<D> {
 | 
			
		||||
 | 
			
		||||
    @PersistenceContext
 | 
			
		||||
    private EntityManager entityManager;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private TransactionTemplate transactionTemplate;
 | 
			
		||||
 | 
			
		||||
    protected abstract Class<E> getEntityClass();
 | 
			
		||||
 | 
			
		||||
    protected abstract JpaRepository<E, UUID> getRepository();
 | 
			
		||||
@ -51,6 +62,33 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
 | 
			
		||||
    @Override
 | 
			
		||||
    @Transactional
 | 
			
		||||
    public D save(TenantId tenantId, D domain) {
 | 
			
		||||
        E entity = prepare(domain);
 | 
			
		||||
        entity = getRepository().save(entity);
 | 
			
		||||
        return DaoUtil.getData(entity);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Transactional
 | 
			
		||||
    public D saveAndFlush(TenantId tenantId, D domain) {
 | 
			
		||||
        D d = save(tenantId, domain);
 | 
			
		||||
        getRepository().flush();
 | 
			
		||||
        return d;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public D create(TenantId tenantId, D domain) {
 | 
			
		||||
        E entity = prepare(domain);
 | 
			
		||||
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
 | 
			
		||||
            entityManager.persist(entity);
 | 
			
		||||
        } else {
 | 
			
		||||
            transactionTemplate.executeWithoutResult(ts -> {
 | 
			
		||||
                entityManager.persist(entity);
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        return DaoUtil.getData(entity);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private E prepare(D domain) {
 | 
			
		||||
        E entity;
 | 
			
		||||
        try {
 | 
			
		||||
            entity = getEntityClass().getConstructor(domain.getClass()).newInstance(domain);
 | 
			
		||||
@ -65,16 +103,7 @@ public abstract class JpaAbstractDao<E extends BaseEntity<D>, D>
 | 
			
		||||
            entity.setUuid(uuid);
 | 
			
		||||
            entity.setCreatedTime(Uuids.unixTimestamp(uuid));
 | 
			
		||||
        }
 | 
			
		||||
        entity = getRepository().save(entity);
 | 
			
		||||
        return DaoUtil.getData(entity);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Transactional
 | 
			
		||||
    public D saveAndFlush(TenantId tenantId, D domain) {
 | 
			
		||||
        D d = save(tenantId, domain);
 | 
			
		||||
        getRepository().flush();
 | 
			
		||||
        return d;
 | 
			
		||||
        return entity;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
@ -57,8 +57,7 @@ public class JpaAlarmCommentDao extends JpaAbstractDao<AlarmCommentEntity, Alarm
 | 
			
		||||
    public AlarmComment createAlarmComment(TenantId tenantId, AlarmComment alarmComment){
 | 
			
		||||
        log.trace("Saving entity {}", alarmComment);
 | 
			
		||||
        partitioningRepository.createPartitionIfNotExists(ALARM_COMMENT_TABLE_NAME, alarmComment.getCreatedTime(), TimeUnit.HOURS.toMillis(partitionSizeInHours));
 | 
			
		||||
        AlarmCommentEntity saved = alarmCommentRepository.save(new AlarmCommentEntity(alarmComment));
 | 
			
		||||
        return DaoUtil.getData(saved);
 | 
			
		||||
        return create(tenantId, alarmComment);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
@ -41,7 +41,6 @@ import org.thingsboard.server.dao.sqlts.insert.sql.SqlPartitioningRepository;
 | 
			
		||||
import org.thingsboard.server.dao.util.SqlDao;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
@ -88,7 +87,7 @@ public class JpaAuditLogDao extends JpaAbstractDao<AuditLogEntity, AuditLog> imp
 | 
			
		||||
            auditLog.setCreatedTime(Uuids.unixTimestamp(uuid));
 | 
			
		||||
        }
 | 
			
		||||
        partitioningRepository.createPartitionIfNotExists(TABLE_NAME, auditLog.getCreatedTime(), TimeUnit.HOURS.toMillis(partitionSizeInHours));
 | 
			
		||||
        return super.save(tenantId, auditLog);
 | 
			
		||||
        return create(tenantId, auditLog);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 | 
			
		||||
@ -16,7 +16,6 @@
 | 
			
		||||
package org.thingsboard.server.dao.sql.notification;
 | 
			
		||||
 | 
			
		||||
import com.datastax.oss.driver.api.core.uuid.Uuids;
 | 
			
		||||
import com.google.common.base.Strings;
 | 
			
		||||
import lombok.RequiredArgsConstructor;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Value;
 | 
			
		||||
import org.springframework.data.jpa.repository.JpaRepository;
 | 
			
		||||
@ -60,6 +59,7 @@ public class JpaNotificationDao extends JpaAbstractDao<NotificationEntity, Notif
 | 
			
		||||
            notification.setCreatedTime(Uuids.unixTimestamp(uuid));
 | 
			
		||||
            partitioningRepository.createPartitionIfNotExists(ModelConstants.NOTIFICATION_TABLE_NAME,
 | 
			
		||||
                    notification.getCreatedTime(), TimeUnit.HOURS.toMillis(partitionSizeInHours));
 | 
			
		||||
            return create(tenantId, notification);
 | 
			
		||||
        }
 | 
			
		||||
        return super.save(tenantId, notification);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user