Search in sources :

Example 76 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project thingsboard by thingsboard.

the class BasicMapperUtils method getOAuth2User.

public static OAuth2User getOAuth2User(String email, Map<String, Object> attributes, OAuth2MapperConfig config) {
    OAuth2User oauth2User = new OAuth2User();
    oauth2User.setEmail(email);
    oauth2User.setTenantName(getTenantName(email, attributes, config));
    if (!StringUtils.isEmpty(config.getBasic().getLastNameAttributeKey())) {
        String lastName = getStringAttributeByKey(attributes, config.getBasic().getLastNameAttributeKey());
        oauth2User.setLastName(lastName);
    }
    if (!StringUtils.isEmpty(config.getBasic().getFirstNameAttributeKey())) {
        String firstName = getStringAttributeByKey(attributes, config.getBasic().getFirstNameAttributeKey());
        oauth2User.setFirstName(firstName);
    }
    if (!StringUtils.isEmpty(config.getBasic().getCustomerNamePattern())) {
        StrSubstitutor sub = new StrSubstitutor(attributes, START_PLACEHOLDER_PREFIX, END_PLACEHOLDER_PREFIX);
        String customerName = sub.replace(config.getBasic().getCustomerNamePattern());
        oauth2User.setCustomerName(customerName);
    }
    oauth2User.setAlwaysFullScreen(config.getBasic().isAlwaysFullScreen());
    if (!StringUtils.isEmpty(config.getBasic().getDefaultDashboardName())) {
        oauth2User.setDefaultDashboardName(config.getBasic().getDefaultDashboardName());
    }
    return oauth2User;
}
Also used : OAuth2User(org.thingsboard.server.dao.oauth2.OAuth2User) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor)

Example 77 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project carbondata by apache.

the class CarbondataSplitManager method getSplits.

