2 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at https://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
8 package wtf
.metio
.yosql
.codegen
.files
;
10 import ch
.qos
.cal10n
.IMessageConveyor
;
11 import org
.slf4j
.cal10n
.LocLogger
;
12 import wtf
.metio
.yosql
.codegen
.lifecycle
.ValidationErrors
;
13 import wtf
.metio
.yosql
.codegen
.orchestration
.ExecutionErrors
;
14 import wtf
.metio
.yosql
.models
.configuration
.SqlParameter
;
15 import wtf
.metio
.yosql
.models
.immutables
.SqlConfiguration
;
17 import java
.nio
.file
.Path
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Collection
;
20 import java
.util
.List
;
22 import java
.util
.function
.Predicate
;
23 import java
.util
.stream
.Collectors
;
24 import java
.util
.stream
.Stream
;
26 public final class DefaultMethodParameterConfigurer
implements MethodParameterConfigurer
{
28 private final LocLogger logger
;
29 private final ExecutionErrors errors
;
30 private final IMessageConveyor messages
;
32 public DefaultMethodParameterConfigurer(
33 final LocLogger logger
,
34 final ExecutionErrors errors
,
35 final IMessageConveyor messages
) {
38 this.messages
= messages
;
42 public SqlConfiguration
configureParameters(final SqlConfiguration configuration
, final Path source
, final Map
<String
, List
<Integer
>> parameterIndices
) {
43 final var currentParameters
= configuration
.parameters();
44 if (parametersAreValid(source
, parameterIndices
, configuration
)) {
45 final var updatedParameters
= updateIndices(currentParameters
, parameterIndices
);
46 final var allParameters
= addMissingParameters(updatedParameters
, parameterIndices
);
47 return SqlConfiguration
.copyOf(configuration
).withParameters(allParameters
);
52 private boolean parametersAreValid(
54 final Map
<String
, List
<Integer
>> parameterIndices
,
55 final SqlConfiguration configuration
) {
56 final var parameterErrors
= Stream
.ofNullable(configuration
.parameters())
57 .flatMap(Collection
::stream
)
58 .filter(param
-> !parameterIndices
.containsKey(param
.name()))
59 .map(param
-> messages
.getMessage(ValidationErrors
.UNKNOWN_PARAMETER
, source
, param
.name()))
60 .peek(errors
::illegalArgument
)
63 return parameterErrors
.isEmpty();
66 private static List
<SqlParameter
> updateIndices(final List
<SqlParameter
> parameters
, final Map
<String
, List
<Integer
>> indices
) {
67 return parameters
.stream()
68 .map(parameter
-> SqlParameter
.builder()
69 .setName(parameter
.name())
70 .setType(parameter
.type())
71 .setIndices(asIntArray(indices
.get(parameter
.name())))
72 .setConverter(parameter
.converter())
74 .collect(Collectors
.toList());
77 private static int[] asIntArray(final List
<Integer
> numbers
) {
78 return numbers
.stream()
79 .mapToInt(Integer
::intValue
)
83 private List
<SqlParameter
> addMissingParameters(final List
<SqlParameter
> parameters
, final Map
<String
, List
<Integer
>> indices
) {
84 final var all
= new ArrayList
<>(parameters
);
85 for (final var entry
: indices
.entrySet()) {
86 final var parameterName
= entry
.getKey();
87 if (isMissingParameter(all
, parameterName
)) {
88 all
.add(SqlParameter
.builder()
89 .setName(parameterName
)
90 .setType("java.lang.Object")
91 .setIndices(asIntArray(entry
.getValue()))
98 private static boolean isMissingParameter(final List
<SqlParameter
> parameters
, final String parameterName
) {
99 return Stream
.ofNullable(parameters
).flatMap(Collection
::stream
).noneMatch(nameMatches(parameterName
));
102 private static Predicate
<?
super SqlParameter
> nameMatches(final String parameterName
) {
103 return parameter
-> parameterName
.equals(parameter
.name());