Refactored to use events
This commit is contained in:
parent
d4fcd4f814
commit
97d8171f07
@ -19,6 +19,7 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.event.TransactionalEventListener;
|
||||||
import org.thingsboard.common.util.JacksonUtil;
|
import org.thingsboard.common.util.JacksonUtil;
|
||||||
import org.thingsboard.server.common.data.EntityType;
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
import org.thingsboard.server.common.data.User;
|
import org.thingsboard.server.common.data.User;
|
||||||
@ -33,9 +34,11 @@ import org.thingsboard.server.common.data.alarm.AlarmUpdateRequest;
|
|||||||
import org.thingsboard.server.common.data.audit.ActionType;
|
import org.thingsboard.server.common.data.audit.ActionType;
|
||||||
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
|
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
|
||||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||||
import org.thingsboard.server.common.data.id.EdgeId;
|
import org.thingsboard.server.common.data.id.EntityId;
|
||||||
import org.thingsboard.server.common.data.id.TenantId;
|
import org.thingsboard.server.common.data.id.TenantId;
|
||||||
import org.thingsboard.server.common.data.id.UserId;
|
import org.thingsboard.server.common.data.id.UserId;
|
||||||
|
import org.thingsboard.server.dao.eventsourcing.DeleteEntityEvent;
|
||||||
|
import org.thingsboard.server.dao.user.UserService;
|
||||||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService;
|
import org.thingsboard.server.service.entitiy.AbstractTbEntityService;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -48,6 +51,9 @@ public class DefaultTbAlarmService extends AbstractTbEntityService implements Tb
|
|||||||
@Autowired
|
@Autowired
|
||||||
protected TbAlarmCommentService alarmCommentService;
|
protected TbAlarmCommentService alarmCommentService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
protected UserService userService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Alarm save(Alarm alarm, User user) throws ThingsboardException {
|
public Alarm save(Alarm alarm, User user) throws ThingsboardException {
|
||||||
ActionType actionType = alarm.getId() == null ? ActionType.ADDED : ActionType.UPDATED;
|
ActionType actionType = alarm.getId() == null ? ActionType.ADDED : ActionType.UPDATED;
|
||||||
@ -216,40 +222,6 @@ public class DefaultTbAlarmService extends AbstractTbEntityService implements Tb
|
|||||||
return alarmInfo;
|
return alarmInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void unassignUserAlarms(TenantId tenantId, User user, long unassignTs) {
|
|
||||||
AlarmQueryV2 alarmQuery = AlarmQueryV2.builder().assigneeId(user.getId()).pageLink(new TimePageLink(Integer.MAX_VALUE)).build();
|
|
||||||
try {
|
|
||||||
List<AlarmInfo> alarms = alarmService.findAlarmsV2(tenantId, alarmQuery).get(30, TimeUnit.SECONDS).getData();
|
|
||||||
for (AlarmInfo alarm : alarms) {
|
|
||||||
AlarmApiCallResult result = alarmSubscriptionService.unassignAlarm(tenantId, alarm.getId(), getOrDefault(unassignTs));
|
|
||||||
if (!result.isSuccessful()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (result.isModified()) {
|
|
||||||
AlarmComment alarmComment = AlarmComment.builder()
|
|
||||||
.alarmId(alarm.getId())
|
|
||||||
.type(AlarmCommentType.SYSTEM)
|
|
||||||
.comment(JacksonUtil.newObjectNode().put("text", String.format("Alarm was unassigned because user %s - was deleted",
|
|
||||||
(user.getFirstName() == null || user.getLastName() == null) ? user.getName() : user.getFirstName() + " " + user.getLastName()))
|
|
||||||
.put("userId", user.getId().toString())
|
|
||||||
.put("subtype", "ASSIGN"))
|
|
||||||
.build();
|
|
||||||
try {
|
|
||||||
alarmCommentService.saveAlarmComment(alarm, alarmComment, user);
|
|
||||||
} catch (ThingsboardException e) {
|
|
||||||
log.error("Failed to save alarm comment", e);
|
|
||||||
}
|
|
||||||
notificationEntityService.logEntityAction(alarm.getTenantId(), alarm.getOriginator(), result.getAlarm(),
|
|
||||||
alarm.getCustomerId(), ActionType.ALARM_UNASSIGNED, user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean delete(Alarm alarm, User user) {
|
public Boolean delete(Alarm alarm, User user) {
|
||||||
TenantId tenantId = alarm.getTenantId();
|
TenantId tenantId = alarm.getTenantId();
|
||||||
@ -258,6 +230,48 @@ public class DefaultTbAlarmService extends AbstractTbEntityService implements Tb
|
|||||||
return alarmSubscriptionService.deleteAlarm(tenantId, alarm.getId());
|
return alarmSubscriptionService.deleteAlarm(tenantId, alarm.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TransactionalEventListener
|
||||||
|
public void unassignDeletedUserAlarms(UserId userId) {
|
||||||
|
List<Alarm> alarms = alarmService.findAlarmsByAssigneeId(userId);
|
||||||
|
for (Alarm alarm : alarms) {
|
||||||
|
AlarmApiCallResult result = alarmSubscriptionService.unassignAlarm(alarm.getTenantId(), alarm.getId(), System.currentTimeMillis());
|
||||||
|
if (!result.isSuccessful()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (result.isModified()) {
|
||||||
|
try {
|
||||||
|
AlarmComment alarmComment = AlarmComment.builder()
|
||||||
|
.alarmId(alarm.getId())
|
||||||
|
.type(AlarmCommentType.SYSTEM)
|
||||||
|
.comment(JacksonUtil.newObjectNode()
|
||||||
|
.put("text", String.format("Alarm was unassigned because user with id %s - was deleted",
|
||||||
|
userId.toString()))
|
||||||
|
.put("userId", userId.toString())
|
||||||
|
.put("subtype", "ASSIGN"))
|
||||||
|
.build();
|
||||||
|
alarmCommentService.saveAlarmComment(alarm, alarmComment, null);
|
||||||
|
} catch (ThingsboardException e) {
|
||||||
|
log.error("Failed to save alarm comment", e);
|
||||||
|
}
|
||||||
|
notificationEntityService.logEntityAction(alarm.getTenantId(), alarm.getOriginator(), result.getAlarm(),
|
||||||
|
alarm.getCustomerId(), ActionType.ALARM_UNASSIGNED, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TransactionalEventListener(fallbackExecution = true)
|
||||||
|
public void handleEvent(DeleteEntityEvent<?> event) {
|
||||||
|
try {
|
||||||
|
log.trace("[{}] DeleteEntityEvent called: {}", event.getTenantId(), event);
|
||||||
|
EntityId entityId = event.getEntityId();
|
||||||
|
if (EntityType.USER.equals(entityId.getEntityType())) {
|
||||||
|
unassignDeletedUserAlarms((UserId) entityId);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("[{}] failed to process DeleteEntityEvent: {}", event.getTenantId(), event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static long getOrDefault(long ts) {
|
private static long getOrDefault(long ts) {
|
||||||
return ts > 0 ? ts : System.currentTimeMillis();
|
return ts > 0 ? ts : System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -39,6 +39,7 @@ import org.thingsboard.server.common.data.query.AlarmDataQuery;
|
|||||||
import org.thingsboard.server.dao.entity.EntityDaoService;
|
import org.thingsboard.server.dao.entity.EntityDaoService;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
public interface AlarmService extends EntityDaoService {
|
public interface AlarmService extends EntityDaoService {
|
||||||
@ -118,7 +119,7 @@ public interface AlarmService extends EntityDaoService {
|
|||||||
PageData<AlarmData> findAlarmDataByQueryForEntities(TenantId tenantId,
|
PageData<AlarmData> findAlarmDataByQueryForEntities(TenantId tenantId,
|
||||||
AlarmDataQuery query, Collection<EntityId> orderedEntityIds);
|
AlarmDataQuery query, Collection<EntityId> orderedEntityIds);
|
||||||
|
|
||||||
void unassignUserAlarms(TenantId tenantId, UserId userId, long unassignTs);
|
List<Alarm> findAlarmsByAssigneeId(UserId userId);
|
||||||
|
|
||||||
void deleteEntityAlarmRelations(TenantId tenantId, EntityId entityId);
|
void deleteEntityAlarmRelations(TenantId tenantId, EntityId entityId);
|
||||||
|
|
||||||
|
|||||||
@ -25,12 +25,9 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.thingsboard.common.util.JacksonUtil;
|
|
||||||
import org.thingsboard.server.common.data.EntityType;
|
import org.thingsboard.server.common.data.EntityType;
|
||||||
import org.thingsboard.server.common.data.alarm.Alarm;
|
import org.thingsboard.server.common.data.alarm.Alarm;
|
||||||
import org.thingsboard.server.common.data.alarm.AlarmApiCallResult;
|
import org.thingsboard.server.common.data.alarm.AlarmApiCallResult;
|
||||||
import org.thingsboard.server.common.data.alarm.AlarmComment;
|
|
||||||
import org.thingsboard.server.common.data.alarm.AlarmCommentType;
|
|
||||||
import org.thingsboard.server.common.data.alarm.AlarmCreateOrUpdateActiveRequest;
|
import org.thingsboard.server.common.data.alarm.AlarmCreateOrUpdateActiveRequest;
|
||||||
import org.thingsboard.server.common.data.alarm.AlarmInfo;
|
import org.thingsboard.server.common.data.alarm.AlarmInfo;
|
||||||
import org.thingsboard.server.common.data.alarm.AlarmModificationRequest;
|
import org.thingsboard.server.common.data.alarm.AlarmModificationRequest;
|
||||||
@ -347,30 +344,6 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void unassignUserAlarms(TenantId tenantId, UserId userId, long unassignTs) {
|
|
||||||
List<Alarm> alarms = findAlarmsByAssigneeId(userId);
|
|
||||||
for (Alarm alarm : alarms) {
|
|
||||||
AlarmApiCallResult result = unassignAlarm(alarm.getTenantId(), alarm.getId(), unassignTs);
|
|
||||||
if (!result.isSuccessful()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (result.isModified()) {
|
|
||||||
try {
|
|
||||||
AlarmComment alarmComment = AlarmComment.builder()
|
|
||||||
.alarmId(alarm.getId())
|
|
||||||
.type(AlarmCommentType.SYSTEM)
|
|
||||||
.comment(JacksonUtil.newObjectNode()
|
|
||||||
.put("text", "Alarm was unassigned because assigned user was deleted!")
|
|
||||||
.put("userId", userId.toString())
|
|
||||||
.put("subtype", "ASSIGN"))
|
|
||||||
.build();
|
|
||||||
alarmCommentDao.save(tenantId, alarmComment);
|
|
||||||
} catch (Exception ignored) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Alarm findAlarmById(TenantId tenantId, AlarmId alarmId) {
|
public Alarm findAlarmById(TenantId tenantId, AlarmId alarmId) {
|
||||||
log.trace("Executing findAlarmById [{}]", alarmId);
|
log.trace("Executing findAlarmById [{}]", alarmId);
|
||||||
@ -412,6 +385,13 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
|
|||||||
return Futures.immediateFuture(alarmDao.findCustomerAlarmsV2(tenantId, customerId, query));
|
return Futures.immediateFuture(alarmDao.findCustomerAlarmsV2(tenantId, customerId, query));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Alarm> findAlarmsByAssigneeId(UserId userId) {
|
||||||
|
log.trace("Executing findAlarmsByAssigneeId [{}]", userId);
|
||||||
|
validateId(userId, "Incorrect alarmId " + userId);
|
||||||
|
return alarmDao.findAlarmByAssigneeId(userId.getId());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AlarmSeverity findHighestAlarmSeverity(TenantId tenantId, EntityId entityId, AlarmSearchStatus alarmSearchStatus,
|
public AlarmSeverity findHighestAlarmSeverity(TenantId tenantId, EntityId entityId, AlarmSearchStatus alarmSearchStatus,
|
||||||
AlarmStatus alarmStatus, String assigneeId) {
|
AlarmStatus alarmStatus, String assigneeId) {
|
||||||
@ -550,11 +530,4 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
|
|||||||
request.setEndTs(request.getStartTs());
|
request.setEndTs(request.getStartTs());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Alarm> findAlarmsByAssigneeId(UserId userId) {
|
|
||||||
log.trace("Executing findAlarmsByAssigneeId [{}]", userId);
|
|
||||||
validateId(userId, "Incorrect alarmId " + userId);
|
|
||||||
return alarmDao.findAlarmByAssigneeId(userId.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -249,7 +249,6 @@ public class UserServiceImpl extends AbstractEntityService implements UserServic
|
|||||||
public void deleteUser(TenantId tenantId, UserId userId) {
|
public void deleteUser(TenantId tenantId, UserId userId) {
|
||||||
log.trace("Executing deleteUser [{}]", userId);
|
log.trace("Executing deleteUser [{}]", userId);
|
||||||
validateId(userId, INCORRECT_USER_ID + userId);
|
validateId(userId, INCORRECT_USER_ID + userId);
|
||||||
alarmService.unassignUserAlarms(tenantId, userId, System.currentTimeMillis());
|
|
||||||
UserCredentials userCredentials = userCredentialsDao.findByUserId(tenantId, userId.getId());
|
UserCredentials userCredentials = userCredentialsDao.findByUserId(tenantId, userId.getId());
|
||||||
userCredentialsDao.removeById(tenantId, userCredentials.getUuidId());
|
userCredentialsDao.removeById(tenantId, userCredentials.getUuidId());
|
||||||
userAuthSettingsDao.removeByUserId(userId);
|
userAuthSettingsDao.removeByUserId(userId);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user