public ConnectorSplitSource getSplits(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorTableLayoutHandle layoutHandle, SplitSchedulingStrategy splitSchedulingStrategy) {
    HiveTableLayoutHandle layout = (HiveTableLayoutHandle) layoutHandle;
    SchemaTableName schemaTableName = layout.getSchemaTableName();
    carbonTableReader.setPrestoQueryId(session.getQueryId());
    // get table metadata
    SemiTransactionalHiveMetastore metastore = metastoreProvider.apply((HiveTransactionHandle) transactionHandle);
    Table table = metastore.getTable(schemaTableName.getSchemaName(), schemaTableName.getTableName()).orElseThrow(() -> new TableNotFoundException(schemaTableName));
    if (!table.getStorage().getStorageFormat().getInputFormat().contains("carbon")) {
        return super.getSplits(transactionHandle, session, layoutHandle, splitSchedulingStrategy);
    }
    // for hive metastore, get table location from catalog table's tablePath
    String location = table.getStorage().getSerdeParameters().get("tablePath");
    if (StringUtils.isEmpty(location)) {
        // file metastore case tablePath can be null, so get from location
        location = table.getStorage().getLocation();
    }
    List<PartitionSpec> filteredPartitions = new ArrayList<>();
    if (layout.getPartitionColumns().size() > 0 && layout.getPartitions().isPresent()) {
        List<String> colNames = layout.getPartitionColumns().stream().map(x -> ((HiveColumnHandle) x).getName()).collect(Collectors.toList());
        for (HivePartition partition : layout.getPartitions().get()) {
            filteredPartitions.add(new PartitionSpec(colNames, location + CarbonCommonConstants.FILE_SEPARATOR + partition.getPartitionId()));
        }
    }
    String queryId = System.nanoTime() + "";
    QueryStatistic statistic = new QueryStatistic();
    QueryStatisticsRecorder statisticRecorder = CarbonTimeStatisticsFactory.createDriverRecorder();
    statistic.addStatistics(QueryStatisticsConstants.BLOCK_ALLOCATION, System.currentTimeMillis());
    statisticRecorder.recordStatisticsForDriver(statistic, queryId);
    statistic = new QueryStatistic();
    carbonTableReader.setQueryId(queryId);
    TupleDomain<HiveColumnHandle> predicate = (TupleDomain<HiveColumnHandle>) layout.getCompactEffectivePredicate();
    Configuration configuration = this.hdfsEnvironment.getConfiguration(new HdfsEnvironment.HdfsContext(session, schemaTableName.getSchemaName(), schemaTableName.getTableName()), new Path(location));
    configuration = carbonTableReader.updateS3Properties(configuration);
    for (Map.Entry<String, String> entry : table.getStorage().getSerdeParameters().entrySet()) {
        configuration.set(entry.getKey(), entry.getValue());
    }
    // set the hadoop configuration to thread local, so that FileFactory can use it.
    ThreadLocalSessionInfo.setConfigurationToCurrentThread(configuration);
    CarbonTableCacheModel cache = carbonTableReader.getCarbonCache(schemaTableName, location, configuration);
    Expression filters = PrestoFilterUtil.parseFilterExpression(predicate);
    try {
        List<CarbonLocalMultiBlockSplit> splits = carbonTableReader.getInputSplits(cache, filters, filteredPartitions, configuration);
        ImmutableList.Builder<ConnectorSplit> cSplits = ImmutableList.builder();
        long index = 0;
        for (CarbonLocalMultiBlockSplit split : splits) {
            index++;
            Properties properties = new Properties();
            for (Map.Entry<String, String> entry : table.getStorage().getSerdeParameters().entrySet()) {
                properties.setProperty(entry.getKey(), entry.getValue());
            }
            properties.setProperty("tablePath", cache.getCarbonTable().getTablePath());
            properties.setProperty("carbonSplit", split.getJsonString());
            properties.setProperty("queryId", queryId);
            properties.setProperty("index", String.valueOf(index));
            cSplits.add(new HiveSplit(schemaTableName.getSchemaName(), schemaTableName.getTableName(), schemaTableName.getTableName(), "", 0, 0, 0, properties, new ArrayList(), getHostAddresses(split.getLocations()), OptionalInt.empty(), false, predicate, new HashMap<>(), Optional.empty(), false));
        }
        statisticRecorder.logStatisticsAsTableDriver();
        statistic.addStatistics(QueryStatisticsConstants.BLOCK_IDENTIFICATION, System.currentTimeMillis());
        statisticRecorder.recordStatisticsForDriver(statistic, queryId);
        statisticRecorder.logStatisticsAsTableDriver();
        return new FixedSplitSource(cSplits.build());
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
}
Also used : HdfsEnvironment(com.facebook.presto.hive.HdfsEnvironment) Arrays(java.util.Arrays) ConnectorSplitSource(com.facebook.presto.spi.ConnectorSplitSource) CarbonLocalMultiBlockSplit(org.apache.carbondata.presto.impl.CarbonLocalMultiBlockSplit) HiveTableLayoutHandle(com.facebook.presto.hive.HiveTableLayoutHandle) QueryStatistic(org.apache.carbondata.core.stats.QueryStatistic) StringUtils(org.apache.commons.lang3.StringUtils) ConnectorTransactionHandle(com.facebook.presto.spi.connector.ConnectorTransactionHandle) CarbonCommonConstants(org.apache.carbondata.core.constants.CarbonCommonConstants) QueryStatisticsRecorder(org.apache.carbondata.core.stats.QueryStatisticsRecorder) CarbonTableReader(org.apache.carbondata.presto.impl.CarbonTableReader) HiveTransactionHandle(com.facebook.presto.hive.HiveTransactionHandle) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Map(java.util.Map) Configuration(org.apache.hadoop.conf.Configuration) Path(org.apache.hadoop.fs.Path) ForHiveClient(com.facebook.presto.hive.ForHiveClient) CarbonTableCacheModel(org.apache.carbondata.presto.impl.CarbonTableCacheModel) Expression(org.apache.carbondata.core.scan.expression.Expression) HiveClientConfig(com.facebook.presto.hive.HiveClientConfig) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) HostAddress(com.facebook.presto.spi.HostAddress) DirectoryLister(com.facebook.presto.hive.DirectoryLister) SemiTransactionalHiveMetastore(com.facebook.presto.hive.metastore.SemiTransactionalHiveMetastore) Collectors(java.util.stream.Collectors) ConnectorSession(com.facebook.presto.spi.ConnectorSession) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) List(java.util.List) Optional(java.util.Optional) HiveColumnHandle(com.facebook.presto.hive.HiveColumnHandle) Table(com.facebook.presto.hive.metastore.Table) HiveSplitManager(com.facebook.presto.hive.HiveSplitManager) ConnectorTableLayoutHandle(com.facebook.presto.spi.ConnectorTableLayoutHandle) HashMap(java.util.HashMap) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ThreadLocalSessionInfo(org.apache.carbondata.core.util.ThreadLocalSessionInfo) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) CarbonTimeStatisticsFactory(org.apache.carbondata.core.util.CarbonTimeStatisticsFactory) NamenodeStats(com.facebook.presto.hive.NamenodeStats) ExecutorService(java.util.concurrent.ExecutorService) Properties(java.util.Properties) CoercionPolicy(com.facebook.presto.hive.CoercionPolicy) FixedSplitSource(com.facebook.presto.spi.FixedSplitSource) QueryStatisticsConstants(org.apache.carbondata.core.stats.QueryStatisticsConstants) PartitionSpec(org.apache.carbondata.core.indexstore.PartitionSpec) ConnectorSplit(com.facebook.presto.spi.ConnectorSplit) HivePartition(com.facebook.presto.hive.HivePartition) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnHandle(com.facebook.presto.spi.ColumnHandle) HiveSplit(com.facebook.presto.hive.HiveSplit) Configuration(org.apache.hadoop.conf.Configuration) SemiTransactionalHiveMetastore(com.facebook.presto.hive.metastore.SemiTransactionalHiveMetastore) HashMap(java.util.HashMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Properties(java.util.Properties) HdfsEnvironment(com.facebook.presto.hive.HdfsEnvironment) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) FixedSplitSource(com.facebook.presto.spi.FixedSplitSource) HiveTableLayoutHandle(com.facebook.presto.hive.HiveTableLayoutHandle) HiveColumnHandle(com.facebook.presto.hive.HiveColumnHandle) QueryStatistic(org.apache.carbondata.core.stats.QueryStatistic) Path(org.apache.hadoop.fs.Path) Table(com.facebook.presto.hive.metastore.Table) SchemaTableName(com.facebook.presto.spi.SchemaTableName) PartitionSpec(org.apache.carbondata.core.indexstore.PartitionSpec) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) HiveSplit(com.facebook.presto.hive.HiveSplit) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) Expression(org.apache.carbondata.core.scan.expression.Expression) CarbonTableCacheModel(org.apache.carbondata.presto.impl.CarbonTableCacheModel) QueryStatisticsRecorder(org.apache.carbondata.core.stats.QueryStatisticsRecorder) CarbonLocalMultiBlockSplit(org.apache.carbondata.presto.impl.CarbonLocalMultiBlockSplit) Map(java.util.Map) HashMap(java.util.HashMap) ConnectorSplit(com.facebook.presto.spi.ConnectorSplit) HivePartition(com.facebook.presto.hive.HivePartition)

