@ -15,21 +15,28 @@
* /
package org.thingsboard.server.service.entitiy.tenant.profile ;
import jakarta.validation.constraints.NotNull ;
import lombok.AllArgsConstructor ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.stereotype.Service ;
import org.thingsboard.server.common.data.TenantProfile ;
import org.thingsboard.server.common.data.audit.ActionType ;
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode ;
import org.thingsboard.server.common.data.exception.ThingsboardException ;
import org.thingsboard.server.common.data.id.TenantId ;
import org.thingsboard.server.common.data.User ;
import org.thingsboard.server.dao.tenant.TbTenantProfileCache ;
import org.thingsboard.server.dao.tenant.TenantProfileService ;
import org.thingsboard.server.dao.tenant.TenantService ;
import org.thingsboard.server.exception.DataValidationException ;
import org.thingsboard.server.queue.util.TbCoreComponent ;
import org.thingsboard.server.service.entitiy.AbstractTbEntityService ;
import org.thingsboard.server.service.entitiy.queue.TbQueueService ;
import java.util.List ;
import static org.thingsboard.server.common.data.EntityType.TENANT_PROFILE ;
@Slf4j
@Service
@TbCoreComponent
@ -41,18 +48,62 @@ public class DefaultTbTenantProfileService extends AbstractTbEntityService imple
private final TbTenantProfileCache tenantProfileCache ;
@Override
public TenantProfile save ( TenantId tenantId , TenantProfile tenantProfile , TenantProfile oldTenantProfile ) throws ThingsboardException {
TenantProfile savedTenantProfile = checkNotNull ( tenantProfileService . saveTenantProfile ( tenantId , tenantProfile ) ) ;
tenantProfileCache . put ( savedTenantProfile ) ;
public TenantProfile
save ( TenantId tenantId , TenantProfile tenantProfile , TenantProfile oldTenantProfile , User user ) throws ThingsboardException {
ActionType actionType = tenantProfile . getId ( ) = = null ? ActionType . ADDED : ActionType . UPDATED ;
try {
TenantProfile savedTenantProfile = checkNotNull ( tenantProfileService . saveTenantProfile ( tenantId , tenantProfile ) ) ;
tenantProfileCache . put ( savedTenantProfile ) ;
logEntityActionService . logEntityAction ( tenantId , savedTenantProfile . getId ( ) , savedTenantProfile , null ,
actionType , user ) ;
List < TenantId > tenantIds = tenantService . findTenantIdsByTenantProfileId ( savedTenantProfile . getId ( ) ) ;
tbQueueService . updateQueuesByTenants ( tenantIds , savedTenantProfile , oldTenantProfile ) ;
List < TenantId > tenantIds = tenantService . findTenantIdsByTenantProfileId ( savedTenantProfile . getId ( ) ) ;
tbQueueService . updateQueuesByTenants ( tenantIds , savedTenantProfile , oldTenantProfile ) ;
return savedTenantProfile ;
} catch ( ThingsboardException e ) {
log . debug ( "Failed to save tenant profile because ThingsboardException [{}]" , tenantProfile , e ) ;
logEntityActionService . logEntityAction ( tenantId , getOrEmptyId ( tenantProfile . getId ( ) , TENANT_PROFILE ) , tenantProfile , actionType , user , e ) ;
throw e ;
} catch ( DataValidationException e ) {
log . debug ( "Failed to save tenant profile because data validation [{}]" , tenantProfile , e ) ;
logEntityActionService . logEntityAction ( tenantId , getOrEmptyId ( tenantProfile . getId ( ) , TENANT_PROFILE ) , tenantProfile , actionType , user , e ) ;
throw new ThingsboardException ( e . getMessage ( ) , e , ThingsboardErrorCode . BAD_REQUEST_PARAMS ) ;
} catch ( Exception e ) {
log . debug ( "Failed to save tenant profile because Exception [{}]" , tenantProfile , e ) ;
logEntityActionService . logEntityAction ( tenantId , getOrEmptyId ( tenantProfile . getId ( ) , TENANT_PROFILE ) , tenantProfile , actionType , user , e ) ;
throw new ThingsboardException ( e . getMessage ( ) , e , ThingsboardErrorCode . GENERAL ) ;
}
return savedTenantProfile ;
}
@Override
public void delete ( TenantId tenantId , TenantProfile tenantProfile ) throws ThingsboardException {
tenantProfileService . deleteTenantProfile ( tenantId , tenantProfile . getId ( ) ) ;
public void delete ( TenantId tenantId , @NotNull TenantProfile tenantProfile , User user ) throws ThingsboardException {
ActionType actionType = ActionType . DELETED ;
try {
tenantProfileService . deleteTenantProfile ( tenantId , tenantProfile . getId ( ) ) ;
logEntityActionService . logEntityAction ( tenantId , tenantProfile . getId ( ) , tenantProfile , null , actionType , user ) ;
} catch ( Exception e ) {
logEntityActionService . logEntityAction ( tenantId , tenantProfile . getId ( ) , tenantProfile , null , actionType , user , e ) ;
throw e ;
}
}
@Override
public TenantProfile setDefaultTenantProfile ( TenantId tenantId , @NotNull TenantProfile tenantProfile , User user ) throws ThingsboardException {
ActionType actionType = ActionType . UPDATED ;
try {
TenantProfile savedTenantProfile = tenantProfileService . setDefaultTenantProfile ( tenantId , tenantProfile . getId ( ) ) ;
logEntityActionService . logEntityAction ( tenantId , tenantProfile . getId ( ) , savedTenantProfile , null , actionType , user ) ;
return savedTenantProfile ;
} catch ( DataValidationException e ) {
log . debug ( "Failed to set default tenant profile due to data validation [{}]" , tenantProfile , e ) ;
logEntityActionService . logEntityAction ( tenantId , tenantProfile . getId ( ) , tenantProfile , actionType , user , e ) ;
throw new ThingsboardException ( e . getMessage ( ) , e , ThingsboardErrorCode . BAD_REQUEST_PARAMS ) ;
} catch ( Exception e ) {
log . debug ( "Failed to set default tenant profile [{}]" , tenantProfile , e ) ;
logEntityActionService . logEntityAction ( tenantId , tenantProfile . getId ( ) , tenantProfile , actionType , user , e ) ;
throw new ThingsboardException ( e . getMessage ( ) , e , ThingsboardErrorCode . GENERAL ) ;
}
}
}