|
|
|
@ -26,6 +26,7 @@ import org.thingsboard.server.dao.service.DataValidator; |
|
|
|
import org.thingsboard.server.gen.edge.v1.DashboardUpdateMsg; |
|
|
|
import org.thingsboard.server.service.edge.rpc.processor.BaseEdgeProcessor; |
|
|
|
|
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@ -40,54 +41,63 @@ public abstract class BaseDashboardProcessor extends BaseEdgeProcessor { |
|
|
|
if (dashboard == null) { |
|
|
|
throw new RuntimeException("[{" + tenantId + "}] dashboardUpdateMsg {" + dashboardUpdateMsg + "} cannot be converted to dashboard"); |
|
|
|
} |
|
|
|
Set<ShortCustomerInfo> assignedCustomers = null; |
|
|
|
Set<ShortCustomerInfo> newAssignedCustomers = new HashSet<>(dashboard.getAssignedCustomers()); |
|
|
|
Dashboard dashboardById = edgeCtx.getDashboardService().findDashboardById(tenantId, dashboardId); |
|
|
|
if (dashboardById == null) { |
|
|
|
created = true; |
|
|
|
dashboard.setId(null); |
|
|
|
dashboard.setAssignedCustomers(null); |
|
|
|
} else { |
|
|
|
dashboard.setId(dashboardId); |
|
|
|
assignedCustomers = filterNonExistingCustomers(tenantId, dashboardById.getAssignedCustomers()); |
|
|
|
dashboard.setAssignedCustomers(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 savedDashboard = edgeCtx.getDashboardService().saveDashboard(dashboard, false); |
|
|
|
|
|
|
|
updateDashboardAssignments(tenantId, dashboardById, savedDashboard, newAssignedCustomers); |
|
|
|
|
|
|
|
return created; |
|
|
|
} |
|
|
|
|
|
|
|
private void updateDashboardAssignments(TenantId tenantId, Dashboard dashboardById, Dashboard savedDashboard, Set<ShortCustomerInfo> newAssignedCustomers) { |
|
|
|
Set<ShortCustomerInfo> currentAssignedCustomers = new HashSet<>(); |
|
|
|
if (dashboardById != null) { |
|
|
|
if (dashboardById.getAssignedCustomers() != null) { |
|
|
|
currentAssignedCustomers.addAll(dashboardById.getAssignedCustomers()); |
|
|
|
} |
|
|
|
} |
|
|
|
dashboard.setAssignedCustomers(assignedCustomers); |
|
|
|
Dashboard savedDashboard = edgeCtx.getDashboardService().saveDashboard(dashboard, false); |
|
|
|
if (msgAssignedCustomers != null && !msgAssignedCustomers.isEmpty()) { |
|
|
|
for (ShortCustomerInfo assignedCustomer : msgAssignedCustomers) { |
|
|
|
if (assignedCustomer.getCustomerId().equals(customerId)) { |
|
|
|
edgeCtx.getDashboardService().assignDashboardToCustomer(tenantId, savedDashboard.getId(), assignedCustomer.getCustomerId()); |
|
|
|
} |
|
|
|
|
|
|
|
newAssignedCustomers = filterNonExistingCustomers(tenantId, currentAssignedCustomers, newAssignedCustomers); |
|
|
|
|
|
|
|
Set<CustomerId> addedCustomerIds = new HashSet<>(); |
|
|
|
Set<CustomerId> removedCustomerIds = new HashSet<>(); |
|
|
|
for (ShortCustomerInfo newAssignedCustomer : newAssignedCustomers) { |
|
|
|
if (!savedDashboard.isAssignedToCustomer(newAssignedCustomer.getCustomerId())) { |
|
|
|
addedCustomerIds.add(newAssignedCustomer.getCustomerId()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
unassignCustomersFromDashboard(tenantId, savedDashboard, customerId); |
|
|
|
} |
|
|
|
return created; |
|
|
|
} |
|
|
|
|
|
|
|
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)) { |
|
|
|
edgeCtx.getDashboardService().unassignDashboardFromCustomer(tenantId, dashboard.getId(), assignedCustomer.getCustomerId()); |
|
|
|
} |
|
|
|
for (ShortCustomerInfo currentAssignedCustomer : currentAssignedCustomers) { |
|
|
|
if (!newAssignedCustomers.contains(currentAssignedCustomer)) { |
|
|
|
removedCustomerIds.add(currentAssignedCustomer.getCustomerId()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for (CustomerId customerIdToAdd : addedCustomerIds) { |
|
|
|
edgeCtx.getDashboardService().assignDashboardToCustomer(tenantId, savedDashboard.getId(), customerIdToAdd); |
|
|
|
} |
|
|
|
for (CustomerId customerIdToRemove : removedCustomerIds) { |
|
|
|
edgeCtx.getDashboardService().unassignDashboardFromCustomer(tenantId, savedDashboard.getId(), customerIdToRemove); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected abstract Dashboard constructDashboardFromUpdateMsg(TenantId tenantId, DashboardId dashboardId, DashboardUpdateMsg dashboardUpdateMsg); |
|
|
|
|
|
|
|
protected abstract Set<ShortCustomerInfo> filterNonExistingCustomers(TenantId tenantId, Set<ShortCustomerInfo> assignedCustomers); |
|
|
|
protected abstract Set<ShortCustomerInfo> filterNonExistingCustomers(TenantId tenantId, Set<ShortCustomerInfo> currentAssignedCustomers, Set<ShortCustomerInfo> newAssignedCustomers); |
|
|
|
|
|
|
|
} |
|
|
|
|