Skip to content

Commit

Permalink
Endre environment variabel til å bli privat
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur93 committed Nov 21, 2023
1 parent f95d2b5 commit e30ad45
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,32 @@ Filter array of objects by fieldvalue
<div>{{ barn.navn }}</div>
{{/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

Expand All @@ -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`

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/no/nav/pdfgen/core/Environment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/no/nav/pdfgen/core/PDFGenCore.kt
Original file line number Diff line number Diff line change
@@ -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())
}
}
}
7 changes: 3 additions & 4 deletions src/main/kotlin/no/nav/pdfgen/core/pdf/CreateHtml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -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)
Expand All @@ -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)) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/no/nav/pdfgen/core/pdf/CreatePdf.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/no/nav/pdfgen/core/template/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -183,14 +183,14 @@ fun registerNavHelpers(
registerHelper(
"image",
Helper<String> { context, _ ->
if (context == null) "" else environment.get().images[context]
if (context == null) "" else PDFGenCore.environment.images[context]
},
)

registerHelper(
"resource",
Helper<String> { context, _ ->
environment.get().resources[context]?.toString(Charsets.UTF_8) ?: ""
PDFGenCore.environment.resources[context]?.toString(Charsets.UTF_8) ?: ""
},
)

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/no/nav/pdfgen/core/util/FontMetadata.kt
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)
}

0 comments on commit e30ad45

Please sign in to comment.