List of usage examples for com.fasterxml.jackson.databind JsonNode forEach
default void forEach(Consumer<? super T> action)
From source file:org.thingsboard.demo.loader.data.DemoData.java
public void uploadData(RestTemplate restTemplate, String baseUrl) throws Exception { plugins.forEach(plugin -> {/*from www. j a v a2s . c om*/ try { String apiToken = plugin.getApiToken(); PluginMetaData savedPlugin = null; try { savedPlugin = restTemplate.getForObject(baseUrl + "/api/plugin/token/" + apiToken, PluginMetaData.class); } catch (HttpClientErrorException e) { if (e.getStatusCode() != HttpStatus.NOT_FOUND) { throw e; } } if (savedPlugin == null) { savedPlugin = restTemplate.postForObject(baseUrl + "/api/plugin", plugin, PluginMetaData.class); } if (savedPlugin.getState() == ComponentLifecycleState.SUSPENDED) { restTemplate.postForLocation( baseUrl + "/api/plugin/" + savedPlugin.getId().getId().toString() + "/activate", null); } } catch (Exception e) { log.error("Unable to upload plugin!"); log.error("Cause:", e); } }); rules.forEach(rule -> { try { RuleMetaData savedRule = null; TextPageData<RuleMetaData> rules; ResponseEntity<TextPageData<RuleMetaData>> entity = restTemplate.exchange( baseUrl + "/api/rule?limit={limit}&textSearch={textSearch}", HttpMethod.GET, null, new ParameterizedTypeReference<TextPageData<RuleMetaData>>() { }, 1000, rule.getName()); rules = entity.getBody(); if (rules.getData().size() > 0) { savedRule = rules.getData().get(0); } if (savedRule == null) { savedRule = restTemplate.postForObject(baseUrl + "/api/rule", rule, RuleMetaData.class); } if (savedRule.getState() == ComponentLifecycleState.SUSPENDED) { restTemplate.postForLocation( baseUrl + "/api/rule/" + savedRule.getId().getId().toString() + "/activate", null); } } catch (Exception e) { log.error("Unable to upload rule!"); log.error("Cause:", e); } }); Map<String, CustomerId> customerIdMap = new HashMap<>(); customers.forEach(customer -> { try { Customer savedCustomer = null; TextPageData<Customer> customers; ResponseEntity<TextPageData<Customer>> entity = restTemplate.exchange( baseUrl + "/api/customers?limit={limit}&textSearch={textSearch}", HttpMethod.GET, null, new ParameterizedTypeReference<TextPageData<Customer>>() { }, 1000, customer.getTitle()); customers = entity.getBody(); if (customers.getData().size() > 0) { savedCustomer = customers.getData().get(0); } if (savedCustomer == null) { savedCustomer = restTemplate.postForObject(baseUrl + "/api/customer", customer, Customer.class); } customerIdMap.put(savedCustomer.getTitle(), savedCustomer.getId()); } catch (Exception e) { log.error("Unable to upload customer!"); log.error("Cause:", e); } }); List<Device> loadedDevices = new ArrayList<>(); Map<String, DeviceId> deviceIdMap = new HashMap<>(); devices.forEach(device -> { try { CustomerId customerId = null; String customerTitle = customerDevices.get(device.getName()); if (customerTitle != null) { customerId = customerIdMap.get(customerTitle); } Device savedDevice = null; TextPageData<Device> devices; if (customerId != null) { ResponseEntity<TextPageData<Device>> entity = restTemplate.exchange( baseUrl + "/api/customer/{customerId}/devices?limit={limit}&textSearch={textSearch}", HttpMethod.GET, null, new ParameterizedTypeReference<TextPageData<Device>>() { }, customerId.getId().toString(), 1000, device.getName()); devices = entity.getBody(); if (devices.getData().size() > 0) { savedDevice = devices.getData().get(0); } } if (savedDevice == null) { ResponseEntity<TextPageData<Device>> entity = restTemplate.exchange( baseUrl + "/api/tenant/devices?limit={limit}&textSearch={textSearch}", HttpMethod.GET, null, new ParameterizedTypeReference<TextPageData<Device>>() { }, 1000, device.getName()); devices = entity.getBody(); if (devices.getData().size() > 0) { savedDevice = devices.getData().get(0); } } if (savedDevice == null) { savedDevice = restTemplate.postForObject(baseUrl + "/api/device", device, Device.class); } if (customerId != null && savedDevice.getCustomerId().isNullUid()) { restTemplate.postForLocation(baseUrl + "/api/customer/{customerId}/device/{deviceId}", null, customerId.getId().toString(), savedDevice.getId().toString()); } String deviceName = savedDevice.getName(); DeviceId deviceId = savedDevice.getId(); deviceIdMap.put(deviceName, deviceId); loadedDevices.add(savedDevice); DeviceCredentials credentials = restTemplate.getForObject( baseUrl + "/api/device/{deviceId}/credentials", DeviceCredentials.class, deviceId.getId().toString()); loadedDevicesCredentialsMap.put(deviceId, credentials); Map<String, JsonNode> attributesMap = devicesAttributes.get(deviceName); if (attributesMap != null) { attributesMap.forEach((k, v) -> { restTemplate.postForObject(baseUrl + "/api/plugins/telemetry/{deviceId}/{attributeScope}", v, Void.class, deviceId.getId().toString(), k); }); } } catch (Exception e) { log.error("Unable to upload device!"); log.error("Cause:", e); } }); devices = loadedDevices; Map<String, Dashboard> pendingDashboards = dashboards.stream() .collect(Collectors.toMap(Dashboard::getTitle, Function.identity())); Map<String, Dashboard> savedDashboards = new HashMap<>(); while (pendingDashboards.size() != 0) { Dashboard savedDashboard = null; for (Dashboard dashboard : pendingDashboards.values()) { savedDashboard = getDashboardByName(restTemplate, baseUrl, dashboard); if (savedDashboard == null) { try { Optional<String> dashboardConfigurationBody = resolveLinks(savedDashboards, dashboard.getConfiguration()); if (dashboardConfigurationBody.isPresent()) { dashboard.setConfiguration(objectMapper.readTree(dashboardConfigurationBody.get())); JsonNode dashboardConfiguration = dashboard.getConfiguration(); JsonNode deviceAliases = dashboardConfiguration.get("deviceAliases"); deviceAliases.forEach(jsonNode -> { String aliasName = jsonNode.get("alias").asText(); DeviceId deviceId = deviceIdMap.get(aliasName); if (deviceId != null) { ((ObjectNode) jsonNode).put("deviceId", deviceId.getId().toString()); } }); savedDashboard = restTemplate.postForObject(baseUrl + "/api/dashboard", dashboard, Dashboard.class); } } catch (Exception e) { log.error("Unable to upload dashboard!"); log.error("Cause:", e); } } if (savedDashboard != null) { break; } } if (savedDashboard != null) { savedDashboards.put(savedDashboard.getTitle(), savedDashboard); pendingDashboards.remove(savedDashboard.getTitle()); } else { log.error("Unable to upload dashboards due to unresolved references!"); break; } } dashboards = new ArrayList<>(savedDashboards.values()); }