Search in sources :

Example 1 with ColumnMapping

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);
        }
    });
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) CsvUtils(org.thingsboard.server.utils.CsvUtils) SneakyThrows(lombok.SneakyThrows) DonAsynchron(org.thingsboard.common.util.DonAsynchron) Autowired(org.springframework.beans.factory.annotation.Autowired) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) TelemetrySubscriptionService(org.thingsboard.server.service.telemetry.TelemetrySubscriptionService) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) PreDestroy(javax.annotation.PreDestroy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) TypeCastUtil(org.thingsboard.server.utils.TypeCastUtil) HasTenantId(org.thingsboard.server.common.data.HasTenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) SecurityContext(org.springframework.security.core.context.SecurityContext) PostConstruct(javax.annotation.PostConstruct) HasId(org.thingsboard.server.common.data.id.HasId) Operation(org.thingsboard.server.service.security.permission.Operation) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ActionType(org.thingsboard.server.common.data.audit.ActionType) DataType(org.thingsboard.server.common.data.kv.DataType) EntityId(org.thingsboard.server.common.data.id.EntityId) BaseController(org.thingsboard.server.controller.BaseController) JsonPrimitive(com.google.gson.JsonPrimitive) UUIDBased(org.thingsboard.server.common.data.id.UUIDBased) Nullable(javax.annotation.Nullable) AccessValidator(org.thingsboard.server.service.security.AccessValidator) FutureCallback(com.google.common.util.concurrent.FutureCallback) ColumnMapping(org.thingsboard.server.service.importing.BulkImportRequest.ColumnMapping) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EntityActionService(org.thingsboard.server.service.action.EntityActionService) Data(lombok.Data) Resource(org.thingsboard.server.service.security.permission.Resource) AccessControlService(org.thingsboard.server.service.security.permission.AccessControlService) JsonObject(com.google.gson.JsonObject)

Example 2 with ColumnMapping

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());
}
Also used : JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) CsvUtils(org.thingsboard.server.utils.CsvUtils) SneakyThrows(lombok.SneakyThrows) DonAsynchron(org.thingsboard.common.util.DonAsynchron) Autowired(org.springframework.beans.factory.annotation.Autowired) DefaultTenantProfileConfiguration(org.thingsboard.server.common.data.tenant.profile.DefaultTenantProfileConfiguration) TelemetrySubscriptionService(org.thingsboard.server.service.telemetry.TelemetrySubscriptionService) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) PreDestroy(javax.annotation.PreDestroy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) EntityType(org.thingsboard.server.common.data.EntityType) JsonConverter(org.thingsboard.server.common.transport.adaptor.JsonConverter) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) TypeCastUtil(org.thingsboard.server.utils.TypeCastUtil) HasTenantId(org.thingsboard.server.common.data.HasTenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) ThingsBoardThreadFactory(org.thingsboard.common.util.ThingsBoardThreadFactory) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) SecurityContext(org.springframework.security.core.context.SecurityContext) PostConstruct(javax.annotation.PostConstruct) HasId(org.thingsboard.server.common.data.id.HasId) Operation(org.thingsboard.server.service.security.permission.Operation) TbTenantProfileCache(org.thingsboard.server.dao.tenant.TbTenantProfileCache) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) TenantProfile(org.thingsboard.server.common.data.TenantProfile) ActionType(org.thingsboard.server.common.data.audit.ActionType) DataType(org.thingsboard.server.common.data.kv.DataType) EntityId(org.thingsboard.server.common.data.id.EntityId) BaseController(org.thingsboard.server.controller.BaseController) JsonPrimitive(com.google.gson.JsonPrimitive) UUIDBased(org.thingsboard.server.common.data.id.UUIDBased) Nullable(javax.annotation.Nullable) AccessValidator(org.thingsboard.server.service.security.AccessValidator) FutureCallback(com.google.common.util.concurrent.FutureCallback) ColumnMapping(org.thingsboard.server.service.importing.BulkImportRequest.ColumnMapping) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) EntityActionService(org.thingsboard.server.service.action.EntityActionService) Data(lombok.Data) Resource(org.thingsboard.server.service.security.permission.Resource) AccessControlService(org.thingsboard.server.service.security.permission.AccessControlService) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) AttributeKvEntry(org.thingsboard.server.common.data.kv.AttributeKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) ColumnMapping(org.thingsboard.server.service.importing.BulkImportRequest.ColumnMapping)

Aggregations

FutureCallback (com.google.common.util.concurrent.FutureCallback)2 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Consumer (java.util.function.Consumer)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Nullable (javax.annotation.Nullable)2 PostConstruct (javax.annotation.PostConstruct)2 PreDestroy (javax.annotation.PreDestroy)2 Data (lombok.Data)2