Skip to content

Commit

Permalink
OK-493 Tallennetaan jatkuvan sijoittelun data sijoittelun kantaan
Browse files Browse the repository at this point in the history
  • Loading branch information
jkorri committed Apr 19, 2024
1 parent fc0e128 commit d0dca19
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 85 deletions.
4 changes: 4 additions & 0 deletions sijoittelu-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

set -euo pipefail

DB_APP_DB=sijoittelu
DB_APP_VALINTAREKISTERI_DB=valintarekisteri
DB_APP_SIJOITTELU_DB=sijoittelu
DB_APP_USER=oph
DB_APP_PASSWORD=oph

echo "Creating database \"$DB_APP_DB\", creating role \"$DB_APP_USER\" with database owner privileges…"
echo "Creating databases \"$DB_APP_VALINTAREKISTERI_DB\", \"$DB_APP_SIJOITTELU_DB\", creating role \"$DB_APP_USER\" with database owner privileges…"

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-END
create role "${DB_APP_USER}" with password '${DB_APP_PASSWORD}' login;
create database "${DB_APP_DB}" encoding 'UTF-8' lc_collate 'C' lc_ctype 'C' TEMPLATE template0;
grant all privileges on database "${DB_APP_DB}" to "${DB_APP_USER}";
create database "${DB_APP_VALINTAREKISTERI_DB}" encoding 'UTF-8' lc_collate 'C' lc_ctype 'C' TEMPLATE template0;
grant all privileges on database "${DB_APP_VALINTAREKISTERI_DB}" to "${DB_APP_USER}";
create database "${DB_APP_SIJOITTELU_DB}" encoding 'UTF-8' lc_collate 'C' lc_ctype 'C' TEMPLATE template0;
grant all privileges on database "${DB_APP_SIJOITTELU_DB}" to "${DB_APP_USER}";
END

