Skip to content

Commit

Permalink
Use sequences for generating zaak and document id (#372)
Browse files Browse the repository at this point in the history
* Use pessimistic lock while generating generating Id

* use sequences for id generation

* include sequence creation in data.sql

* move id fields to zaakservice

* docker-compose to docker compose

* get prefix from the database and the starting value of sequences
  • Loading branch information
alisihab authored Sep 19, 2024
1 parent 048f7a9 commit 2478196
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 17 deletions.
4 changes: 2 additions & 2 deletions docker_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mv ./src/main/resources/config.json_example ./src/main/resources/config.json

mvn install -Dmaven.javadoc.skip=true -B -V -DskipTests

sudo docker-compose -f docker-compose.yml up --build -d
sudo docker compose -f docker-compose.yml up --build -d

# start the counter from 5 seconds
timeCounter=5
Expand All @@ -30,4 +30,4 @@ else
echo "=== Something went wrong while starting the application check log files. ==="
exit 1
fi
sudo docker-compose logs
sudo docker compose logs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 The Open Zaakbrug Contributors
* Copyright 2020-2021, 2024 The Open Zaakbrug Contributors
*
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the
* European Commission - subsequent versions of the EUPL (the "Licence");
Expand Down Expand Up @@ -47,11 +47,8 @@ public void load() throws ResponseStatusException {
@Override
public ResponseEntity<?> execute() throws ConverterException {
EmulateParameterRepository repository = SpringContext.getBean(EmulateParameterRepository.class);
var prefixparam = repository.getOne("DocumentIdentificatiePrefix");
var idparam = repository.getOne("DocumentIdentificatieHuidige");
var identificatie = Long.parseLong(idparam.getParameterValue()) + 1;
idparam.setParameterValue(Long.toString(identificatie));
repository.save(idparam);
var prefixparam = repository.getById("DocumentIdentificatiePrefix");
var identificatie = repository.getDocumentId();
var did = prefixparam.getParameterValue() + identificatie;
this.getSession().setFunctie("GenereerDocumentIdentificatie");
this.getSession().setKenmerk("documentidentificatie:" + did);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 The Open Zaakbrug Contributors
* Copyright 2020-2021, 2024 The Open Zaakbrug Contributors
*
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the
* European Commission - subsequent versions of the EUPL (the "Licence");
Expand Down Expand Up @@ -31,6 +31,7 @@
import nl.haarlem.translations.zdstozgw.translation.zds.services.ZaakService;
import nl.haarlem.translations.zdstozgw.utils.XmlUtils;


public class GenereerZaakIdentificatieEmulator extends Converter {

public GenereerZaakIdentificatieEmulator(RequestResponseCycle session, Translation translation,
Expand All @@ -50,14 +51,12 @@ zaakidentificatie, hiervoor gelden de volgende regels (genomen uit RGBZ):
1e 4 posities: gemeentecode van de gemeente die verantwoordelijk is voor de behandeling van de zaak;
pos. 5 – 40: alle alfanumerieke tekens m.u.v. diacrieten
*/

EmulateParameterRepository repository = SpringContext.getBean(EmulateParameterRepository.class);
var prefixparam = repository.getOne("ZaakIdentificatiePrefix");
var idparam = repository.getOne("ZaakIdentificatieHuidige");
var identificatie = Long.parseLong(idparam.getParameterValue()) + 1;
idparam.setParameterValue(Long.toString(identificatie));
repository.save(idparam);
var prefixparam = repository.getById("ZaakIdentificatiePrefix");
var identificatie = repository.getZaakId();
var zid = prefixparam.getParameterValue() + identificatie;

this.getSession().setFunctie("GenereerZaakIdentificatie");
this.getSession().setKenmerk("zaakidentificatie:" + zid);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 The Open Zaakbrug Contributors
* Copyright 2020-2021, 2024 The Open Zaakbrug Contributors
*
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the
* European Commission - subsequent versions of the EUPL (the "Licence");
Expand All @@ -16,10 +16,18 @@
package nl.haarlem.translations.zdstozgw.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import nl.haarlem.translations.zdstozgw.jpa.model.EmulateParameter;

@Repository
public interface EmulateParameterRepository extends JpaRepository<EmulateParameter, String> {

@Query(value = "SELECT NEXTVAL('HuidigeZaakIdentificatie')", nativeQuery = true)
Long getZaakId();

@Query(value = "SELECT NEXTVAL('HuidigeDocumentIdentificatie')", nativeQuery = true)
Long getDocumentId();

}
2 changes: 2 additions & 0 deletions src/main/resources/data-h2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ INSERT INTO emulate_parameter (parameter_name, parameter_description, parameter_
SELECT 'DocumentIdentificatieHuidige', 'Het laatste volgnummer dat is gebruikt voor de documentidentificatie in nl.haarlem.translations.zdstozgw.convertor.impl.GenereerDocumentIdentificatie', '1'
WHERE ( SELECT COUNT(*) FROM emulate_parameter WHERE parameter_name = 'DocumentIdentificatieHuidige') = 0;

CREATE SEQUENCE IF NOT EXISTS HuidigeZaakIdentificatie START WITH (SELECT parameter_value FROM emulate_parameter where parameter_name='ZaakIdentificatieHuidige') + 1 INCREMENT BY 1;
CREATE SEQUENCE IF NOT EXISTS HuidigeDocumentIdentificatie START WITH (SELECT parameter_value FROM emulate_parameter where parameter_name='DocumentIdentificatieHuidige') + 1 INCREMENT BY 1;
19 changes: 18 additions & 1 deletion src/main/resources/data-postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,21 @@ BEGIN
resultaat := SUBSTRING(resultaat, 0, positie);
RETURN resultaat;
END;
$$ LANGUAGE 'plpgsql'
$$ LANGUAGE 'plpgsql'

CREATE OR REPLACE FUNCTION create_sequences_if_not_exists()
RETURNS VOID AS '
DECLARE
zaak_id_start_value BIGINT;
document_id_start_value BIGINT;
BEGIN
SELECT parameter_value INTO zaak_id_start_value FROM emulate_parameter WHERE parameter_name = ''ZaakIdentificatieHuidige'';
EXECUTE ''CREATE SEQUENCE HuidigeZaakIdentificatie START WITH '' || zaak_id_start_value + 1;
SELECT parameter_value INTO document_id_start_value FROM emulate_parameter WHERE parameter_name = ''DocumentIdentificatieHuidige'';
EXECUTE ''CREATE SEQUENCE HuidigeDocumentIdentificatie START WITH '' || document_id_start_value + 1;
END;
' LANGUAGE plpgsql;

SELECT create_sequences_if_not_exists();

0 comments on commit 2478196

Please sign in to comment.