Skip to content

Commit 65af213

Browse files
committed
Trekker ut moduler fra behandlingsflyt for gjenbruk
1 parent 3a5c9bc commit 65af213

File tree

70 files changed

+4174
-23
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4174
-23
lines changed

.editorconfig

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 4
7+
indent_style = space
8+
insert_final_newline = false
9+
max_line_length = 120
10+
tab_width = 4
11+
ij_continuation_indent_size = 8
12+
ij_formatter_off_tag = @formatter:off
13+
ij_formatter_on_tag = @formatter:on
14+
ij_formatter_tags_enabled = true
15+
ij_smart_tabs = false
16+
ij_wrap_on_typing = false
17+
18+
[{*.gradle.kts,*.kt,*.kts,*.main.kts,*.space.kts}]
19+
ij_kotlin_align_in_columns_case_branch = false
20+
ij_kotlin_align_multiline_binary_operation = false
21+
ij_kotlin_align_multiline_extends_list = false
22+
ij_kotlin_align_multiline_method_parentheses = false
23+
ij_kotlin_align_multiline_parameters = true
24+
ij_kotlin_align_multiline_parameters_in_calls = false
25+
ij_kotlin_allow_trailing_comma = false
26+
ij_kotlin_allow_trailing_comma_on_call_site = false
27+
ij_kotlin_assignment_wrap = normal
28+
ij_kotlin_blank_lines_after_class_header = 0
29+
ij_kotlin_blank_lines_around_block_when_branches = 0
30+
ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_line = 1
31+
ij_kotlin_block_comment_add_space = false
32+
ij_kotlin_block_comment_at_first_column = true
33+
ij_kotlin_call_parameters_new_line_after_left_paren = true
34+
ij_kotlin_call_parameters_right_paren_on_new_line = true
35+
ij_kotlin_call_parameters_wrap = on_every_item
36+
ij_kotlin_catch_on_new_line = false
37+
ij_kotlin_class_annotation_wrap = split_into_lines
38+
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
39+
ij_kotlin_continuation_indent_for_chained_calls = false
40+
ij_kotlin_continuation_indent_for_expression_bodies = false
41+
ij_kotlin_continuation_indent_in_argument_lists = false
42+
ij_kotlin_continuation_indent_in_elvis = false
43+
ij_kotlin_continuation_indent_in_if_conditions = false
44+
ij_kotlin_continuation_indent_in_parameter_lists = false
45+
ij_kotlin_continuation_indent_in_supertype_lists = false
46+
ij_kotlin_else_on_new_line = false
47+
ij_kotlin_enum_constants_wrap = off
48+
ij_kotlin_extends_list_wrap = normal
49+
ij_kotlin_field_annotation_wrap = split_into_lines
50+
ij_kotlin_finally_on_new_line = false
51+
ij_kotlin_if_rparen_on_new_line = true
52+
ij_kotlin_import_nested_classes = false
53+
ij_kotlin_imports_layout = *,java.**,javax.**,kotlin.**,^
54+
ij_kotlin_insert_whitespaces_in_simple_one_line_method = true
55+
ij_kotlin_keep_blank_lines_before_right_brace = 2
56+
ij_kotlin_keep_blank_lines_in_code = 2
57+
ij_kotlin_keep_blank_lines_in_declarations = 2
58+
ij_kotlin_keep_first_column_comment = true
59+
ij_kotlin_keep_indents_on_empty_lines = false
60+
ij_kotlin_keep_line_breaks = true
61+
ij_kotlin_lbrace_on_next_line = false
62+
ij_kotlin_line_break_after_multiline_when_entry = true
63+
ij_kotlin_line_comment_add_space = false
64+
ij_kotlin_line_comment_add_space_on_reformat = false
65+
ij_kotlin_line_comment_at_first_column = true
66+
ij_kotlin_method_annotation_wrap = split_into_lines
67+
ij_kotlin_method_call_chain_wrap = normal
68+
ij_kotlin_method_parameters_new_line_after_left_paren = true
69+
ij_kotlin_method_parameters_right_paren_on_new_line = true
70+
ij_kotlin_method_parameters_wrap = on_every_item
71+
ij_kotlin_name_count_to_use_star_import = 2147483647
72+
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
73+
ij_kotlin_packages_to_use_import_on_demand = java.util.*,kotlinx.android.synthetic.**,io.ktor.**
74+
ij_kotlin_parameter_annotation_wrap = off
75+
ij_kotlin_space_after_comma = true
76+
ij_kotlin_space_after_extend_colon = true
77+
ij_kotlin_space_after_type_colon = true
78+
ij_kotlin_space_before_catch_parentheses = true
79+
ij_kotlin_space_before_comma = false
80+
ij_kotlin_space_before_extend_colon = true
81+
ij_kotlin_space_before_for_parentheses = true
82+
ij_kotlin_space_before_if_parentheses = true
83+
ij_kotlin_space_before_lambda_arrow = true
84+
ij_kotlin_space_before_type_colon = false
85+
ij_kotlin_space_before_when_parentheses = true
86+
ij_kotlin_space_before_while_parentheses = true
87+
ij_kotlin_spaces_around_additive_operators = true
88+
ij_kotlin_spaces_around_assignment_operators = true
89+
ij_kotlin_spaces_around_equality_operators = true
90+
ij_kotlin_spaces_around_function_type_arrow = true
91+
ij_kotlin_spaces_around_logical_operators = true
92+
ij_kotlin_spaces_around_multiplicative_operators = true
93+
ij_kotlin_spaces_around_range = false
94+
ij_kotlin_spaces_around_relational_operators = true
95+
ij_kotlin_spaces_around_unary_operator = false
96+
ij_kotlin_spaces_around_when_arrow = true
97+
ij_kotlin_variable_annotation_wrap = off
98+
ij_kotlin_while_on_new_line = false
99+
ij_kotlin_wrap_elvis_expressions = 1
100+
ij_kotlin_wrap_expression_body_functions = 1
101+
ij_kotlin_wrap_first_method_in_call_chain = false

