Skip to content

Commit

Permalink
work on file generation
Browse files Browse the repository at this point in the history
  • Loading branch information
floris-thijssen-ritense committed Oct 21, 2024
1 parent fd02bd4 commit 265b7cb
Show file tree
Hide file tree
Showing 16 changed files with 392 additions and 47 deletions.
31 changes: 31 additions & 0 deletions backend/app/.env.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file with secrets has been made public for demonstration purposes.

VALTIMO_PLUGIN_ENCRYPTION-SECRET=abcdefghijklmnop
VALTIMO_CONNECTOR-ENCRYPTION_SECRET=0123456789101112

SPRING_DATASOURCE_PASSWORD=password
SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_KEYCLOAKAPI_CLIENT-SECRET=6ef6ca16-6b86-482a-a3d9-0561704c1db9

VALTIMO_API_URL=http://host.docker.internal:8080/api/v1/
ZAKEN_API_URL=http://localhost:8001/zaken/api/v1/
BESLUITEN_API_URL=http://localhost:8001/besluiten/api/v1/
CATALOGI_API_URL=http://localhost:8001/catalogi/api/v1/
DOCUMENTEN_API_URL=http://localhost:8001/documenten/api/v1/
NOTIFICATIES_API_URL=http://localhost:8002/api/v1/
OBJECTEN_API_URL=http://localhost:8010/api/v2/
OBJECTTYPEN_API_URL=http://localhost:8011/api/v1/

OPEN_ZAAK_CLIENT_ID=valtimo_client
OPEN_ZAAK_CLIENT_SECRET=e09b8bc5-5831-4618-ab28-41411304309d

OPEN_NOTIFICATIES_CLIENT_ID=valtimo
OPEN_NOTIFICATIES_CLIENT_SECRET=zZ!xRP&$qTn4A9ETa^ZMKepDm^8egjPz

OBJECTEN_API_TOKEN=182c13e2209161852c53cef53a879f7a2f923430
OBJECTTYPEN_API_TOKEN=cd63e158f3aca276ef284e3033d020a22899c728

SMART_DOCUMENTS_URL=https://example.com/
SMART_DOCUMENTS_USERNAME=test-user
SMART_DOCUMENTS_PASSWORD=test-password