Example 78 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project thingsboard by thingsboard.

the class DefaultEntityQueryRepository method countEntitiesByQuery.

@Override
public long countEntitiesByQuery(TenantId tenantId, CustomerId customerId, EntityCountQuery query) {
    EntityType entityType = resolveEntityType(query.getEntityFilter());
    QueryContext ctx = new QueryContext(new QuerySecurityContext(tenantId, customerId, entityType));
    if (query.getKeyFilters() == null || query.getKeyFilters().isEmpty()) {
        ctx.append("select count(e.id) from ");
        ctx.append(addEntityTableQuery(ctx, query.getEntityFilter()));
        ctx.append(" e where ");
        ctx.append(buildEntityWhere(ctx, query.getEntityFilter(), Collections.emptyList()));
        return transactionTemplate.execute(status -> {
            long startTs = System.currentTimeMillis();
            try {
                return jdbcTemplate.queryForObject(ctx.getQuery(), ctx, Long.class);
            } finally {
                queryLog.logQuery(ctx, ctx.getQuery(), System.currentTimeMillis() - startTs);
            }
        });
    } else {
        List<EntityKeyMapping> mappings = EntityKeyMapping.prepareEntityCountKeyMapping(query);
        List<EntityKeyMapping> selectionMapping = mappings.stream().filter(EntityKeyMapping::isSelection).collect(Collectors.toList());
        List<EntityKeyMapping> entityFieldsSelectionMapping = selectionMapping.stream().filter(mapping -> !mapping.isLatest()).collect(Collectors.toList());
        List<EntityKeyMapping> filterMapping = mappings.stream().filter(EntityKeyMapping::hasFilter).collect(Collectors.toList());
        List<EntityKeyMapping> entityFieldsFiltersMapping = filterMapping.stream().filter(mapping -> !mapping.isLatest()).collect(Collectors.toList());
        List<EntityKeyMapping> allLatestMappings = mappings.stream().filter(EntityKeyMapping::isLatest).collect(Collectors.toList());
        String entityWhereClause = DefaultEntityQueryRepository.this.buildEntityWhere(ctx, query.getEntityFilter(), entityFieldsFiltersMapping);
        String latestJoinsCnt = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings, true);
        String entityFieldsSelection = EntityKeyMapping.buildSelections(entityFieldsSelectionMapping, query.getEntityFilter().getType(), entityType);
        String entityTypeStr;
        if (query.getEntityFilter().getType().equals(EntityFilterType.RELATIONS_QUERY)) {
            entityTypeStr = "e.entity_type";
        } else {
            entityTypeStr = "'" + entityType.name() + "'";
        }
        if (!StringUtils.isEmpty(entityFieldsSelection)) {
            entityFieldsSelection = String.format("e.id id, %s entity_type, %s", entityTypeStr, entityFieldsSelection);
        } else {
            entityFieldsSelection = String.format("e.id id, %s entity_type", entityTypeStr);
        }
        String fromClauseCount = String.format("from (select %s from (select %s from %s e where %s) entities %s ) result %s", "entities.*", entityFieldsSelection, addEntityTableQuery(ctx, query.getEntityFilter()), entityWhereClause, latestJoinsCnt, "");
        String countQuery = String.format("select count(id) %s", fromClauseCount);
        return transactionTemplate.execute(status -> {
            long startTs = System.currentTimeMillis();
            try {
                return jdbcTemplate.queryForObject(countQuery, ctx, Long.class);
            } finally {
                queryLog.logQuery(ctx, ctx.getQuery(), System.currentTimeMillis() - startTs);
            }
        });
    }
}
Also used : EntityType(org.thingsboard.server.common.data.EntityType) Arrays(java.util.Arrays) Getter(lombok.Getter) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) EntityTypeFilter(org.thingsboard.server.common.data.query.EntityTypeFilter) HashMap(java.util.HashMap) AssetTypeFilter(org.thingsboard.server.common.data.query.AssetTypeFilter) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) Value(org.springframework.beans.factory.annotation.Value) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) Repository(org.springframework.stereotype.Repository) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityViewTypeFilter(org.thingsboard.server.common.data.query.EntityViewTypeFilter) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ApiUsageStateFilter(org.thingsboard.server.common.data.query.ApiUsageStateFilter) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) UUID(java.util.UUID) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Collectors(java.util.stream.Collectors) EntitySearchQueryFilter(org.thingsboard.server.common.data.query.EntitySearchQueryFilter) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) EntityFilterType(org.thingsboard.server.common.data.query.EntityFilterType) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) EntityNameFilter(org.thingsboard.server.common.data.query.EntityNameFilter) SingleEntityFilter(org.thingsboard.server.common.data.query.SingleEntityFilter) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PageData(org.thingsboard.server.common.data.page.PageData) Optional(java.util.Optional) EntityViewSearchQueryFilter(org.thingsboard.server.common.data.query.EntityViewSearchQueryFilter) Collections(java.util.Collections) EntityFilter(org.thingsboard.server.common.data.query.EntityFilter) CustomerId(org.thingsboard.server.common.data.id.CustomerId)