.gitignore

+42-23
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
1-
# Compiled class file
2-
*.class
3-
4-
# Log file
5-
*.log
6-
7-
# BlueJ files
8-
*.ctxt
9-
10-
# Mobile Tools for Java (J2ME)
11-
.mtj.tmp/
12-
13-
# Package Files #
14-
*.jar
15-
*.war
16-
*.nar
17-
*.ear
1+
.settings
2+
.wtpmodules
3+
target
4+
.project
5+
.classpath
6+
outdated-dependencies.txt
7+
bin
8+
.idea
9+
*.iml
10+
_*.sh
11+
*.local
12+
*.localz
13+
es.properties
14+
es-local.properties
15+
es.properties.backup
16+
app.properties
17+
app-local.properties
18+
app.properties.backup
1819
*.zip
19-
*.tar.gz
20-
*.rar
21-
22-
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23-
hs_err_pid*
24-
replay_pid*
20+
*.versionsBackup
21+
*.log
22+
*.db
23+
mqjms.log*
24+
maven-eclipse.xml
25+
.externalToolBuilders
26+
dep.txt
27+
/docs/asciidoc/index.html
28+
.eslint-loader-cache
29+
.mvn/maven.config
30+
.mvn/jvm.config
31+
.flattened*
32+
*.patch
33+
/.mvn/
34+
/logback.xml
35+
.temp*.txt
36+
.env
37+
APP_LOG_HOME_IS_UNDEFINED
38+
.factorypath
39+
.DS_Store
40+
out/
41+
build/
42+
.gradle
43+
.java-version

CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @navikt/aap

build.gradle.kts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
2+
3+
plugins {
4+
kotlin("jvm") version "2.0.20"
5+
id("io.ktor.plugin") version "2.3.12" apply false
6+
}
7+
8+
group = "no.nav.aap.kelvin"
9+
10+
allprojects {
11+
repositories {
12+
mavenCentral()
13+
maven("https://github-package-registry-mirror.gc.nav.no/cached/maven-release")
14+
}
15+
}
16+
17+
subprojects {
18+
apply(plugin = "org.jetbrains.kotlin.jvm")
19+
kotlin {
20+
compilerOptions {
21+
apiVersion.set(org.jetbrains.kotlin.gradle.dsl.KotlinVersion.KOTLIN_2_0)
22+
}
23+
}
24+
25+
tasks {
26+
withType<ShadowJar> {
27+
mergeServiceFiles()
28+
}
29+
30+
withType<Test> {
31+
reports.html.required.set(false)
32+
useJUnitPlatform()
33+
maxParallelForks = Runtime.getRuntime().availableProcessors()
34+
}
35+
}
36+
37+
kotlin.sourceSets["main"].kotlin.srcDirs("main/kotlin")
38+
kotlin.sourceSets["test"].kotlin.srcDirs("test/kotlin")
39+
sourceSets["main"].resources.srcDirs("main/resources")
40+
sourceSets["test"].resources.srcDirs("test/resources")
41+
}