VALTIMO_OPENKLANT_SECRET=e09b8bc5-5831-4618-ab28-41411304309d
15 changes: 15 additions & 0 deletions backend/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dependencies {
implementation "com.ritense.valtimo:process-document:${valtimoVersion}"
implementation "com.ritense.valtimo:plugin:${valtimoVersion}"
implementation "com.ritense.valtimo:plugin-valtimo:${valtimoVersion}"
implementation "com.ritense.valtimo:objecten-api-authentication:${valtimoVersion}"
implementation "com.ritense.valtimo:zgw:${valtimoVersion}"
implementation "com.ritense.valtimo:web:${valtimoVersion}"

implementation "io.github.microutils:kotlin-logging:3.0.5"
Expand All @@ -55,3 +57,16 @@ dependencies {
test {
useJUnitPlatform()
}

bootRun {
doFirst {
File f = file(".env.properties")
if (f.isFile()) {
def props = new Properties()
f.withInputStream { props.load(it) }
props.each { key, value ->
environment key.toString(), value.toString()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
[
{
"id": "b609a0a3-886e-4b3d-ae0d-c01effb311ee",
"title": "OpenZaak Authentication",
"pluginDefinitionKey": "openzaak",
"properties": {
"clientId": "${OPEN_ZAAK_CLIENT_ID}",
"clientSecret": "${OPEN_ZAAK_CLIENT_SECRET}"
}
},
{
"id": "3079d6fe-42e3-4f8f-a9db-52ce2507b7ee",
"title": "Zaken API",
"pluginDefinitionKey": "zakenapi",
"properties": {
"url": "${ZAKEN_API_URL}",
"authenticationPluginConfiguration": "b609a0a3-886e-4b3d-ae0d-c01effb311ee"
}
},
{
"id": "5474fe57-532a-4050-8d89-32e62ca3e895",
"title": "Documenten API",
"pluginDefinitionKey": "documentenapi",
"properties": {
"url": "${DOCUMENTEN_API_URL}",
"bronorganisatie": "051845623",
"authenticationPluginConfiguration": "b609a0a3-886e-4b3d-ae0d-c01effb311ee",
"apiVersion": "1.4.3"
}
},
{
"id": "21a006f9-7833-4cdf-a6b7-1927705dd543",
"title": "Objecten API Authentication",
"pluginDefinitionKey": "objecttokenauthentication",
"properties": {
"token": "${OBJECTEN_API_TOKEN}"
}
},
{
"id": "b6d83348-97e7-4660-bd35-2e5fcc9629b4",
"title": "Objecten API",
"pluginDefinitionKey": "objectenapi",
"properties": {
"url": "${OBJECTEN_API_URL}",
"authenticationPluginConfiguration": "21a006f9-7833-4cdf-a6b7-1927705dd543"
}
},
{
"id": "acb0687a-075e-4435-923b-e6cb01d4d5db",
"title": "Objecttypen API Authentication",
"pluginDefinitionKey": "objecttokenauthentication",
"properties": {
"token": "${OBJECTTYPEN_API_TOKEN}"
}
},
{
"id": "4021bb75-18c8-4ca5-8658-b9f9c728bba0",
"title": "Objecttypen API",
"pluginDefinitionKey": "objecttypenapi",
"properties": {
"url": "${OBJECTTYPEN_API_URL}",
"authenticationPluginConfiguration": "acb0687a-075e-4435-923b-e6cb01d4d5db"
}
}
]
3 changes: 3 additions & 0 deletions backend/plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ dependencies {
implementation "com.ritense.valtimo:plugin-valtimo:${valtimoVersion}"
implementation "com.ritense.valtimo:temporary-resource-storage:${valtimoVersion}"
implementation "com.ritense.valtimo:value-resolver:${valtimoVersion}"
implementation "com.ritense.valtimo:openzaak-plugin-authentication:${valtimoVersion}"
implementation "com.ritense.valtimo:zaken-api:${valtimoVersion}"
implementation "com.ritense.valtimo:documenten-api:${valtimoVersion}"

implementation "org.springframework.boot:spring-boot-starter-webflux:${springVersion}"
implementation "org.springframework.boot:spring-boot-starter-data-jpa:${springVersion}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ritense.valtimo.xential.domain

data class DocumentCreatedMessage(
val taakapplicatie: String,
val gebruiker: String,
val documentCreatieSessieId: String,
val formaat: FileFormat,
val documentkenmerk: String,
val data: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ritense.valtimo.xential.domain

enum class FileFormat {
WORD, PDF
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ data class GenerateDocumentProperties(
val templateId: UUID,
val fileFormat: FileFormat,
val documentId: String,
val messageName: String,
val templateData: Map<String, String>
)

enum class FileFormat {
WORD, PDF
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ data class XentialToken (
val token: UUID,
@Column(name = "process_id", nullable = false, updatable = false)
val processId: UUID,
@Column(name = "message_name", nullable = false, updatable = false)
val messageName: String,
@Column(name = "resume_url", nullable = true, updatable = false)
val resumeUrl: URI?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@

package com.ritense.valtimo.xential.plugin

import com.ritense.documentenapi.DocumentenApiPlugin
import com.ritense.openzaak.plugin.OpenZaakPlugin
import com.ritense.plugin.annotation.Plugin
import com.ritense.plugin.annotation.PluginAction
import com.ritense.plugin.annotation.PluginProperty
import com.ritense.processlink.domain.ActivityTypeWithEventName
import com.ritense.valtimo.xential.domain.GenerateDocumentProperties
import com.ritense.valtimo.xential.domain.XentialToken
import com.ritense.valtimo.xential.plugin.XentialPlugin.Companion.PLUGIN_KEY
import com.ritense.valtimo.xential.repository.XentialTokenRepository
import com.ritense.valtimo.xential.service.DocumentGenerationService
import com.ritense.zakenapi.ZakenApiPlugin
import com.rotterdam.xential.api.DefaultApi
import com.rotterdam.xential.model.Sjabloondata
import org.camunda.bpm.engine.delegate.DelegateExecution
import java.util.UUID

@Plugin(
key = "xential",
key = PLUGIN_KEY,
title = "Xential Plugin",
description = ""
)
Expand All @@ -44,6 +48,12 @@ class XentialPlugin(
@PluginProperty(key = "clientPassword", secret = true)
private lateinit var clientPassword: String

@PluginProperty(key = "documentenApiPluginConfiguration", secret = false)
lateinit var documentenApiPluginConfiguration: DocumentenApiPlugin

@PluginProperty(key = "zakenApiPluginConfiguration", secret = false)
lateinit var zakenApiPluginConfiguration: ZakenApiPlugin

@PluginAction(
key = "generate-document",
title = "Generate document",
Expand All @@ -59,4 +69,8 @@ class XentialPlugin(
)
}

companion object {
const val PLUGIN_KEY = "xential"
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,40 @@
package com.ritense.valtimo.xential.service

import com.ritense.documentenapi.DocumentenApiPlugin
import com.ritense.documentenapi.client.CreateDocumentRequest
import com.ritense.documentenapi.client.DocumentStatusType
import com.ritense.documentenapi.client.DocumentenApiClient
import com.ritense.documentenapi.event.DocumentCreated
import com.ritense.plugin.service.PluginService
import com.ritense.valtimo.xential.domain.DocumentCreatedMessage
import com.ritense.valtimo.xential.domain.GenerateDocumentProperties
import com.ritense.valtimo.xential.domain.XentialToken
import com.ritense.valtimo.xential.plugin.XentialPlugin
import com.ritense.valtimo.xential.repository.XentialTokenRepository
import com.ritense.zakenapi.ZaakUrlProvider
import com.ritense.zakenapi.ZakenApiPlugin
import com.ritense.zakenapi.client.LinkDocumentRequest
import com.ritense.zakenapi.client.ZakenApiClient
import com.rotterdam.xential.api.DefaultApi
import com.rotterdam.xential.model.Sjabloondata
import org.camunda.bpm.engine.RuntimeService
import org.openapitools.client.infrastructure.ApiClient
import org.springframework.context.ApplicationEventPublisher
import org.springframework.security.core.context.SecurityContextHolder
import java.io.ByteArrayInputStream
import java.time.LocalDate
import java.util.Base64
import java.util.UUID

class DocumentGenerationService(
val defaultApi: DefaultApi,
val xentialTokenRepository: XentialTokenRepository,
val pluginService: PluginService,
val documentenApiClient: DocumentenApiClient,
val applicationEventPublisher: ApplicationEventPublisher,
val zaakUrlProvider: ZaakUrlProvider,
val zakenApiClient: ZakenApiClient,
val runtimeService: RuntimeService,
) {

fun generateDocument(
Expand All @@ -37,9 +61,77 @@ class DocumentGenerationService(
val xentialToken = XentialToken(
token = UUID.fromString(result.documentCreatieSessieId),
processId = processId,
messageName = generateDocumentProperties.messageName,
resumeUrl = result.resumeUrl
)

xentialTokenRepository.save(xentialToken)
}

fun onDocumentGenerated(message: DocumentCreatedMessage) {
val plugin = getXentialPlugin(message)
val documentenApiPlugin = plugin.documentenApiPluginConfiguration
val zakenApiPlugin = plugin.zakenApiPluginConfiguration

val bytes = Base64.getDecoder().decode(message.data)

val xentialToken = xentialTokenRepository.findById(UUID.fromString(message.documentCreatieSessieId))
.orElseThrow { NoSuchElementException("Could not find Xential Token ${message.documentCreatieSessieId}") }

ByteArrayInputStream(bytes).use { inputStream ->
val createDocumentRequest = CreateDocumentRequest(
bronorganisatie = documentenApiPlugin.bronorganisatie,
creatiedatum = LocalDate.now(),
titel = message.documentkenmerk,
auteur = message.gebruiker,
status = DocumentStatusType.DEFINITIEF,
taal = "nld",
bestandsnaam = message.documentkenmerk,
bestandsomvang = bytes.size.toLong(),
inhoud = inputStream,
beschrijving = "",
ontvangstdatum = LocalDate.now(),
verzenddatum = LocalDate.now(),
informatieobjecttype = null,
formaat = message.formaat.name,
)

val documentCreateResult = documentenApiClient.storeDocument(
documentenApiPlugin.authenticationPluginConfiguration,
documentenApiPlugin.url,
createDocumentRequest
)
val event = DocumentCreated(
documentCreateResult.url,
documentCreateResult.auteur,
documentCreateResult.bestandsnaam,
documentCreateResult.bestandsomvang,
documentCreateResult.beginRegistratie
)
applicationEventPublisher.publishEvent(event)

val zaakUrl = zaakUrlProvider.getZaakUrl(xentialToken.processId)
zakenApiClient.linkDocument(
zakenApiPlugin.authenticationPluginConfiguration,
zakenApiPlugin.url,
LinkDocumentRequest(
documentCreateResult.url,
zaakUrl.toString(),
message.documentkenmerk,
""
)
)

runtimeService.createMessageCorrelation(xentialToken.messageName)
.processInstanceId(xentialToken.processId.toString())
.correlate()
}
}

private fun getXentialPlugin(message: DocumentCreatedMessage): XentialPlugin {
//FIXME needs a way of determining the right plugin
val pluginConfig = pluginService.findPluginConfiguration(XentialPlugin.PLUGIN_KEY) { _ -> true}
?: throw NoSuchElementException("Could not find Xential plugin")
return pluginService.createInstance(pluginConfig) as XentialPlugin
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ritense.valtimo.xential.web.rest

import com.fasterxml.jackson.databind.JsonNode
import com.ritense.valtimo.contract.annotation.SkipComponentScan
import com.ritense.valtimo.contract.domain.ValtimoMediaType
import com.ritense.valtimo.xential.domain.DocumentCreatedMessage
import com.ritense.valtimo.xential.service.DocumentGenerationService
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.util.UUID

@RestController
@SkipComponentScan
@RequestMapping("/api/xential", produces = [ValtimoMediaType.APPLICATION_JSON_UTF8_VALUE])
class DocumentResource(
val documentGenerationService: DocumentGenerationService
) {

@PostMapping("/v1/document")
fun handleSubmission(
@RequestBody message: DocumentCreatedMessage
) {
documentGenerationService.onDocumentGenerated(message)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<createTable tableName="xential_tokens">
<column name="token" type="uuid"/>
<column name="process_id" type="uuid"/>
<column name="message_name" type="VARCHAR(256)"/>
<column name="resume_url" type="VARCHAR(2083)"/>
</createTable>
</changeSet>
Expand Down
Loading

0 comments on commit 265b7cb

Please sign in to comment.