From cb4a31f29ef8a2ba2349e725db9455e738ba7e5e Mon Sep 17 00:00:00 2001 From: Sander Nilsen Rysjedal <117441169+Sander14121@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:55:13 +0100 Subject: [PATCH] Database (#5) * clean whitespaces & use requiredArgsConstructor * only deploy to alpha * adjust postgres db * adjust local config * remove hikari schema * adjust base-url locally * trigger on push if branch is named november * fix small bugs & rename entity class * upgrade adapter-common library * add alpha deployment for fintlabs * only deploy to alpha * change image-name * upgrade FINT version * adjust dependencies & add java object sourceCompatibility * change base-url to alpha * deploy to beta * Add mapping to links and change database identificator * Improve empty value handeling * remove links * include rogfk in beta deployment * add rogfk to kustomize beta * only set beskrivelseGyldighetsPeriode if not null * remove rogfk from beta * returns exception if you put resource that does not excist in database --------- Co-authored-by: Hknots Co-authored-by: Trond Magnus Sevre --- .github/workflows/CD.yaml | 15 ++++-- build.gradle | 29 +++++----- docker-compose.yaml | 12 +++++ gradle.properties | 2 +- .../overlays/alpha/fintlabs-no/flais.yaml | 28 ++++++++++ .../alpha/fintlabs-no/kustomization.yaml | 10 +++- .../overlays/beta/fintlabs-no/flais.yaml | 28 ++++++++++ .../beta/fintlabs-no/kustomization.yaml | 9 +++- kustomize/overlays/beta/rogfk-no/flais.yaml | 28 ++++++++++ .../overlays/beta/rogfk-no/kustomization.yaml | 10 +++- .../no/fintlabs/NotInDataBaseException.java | 7 +++ .../BehandlingsgrunnlagPublisher.java | 3 +- .../BehandlingsgrunnlagRepository.java | 8 ++- .../BehandlingsgrunnlagSubscriber.java | 7 +-- .../PersonopplysningJpaRepository.java | 9 ++++ .../PersonopplysningMappingService.java | 54 +++++++++++++++++++ .../PersonopplysningPublisher.java | 3 +- .../PersonopplysningRepository.java | 26 ++++++--- .../PersonopplysningRestTemplate.java | 1 - .../PersonopplysningSubscriber.java | 7 +-- .../event/PersonopplysningEventPublisher.java | 53 ++++++++++++++++++ .../PersonopplysningEventSubscriber.java | 24 +++++++++ .../model/PersonopplysningEntity.java | 37 +++++++++++++ src/main/resources/application-db.yaml | 17 ++++++ src/main/resources/application-local.yml | 22 ++++++++ 25 files changed, 404 insertions(+), 45 deletions(-) create mode 100644 docker-compose.yaml create mode 100644 kustomize/overlays/alpha/fintlabs-no/flais.yaml create mode 100644 kustomize/overlays/beta/fintlabs-no/flais.yaml create mode 100644 kustomize/overlays/beta/rogfk-no/flais.yaml create mode 100644 src/main/java/no/fintlabs/NotInDataBaseException.java create mode 100644 src/main/java/no/fintlabs/model/personopplysning/PersonopplysningJpaRepository.java create mode 100644 src/main/java/no/fintlabs/model/personopplysning/PersonopplysningMappingService.java create mode 100644 src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventPublisher.java create mode 100644 src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventSubscriber.java create mode 100644 src/main/java/no/fintlabs/model/personopplysning/model/PersonopplysningEntity.java create mode 100644 src/main/resources/application-db.yaml diff --git a/.github/workflows/CD.yaml b/.github/workflows/CD.yaml index 4e5484f..4a21fa2 100644 --- a/.github/workflows/CD.yaml +++ b/.github/workflows/CD.yaml @@ -1,12 +1,15 @@ name: CD on: - repository_dispatch: - types: [trigger-cd] +# repository_dispatch: +# types: [trigger-cd] + push: + branches: + - november env: REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} + IMAGE_NAME: ${{ github.repository }}-database jobs: build-and-push: @@ -58,9 +61,15 @@ jobs: # - viken-no # - visma-com cluster: +<<<<<<< HEAD +# - aks-api-fint-2022-02-08 + - aks-beta-fint-2021-11-23 + - aks-alpha-fint-2021-11-18 +======= # - aks-alpha-fint-2021-11-18 - aks-beta-fint-2021-11-23 - aks-api-fint-2022-02-08 +>>>>>>> main # - aks-pwf-fint-2021-10-20 steps: diff --git a/build.gradle b/build.gradle index 4f95534..7abc734 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'org.springframework.boot' version '2.7.11' + id 'org.springframework.boot' version '3.2.0' id 'io.spring.dependency-management' version '1.1.0' id 'java' id 'groovy' @@ -7,7 +7,9 @@ plugins { group = 'no.fintlabs' version = '0.0.1-SNAPSHOT' -sourceCompatibility = '17' +java { + sourceCompatibility = JavaVersion.VERSION_17 +} configurations { compileOnly { @@ -20,36 +22,35 @@ jar { } repositories { - mavenCentral() - repositories { - maven { - url "https://repo.fintlabs.no/releases" - } - } mavenLocal() + maven { + url "https://repo.fintlabs.no/releases" + } + mavenCentral() } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-webflux' + implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'io.netty:netty-resolver-dns-native-macos:4.1.86.Final:osx-aarch_64' - implementation 'no.fintlabs:fint-core-infra-models:1.1.1' + implementation 'no.fintlabs:fint-core-infra-models:2.0.0-rc-12' implementation 'no.fintlabs:fint-core-adapter-common:0.1.2' implementation "no.fint:fint-personvern-resource-model-java:${apiVersion}" - implementation "no.fint:fint-felles-resource-model-java:${apiVersion}" implementation 'no.fint:fint-event-model:3.0.2' implementation 'no.fint:fint-model-resource:0.4.1' + implementation 'no.fintlabs:fint-core-adapter-common:0.1.6-rc-3' + implementation 'org.postgresql:postgresql' implementation 'no.fintlabs:fint-metamodell-model-java:1.1.0' runtimeOnly 'org.springframework.boot:spring-boot-actuator' - testImplementation 'org.springframework.boot:spring-boot-starter-test' - + runtimeOnly 'org.spockframework:spock-spring:2.1-groovy-3.0' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' - runtimeOnly 'org.spockframework:spock-spring:2.1-groovy-3.0' + testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.spockframework:spock-core:2.1-groovy-3.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..928aecb --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,12 @@ +version: '3.7' +services: + postgres: + image: postgres + container_name: my_postgres + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: fint-personvern-kodeverk-adapter + ports: + - "5432:5432" + restart: unless-stopped diff --git a/gradle.properties b/gradle.properties index 1ea05b0..7b2d523 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -apiVersion=3.13.10 +apiVersion=3.18.1-rc-4 \ No newline at end of file diff --git a/kustomize/overlays/alpha/fintlabs-no/flais.yaml b/kustomize/overlays/alpha/fintlabs-no/flais.yaml new file mode 100644 index 0000000..c07b7aa --- /dev/null +++ b/kustomize/overlays/alpha/fintlabs-no/flais.yaml @@ -0,0 +1,28 @@ +apiVersion: fintlabs.no/v1alpha1 +kind: Application +metadata: + name: fint-personvern-kodeverk-adapter + namespace: fintlabs-no + labels: + app.kubernetes.io/name: fint-personvern-kodeverk-adapter + app.kubernetes.io/instance: fint-personvern-kodeverk-adapter_fintlabs_no + app.kubernetes.io/version: latest + app.kubernetes.io/component: adapter + app.kubernetes.io/part-of: fint-core + fintlabs.no/team: core + fintlabs.no/org-id: fintlabs.no +spec: + orgId: fintlabs.no + image: ghcr.io/fintlabs/fint-personvern-kodeverk-adapter-database:latest + env: + - name: fint.adapter.base-url + value: https://alpha.felleskomponent.no + onePassword: + itemPath: vaults/aks-alpha-vault/items/fint-personvern-kodeverk-adapter + resources: + limits: + memory: 512Mi + cpu: 500m + requests: + memory: 256Mi + cpu: 250m diff --git a/kustomize/overlays/alpha/fintlabs-no/kustomization.yaml b/kustomize/overlays/alpha/fintlabs-no/kustomization.yaml index c7bfaa7..56e9a85 100644 --- a/kustomize/overlays/alpha/fintlabs-no/kustomization.yaml +++ b/kustomize/overlays/alpha/fintlabs-no/kustomization.yaml @@ -1,5 +1,12 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization +<<<<<<< HEAD +resources: + - ../../../base +patches: + - path: flais.yaml + +======= namespace: fintlabs-no resources: - ../../../base @@ -13,4 +20,5 @@ commonLabels: app.kubernetes.io/component: backend app.kubernetes.io/part-of: fint-core fintlabs.no/team: core - fintlabs.no/org-id: fintlabs.no \ No newline at end of file + fintlabs.no/org-id: fintlabs.no +>>>>>>> main diff --git a/kustomize/overlays/beta/fintlabs-no/flais.yaml b/kustomize/overlays/beta/fintlabs-no/flais.yaml new file mode 100644 index 0000000..8bbc5c9 --- /dev/null +++ b/kustomize/overlays/beta/fintlabs-no/flais.yaml @@ -0,0 +1,28 @@ +apiVersion: fintlabs.no/v1alpha1 +kind: Application +metadata: + name: fint-personvern-kodeverk-adapter + namespace: fintlabs-no + labels: + app.kubernetes.io/name: fint-personvern-kodeverk-adapter + app.kubernetes.io/instance: fint-personvern-kodeverk-adapter_fintlabs_no + app.kubernetes.io/version: latest + app.kubernetes.io/component: adapter + app.kubernetes.io/part-of: fint-core + fintlabs.no/team: core + fintlabs.no/org-id: fintlabs.no +spec: + orgId: fintlabs.no + image: ghcr.io/fintlabs/fint-personvern-kodeverk-adapter-database:latest + env: + - name: fint.adapter.base-url + value: https://beta.felleskomponent.no + onePassword: + itemPath: vaults/aks-beta-vault/items/fint-personvern-kodeverk-adapter + resources: + limits: + memory: 512Mi + cpu: 500m + requests: + memory: 256Mi + cpu: 250m diff --git a/kustomize/overlays/beta/fintlabs-no/kustomization.yaml b/kustomize/overlays/beta/fintlabs-no/kustomization.yaml index c7bfaa7..3006012 100644 --- a/kustomize/overlays/beta/fintlabs-no/kustomization.yaml +++ b/kustomize/overlays/beta/fintlabs-no/kustomization.yaml @@ -1,8 +1,12 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization -namespace: fintlabs-no resources: - ../../../base +<<<<<<< HEAD +patches: + - path: flais.yaml + +======= patchesStrategicMerge: - config-map.yaml - one-password.yaml @@ -13,4 +17,5 @@ commonLabels: app.kubernetes.io/component: backend app.kubernetes.io/part-of: fint-core fintlabs.no/team: core - fintlabs.no/org-id: fintlabs.no \ No newline at end of file + fintlabs.no/org-id: fintlabs.no +>>>>>>> main diff --git a/kustomize/overlays/beta/rogfk-no/flais.yaml b/kustomize/overlays/beta/rogfk-no/flais.yaml new file mode 100644 index 0000000..5870fda --- /dev/null +++ b/kustomize/overlays/beta/rogfk-no/flais.yaml @@ -0,0 +1,28 @@ +apiVersion: fintlabs.no/v1alpha1 +kind: Application +metadata: + name: fint-personvern-kodeverk-adapter + namespace: rogfk-no + labels: + app.kubernetes.io/name: fint-personvern-kodeverk-adapter + app.kubernetes.io/instance: fint-personvern-kodeverk-adapter_rogfk_no + app.kubernetes.io/version: latest + app.kubernetes.io/component: adapter + app.kubernetes.io/part-of: fint-core + fintlabs.no/team: core + fintlabs.no/org-id: rogfk.no +spec: + orgId: rogfk.no + image: ghcr.io/fintlabs/fint-personvern-kodeverk-adapter-database:latest + env: + - name: fint.adapter.base-url + value: https://beta.felleskomponent.no + onePassword: + itemPath: vaults/aks-beta-vault/items/fint-personvern-kodeverk-adapter + resources: + limits: + memory: 512Mi + cpu: 500m + requests: + memory: 256Mi + cpu: 250m diff --git a/kustomize/overlays/beta/rogfk-no/kustomization.yaml b/kustomize/overlays/beta/rogfk-no/kustomization.yaml index beba8f8..ef1855e 100644 --- a/kustomize/overlays/beta/rogfk-no/kustomization.yaml +++ b/kustomize/overlays/beta/rogfk-no/kustomization.yaml @@ -1,5 +1,12 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization +<<<<<<< HEAD +resources: + - ../../../base +patches: + - path: flais.yaml + +======= namespace: rogfk-no resources: - ../../../base @@ -13,4 +20,5 @@ commonLabels: app.kubernetes.io/component: backend app.kubernetes.io/part-of: fint-core fintlabs.no/team: core - fintlabs.no/org-id: rogfk-no \ No newline at end of file + fintlabs.no/org-id: rogfk-no +>>>>>>> main diff --git a/src/main/java/no/fintlabs/NotInDataBaseException.java b/src/main/java/no/fintlabs/NotInDataBaseException.java new file mode 100644 index 0000000..dd6d9bd --- /dev/null +++ b/src/main/java/no/fintlabs/NotInDataBaseException.java @@ -0,0 +1,7 @@ +package no.fintlabs; + +public class NotInDataBaseException extends RuntimeException { + public NotInDataBaseException(String message) { + super(message); + } +} diff --git a/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagPublisher.java b/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagPublisher.java index 9d71851..3eebbb3 100644 --- a/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagPublisher.java +++ b/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagPublisher.java @@ -1,8 +1,8 @@ package no.fintlabs.model.behandlingsgrunnlag; +import jakarta.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import no.fint.model.resource.personvern.kodeverk.BehandlingsgrunnlagResource; -import no.fint.model.resource.personvern.samtykke.SamtykkeResource; import no.fintlabs.adapter.config.AdapterProperties; import no.fintlabs.adapter.datasync.ResourcePublisher; import no.fintlabs.adapter.datasync.ResourceRepository; @@ -11,7 +11,6 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import javax.annotation.PostConstruct; @Slf4j @Service diff --git a/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagRepository.java b/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagRepository.java index 3c22108..f2ac019 100644 --- a/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagRepository.java +++ b/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagRepository.java @@ -1,9 +1,10 @@ package no.fintlabs.model.behandlingsgrunnlag; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import no.fint.model.resource.personvern.kodeverk.BehandlingsgrunnlagResource; import no.fintlabs.adapter.events.WriteableResourceRepository; -import no.fintlabs.adapter.models.RequestFintEvent; +import no.fintlabs.adapter.models.event.RequestFintEvent; import org.springframework.stereotype.Repository; import java.util.ArrayList; @@ -11,14 +12,11 @@ @Slf4j @Repository +@RequiredArgsConstructor public class BehandlingsgrunnlagRepository implements WriteableResourceRepository { private final BehandlingsgrunnlagResources behandlingsgrunnlagJpaRepository; - public BehandlingsgrunnlagRepository(BehandlingsgrunnlagResources behandlingsgrunnlagJpaRepository) { - this.behandlingsgrunnlagJpaRepository = behandlingsgrunnlagJpaRepository; - } - @Override public List getResources() { return behandlingsgrunnlagJpaRepository.get(); diff --git a/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagSubscriber.java b/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagSubscriber.java index 8a8fed4..f1badd4 100644 --- a/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagSubscriber.java +++ b/src/main/java/no/fintlabs/model/behandlingsgrunnlag/BehandlingsgrunnlagSubscriber.java @@ -5,7 +5,7 @@ import no.fintlabs.adapter.config.AdapterProperties; import no.fintlabs.adapter.datasync.ResourceSubscriber; import no.fintlabs.adapter.models.AdapterCapability; -import no.fintlabs.adapter.models.SyncPageEntry; +import no.fintlabs.adapter.models.sync.SyncPageEntry; import no.fintlabs.adapter.validator.ValidatorService; import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.client.WebClient; @@ -14,7 +14,7 @@ @Service public class BehandlingsgrunnlagSubscriber extends ResourceSubscriber { - protected BehandlingsgrunnlagSubscriber(WebClient webClient, AdapterProperties props, BehandlingsgrunnlagPublisher publisher, ValidatorService validatorService) { + protected BehandlingsgrunnlagSubscriber(WebClient webClient, AdapterProperties props, BehandlingsgrunnlagPublisher publisher, ValidatorService validatorService) { super(webClient, props, publisher, validatorService); } @@ -25,8 +25,9 @@ protected AdapterCapability getCapability() { } @Override - protected SyncPageEntry createSyncPageEntry(BehandlingsgrunnlagResource resource) { + protected SyncPageEntry createSyncPageEntry(BehandlingsgrunnlagResource resource) { String identificationValue = resource.getSystemId().getIdentifikatorverdi(); return SyncPageEntry.of(identificationValue, resource); } + } diff --git a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningJpaRepository.java b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningJpaRepository.java new file mode 100644 index 0000000..cf67d89 --- /dev/null +++ b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningJpaRepository.java @@ -0,0 +1,9 @@ +package no.fintlabs.model.personopplysning; + +import no.fintlabs.model.personopplysning.model.PersonopplysningEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PersonopplysningJpaRepository extends JpaRepository { +} diff --git a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningMappingService.java b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningMappingService.java new file mode 100644 index 0000000..7ee211a --- /dev/null +++ b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningMappingService.java @@ -0,0 +1,54 @@ +package no.fintlabs.model.personopplysning; + +import no.fint.model.felles.kompleksedatatyper.Identifikator; +import no.fint.model.felles.kompleksedatatyper.Periode; +import no.fint.model.resource.personvern.kodeverk.PersonopplysningResource; +import no.fintlabs.model.personopplysning.model.PersonopplysningEntity; +import org.springframework.stereotype.Service; + +import java.util.Date; + +@Service +public class PersonopplysningMappingService { + + public static PersonopplysningResource toResource(PersonopplysningEntity personopplysningEntity) { + PersonopplysningResource personopplysningResource = new PersonopplysningResource(); + Identifikator identifikator = new Identifikator(); + identifikator.setIdentifikatorverdi(personopplysningEntity.getIdentifikatorVerdi()); + personopplysningResource.setKode(personopplysningEntity.getKode()); + personopplysningResource.setNavn(personopplysningEntity.getNavn()); + personopplysningResource.setPassiv(personopplysningEntity.isPassiv()); + personopplysningResource.setGyldighetsperiode(mapToPeriode(personopplysningEntity.getStartGyldighetsdato(), personopplysningEntity.getEndGyldighetsdato(), personopplysningEntity.getBeskrivelseGyldighetsPeriode())); + personopplysningResource.setSystemId(identifikator); + return personopplysningResource; + } + + public static PersonopplysningEntity toEntity(PersonopplysningResource personopplysningResource) { + PersonopplysningEntity personopplysningEntity = new PersonopplysningEntity(); + personopplysningEntity.setKode(personopplysningResource.getKode()); + if (personopplysningResource.getGyldighetsperiode() != null) { + if (personopplysningResource.getGyldighetsperiode().getStart() != null) + personopplysningEntity.setStartGyldighetsdato(personopplysningResource.getGyldighetsperiode().getStart()); + + if (personopplysningResource.getGyldighetsperiode().getSlutt() != null) + personopplysningEntity.setEndGyldighetsdato(personopplysningResource.getGyldighetsperiode().getSlutt()); + + personopplysningEntity.setBeskrivelseGyldighetsPeriode(personopplysningResource.getGyldighetsperiode().getBeskrivelse()); + } + + personopplysningEntity.setNavn(personopplysningResource.getNavn()); + if (personopplysningResource.getPassiv() != null) + personopplysningEntity.setPassiv(personopplysningResource.getPassiv()); + personopplysningEntity.setIdentifikatorVerdi(personopplysningResource.getSystemId().getIdentifikatorverdi()); + + return personopplysningEntity; + } + + private static Periode mapToPeriode(Date startDate, Date endDate, String beskrivelse) { + Periode periode = new Periode(); + periode.setStart(startDate); + periode.setSlutt(endDate); + periode.setBeskrivelse(beskrivelse); + return periode; + } +} diff --git a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningPublisher.java b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningPublisher.java index 3d893a0..a0ce6dd 100644 --- a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningPublisher.java +++ b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningPublisher.java @@ -1,8 +1,8 @@ package no.fintlabs.model.personopplysning; +import jakarta.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import no.fint.model.resource.personvern.kodeverk.PersonopplysningResource; -import no.fint.model.resource.personvern.samtykke.SamtykkeResource; import no.fintlabs.adapter.config.AdapterProperties; import no.fintlabs.adapter.datasync.ResourcePublisher; import no.fintlabs.adapter.datasync.ResourceRepository; @@ -11,7 +11,6 @@ import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import javax.annotation.PostConstruct; @Slf4j @Service diff --git a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRepository.java b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRepository.java index 6897eb4..e0158d8 100644 --- a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRepository.java +++ b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRepository.java @@ -1,9 +1,13 @@ package no.fintlabs.model.personopplysning; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import no.fint.model.resource.personvern.kodeverk.PersonopplysningResource; +import no.fintlabs.NotInDataBaseException; import no.fintlabs.adapter.events.WriteableResourceRepository; -import no.fintlabs.adapter.models.RequestFintEvent; +import no.fintlabs.adapter.models.event.RequestFintEvent; +import no.fintlabs.adapter.operation.OperationType; +import no.fintlabs.model.personopplysning.model.PersonopplysningEntity; import org.springframework.stereotype.Repository; import java.util.ArrayList; @@ -11,17 +15,17 @@ @Slf4j @Repository +@RequiredArgsConstructor public class PersonopplysningRepository implements WriteableResourceRepository { + private final PersonopplysningJpaRepository personopplysningJpaRepository; private final PersonopplysningRestTemplate personopplysningRestTemplate; - public PersonopplysningRepository(PersonopplysningRestTemplate personopplysningRestTemplate) { - this.personopplysningRestTemplate = personopplysningRestTemplate; - } - @Override public List getResources() { - return personopplysningRestTemplate.getPersonopplysningResources(); + return personopplysningJpaRepository.findAll().stream() + .map(PersonopplysningMappingService::toResource) + .toList(); } @Override @@ -31,6 +35,14 @@ public List getUpdatedResources() { @Override public PersonopplysningResource saveResources(PersonopplysningResource personopplysningResource, RequestFintEvent requestFintEvent) { - return null; + if (requestFintEvent.getOperationType().equals(OperationType.UPDATE) && !personopplysningJpaRepository.existsById(personopplysningResource.getSystemId().getIdentifikatorverdi())) { + throw new NotInDataBaseException("Can not update resource that is not in the database"); + } + + PersonopplysningEntity personopplysningEntity = PersonopplysningMappingService.toEntity(personopplysningResource); + log.info("Save personopplysning {}", personopplysningResource.getNavn()); + personopplysningJpaRepository.save(personopplysningEntity); + return personopplysningResource; } + } \ No newline at end of file diff --git a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRestTemplate.java b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRestTemplate.java index dff0f05..462076b 100644 --- a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRestTemplate.java +++ b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningRestTemplate.java @@ -15,7 +15,6 @@ import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; -import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; diff --git a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningSubscriber.java b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningSubscriber.java index a6d8d47..8d395fe 100644 --- a/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningSubscriber.java +++ b/src/main/java/no/fintlabs/model/personopplysning/PersonopplysningSubscriber.java @@ -5,7 +5,7 @@ import no.fintlabs.adapter.config.AdapterProperties; import no.fintlabs.adapter.datasync.ResourceSubscriber; import no.fintlabs.adapter.models.AdapterCapability; -import no.fintlabs.adapter.models.SyncPageEntry; +import no.fintlabs.adapter.models.sync.SyncPageEntry; import no.fintlabs.adapter.validator.ValidatorService; import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.client.WebClient; @@ -14,7 +14,8 @@ @Service public class PersonopplysningSubscriber extends ResourceSubscriber { - protected PersonopplysningSubscriber(WebClient webClient, AdapterProperties props, PersonopplysningPublisher publisher, ValidatorService validatorService) { + + protected PersonopplysningSubscriber(WebClient webClient, AdapterProperties props, PersonopplysningPublisher publisher, ValidatorService validatorService) { super(webClient, props, publisher, validatorService); } @@ -24,7 +25,7 @@ protected AdapterCapability getCapability() { } @Override - protected SyncPageEntry createSyncPageEntry(PersonopplysningResource resource) { + protected SyncPageEntry createSyncPageEntry(PersonopplysningResource resource) { String identificationValue = resource.getSystemId().getIdentifikatorverdi(); return SyncPageEntry.of(identificationValue, resource); } diff --git a/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventPublisher.java b/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventPublisher.java new file mode 100644 index 0000000..a0f2e12 --- /dev/null +++ b/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventPublisher.java @@ -0,0 +1,53 @@ +package no.fintlabs.model.personopplysning.event; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.extern.slf4j.Slf4j; +import no.fint.model.resource.personvern.kodeverk.PersonopplysningResource; +import no.fintlabs.adapter.config.AdapterProperties; +import no.fintlabs.adapter.events.EventPublisher; +import no.fintlabs.adapter.models.event.RequestFintEvent; +import no.fintlabs.adapter.models.event.ResponseFintEvent; +import no.fintlabs.adapter.models.sync.SyncPageEntry; +import no.fintlabs.model.personopplysning.PersonopplysningJpaRepository; +import no.fintlabs.model.personopplysning.PersonopplysningRepository; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; + +@Slf4j +@Service +public class PersonopplysningEventPublisher extends EventPublisher { + + + protected PersonopplysningEventPublisher(WebClient webClient, AdapterProperties adapterProperties, PersonopplysningRepository repository, ObjectMapper objectMapper, PersonopplysningJpaRepository personopplysningJpaRepository) { + super("personopplysning", PersonopplysningResource.class, webClient, adapterProperties, repository, objectMapper); + } + + @Override + @Scheduled(initialDelay = 5000, fixedDelay = 50000) + public void doCheckForNewEvents() { + checkForNewEvents(); + } + + @Override + public void handleEvent(RequestFintEvent requestFintEvent, PersonopplysningResource personopplysningResource) { + ResponseFintEvent response = createResponse(requestFintEvent); + response.setOperationType(requestFintEvent.getOperationType()); + + try { + PersonopplysningResource savedResource = repository.saveResources(personopplysningResource, requestFintEvent); + response.setValue(createSyncpageEntry(savedResource)); + } catch (Exception exception) { + response.setFailed(true); + response.setErrorMessage(exception.getMessage()); + log.error("Error in repository.saveResource", exception); + } + + submit(response); + } + + private SyncPageEntry createSyncpageEntry(PersonopplysningResource savedResource) { + return SyncPageEntry.of(savedResource.getSystemId().getIdentifikatorverdi(), savedResource); + } + +} diff --git a/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventSubscriber.java b/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventSubscriber.java new file mode 100644 index 0000000..a985b62 --- /dev/null +++ b/src/main/java/no/fintlabs/model/personopplysning/event/PersonopplysningEventSubscriber.java @@ -0,0 +1,24 @@ +package no.fintlabs.model.personopplysning.event; + +import lombok.extern.slf4j.Slf4j; +import no.fint.model.resource.personvern.kodeverk.PersonopplysningResource; +import no.fintlabs.adapter.config.AdapterProperties; +import no.fintlabs.adapter.events.EventSubscriber; +import no.fintlabs.adapter.models.event.ResponseFintEvent; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; + +@Slf4j +@Service +public class PersonopplysningEventSubscriber extends EventSubscriber { + + protected PersonopplysningEventSubscriber(WebClient webClient, AdapterProperties adapterProperties, PersonopplysningEventPublisher publisher) { + super(webClient, adapterProperties, publisher, "personopplysning"); + } + + @Override + protected void responsePostingEvent(ResponseEntity responseEntity, ResponseFintEvent responseFintEvent) { + log.info("Posting response for event: {} status code: {} ", responseFintEvent.getCorrId(), responseEntity.getStatusCode()); + } +} diff --git a/src/main/java/no/fintlabs/model/personopplysning/model/PersonopplysningEntity.java b/src/main/java/no/fintlabs/model/personopplysning/model/PersonopplysningEntity.java new file mode 100644 index 0000000..49c5cd1 --- /dev/null +++ b/src/main/java/no/fintlabs/model/personopplysning/model/PersonopplysningEntity.java @@ -0,0 +1,37 @@ +package no.fintlabs.model.personopplysning.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import lombok.Getter; +import lombok.Setter; + +import java.util.Date; +import java.util.List; +import java.util.Map; + +@Entity +@Getter +@Setter +public class PersonopplysningEntity { + @Id + @Column(nullable = false, unique = true) + private String identifikatorVerdi; + + private String kode; + + private Date startGyldighetsdato; + + private Date endGyldighetsdato; + + private String beskrivelseGyldighetsPeriode; + + @Column(nullable = false) + private String navn; + + private boolean passiv; + + private Date startIdentifikatorDato; + + private Date endIdentifikatorDato; +} diff --git a/src/main/resources/application-db.yaml b/src/main/resources/application-db.yaml new file mode 100644 index 0000000..496ebb0 --- /dev/null +++ b/src/main/resources/application-db.yaml @@ -0,0 +1,17 @@ +spring: + jpa: + properties: + hibernate: + dialect: org.hibernate.dialect.PostgreSQLDialect + jdbc: + time_zone: UTC + lob: + non_contextual_creation: true + enable_lazy_load_no_trans: true + hibernate: + ddl-auto: update + datasource: + driver-class-name: org.postgresql.Driver + username: ${fint.database.username} + url: ${fint.database.url} + password: ${fint.database.password} \ No newline at end of file diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index bbe0d4f..21c3679 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -1,10 +1,20 @@ fint: metamodell: https://beta.felleskomponent.no/fint/metamodell/klasse + database: + username: postgres + password: password + url: jdbc:postgresql://localhost:5436/fint-personvern-kodeverk-adapter adapter: id: https://vigoiks.no/fintlabs-no/personvern/kodeverk-henrik-test +<<<<<<< HEAD + password: ${fint.adapter.password} + username: ${fint.adapter.username} + base-url: http://localhost:8081 +======= password: username: base-url: 'https://beta.felleskomponent.no' +>>>>>>> main registration-id: fint org-id: fintlabs.no heartbeat-interval: 1 @@ -23,6 +33,13 @@ fint: deltaSyncInterval: IMMEDIATE spring: +<<<<<<< HEAD + datasource: + url: jdbc:postgresql://localhost:5436/fint-personvern-kodeverk-adapter + username: postgres + password: password +======= +>>>>>>> main security: oauth2: client: @@ -32,7 +49,12 @@ spring: registration: fint: authorization-grant-type: password +<<<<<<< HEAD + client-id: ${fint.adapter.client-id} + client-secret: ${fint.adapter.client-secret} +======= client-id: client-secret: +>>>>>>> main provider: fint-idp scope: fint-adapter