From e30ad45a4baf782769045ee000b59d03326f2bc7 Mon Sep 17 00:00:00 2001 From: Ugur Cenar Date: Tue, 21 Nov 2023 14:08:47 +0100 Subject: [PATCH] =?UTF-8?q?Endre=20environment=20variabel=20til=20=C3=A5?= =?UTF-8?q?=20bli=20privat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 ++++++++++++++++++- .../kotlin/no/nav/pdfgen/core/Environment.kt | 2 +- .../kotlin/no/nav/pdfgen/core/PDFGenCore.kt | 10 +++++-- .../no/nav/pdfgen/core/pdf/CreateHtml.kt | 7 ++--- .../no/nav/pdfgen/core/pdf/CreatePdf.kt | 8 +++--- .../no/nav/pdfgen/core/template/Helpers.kt | 6 ++-- .../no/nav/pdfgen/core/util/FontMetadata.kt | 4 +-- 7 files changed, 47 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 09be53d..b07a424 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,32 @@ Filter array of objects by fieldvalue
{{ barn.navn }}
{{/filter}} +``` + +### {{json_to_period}} + +Format json with parameters `fom`, `tom`/`til` as period string + +**Example data** + +```json +{ + "periode": { + "fom": "2020-03-20", + "tom": "2021-09-23" + } +} +``` + +**Params** + +* `json` **{Periode}** Object with fields `fom` and `tom` or`til` +* `returns` **{List}** Formatted date (ex `20.03.2020 - 23.09.2021`) + +**Example** + +```handlebars +{{json_to_period periode}} ``` ## Developing pdfgen-core @@ -139,7 +165,7 @@ Filter array of objects by fieldvalue ### Publish to local maven repository Run the following command -`./gradlew publishToMavenLocal` +`./gradlew publishToMavenLocal` (or `./gradlew -t publishToMavenLocal` if you want to enable autobuild on changes) This will publish `pdfgen-core` to local maven repository with version `local-build` diff --git a/src/main/kotlin/no/nav/pdfgen/core/Environment.kt b/src/main/kotlin/no/nav/pdfgen/core/Environment.kt index 0aa0c47..f524ee8 100644 --- a/src/main/kotlin/no/nav/pdfgen/core/Environment.kt +++ b/src/main/kotlin/no/nav/pdfgen/core/Environment.kt @@ -78,7 +78,7 @@ data class PDFGenResource(val path: String) { fun getPath(filename: String? = null): Path { val filePath = filename?.let { _path.resolve(it) } ?: _path - log.info { "Reading file from path $filePath. File exists on path = ${filePath.exists()}" } + log.trace { "Reading file from path $filePath. File exists on path = ${filePath.exists()}" } return if (filePath.exists()) filePath else Path.of(Environment::class.java.classLoader.getResource(filePath.pathString)!!.toURI()) } diff --git a/src/main/kotlin/no/nav/pdfgen/core/PDFGenCore.kt b/src/main/kotlin/no/nav/pdfgen/core/PDFGenCore.kt index 71c8f60..e44b88f 100644 --- a/src/main/kotlin/no/nav/pdfgen/core/PDFGenCore.kt +++ b/src/main/kotlin/no/nav/pdfgen/core/PDFGenCore.kt @@ -1,17 +1,21 @@ package no.nav.pdfgen.core +import io.github.oshai.kotlinlogging.KotlinLogging import java.util.concurrent.atomic.AtomicReference -val environment = AtomicReference(Environment()) +private val coreEnvironment = AtomicReference(Environment()) +private val log = KotlinLogging.logger {} class PDFGenCore { companion object { fun init(initialEnvironment: Environment) { - environment.set(initialEnvironment) + coreEnvironment.set(initialEnvironment) } + val environment: Environment get() = coreEnvironment.get() fun reloadEnvironment() { - environment.set(environment.get().copy()) + log.debug { "Reloading environment" } + coreEnvironment.set(coreEnvironment.get().copy()) } } } diff --git a/src/main/kotlin/no/nav/pdfgen/core/pdf/CreateHtml.kt b/src/main/kotlin/no/nav/pdfgen/core/pdf/CreateHtml.kt index fbde3c0..bc71d41 100644 --- a/src/main/kotlin/no/nav/pdfgen/core/pdf/CreateHtml.kt +++ b/src/main/kotlin/no/nav/pdfgen/core/pdf/CreateHtml.kt @@ -8,7 +8,7 @@ import io.github.oshai.kotlinlogging.KotlinLogging import java.nio.file.Files import net.logstash.logback.argument.StructuredArguments import no.nav.pdfgen.core.HANDLEBARS_RENDERING_SUMMARY -import no.nav.pdfgen.core.environment +import no.nav.pdfgen.core.PDFGenCore import no.nav.pdfgen.core.objectMapper private val log = KotlinLogging.logger {} @@ -27,8 +27,7 @@ fun createHtmlFromTemplateData(template: String, directoryName: String): String? fun render(directoryName: String, template: String, jsonNode: JsonNode): String? { return HANDLEBARS_RENDERING_SUMMARY.startTimer() .use { - environment - .get() + PDFGenCore.environment .templates[directoryName to template] ?.apply( Context.newBuilder(jsonNode) @@ -52,7 +51,7 @@ fun render(directoryName: String, template: String, jsonNode: JsonNode): String? } private fun hotTemplateData(applicationName: String, template: String): JsonNode { - val dataFile = environment.get().dataRoot.getPath("$applicationName/$template.json") + val dataFile = PDFGenCore.environment.dataRoot.getPath("$applicationName/$template.json") val data = objectMapper.readValue( if (Files.exists(dataFile)) { diff --git a/src/main/kotlin/no/nav/pdfgen/core/pdf/CreatePdf.kt b/src/main/kotlin/no/nav/pdfgen/core/pdf/CreatePdf.kt index a82cd0c..534e0b2 100644 --- a/src/main/kotlin/no/nav/pdfgen/core/pdf/CreatePdf.kt +++ b/src/main/kotlin/no/nav/pdfgen/core/pdf/CreatePdf.kt @@ -4,13 +4,13 @@ import com.fasterxml.jackson.databind.JsonNode import com.openhtmltopdf.pdfboxout.PdfRendererBuilder import com.openhtmltopdf.svgsupport.BatikSVGDrawer import io.github.oshai.kotlinlogging.KotlinLogging +import no.nav.pdfgen.core.PDFGenCore import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.InputStream import java.io.OutputStream import java.util.Calendar import javax.imageio.ImageIO -import no.nav.pdfgen.core.environment import no.nav.pdfgen.core.util.scale import no.nav.pdfgen.core.util.toPortait import org.apache.pdfbox.pdmodel.PDDocument @@ -45,7 +45,7 @@ fun createPDFA(html: String): ByteArray { .apply { PdfRendererBuilder() .apply { - for (font in environment.get().fonts) { + for (font in PDFGenCore.environment.fonts) { useFont( { ByteArrayInputStream(font.bytes) }, font.family, @@ -57,7 +57,7 @@ fun createPDFA(html: String): ByteArray { } .usePdfAConformance(PdfRendererBuilder.PdfAConformance.PDFA_2_A) .usePdfUaAccessbility(true) - .useColorProfile(environment.get().colorProfile) + .useColorProfile(PDFGenCore.environment.colorProfile) .useSVGDrawer(BatikSVGDrawer()) .withHtmlContent(html, null) .toStream(this) @@ -107,7 +107,7 @@ fun createPDFA(imageStream: InputStream, outputStream: OutputStream) { throw IllegalArgumentException(e) } - val intent = PDOutputIntent(document, environment.get().colorProfile.inputStream()) + val intent = PDOutputIntent(document, PDFGenCore.environment.colorProfile.inputStream()) intent.info = "sRGB IEC61966-2.1" intent.outputCondition = "sRGB IEC61966-2.1" intent.outputConditionIdentifier = "sRGB IEC61966-2.1" diff --git a/src/main/kotlin/no/nav/pdfgen/core/template/Helpers.kt b/src/main/kotlin/no/nav/pdfgen/core/template/Helpers.kt index d790f8e..8750901 100644 --- a/src/main/kotlin/no/nav/pdfgen/core/template/Helpers.kt +++ b/src/main/kotlin/no/nav/pdfgen/core/template/Helpers.kt @@ -5,9 +5,9 @@ import com.fasterxml.jackson.databind.node.ArrayNode import com.github.jknack.handlebars.Context import com.github.jknack.handlebars.Handlebars import com.github.jknack.handlebars.Helper +import no.nav.pdfgen.core.PDFGenCore import no.nav.pdfgen.core.domain.Periode import no.nav.pdfgen.core.domain.PeriodeMapper -import no.nav.pdfgen.core.environment import no.nav.pdfgen.core.objectMapper import java.time.LocalDate import java.time.format.DateTimeFormatter @@ -183,14 +183,14 @@ fun registerNavHelpers( registerHelper( "image", Helper { context, _ -> - if (context == null) "" else environment.get().images[context] + if (context == null) "" else PDFGenCore.environment.images[context] }, ) registerHelper( "resource", Helper { context, _ -> - environment.get().resources[context]?.toString(Charsets.UTF_8) ?: "" + PDFGenCore.environment.resources[context]?.toString(Charsets.UTF_8) ?: "" }, ) diff --git a/src/main/kotlin/no/nav/pdfgen/core/util/FontMetadata.kt b/src/main/kotlin/no/nav/pdfgen/core/util/FontMetadata.kt index 4f2929d..6eb9754 100644 --- a/src/main/kotlin/no/nav/pdfgen/core/util/FontMetadata.kt +++ b/src/main/kotlin/no/nav/pdfgen/core/util/FontMetadata.kt @@ -1,7 +1,7 @@ package no.nav.pdfgen.core.util import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder -import no.nav.pdfgen.core.environment +import no.nav.pdfgen.core.PDFGenCore data class FontMetadata( val family: String, @@ -11,5 +11,5 @@ data class FontMetadata( val subset: Boolean, ) { val bytes: ByteArray - get() = environment.get().fontsRoot.readAllBytes(path) + get() = PDFGenCore.environment.fontsRoot.readAllBytes(path) }