|
|
|
@ -21,6 +21,7 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.server.common.data.Dashboard; |
|
|
|
import org.thingsboard.server.common.data.ShortCustomerInfo; |
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
import org.thingsboard.server.common.data.id.DashboardId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.gen.edge.v1.DashboardUpdateMsg; |
|
|
|
@ -31,7 +32,7 @@ import java.util.Set; |
|
|
|
@Slf4j |
|
|
|
public abstract class BaseDashboardProcessor extends BaseEdgeProcessor { |
|
|
|
|
|
|
|
protected boolean saveOrUpdateDashboard(TenantId tenantId, DashboardId dashboardId, DashboardUpdateMsg dashboardUpdateMsg, boolean isEdgeProtoDeprecated) { |
|
|
|
protected boolean saveOrUpdateDashboard(TenantId tenantId, DashboardId dashboardId, DashboardUpdateMsg dashboardUpdateMsg, boolean isEdgeProtoDeprecated, CustomerId customerId) { |
|
|
|
boolean created = false; |
|
|
|
Dashboard dashboard = isEdgeProtoDeprecated |
|
|
|
? createDashboard(tenantId, dashboardId, dashboardUpdateMsg) |
|
|
|
@ -39,23 +40,38 @@ public abstract class BaseDashboardProcessor extends BaseEdgeProcessor { |
|
|
|
if (dashboard == null) { |
|
|
|
throw new RuntimeException("[{" + tenantId + "}] dashboardUpdateMsg {" + dashboardUpdateMsg + "} cannot be converted to dashboard"); |
|
|
|
} |
|
|
|
Set<ShortCustomerInfo> assignedCustomers = null; |
|
|
|
Dashboard dashboardById = dashboardService.findDashboardById(tenantId, dashboardId); |
|
|
|
if (dashboardById == null) { |
|
|
|
created = true; |
|
|
|
dashboard.setId(null); |
|
|
|
} else { |
|
|
|
dashboard.setId(dashboardId); |
|
|
|
assignedCustomers = filterNonExistingCustomers(tenantId, dashboardById.getAssignedCustomers()); |
|
|
|
} |
|
|
|
|
|
|
|
dashboardValidator.validate(dashboard, Dashboard::getTenantId); |
|
|
|
if (created) { |
|
|
|
dashboard.setId(dashboardId); |
|
|
|
} |
|
|
|
Set<ShortCustomerInfo> msgAssignedCustomers = filterNonExistingCustomers(tenantId, dashboard.getAssignedCustomers()); |
|
|
|
if (msgAssignedCustomers != null) { |
|
|
|
if (assignedCustomers == null) { |
|
|
|
assignedCustomers = msgAssignedCustomers; |
|
|
|
} else { |
|
|
|
assignedCustomers.addAll(msgAssignedCustomers); |
|
|
|
} |
|
|
|
} |
|
|
|
dashboard.setAssignedCustomers(assignedCustomers); |
|
|
|
Dashboard savedDashboard = dashboardService.saveDashboard(dashboard, false); |
|
|
|
if (savedDashboard.getAssignedCustomers() != null && savedDashboard.getAssignedCustomers().isEmpty()) { |
|
|
|
for (ShortCustomerInfo assignedCustomer : savedDashboard.getAssignedCustomers()) { |
|
|
|
dashboardService.unassignDashboardFromCustomer(tenantId, savedDashboard.getId(), assignedCustomer.getCustomerId()); |
|
|
|
if (msgAssignedCustomers != null && !msgAssignedCustomers.isEmpty()) { |
|
|
|
for (ShortCustomerInfo assignedCustomer : msgAssignedCustomers) { |
|
|
|
if (assignedCustomer.getCustomerId().equals(customerId)) { |
|
|
|
dashboardService.assignDashboardToCustomer(tenantId, savedDashboard.getId(), assignedCustomer.getCustomerId()); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
unassignCustomersFromDashboard(tenantId, savedDashboard, customerId); |
|
|
|
} |
|
|
|
return created; |
|
|
|
} |
|
|
|
@ -80,6 +96,15 @@ public abstract class BaseDashboardProcessor extends BaseEdgeProcessor { |
|
|
|
return dashboard; |
|
|
|
} |
|
|
|
|
|
|
|
private void unassignCustomersFromDashboard(TenantId tenantId, Dashboard dashboard, CustomerId customerId) { |
|
|
|
if (dashboard.getAssignedCustomers() != null && !dashboard.getAssignedCustomers().isEmpty()) { |
|
|
|
for (ShortCustomerInfo assignedCustomer : dashboard.getAssignedCustomers()) { |
|
|
|
if (assignedCustomer.getCustomerId().equals(customerId)) { |
|
|
|
dashboardService.unassignDashboardFromCustomer(tenantId, dashboard.getId(), assignedCustomer.getCustomerId()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected abstract Set<ShortCustomerInfo> filterNonExistingCustomers(TenantId tenantId, Set<ShortCustomerInfo> assignedCustomers); |
|
|
|
} |
|
|
|
|