psql "${DB_APP_DB}" -c "ALTER SCHEMA \"public\" OWNER TO \"${DB_APP_USER}\"" \
psql "${DB_APP_VALINTAREKISTERI_DB}" -c "ALTER SCHEMA \"public\" OWNER TO \"${DB_APP_USER}\"" \
-c "GRANT ALL ON SCHEMA \"public\" TO \"${DB_APP_USER}\""
psql "${DB_APP_SIJOITTELU_DB}" -c "ALTER SCHEMA \"public\" OWNER TO \"${DB_APP_USER}\"" \
-c "GRANT ALL ON SCHEMA \"public\" TO \"${DB_APP_USER}\""
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, MongoAutoConfiguration.class })
@SpringBootApplication(exclude = { FlywayAutoConfiguration.class, MongoAutoConfiguration.class })
public class App {

public static final String CONTEXT_PATH = "/sijoittelu-service";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
package fi.vm.sade.sijoittelu.kooste.external.resource.seuranta.impl;

import com.google.gson.reflect.TypeToken;
import fi.vm.sade.sijoittelu.kooste.external.resource.seuranta.SijoitteluSeurantaResource;
import fi.vm.sade.sijoittelu.kooste.external.resource.viestintapalvelu.RestCasClient;
import fi.vm.sade.sijoittelu.laskenta.util.UrlProperties;
import fi.vm.sade.valinta.seuranta.sijoittelu.dto.SijoitteluDto;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

import java.time.Instant;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

// TODO: clean up implementation
@Service
public class SijoitteluSeurantaResourceImpl implements SijoitteluSeurantaResource {

private final RestCasClient restCasClient;
private static final Logger LOG = LoggerFactory.getLogger(SijoitteluSeurantaResourceImpl.class);

private final JdbcTemplate jdbcTemplate;

private final UrlProperties urlProperties;
private final RowMapper<SijoitteluDto> sijoitteluDtoRowMapper = (rs, rowNum) ->
new SijoitteluDto(
rs.getString("haku_oid"),
rs.getBoolean("jatkuva_paalla"),
rs.getDate("viimeksi_ajettu"),
null,
rs.getDate("aloitus"),
rs.getInt("ajotiheys")
);

@Autowired
public SijoitteluSeurantaResourceImpl(
@Qualifier("SeurantaCasClient") RestCasClient restCasClient, UrlProperties urlProperties) {
this.restCasClient = restCasClient;
this.urlProperties = urlProperties;
public SijoitteluSeurantaResourceImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
;

@Override
public SijoitteluDto hae(String hakuOid) {
try {
return this.restCasClient
.get(
this.urlProperties.url("valintalaskentakoostepalvelu.seuranta.rest.url")
+ "/sijoittelunseuranta/hae/"
+ hakuOid,
new TypeToken<SijoitteluDto>() {},
Collections.emptyMap(),
10 * 60 * 1000)
.get();
return jdbcTemplate.queryForObject("SELECT haku_oid, jatkuva_paalla, viimeksi_ajettu, aloitus, ajotiheys FROM jatkuvat WHERE haku_oid=?",
sijoitteluDtoRowMapper, hakuOid);
} catch (EmptyResultDataAccessException e) {
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -48,14 +52,7 @@ public SijoitteluDto hae(String hakuOid) {
@Override
public Collection<SijoitteluDto> hae() {
try {
return this.restCasClient
.get(
this.urlProperties.url("valintalaskentakoostepalvelu.seuranta.rest.url")
+ "/sijoittelunseuranta/hae",
new TypeToken<Collection<SijoitteluDto>>() {},
Collections.emptyMap(),
10 * 60 * 1000)
.get();
return jdbcTemplate.query("SELECT haku_oid, jatkuva_paalla, viimeksi_ajettu, aloitus, ajotiheys FROM jatkuvat", sijoitteluDtoRowMapper);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -64,18 +61,13 @@ public Collection<SijoitteluDto> hae() {
@Override
public SijoitteluDto merkkaaSijoittelunAjossaTila(String hakuOid, boolean tila) {
try {
return this.restCasClient
.put(
this.urlProperties.url("valintalaskentakoostepalvelu.seuranta.rest.url")
+ "/sijoittelunseuranta/sijoittelu/"
+ hakuOid
+ "/ajossa/"
+ tila,
new TypeToken<SijoitteluDto>() {},
Optional.empty(),
Collections.emptyMap(),
10 * 60 * 1000)
.get();
this.jdbcTemplate.update(
"INSERT INTO jatkuvat " +
"(haku_oid, jatkuva_paalla, viimeksi_ajettu, aloitus, ajotiheys) " +
"VALUES (?, ?, null, null, null) " +
"ON CONFLICT (haku_oid) DO UPDATE SET jatkuva_paalla=?",
hakuOid, tila, tila);
return this.hae(hakuOid);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -84,16 +76,13 @@ public SijoitteluDto merkkaaSijoittelunAjossaTila(String hakuOid, boolean tila)
@Override
public SijoitteluDto merkkaaSijoittelunAjetuksi(String hakuOid) {
try {
return this.restCasClient
.put(
this.urlProperties.url("valintalaskentakoostepalvelu.seuranta.rest.url")
+ "/sijoittelunseuranta/sijoittelu/"
+ hakuOid,
new TypeToken<SijoitteluDto>() {},
Optional.empty(),
Collections.emptyMap(),
10 * 60 * 1000)
.get();
String now = new Date().toString();
this.jdbcTemplate.update("INSERT INTO jatkuvat " +
"(haku_oid, jatkuva_paalla, viimeksi_ajettu, aloitus, ajotiheys) " +
"VALUES (?, false, ?::timestamptz, null, null) " +
"ON CONFLICT (haku_oid) DO UPDATE SET viimeksi_ajettu=?::timestamptz",
hakuOid, now, now);
return this.hae(hakuOid);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -102,14 +91,7 @@ public SijoitteluDto merkkaaSijoittelunAjetuksi(String hakuOid) {
@Override
public void poistaSijoittelu(String hakuOid) {
try {
this.restCasClient
.delete(
this.urlProperties.url("valintalaskentakoostepalvelu.seuranta.rest.url")
+ "/sijoittelunseuranta/sijoittelu/"
+ hakuOid,
Collections.emptyMap(),
10 * 60 * 1000)
.get();
this.jdbcTemplate.update("DELETE FROM jatkuvat WHERE haku_oid=?", hakuOid);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -119,23 +101,14 @@ public void poistaSijoittelu(String hakuOid) {
public void paivitaSijoittelunAloitusajankohta(
String hakuOid, long aloitusajankohta, int ajotiheys) {
try {
this.restCasClient
.put(
this.urlProperties.url("valintalaskentakoostepalvelu.seuranta.rest.url")
+ "/sijoittelunseuranta/sijoittelu/"
+ hakuOid
+ "/paivita"
+ "?aloitusajankohta="
+ aloitusajankohta
+ "&ajotiheys="
+ ajotiheys,
new TypeToken<String>() {},
Optional.empty(),
Collections.emptyMap(),
10 * 60 * 1000)
.get();
String aloitus = Date.from(Instant.ofEpochMilli(aloitusajankohta)).toString();
this.jdbcTemplate.update("INSERT INTO jatkuvat " +
"(haku_oid, jatkuva_paalla, viimeksi_ajettu, aloitus, ajotiheys) " +
"VALUES (?, false, null, ?::timestamptz, ?) " +
"ON CONFLICT (haku_oid) DO UPDATE SET aloitus=?::timestamptz, ajotiheys=?",
hakuOid, aloitus, ajotiheys, aloitus, ajotiheys);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
import fi.vm.sade.valinta.sharedutils.AuditLogger;
import fi.vm.sade.valintalaskenta.tulos.logging.LaskentaAuditLogImpl;
import fi.vm.sade.valintalaskenta.tulos.mapping.ValintalaskentaModelMapper;
import org.flywaydb.core.Flyway;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
Expand All @@ -23,10 +27,13 @@
"fi.vm.sade.valintalaskenta.tulos.service.impl",
"fi.vm.sade.valintalaskenta.tulos.service.impl.converters",
})
public class SijoitteluServiceConfiguration {
public class SijoitteluServiceConfiguration implements InitializingBean {

public static final String CALLER_ID = "1.2.246.562.10.00000000001.sijoittelu.sijoittelu-service";

@Autowired
JdbcTemplate jdbcTemplate;

@Bean
public Audit audit() { return new Audit(new AuditLogger(), "sijoittelu", ApplicationType.VIRKAILIJA); }

Expand Down Expand Up @@ -59,4 +66,14 @@ public void addCorsMappings(CorsRegistry registry) {
}
};
}

@Override
public void afterPropertiesSet() throws Exception {
Flyway flyway = new Flyway();
flyway.setSchemas("public");
flyway.setDataSource(jdbcTemplate.getDataSource());
flyway.setLocations("/db/migration");
flyway.setTable("sijoittelu_schema_version");
flyway.migrate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE TABLE IF NOT EXISTS jatkuvat (
haku_oid TEXT PRIMARY KEY,
jatkuva_paalla BOOLEAN,
viimeksi_ajettu TIMESTAMP,
aloitus TIMESTAMP,
ajotiheys INTEGER
);

CREATE TABLE IF NOT EXISTS jatkuva_virheet (
haku_oid TEXT,
aika TIMESTAMP,
virhe TEXT,
CONSTRAINT fk_haku_oid FOREIGN KEY (haku_oid) REFERENCES jatkuvat(haku_oid) ON DELETE CASCADE
);

DROP INDEX IF EXISTS jatkuva_virheet_haku_oid;

CREATE INDEX jatkuva_virheet_haku_oid ON jatkuva_virheet (haku_oid);
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public static void main(String[] args) {
System.setProperty("web.url.cas", "https://virkailija.hahtuvaopintopolku.fi/cas");

// postgres
System.setProperty("valintarekisteri.db.url", "jdbc:postgresql://localhost:5433/sijoittelu");
System.setProperty("valintarekisteri.db.url", "jdbc:postgresql://localhost:5433/valintarekisteri");
System.setProperty("valintarekisteri.db.user", "oph");
System.setProperty("valintarekisteri.db.password", "oph");

System.setProperty("spring.datasource.url", "jdbc:postgresql://localhost:5433/sijoittelu");
System.setProperty("spring.datasource.username", "oph");
System.setProperty("spring.datasource.password", "oph");

// mongo
System.setProperty("valintalaskenta-laskenta-service.mongodb.uri", "mongodb://${valintalaskentadb.user}:${valintalaskentadb.password}@localhost/valintalaskentadb");
System.setProperty("valintalaskenta-laskenta-service.mongodb.dbname", "valintalaskentadb");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ sijoittelu.email.smtp.use_tls={{ sijoittelu_email_smtp_use_tls | default('false'
sijoittelu.email.smtp.toinen_aste_emails={{ sijoittelu_email_smtp_toinen_aste_emails | default('') }}
sijoittelu.email.smtp.kk_emails={{ sijoittelu_email_smtp_kk_emails | default('') }}
sijoittelu.email.smtp.use_authentication={{ sijoittelu_email_smtp_use_authentication | default('false') }}

spring.datasource.username={{postgres_app_user}}
spring.datasource.password={{host_postgresql_sijoittelu_app_password}}
spring.datasource.url=jdbc:postgresql://{{host_postgresql_sijoittelu}}/sijoittelu

0 comments on commit d0dca19

Please # to comment.