Search in sources :

Example 46 with StringUtils.isNotEmpty

use of org.apache.commons.lang3.StringUtils.isNotEmpty 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 47 with StringUtils.isNotEmpty

use of org.apache.commons.lang3.StringUtils.isNotEmpty 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)

Example 48 with StringUtils.isNotEmpty

use of org.apache.commons.lang3.StringUtils.isNotEmpty in project incubator-servicecomb-java-chassis by apache.

the class PojoOperationGenerator method wrapParametersToBody.

private void wrapParametersToBody(List<ParameterGenerator> bodyFields) {
    String simpleRef = MethodUtils.findSwaggerMethodName(method) + "Body";
    bodyModel = new ModelImpl();
    bodyModel.setType(ModelImpl.OBJECT);
    for (ParameterGenerator parameterGenerator : bodyFields) {
        // to collect all information by swagger mechanism
        // must have a parameter type
        // but all these parameters will be wrap to be one body parameter, their parameter type must be null
        // so we first set to be BODY, after collected, set back to be null
        parameterGenerator.setHttpParameterType(HttpParameterType.BODY);
        scanMethodParameter(parameterGenerator);
        Property property = ModelConverters.getInstance().readAsProperty(parameterGenerator.getGenericType());
        property.setDescription(parameterGenerator.getGeneratedParameter().getDescription());
        bodyModel.addProperty(parameterGenerator.getParameterName(), property);
        parameterGenerator.setHttpParameterType(null);
    }
    swagger.addDefinition(simpleRef, bodyModel);
    SwaggerGeneratorFeature feature = swaggerGenerator.getSwaggerGeneratorFeature();
    // if not care for this, then can just delete all logic about EXT_JAVA_CLASS/EXT_JAVA_INTF
    if (feature.isExtJavaClassInVendor() && bodyFields.size() > 1 && StringUtils.isNotEmpty(feature.getPackageName())) {
        bodyModel.getVendorExtensions().put(SwaggerConst.EXT_JAVA_CLASS, feature.getPackageName() + "." + simpleRef);
    }
    RefModel refModel = new RefModel();
    refModel.setReference("#/definitions/" + simpleRef);
    bodyParameter = new BodyParameter();
    bodyParameter.name(simpleRef);
    bodyParameter.setSchema(refModel);
    bodyParameter.setName(parameterGenerators.size() == 1 ? parameterGenerators.get(0).getParameterName() : simpleRef);
    List<ParameterGenerator> newParameterGenerators = new ArrayList<>();
    newParameterGenerators.add(new ParameterGenerator(bodyParameter.getName(), Collections.emptyList(), null, HttpParameterType.BODY, bodyParameter));
    parameterGenerators.stream().filter(p -> p.getHttpParameterType() != null).forEach(p -> newParameterGenerators.add(p));
    parameterGenerators = newParameterGenerators;
}
Also used : ModelConverters(io.swagger.converter.ModelConverters) Swagger(io.swagger.models.Swagger) HttpParameterType(org.apache.servicecomb.swagger.generator.core.model.HttpParameterType) SwaggerUtils(org.apache.servicecomb.swagger.SwaggerUtils) ModelImpl(io.swagger.models.ModelImpl) StringUtils(org.apache.commons.lang3.StringUtils) HttpMethod(javax.ws.rs.HttpMethod) ArrayList(java.util.ArrayList) Map(java.util.Map) SwaggerGeneratorFeature(org.apache.servicecomb.swagger.generator.SwaggerGeneratorFeature) AbstractOperationGenerator(org.apache.servicecomb.swagger.generator.core.AbstractOperationGenerator) JavaType(com.fasterxml.jackson.databind.JavaType) Method(java.lang.reflect.Method) Property(io.swagger.models.properties.Property) RefModel(io.swagger.models.RefModel) BodyParameter(io.swagger.models.parameters.BodyParameter) AbstractSwaggerGenerator(org.apache.servicecomb.swagger.generator.core.AbstractSwaggerGenerator) Parameter(io.swagger.models.parameters.Parameter) MethodUtils(org.apache.servicecomb.swagger.generator.core.utils.MethodUtils) Collectors(java.util.stream.Collectors) List(java.util.List) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator) SwaggerConst(org.apache.servicecomb.swagger.generator.SwaggerConst) Annotation(java.lang.annotation.Annotation) Collections(java.util.Collections) SwaggerGeneratorFeature(org.apache.servicecomb.swagger.generator.SwaggerGeneratorFeature) RefModel(io.swagger.models.RefModel) ArrayList(java.util.ArrayList) ModelImpl(io.swagger.models.ModelImpl) ParameterGenerator(org.apache.servicecomb.swagger.generator.ParameterGenerator) BodyParameter(io.swagger.models.parameters.BodyParameter) Property(io.swagger.models.properties.Property)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)42 List (java.util.List)31 Map (java.util.Map)28 Collectors (java.util.stream.Collectors)21 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)19 IOException (java.io.IOException)17 LoggerFactory (org.slf4j.LoggerFactory)15 Set (java.util.Set)14 Autowired (org.springframework.beans.factory.annotation.Autowired)14 Logger (org.slf4j.Logger)13 Collections (java.util.Collections)12 HashSet (java.util.HashSet)12 Optional (java.util.Optional)12 Arrays (java.util.Arrays)11 File (java.io.File)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 java.util (java.util)7 Collection (java.util.Collection)7 Pattern (java.util.regex.Pattern)7