Search in sources :

Example 1 with ShortCustomerInfo

use of org.thingsboard.server.common.data.ShortCustomerInfo in project thingsboard by thingsboard.

the class DatabaseHelper method upgradeTo40_assignDashboards.

public static void upgradeTo40_assignDashboards(Path dashboardsDump, DashboardService dashboardService, boolean sql) throws Exception {
    JavaType assignedCustomersType = objectMapper.getTypeFactory().constructCollectionType(HashSet.class, ShortCustomerInfo.class);
    try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(dashboardsDump), CSV_DUMP_FORMAT.withFirstRecordAsHeader())) {
        csvParser.forEach(record -> {
            String customerIdString = record.get(CUSTOMER_ID);
            String assignedCustomersString = record.get(ASSIGNED_CUSTOMERS);
            DashboardId dashboardId = new DashboardId(toUUID(record.get(ID), sql));
            List<CustomerId> customerIds = new ArrayList<>();
            if (!StringUtils.isEmpty(assignedCustomersString)) {
                try {
                    Set<ShortCustomerInfo> assignedCustomers = objectMapper.readValue(assignedCustomersString, assignedCustomersType);
                    assignedCustomers.forEach((customerInfo) -> {
                        CustomerId customerId = customerInfo.getCustomerId();
                        if (!customerId.isNullUid()) {
                            customerIds.add(customerId);
                        }
                    });
                } catch (IOException e) {
                    log.error("Unable to parse assigned customers field", e);
                }
            }
            if (!StringUtils.isEmpty(customerIdString)) {
                CustomerId customerId = new CustomerId(toUUID(customerIdString, sql));
                if (!customerId.isNullUid()) {
                    customerIds.add(customerId);
                }
            }
            for (CustomerId customerId : customerIds) {
                dashboardService.assignDashboardToCustomer(dashboardId, customerId);
            }
        });
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ShortCustomerInfo(org.thingsboard.server.common.data.ShortCustomerInfo) CSVParser(org.apache.commons.csv.CSVParser) DashboardId(org.thingsboard.server.common.data.id.DashboardId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) IOException(java.io.IOException)

Aggregations

JavaType (com.fasterxml.jackson.databind.JavaType)1 IOException (java.io.IOException)1 CSVParser (org.apache.commons.csv.CSVParser)1 ShortCustomerInfo (org.thingsboard.server.common.data.ShortCustomerInfo)1 CustomerId (org.thingsboard.server.common.data.id.CustomerId)1 DashboardId (org.thingsboard.server.common.data.id.DashboardId)1