dbconnect/build.gradle.kts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dependencies {
2+
implementation("org.slf4j:slf4j-api:2.0.16")
3+
4+
testImplementation(project(":dbtest"))
5+
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.0")
6+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.11.0")
7+
testImplementation("org.assertj:assertj-core:3.26.3")
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package no.nav.aap.komponenter.dbconnect
2+
3+
import javax.sql.DataSource
4+
5+
fun <T> DataSource.transaction(readOnly: Boolean = false, block: (DBConnection) -> T): T {
6+
return this.connection.use { connection ->
7+
if (readOnly) {
8+
connection.isReadOnly = true // Setter transaction i read-only
9+
}
10+
val dbTransaction = no.nav.aap.komponenter.dbconnect.DBTransaction(connection)
11+
dbTransaction.transaction(block)
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package no.nav.aap.komponenter.dbconnect
2+
3+
import org.intellij.lang.annotations.Language
4+
import java.sql.Connection
5+
import java.sql.Savepoint
6+
import java.sql.Statement
7+
8+
private const val QUERY_TIMEOUT_IN_SECONDS = 30
9+
10+
class DBConnection internal constructor(private val connection: Connection) {
11+
private var savepoint: Savepoint? = null
12+
13+
fun execute(
14+
@Language("PostgreSQL")
15+
query: String,
16+
block: Execute.() -> Unit = {}
17+
) {
18+
return this.connection.prepareStatement(query).use { preparedStatement ->
19+
preparedStatement.queryTimeout = QUERY_TIMEOUT_IN_SECONDS
20+
val executeStatement = Execute(preparedStatement)
21+
executeStatement.block()
22+
executeStatement.execute()
23+
}
24+
}
25+
26+
fun <T> executeBatch(
27+
@Language("PostgreSQL")
28+
query: String,
29+
elements: Iterable<T>,
30+
block: ExecuteBatch<T>.() -> Unit = {}
31+
) {
32+
if (elements.none()) {
33+
return
34+
}
35+
elements.chunked(8000).forEach { subelement ->
36+
this.connection.prepareStatement(query).use { preparedStatement ->
37+
preparedStatement.queryTimeout = QUERY_TIMEOUT_IN_SECONDS
38+
val executeStatement = ExecuteBatch(preparedStatement, subelement)
39+
executeStatement.block()
40+
executeStatement.execute()
41+
}
42+
}
43+
}
44+
45+
fun executeReturnKey(
46+
@Language("PostgreSQL")
47+
query: String,
48+
block: Execute.() -> Unit = {}
49+
): Long {
50+
return this.connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS).use { preparedStatement ->
51+
preparedStatement.queryTimeout = QUERY_TIMEOUT_IN_SECONDS
52+
val executeStatement = Execute(preparedStatement)
53+
executeStatement.block()
54+
return@use executeStatement.executeReturnKey()
55+
}
56+
}
57+
58+
fun executeReturnKeys(
59+
@Language("PostgreSQL")
60+
query: String,
61+
block: Execute.() -> Unit = {}
62+
): List<Long> {
63+
return this.connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS).use { preparedStatement ->
64+
preparedStatement.queryTimeout = QUERY_TIMEOUT_IN_SECONDS
65+
val executeStatement = Execute(preparedStatement)
66+
executeStatement.block()
67+
return@use executeStatement.executeReturnKeys()
68+
}
69+
}
70+
71+
fun <T> queryFirstOrNull(
72+
@Language("PostgreSQL")
73+
query: String,
74+
block: Query<T>.() -> Unit
75+
): T? {
76+
return querySeq(query, block, Sequence<T>::firstOrNull)
77+
}
78+
79+
/**
80+
* Executes a query and retrieves the first result.
81+
*
82+
* @param query The SQL query to execute
83+
* @param block The block of code to customize the query
84+
* @return The first result of the query.
85+
* @throws java.util.NoSuchElementException If there are zero results.
86+
*/
87+
fun <T : Any> queryFirst(
88+
@Language("PostgreSQL")
89+
query: String,
90+
block: Query<T>.() -> Unit
91+
): T {
92+
return querySeq(query, block, Sequence<T>::first)
93+
}
94+
95+
fun <T : Any> queryList(
96+
@Language("PostgreSQL")
97+
query: String,
98+
block: Query<T>.() -> Unit
99+
): List<T> {
100+
return querySeq(query, block, Sequence<T>::toList)
101+
}
102+
103+
fun <T : Any> querySet(
104+
@Language("PostgreSQL")
105+
query: String,
106+
block: Query<T>.() -> Unit
107+
): Set<T> {
108+
return querySeq(query, block, Sequence<T>::toSet)
109+
}
110+
111+
private fun <T, U> querySeq(
112+
@Language("PostgreSQL")
113+
query: String,
114+
block: Query<T>.() -> Unit,
115+
extractor: Sequence<T>.() -> U
116+
): U {
117+
val queryparser = Queryparser(query)
118+
return this.connection.prepareStatement(queryparser.getPreparedQuery()).use { preparedStatement ->
119+
preparedStatement.queryTimeout = QUERY_TIMEOUT_IN_SECONDS
120+
val queryStatement = Query<T>(preparedStatement, queryparser)
121+
queryStatement.block()
122+
val result = queryStatement.executeQuery()
123+
return@use result.extractor()
124+
}
125+
}
126+
127+
fun markerSavepoint() {
128+
savepoint = this.connection.setSavepoint()
129+
}
130+
131+
internal fun rollback() {
132+
if (savepoint != null) {
133+
this.connection.rollback(savepoint)
134+
} else {
135+
this.connection.rollback()
136+
}
137+
}
138+
139+
internal fun commit() {
140+
this.connection.commit()
141+
}
142+
143+
internal fun autoCommitOn() {
144+
this.connection.autoCommit = true
145+
}
146+
147+
internal fun autoCommitOff() {
148+
this.connection.autoCommit = false
149+
}
150+
}

0 commit comments

Comments
 (0)