Example usage for org.springframework.integration.jdbc JdbcMessageHandler setSqlParameterSourceFactory

List of usage examples for org.springframework.integration.jdbc JdbcMessageHandler setSqlParameterSourceFactory

Introduction

In this page you can find the example usage for org.springframework.integration.jdbc JdbcMessageHandler setSqlParameterSourceFactory.

Prototype

public void setSqlParameterSourceFactory(SqlParameterSourceFactory sqlParameterSourceFactory) 

Source Link

Usage

From source file:org.springframework.cloud.stream.app.jdbc.sink.JdbcSinkConfiguration.java

@Bean
@ServiceActivator(autoStartup = "false", inputChannel = Sink.INPUT)
public JdbcMessageHandler jdbcMessageHandler(DataSource dataSource) {
    final MultiValueMap<String, Expression> columnExpressionVariations = new LinkedMultiValueMap<>();
    for (Map.Entry<String, String> entry : properties.getColumns().entrySet()) {
        String value = entry.getValue();
        columnExpressionVariations.add(entry.getKey(), spelExpressionParser.parseExpression(value));
        if (!value.startsWith("payload")) {
            columnExpressionVariations.add(entry.getKey(),
                    spelExpressionParser.parseExpression("payload." + value));
        }//from  w w w . ja  v a  2  s .  c  o m
    }
    JdbcMessageHandler jdbcMessageHandler = new JdbcMessageHandler(dataSource,
            generateSql(properties.getTableName(), columnExpressionVariations.keySet()));
    jdbcMessageHandler.setSqlParameterSourceFactory(new SqlParameterSourceFactory() {
        @Override
        public SqlParameterSource createParameterSource(Object o) {
            if (!(o instanceof Message)) {
                throw new IllegalArgumentException("Unable to handle type " + o.getClass().getName());
            }
            Message<?> message = (Message<?>) o;
            MapSqlParameterSource parameterSource = new MapSqlParameterSource();
            for (String key : columnExpressionVariations.keySet()) {
                List<Expression> spels = columnExpressionVariations.get(key);
                Object value = NOT_SET;
                EvaluationException lastException = null;
                for (Expression spel : spels) {
                    try {
                        value = spel.getValue(evaluationContext, message);
                        break;
                    } catch (EvaluationException e) {
                        lastException = e;
                    }
                }
                if (value == NOT_SET) {
                    if (lastException != null) {
                        logger.info(
                                "Could not find value for column '" + key + "': " + lastException.getMessage());
                    }
                    parameterSource.addValue(key, null);
                } else {
                    if (value instanceof JsonPropertyAccessor.ToStringFriendlyJsonNode) {
                        // Need to do some reflection until we have a getter for the Node
                        DirectFieldAccessor dfa = new DirectFieldAccessor(value);
                        JsonNode node = (JsonNode) dfa.getPropertyValue("node");
                        Object valueToUse;
                        if (node == null || node.isNull()) {
                            valueToUse = null;
                        } else if (node.isNumber()) {
                            valueToUse = node.numberValue();
                        } else if (node.isBoolean()) {
                            valueToUse = node.booleanValue();
                        } else {
                            valueToUse = node.textValue();
                        }
                        parameterSource.addValue(key, valueToUse);
                    } else {
                        parameterSource.addValue(key, value);
                    }
                }
            }
            return parameterSource;
        }
    });
    return jdbcMessageHandler;
}