Example 79 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project thingsboard by thingsboard.

the class DefaultEntityQueryRepository method findEntityDataByQuery.

@Override
public PageData<EntityData> findEntityDataByQuery(TenantId tenantId, CustomerId customerId, EntityDataQuery query) {
    return transactionTemplate.execute(status -> {
        EntityType entityType = resolveEntityType(query.getEntityFilter());
        QueryContext ctx = new QueryContext(new QuerySecurityContext(tenantId, customerId, entityType));
        EntityDataPageLink pageLink = query.getPageLink();
        List<EntityKeyMapping> mappings = EntityKeyMapping.prepareKeyMapping(query);
        List<EntityKeyMapping> selectionMapping = mappings.stream().filter(EntityKeyMapping::isSelection).collect(Collectors.toList());
        List<EntityKeyMapping> entityFieldsSelectionMapping = selectionMapping.stream().filter(mapping -> !mapping.isLatest()).collect(Collectors.toList());
        List<EntityKeyMapping> latestSelectionMapping = selectionMapping.stream().filter(EntityKeyMapping::isLatest).collect(Collectors.toList());
        List<EntityKeyMapping> filterMapping = mappings.stream().filter(EntityKeyMapping::hasFilter).collect(Collectors.toList());
        List<EntityKeyMapping> entityFieldsFiltersMapping = filterMapping.stream().filter(mapping -> !mapping.isLatest()).collect(Collectors.toList());
        List<EntityKeyMapping> allLatestMappings = mappings.stream().filter(EntityKeyMapping::isLatest).collect(Collectors.toList());
        String entityWhereClause = DefaultEntityQueryRepository.this.buildEntityWhere(ctx, query.getEntityFilter(), entityFieldsFiltersMapping);
        String latestJoinsCnt = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings, true);
        String latestJoinsData = EntityKeyMapping.buildLatestJoins(ctx, query.getEntityFilter(), entityType, allLatestMappings, false);
        String textSearchQuery = DefaultEntityQueryRepository.this.buildTextSearchQuery(ctx, selectionMapping, pageLink.getTextSearch());
        String entityFieldsSelection = EntityKeyMapping.buildSelections(entityFieldsSelectionMapping, query.getEntityFilter().getType(), entityType);
        String entityTypeStr;
        if (query.getEntityFilter().getType().equals(EntityFilterType.RELATIONS_QUERY)) {
            entityTypeStr = "e.entity_type";
        } else {
            entityTypeStr = "'" + entityType.name() + "'";
        }
        if (!StringUtils.isEmpty(entityFieldsSelection)) {
            entityFieldsSelection = String.format("e.id id, %s entity_type, %s", entityTypeStr, entityFieldsSelection);
        } else {
            entityFieldsSelection = String.format("e.id id, %s entity_type", entityTypeStr);
        }
        String latestSelection = EntityKeyMapping.buildSelections(latestSelectionMapping, query.getEntityFilter().getType(), entityType);
        String topSelection = "entities.*";
        if (!StringUtils.isEmpty(latestSelection)) {
            topSelection = topSelection + ", " + latestSelection;
        }
        String fromClauseCount = String.format("from (select %s from (select %s from %s e where %s) entities %s ) result %s", "entities.*", entityFieldsSelection, addEntityTableQuery(ctx, query.getEntityFilter()), entityWhereClause, latestJoinsCnt, textSearchQuery);
        String fromClauseData = String.format("from (select %s from (select %s from %s e where %s) entities %s ) result %s", topSelection, entityFieldsSelection, addEntityTableQuery(ctx, query.getEntityFilter()), entityWhereClause, latestJoinsData, textSearchQuery);
        if (!StringUtils.isEmpty(pageLink.getTextSearch())) {
            // Unfortunately, we need to sacrifice performance in case of full text search, because it is applied to all joined records.
            fromClauseCount = fromClauseData;
        }
        String countQuery = String.format("select count(id) %s", fromClauseCount);
        long startTs = System.currentTimeMillis();
        int totalElements;
        try {
            totalElements = jdbcTemplate.queryForObject(countQuery, ctx, Integer.class);
        } finally {
            queryLog.logQuery(ctx, countQuery, System.currentTimeMillis() - startTs);
        }
        if (totalElements == 0) {
            return new PageData<>();
        }
        String dataQuery = String.format("select * %s", fromClauseData);
        EntityDataSortOrder sortOrder = pageLink.getSortOrder();
        if (sortOrder != null) {
            Optional<EntityKeyMapping> sortOrderMappingOpt = mappings.stream().filter(EntityKeyMapping::isSortOrder).findFirst();
            if (sortOrderMappingOpt.isPresent()) {
                EntityKeyMapping sortOrderMapping = sortOrderMappingOpt.get();
                String direction = sortOrder.getDirection() == EntityDataSortOrder.Direction.ASC ? "asc" : "desc";
                if (sortOrderMapping.getEntityKey().getType() == EntityKeyType.ENTITY_FIELD) {
                    dataQuery = String.format("%s order by %s %s, result.id %s", dataQuery, sortOrderMapping.getValueAlias(), direction, direction);
                } else {
                    dataQuery = String.format("%s order by %s %s, %s %s, result.id %s", dataQuery, sortOrderMapping.getSortOrderNumAlias(), direction, sortOrderMapping.getSortOrderStrAlias(), direction, direction);
                }
            }
        }
        int startIndex = pageLink.getPageSize() * pageLink.getPage();
        if (pageLink.getPageSize() > 0) {
            dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex);
        }
        startTs = System.currentTimeMillis();
        List<Map<String, Object>> rows;
        try {
            rows = jdbcTemplate.queryForList(dataQuery, ctx);
        } finally {
            queryLog.logQuery(ctx, countQuery, System.currentTimeMillis() - startTs);
        }
        return EntityDataAdapter.createEntityData(pageLink, selectionMapping, rows, totalElements);
    });
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) EntitySearchDirection(org.thingsboard.server.common.data.relation.EntitySearchDirection) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) EntityTypeFilter(org.thingsboard.server.common.data.query.EntityTypeFilter) HashMap(java.util.HashMap) AssetTypeFilter(org.thingsboard.server.common.data.query.AssetTypeFilter) DeviceSearchQueryFilter(org.thingsboard.server.common.data.query.DeviceSearchQueryFilter) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) StringUtils(org.apache.commons.lang3.StringUtils) TenantId(org.thingsboard.server.common.data.id.TenantId) Value(org.springframework.beans.factory.annotation.Value) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) RelationsQueryFilter(org.thingsboard.server.common.data.query.RelationsQueryFilter) Map(java.util.Map) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) Repository(org.springframework.stereotype.Repository) EdgeTypeFilter(org.thingsboard.server.common.data.query.EdgeTypeFilter) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityViewTypeFilter(org.thingsboard.server.common.data.query.EntityViewTypeFilter) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) ApiUsageStateFilter(org.thingsboard.server.common.data.query.ApiUsageStateFilter) EdgeSearchQueryFilter(org.thingsboard.server.common.data.query.EdgeSearchQueryFilter) UUID(java.util.UUID) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Collectors(java.util.stream.Collectors) EntitySearchQueryFilter(org.thingsboard.server.common.data.query.EntitySearchQueryFilter) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) EntityFilterType(org.thingsboard.server.common.data.query.EntityFilterType) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) AssetSearchQueryFilter(org.thingsboard.server.common.data.query.AssetSearchQueryFilter) EntityNameFilter(org.thingsboard.server.common.data.query.EntityNameFilter) SingleEntityFilter(org.thingsboard.server.common.data.query.SingleEntityFilter) RelationEntityTypeFilter(org.thingsboard.server.common.data.relation.RelationEntityTypeFilter) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) PageData(org.thingsboard.server.common.data.page.PageData) Optional(java.util.Optional) EntityViewSearchQueryFilter(org.thingsboard.server.common.data.query.EntityViewSearchQueryFilter) Collections(java.util.Collections) EntityFilter(org.thingsboard.server.common.data.query.EntityFilter) CustomerId(org.thingsboard.server.common.data.id.CustomerId) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityType(org.thingsboard.server.common.data.EntityType) PageData(org.thingsboard.server.common.data.page.PageData) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 80 with StringUtils.isEmpty

