All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.flowstep.jdbc.JdbcNewStepProcessor Maven / Gradle / Ivy

The newest version!
/*
 *
 *  Copyright 2022 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.flowstep.jdbc;

import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.flowstep.core.DataProviderException;
import org.flowstep.core.connection.EnvironmentConnection;
import org.flowstep.core.context.FlowNewStepContext;
import org.flowstep.core.context.FlowStepContext;
import org.flowstep.core.factory.FlowFactory;
import org.flowstep.core.processor.FlowStepDataProcessor;
import org.flowstep.core.transform.HasFlowTransform;
import org.flowstep.jdbc.model.JdbcEnvironment;
import org.flowstep.jdbc.model.JdbcStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.Iterator;
import java.util.stream.Stream;

@Component
public class JdbcNewStepProcessor implements FlowStepDataProcessor {
    private static final Logger logger = LoggerFactory.getLogger(JdbcNewStepProcessor.class);

    @Override
    public EnvironmentConnection getConnection(JdbcEnvironment environmentItemSettings) {
        return new EnvironmentConnection(buildTemplate(environmentItemSettings), environmentItemSettings);
    }

    @Override
    public Iterator process(FlowNewStepContext flowStepContext, EnvironmentConnection connection) {
        if (connection != null) {
            try {
                NamedParameterJdbcTemplate jdbcTemplate = connection.getConnectionTemplate();
                FlowStepContext flowContext = new FlowStepContext(flowStepContext.getStepPackageContext(), flowStepContext.getStepGroup(), flowStepContext.getStep());

                Stream result = jdbcTemplate.queryForStream(flowStepContext.getStep().getSql(),
                        buildParameterQuery(flowStepContext, flowContext), new ColumnDocumentRowMapper());

                return result.iterator();
            } catch (DataAccessException e) {
                throw new DataProviderException("Data provider exception", e);
            }

        } else
            logger.error("Error: couldn't find connection for environment: {}", flowStepContext.getStep().getEnvironment());

        return null;
    }

    private DataSource dataSource(JdbcEnvironment settings) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        if (StringUtils.isNoneBlank(settings.getDriver()))
            dataSource.setDriverClassName(settings.getDriver());

        dataSource.setUrl(settings.getUri());
        dataSource.setUsername(settings.getUserName());
        dataSource.setPassword(settings.getPassword());
        return dataSource;
    }

    private NamedParameterJdbcTemplate buildTemplate(JdbcEnvironment settings) {
        NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dataSource(settings));
        if (settings.getTimeout() > 0)
            template.getJdbcTemplate().setQueryTimeout(settings.getTimeout());
        return template;
    }

    private MapSqlParameterSource buildParameterQuery(FlowNewStepContext flowStepContext, FlowStepContext flowContext) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        //TODO need to figure out how to process batches
        flowStepContext.getStep().getFilters()
                .forEach(filter -> {

                    HasFlowTransform transformer = FlowFactory.getTransformFactory()
                            .getFlowTransform(filter.getFieldType());

                    filter.processValueFromTransform(flowContext);

                    Object value = transformer != null ? transformer.transform(filter.getFieldValue())
                            : filter.getFieldValue();

                    if (value != null)
                        params.addValue(filter.getFieldName(), value);
                });
        return params;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy