use of org.thingsboard.server.service.importing.BulkImportRequest.ColumnMapping in project thingsboard by thingsboard.
the class AbstractBulkImportService method saveKvs.
private void saveKvs(SecurityUser user, E entity, Map<ColumnMapping, ParsedValue> data) {
Arrays.stream(BulkImportColumnType.values()).filter(BulkImportColumnType::isKv).map(kvType -> {
JsonObject kvs = new JsonObject();
data.entrySet().stream().filter(dataEntry -> dataEntry.getKey().getType() == kvType && StringUtils.isNotEmpty(dataEntry.getKey().getKey())).forEach(dataEntry -> kvs.add(dataEntry.getKey().getKey(), dataEntry.getValue().toJsonPrimitive()));
return Map.entry(kvType, kvs);
}).filter(kvsEntry -> kvsEntry.getValue().entrySet().size() > 0).forEach(kvsEntry -> {
BulkImportColumnType kvType = kvsEntry.getKey();
if (kvType == BulkImportColumnType.SHARED_ATTRIBUTE || kvType == BulkImportColumnType.SERVER_ATTRIBUTE) {
saveAttributes(user, entity, kvsEntry, kvType);
} else {
saveTelemetry(user, entity, kvsEntry);
}
});
}
use of org.thingsboard.server.service.importing.BulkImportRequest.ColumnMapping in project thingsboard by thingsboard.
the class AbstractBulkImportService method parseData.
private List<EntityData> parseData(BulkImportRequest request) throws Exception {
List<List<String>> records = CsvUtils.parseCsv(request.getFile(), request.getMapping().getDelimiter());
AtomicInteger linesCounter = new AtomicInteger(0);
if (request.getMapping().getHeader()) {
records.remove(0);
linesCounter.incrementAndGet();
}
List<ColumnMapping> columnsMappings = request.getMapping().getColumns();
return records.stream().map(record -> {
EntityData entityData = new EntityData();
Stream.iterate(0, i -> i < record.size(), i -> i + 1).map(i -> Map.entry(columnsMappings.get(i), record.get(i))).filter(entry -> StringUtils.isNotEmpty(entry.getValue())).forEach(entry -> {
if (!entry.getKey().getType().isKv()) {
entityData.getFields().put(entry.getKey().getType(), entry.getValue());
} else {
Map.Entry<DataType, Object> castResult = TypeCastUtil.castValue(entry.getValue());
entityData.getKvs().put(entry.getKey(), new ParsedValue(castResult.getValue(), castResult.getKey()));
}
});
entityData.setLineNumber(linesCounter.incrementAndGet());
return entityData;
}).collect(Collectors.toList());
}
Aggregations