use of org.apache.commons.lang3.StringUtils.isEmpty in project data-prep by Talend.

the class PreparationService method listAll.

public Stream<PreparationDTO> listAll(PreparationSearchCriterion searchCriterion, Sort sort, Order order) {
    LOGGER.debug("Get list of preparations (with details).");
    Stream<PersistentPreparation> preparationStream;
    Expression filter = null;
    Predicate<PersistentPreparation> deprecatedFolderIdFilter = null;
    if (searchCriterion.getName() != null) {
        filter = getNameFilter(searchCriterion.getName(), searchCriterion.isNameExactMatch());
    }
    if (searchCriterion.getDataSetId() != null) {
        Expression dataSetFilter = eq(DATASET_ID, searchCriterion.getDataSetId());
        filter = filter == null ? dataSetFilter : and(filter, dataSetFilter);
    }
    if (searchCriterion.getFolderId() != null) {
        if (preparationRepository.exist(PersistentPreparation.class, isEmpty(FOLDER_ID))) {
            // filter on folder id (DEPRECATED VERSION - only applies if migration isn't completed yet)
            try (Stream<FolderEntry> folders = folderRepository.entries(searchCriterion.getFolderId(), PREPARATION)) {
                final Set<String> entries = folders.map(// 
                FolderEntry::getContentId).collect(toSet());
                deprecatedFolderIdFilter = p -> entries.contains(p.id());
            }
        } else {
            // Once all preparations all have the folder id,
            Expression folderIdFilter = eq(FOLDER_ID, searchCriterion.getFolderId());
            filter = filter == null ? folderIdFilter : and(filter, folderIdFilter);
        }
    }
    // Handles filtering (prefer database filters)
    if (filter != null) {
        preparationStream = preparationRepository.list(PersistentPreparation.class, filter);
    } else {
        preparationStream = preparationRepository.list(PersistentPreparation.class);
    }
    // migration for preparation after the change from dataset ID to dataset name
    // see TDP-6195 and TDP-5696
    preparationStream = preparationStream.map(dataSetNameInjection::injectDatasetNameBasedOnId);
    if (deprecatedFolderIdFilter != null) {
        // filter on folder id (DEPRECATED VERSION - only applies if migration isn't completed yet)
        preparationStream = // 
        preparationStream.filter(// 
        deprecatedFolderIdFilter).peek(p -> p.setFolderId(searchCriterion.getFolderId()));
    }
    // filter on folder path
    if (searchCriterion.getFolderPath() != null) {
        final Optional<Folder> folder = folderRepository.getFolder(searchCriterion.getFolderPath());
        final Set<String> folderEntries = new HashSet<>();
        folder.ifPresent(f -> {
            try (Stream<String> preparationIds = // 
            folderRepository.entries(f.getId(), // 
            PREPARATION).map(FolderEntry::getContentId)) {
                folderEntries.addAll(// 
                preparationIds.collect(toSet()));
            }
        });
        preparationStream = preparationStream.filter(p -> folderEntries.contains(p.id()));
    }
    return preparationStream.map(preparation -> {
        if (StringUtils.isEmpty(preparation.getName())) {
            preparation.setName((preparation.getDataSetName() != null ? preparation.getDataSetName() + " " : "") + message("preparation.create.suffix"));
            preparationRepository.add(preparation);
        }
        return preparation;
    }).map(p -> beanConversionService.convert(p, PreparationDTO.class, ownerInjection.injectIntoPreparation(), // 
    sharedInjection)).sorted(getPreparationComparator(sort, order));
}
Also used : LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) PREPARATION(org.talend.dataprep.api.folder.FolderContentType.PREPARATION) TqlBuilder.match(org.talend.tql.api.TqlBuilder.match) ActionFactory(org.talend.dataprep.transformation.actions.common.ActionFactory) Date(java.util.Date) MAX_VALUE(java.lang.Integer.MAX_VALUE) LoggerFactory(org.slf4j.LoggerFactory) TqlBuilder.and(org.talend.tql.api.TqlBuilder.and) Autowired(org.springframework.beans.factory.annotation.Autowired) PreparationRepository(org.talend.dataprep.preparation.store.PreparationRepository) StringUtils(org.apache.commons.lang3.StringUtils) StepRowMetadata(org.talend.dataprep.api.preparation.StepRowMetadata) LockedResourceRepository(org.talend.dataprep.lock.store.LockedResourceRepository) BeanConversionService(org.talend.dataprep.conversions.BeanConversionService) AppendStep(org.talend.dataprep.api.preparation.AppendStep) Collectors.toMap(java.util.stream.Collectors.toMap) Locale(java.util.Locale) Map(java.util.Map) DataprepBundle.message(org.talend.dataprep.i18n.DataprepBundle.message) PREPARATION_NOT_EMPTY(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_NOT_EMPTY) PreparationActions(org.talend.dataprep.api.preparation.PreparationActions) Collectors.toSet(java.util.stream.Collectors.toSet) StepDiff(org.talend.dataprep.api.preparation.StepDiff) PREPARATION_ROOT_STEP_CANNOT_BE_DELETED(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_ROOT_STEP_CANNOT_BE_DELETED) Order(org.talend.dataprep.util.SortAndOrderHelper.Order) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) PersistentStep(org.talend.dataprep.preparation.store.PersistentStep) Set(java.util.Set) PreparationErrorCodes(org.talend.dataprep.exception.error.PreparationErrorCodes) PREPARATION_STEP_CANNOT_BE_REORDERED(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_STEP_CANNOT_BE_REORDERED) UUID(java.util.UUID) PREPARATION_STEP_DOES_NOT_EXIST(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_STEP_DOES_NOT_EXIST) Objects(java.util.Objects) List(java.util.List) BaseDataprepAuditService(org.talend.dataprep.audit.BaseDataprepAuditService) Stream(java.util.stream.Stream) SortAndOrderHelper.getPreparationComparator(org.talend.dataprep.util.SortAndOrderHelper.getPreparationComparator) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) Security(org.talend.dataprep.security.Security) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) IntStream(java.util.stream.IntStream) TqlBuilder.isEmpty(org.talend.tql.api.TqlBuilder.isEmpty) ImplicitParameters(org.talend.dataprep.transformation.actions.common.ImplicitParameters) TDPException(org.talend.dataprep.exception.TDPException) CREATE_NEW_COLUMN(org.talend.dataprep.transformation.actions.common.ActionsUtils.CREATE_NEW_COLUMN) JsonErrorCodeDescription(org.talend.dataprep.exception.json.JsonErrorCodeDescription) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Deque(java.util.Deque) Expression(org.talend.tql.model.Expression) Function(java.util.function.Function) PersistentPreparation(org.talend.dataprep.preparation.store.PersistentPreparation) DatasetClient(org.talend.dataprep.dataset.adapter.DatasetClient) ArrayList(java.util.ArrayList) PREPARATION_NAME_ALREADY_USED(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_NAME_ALREADY_USED) HashSet(java.util.HashSet) FolderEntry(org.talend.dataprep.api.folder.FolderEntry) ExceptionContext.build(org.talend.daikon.exception.ExceptionContext.build) ExceptionContext(org.talend.daikon.exception.ExceptionContext) Folder(org.talend.dataprep.api.folder.Folder) Service(org.springframework.stereotype.Service) PREPARATION_DOES_NOT_EXIST(org.talend.dataprep.exception.error.PreparationErrorCodes.PREPARATION_DOES_NOT_EXIST) VersionService(org.talend.dataprep.api.service.info.VersionService) Action(org.talend.dataprep.api.preparation.Action) OwnerInjection(org.talend.dataprep.conversions.inject.OwnerInjection) Preparation(org.talend.dataprep.api.preparation.Preparation) PreparationDetailsDTO(org.talend.dataprep.api.preparation.PreparationDetailsDTO) FolderRepository(org.talend.dataprep.folder.store.FolderRepository) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ActionRegistry(org.talend.dataprep.transformation.pipeline.ActionRegistry) DecimalFormat(java.text.DecimalFormat) Sort(org.talend.dataprep.util.SortAndOrderHelper.Sort) Step(org.talend.dataprep.api.preparation.Step) PreparationSearchCriterion.filterPreparation(org.talend.dataprep.preparation.service.PreparationSearchCriterion.filterPreparation) ActionForm(org.talend.dataprep.api.action.ActionForm) PreparationDTO(org.talend.dataprep.api.preparation.PreparationDTO) TqlBuilder.eq(org.talend.tql.api.TqlBuilder.eq) Collectors.toList(java.util.stream.Collectors.toList) PreparationUtils(org.talend.dataprep.api.preparation.PreparationUtils) SharedInjection(org.talend.dataprep.preparation.configuration.SharedInjection) ActionMetadataValidation(org.talend.dataprep.transformation.api.action.validation.ActionMetadataValidation) Lookup(org.talend.dataprep.transformation.actions.datablending.Lookup) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) PATH_SEPARATOR(org.talend.dataprep.folder.store.FoldersRepositoriesConstants.PATH_SEPARATOR) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) Folder(org.talend.dataprep.api.folder.Folder) Expression(org.talend.tql.model.Expression) FolderEntry(org.talend.dataprep.api.folder.FolderEntry) PersistentPreparation(org.talend.dataprep.preparation.store.PersistentPreparation) HashSet(java.util.HashSet)

Aggregations

StringUtils (org.apache.commons.lang3.StringUtils)76 List (java.util.List)53 Collectors (java.util.stream.Collectors)40 ArrayList (java.util.ArrayList)39 Map (java.util.Map)39 HashMap (java.util.HashMap)28 IOException (java.io.IOException)26 Collections (java.util.Collections)25 Set (java.util.Set)25 Logger (org.slf4j.Logger)25 Arrays (java.util.Arrays)24 LoggerFactory (org.slf4j.LoggerFactory)24 Optional (java.util.Optional)22 HashSet (java.util.HashSet)18 Collection (java.util.Collection)14 Autowired (org.springframework.beans.factory.annotation.Autowired)13 Objects (java.util.Objects)12 UUID (java.util.UUID)11 java.util (java.util)10 Component (org.springframework.stereotype.Component)10