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
.blocks
;
10 import ch
.qos
.cal10n
.IMessageConveyor
;
11 import com
.squareup
.javapoet
.CodeBlock
;
12 import de
.xn__ho_hia
.javapoet
.TypeGuesser
;
13 import wtf
.metio
.yosql
.internals
.jdk
.Strings
;
14 import wtf
.metio
.yosql
.models
.configuration
.ResultRowConverter
;
15 import wtf
.metio
.yosql
.models
.immutables
.FilesConfiguration
;
16 import wtf
.metio
.yosql
.models
.immutables
.SqlConfiguration
;
17 import wtf
.metio
.yosql
.models
.immutables
.SqlStatement
;
19 import java
.util
.List
;
20 import java
.util
.Optional
;
22 import static java
.util
.function
.Predicate
.not
;
24 public final class DefaultJavadoc
implements Javadoc
{
26 private final FilesConfiguration files
;
27 private final IMessageConveyor messages
;
29 public DefaultJavadoc(final FilesConfiguration files
, final IMessageConveyor messages
) {
31 this.messages
= messages
;
35 public CodeBlock
repositoryJavadoc(final List
<SqlStatement
> statements
) {
36 final var builder
= CodeBlock
.builder()
37 .add(messages
.getMessage(Javadocs
.USED_FILES
))
38 .add(messages
.getMessage(Javadocs
.LIST_START
));
39 final var input
= files
.inputBaseDirectory();
41 .map(SqlStatement
::getSourcePath
)
43 .map(input
::relativize
)
44 .forEach(path
-> builder
.add(messages
.getMessage(Javadocs
.LIST_ITEM
), path
));
45 builder
.add(messages
.getMessage(Javadocs
.LIST_END
));
46 return builder
.build();
50 public CodeBlock
fieldJavaDoc(final SqlStatement statement
) {
51 final var input
= files
.inputBaseDirectory();
52 final var builder
= CodeBlock
.builder()
53 .add(messages
.getMessage(Javadocs
.USED_FILE
))
54 .add(messages
.getMessage(Javadocs
.LIST_START
))
55 .add(messages
.getMessage(Javadocs
.LIST_ITEM
), input
.relativize(statement
.getSourcePath()));
56 builder
.add(messages
.getMessage(Javadocs
.LIST_END
));
57 return builder
.build();
61 public CodeBlock
methodJavadoc(final List
<SqlStatement
> statements
, final String configuration
) {
62 final var builder
= CodeBlock
.builder();
64 .map(SqlStatement
::getConfiguration
)
65 .flatMap(config
-> config
.description().stream())
66 .filter(not(Strings
::isBlank
))
67 .forEach(description
-> builder
.add(messages
.getMessage(Javadocs
.DESCRIPTION
), description
));
68 if (statements
.size() > 1) {
69 builder
.add(messages
.getMessage(Javadocs
.EXECUTED_STATEMENTS
));
71 builder
.add(messages
.getMessage(Javadocs
.EXECUTED_STATEMENT
));
73 for (final var statement
: statements
) {
74 statement
.getConfiguration().vendor()
75 .filter(not(Strings
::isBlank
))
76 .ifPresentOrElse(vendor
->
77 builder
.add(messages
.getMessage(Javadocs
.VENDOR
), vendor
),
79 if (statements
.size() > 1) {
80 builder
.add(messages
.getMessage(Javadocs
.FALLBACK
));
83 builder
.add(messages
.getMessage(Javadocs
.STATEMENT
), statement
.getRawStatement());
85 builder
.add(messages
.getMessage(Javadocs
.USED_FILES_METHOD
))
86 .add(messages
.getMessage(Javadocs
.LIST_START
));
87 final var input
= files
.inputBaseDirectory();
89 .map(SqlStatement
::getSourcePath
)
91 .map(input
::relativize
)
92 .forEach(path
-> builder
.add(messages
.getMessage(Javadocs
.LIST_ITEM
), path
));
93 builder
.add(messages
.getMessage(Javadocs
.LIST_END
));
94 builder
.add(messages
.getMessage(Javadocs
.DISABLE_WITH
), configuration
);
96 .map(SqlStatement
::getConfiguration
)
97 .map(SqlConfiguration
::resultRowConverter
)
98 .flatMap(Optional
::stream
)
99 .map(ResultRowConverter
::resultType
)
100 .flatMap(Optional
::stream
)
101 .filter(type
-> !type
.startsWith("java"))
102 .map(type
-> type
.substring(0, type
.contains("<") ? type
.indexOf("<") : type
.length()))
104 .map(TypeGuesser
::guessTypeName
)
105 .filter(type
-> !type
.isPrimitive())
106 .filter(type
-> !type
.isBoxedPrimitive())
107 .forEach(type
-> builder
.add(messages
.getMessage(Javadocs
.SEE
), type
));
108 return builder
.build();