Skip to content

Commit

Permalink
Fix a bug in associating Jore4 routes with Jore4 lines.
Browse files Browse the repository at this point in the history
Track Jore4 line identifiers in `network_line_headers` table instead of
`network_lines` table because a Jore4 Line is created for each Jore3 line
header instance.
  • Loading branch information
jarkkoka committed Jan 5, 2024
1 parent d441395 commit a1f5e0b
Show file tree
Hide file tree
Showing 32 changed files with 687 additions and 512 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public LineExportMapper(final IJsonbConverter jsonConverter) {
@Override
public ImporterLine mapRow(final ResultSet resultSet, final int rowNumber) throws SQLException {
return ImporterLine.of(
ExternalId.of(resultSet.getString("external_id")),
ExternalId.of(resultSet.getString("line_header_external_id")),
resultSet.getString("line_number"),
jsonConverter.fromJson(resultSet.getString("line_header_name"), MultilingualString.class),
NetworkType.of(resultSet.getString("network_type")),
jsonConverter.fromJson(resultSet.getString("line_header_short_name"), MultilingualString.class),
DATE_RANGE_CONVERTER.from(resultSet.getString("line_header_valid_date_range")),
DATE_RANGE_CONVERTER.from(resultSet.getString("line_header_cluster_date_range")),
TypeOfLine.of(resultSet.getString("type_of_line")),
LegacyHslMunicipalityCode.valueOf(resultSet.getString("legacy_hsl_municipality_code"))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Jore4Line process(final ImporterLine input) throws Exception {

return Jore4Line.of(
UUID.randomUUID(),
input.externalIdOfLine().value(),
input.externalIdOflineHeader().value(),
input.lineNumber(),
input.name(),
input.shortName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fi.hsl.jore.importer.feature.batch.line;

import fi.hsl.jore.importer.feature.batch.line.support.ILineImportRepository;
import fi.hsl.jore.importer.feature.batch.line_header.support.ILineHeaderImportRepository;
import fi.hsl.jore.importer.feature.jore4.entity.Jore4Line;
import fi.hsl.jore.importer.feature.jore4.repository.IJore4LineRepository;
import fi.hsl.jore.importer.feature.network.line.dto.PersistableLineIdMapping;
Expand All @@ -16,26 +16,26 @@
@Component
public class LineExportWriter implements ItemWriter<Jore4Line> {

private final ILineImportRepository importerRepository;
private final IJore4LineRepository jore4Repository;
private final IJore4LineRepository jore4LineRepository;
private final ILineHeaderImportRepository importerLineHeaderRepository;

@Autowired
public LineExportWriter(final ILineImportRepository importerRepository,
final IJore4LineRepository jore4Repository) {
this.importerRepository = importerRepository;
this.jore4Repository = jore4Repository;
public LineExportWriter(final IJore4LineRepository jore4LineRepository,
final ILineHeaderImportRepository importerLineHeaderRepository) {
this.jore4LineRepository = jore4LineRepository;
this.importerLineHeaderRepository = importerLineHeaderRepository;
}

@Override
public void write(final List<? extends Jore4Line> items) throws Exception {
jore4Repository.insert(items);
jore4LineRepository.insert(items);

final io.vavr.collection.List<PersistableLineIdMapping> jore4IdMappings = items.stream()
.map(item -> PersistableLineIdMapping.of(
item.externalIdOfLine(),
item.externalIdOflineHeader(),
item.lineId()
))
.collect(io.vavr.collection.List.collector());
importerRepository.setJore4Ids(jore4IdMappings);
importerLineHeaderRepository.setJore4Ids(jore4IdMappings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@

import fi.hsl.jore.importer.feature.batch.common.IImportRepository;
import fi.hsl.jore.importer.feature.network.line.dto.PersistableLine;
import fi.hsl.jore.importer.feature.network.line.dto.PersistableLineIdMapping;
import fi.hsl.jore.importer.feature.network.line.dto.generated.LinePK;
import io.vavr.collection.List;

public interface ILineImportRepository extends IImportRepository<PersistableLine, LinePK> {

/**
* Sets the Jore 4 ids which identifies the lines in the Jore 4 database.
*
* @param idMappings The information that's required to set the Jore 4 ids
* of lines.
*/
void setJore4Ids(List<PersistableLineIdMapping> idMappings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import fi.hsl.jore.importer.feature.batch.common.AbstractImportRepository;
import fi.hsl.jore.importer.feature.network.line.dto.PersistableLine;
import fi.hsl.jore.importer.feature.network.line.dto.PersistableLineIdMapping;
import fi.hsl.jore.importer.feature.network.line.dto.generated.LinePK;
import fi.hsl.jore.importer.jooq.network.tables.NetworkLines;
import fi.hsl.jore.importer.jooq.network.tables.NetworkLinesStaging;
import io.vavr.collection.HashSet;
import io.vavr.collection.List;
import io.vavr.collection.Set;
import org.jooq.BatchBindStep;
import org.jooq.DSLContext;
Expand Down Expand Up @@ -102,17 +100,4 @@ protected Set<LinePK> insert() {
.map(row -> LinePK.of(row.value1()))
.collect(HashSet.collector());
}

@Transactional
@Override
public void setJore4Ids(final List<PersistableLineIdMapping> idMappings) {
db.batched(c -> {
idMappings.forEach(idMapping -> {
c.dsl().update(TARGET_TABLE)
.set(TARGET_TABLE.NETWORK_LINE_JORE4_ID, idMapping.jore4IdOfLine())
.where(TARGET_TABLE.NETWORK_LINE_EXT_ID.eq(idMapping.externalIdOfLine()))
.execute();
});
});
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package fi.hsl.jore.importer.feature.batch.line_header.support;

import fi.hsl.jore.importer.feature.batch.common.IImportRepository;
import fi.hsl.jore.importer.feature.network.line.dto.PersistableLineIdMapping;
import fi.hsl.jore.importer.feature.network.line_header.dto.Jore3LineHeader;
import fi.hsl.jore.importer.feature.network.line_header.dto.generated.LineHeaderPK;
import io.vavr.collection.List;

public interface ILineHeaderImportRepository extends IImportRepository<Jore3LineHeader, LineHeaderPK> {

/**
* Sets the Jore 4 ids which identifies the line in the Jore 4 database.
*
* @param idMappings The information that's required to set the Jore 4 ids
* of lines.
*/
void setJore4Ids(List<PersistableLineIdMapping> idMappings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import fi.hsl.jore.importer.feature.batch.common.AbstractImportRepository;
import fi.hsl.jore.importer.feature.common.converter.IJsonbConverter;
import fi.hsl.jore.importer.feature.network.line.dto.PersistableLineIdMapping;
import fi.hsl.jore.importer.feature.network.line_header.dto.Jore3LineHeader;
import fi.hsl.jore.importer.feature.network.line_header.dto.generated.LineHeaderPK;
import fi.hsl.jore.importer.jooq.network.tables.NetworkLineHeaders;
import fi.hsl.jore.importer.jooq.network.tables.NetworkLineHeadersStaging;
import fi.hsl.jore.importer.jooq.network.tables.NetworkLines;
import io.vavr.collection.HashSet;
import io.vavr.collection.List;
import io.vavr.collection.Set;
import org.jooq.BatchBindStep;
import org.jooq.DSLContext;
Expand Down Expand Up @@ -139,4 +141,17 @@ protected Set<LineHeaderPK> insert() {
.map(row -> LineHeaderPK.of(row.value1()))
.collect(HashSet.collector());
}

@Transactional
@Override
public void setJore4Ids(final List<PersistableLineIdMapping> idMappings) {
db.batched(c -> {
idMappings.forEach(idMapping -> {
c.dsl().update(TARGET_TABLE)
.set(TARGET_TABLE.JORE4_LINE_ID, idMapping.jore4IdOfLine())
.where(TARGET_TABLE.NETWORK_LINE_HEADER_EXT_ID.eq(idMapping.externalIdOfLineHeader()))
.execute();
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface Jore4Line {

UUID lineId();

String externalIdOfLine();
String externalIdOflineHeader();

String label();

Expand All @@ -37,7 +37,7 @@ public interface Jore4Line {
LegacyHslMunicipalityCode legacyHslMunicipalityCode();

static ImmutableJore4Line of(final UUID lineId,
final String externalIdOfLine,
final String externalIdOflineHeader,
final String label,
final MultilingualString name,
final MultilingualString shortName,
Expand All @@ -49,7 +49,7 @@ static ImmutableJore4Line of(final UUID lineId,
final LegacyHslMunicipalityCode legacyHslMunicipalityCode) {
return ImmutableJore4Line.builder()
.lineId(lineId)
.externalIdOfLine(externalIdOfLine)
.externalIdOflineHeader(externalIdOflineHeader)
.label(label)
.name(name)
.shortName(shortName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@Value.Immutable
public interface ImporterLine {

ExternalId externalIdOfLine();
ExternalId externalIdOflineHeader();

String lineNumber();

Expand All @@ -31,7 +31,7 @@ public interface ImporterLine {

LegacyHslMunicipalityCode legacyHslMunicipalityCode();

static ImmutableImporterLine of (final ExternalId externalIdOfLine,
static ImmutableImporterLine of (final ExternalId externalIdOflineHeader,
final String lineNumber,
final MultilingualString name,
final NetworkType networkType,
Expand All @@ -40,7 +40,7 @@ static ImmutableImporterLine of (final ExternalId externalIdOfLine,
final TypeOfLine typeOfLine,
final LegacyHslMunicipalityCode legacyHslMunicipalityCode) {
return ImmutableImporterLine.builder()
.externalIdOfLine(externalIdOfLine)
.externalIdOflineHeader(externalIdOflineHeader)
.lineNumber(lineNumber)
.name(name)
.networkType(networkType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ public interface Line
IHasSystemTime,
CommonFields<Line> {

Optional<UUID> jore4Id();

LegacyHslMunicipalityCode legacyHslMunicipalityCode();

static Line of(final LinePK pk,
final ExternalId externalId,
final NetworkType networkType,
final String lineNumber,
final TimeRange systemTime,
final Optional<UUID> jore4Id,
final TypeOfLine typeOfLine,
final LegacyHslMunicipalityCode legacyHslMunicipalityCode) {
return ImmutableLine.builder()
Expand All @@ -39,7 +36,6 @@ static Line of(final LinePK pk,
.networkType(networkType)
.lineNumber(lineNumber)
.systemTime(systemTime)
.jore4Id(jore4Id)
.typeOfLine(typeOfLine)
.legacyHslMunicipalityCode(legacyHslMunicipalityCode)
.build();
Expand All @@ -52,7 +48,6 @@ static Line from(final NetworkLinesRecord record) {
NetworkType.of(record.getInfrastructureNetworkType()),
record.getNetworkLineNumber(),
record.getNetworkLineSysPeriod(),
Optional.ofNullable(record.getNetworkLineJore4Id()),
TypeOfLine.of(record.getNetworkLineTypeOfLine()),
LegacyHslMunicipalityCode.valueOf(record.getNetworkLineLegacyHslMunicipalityCode())
);
Expand All @@ -65,7 +60,6 @@ static Line from(final NetworkLinesWithHistoryRecord record) {
NetworkType.of(record.getInfrastructureNetworkType()),
record.getNetworkLineNumber(),
record.getNetworkLineSysPeriod(),
Optional.ofNullable(record.getNetworkLineJore4Id()),
TypeOfLine.of(record.getNetworkLineTypeOfLine()),
LegacyHslMunicipalityCode.valueOf(record.getNetworkLineLegacyHslMunicipalityCode())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

/**
* Contains the information that's required to update the Jore 4 ID (primary
* key of a line found from the Jore 4 database) of a line stored in the
* database of the importer application.
* key of a line found from Jore 4 database) to the line header stored in the
* internal database of the importer application.
*/
@Value.Immutable
public interface PersistableLineIdMapping {

String externalIdOfLine();
String externalIdOfLineHeader();

UUID jore4IdOfLine();

static PersistableLineIdMapping of(final String externalIdOfLine,
static PersistableLineIdMapping of(final String externalIdOfLineHeader,
final UUID jore4IdOfLine) {
return ImmutablePersistableLineIdMapping.builder()
.externalIdOfLine(externalIdOfLine)
.externalIdOfLineHeader(externalIdOfLineHeader)
.jore4IdOfLine(jore4IdOfLine)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import fi.hsl.jore.importer.jooq.network.tables.records.NetworkLineHeadersWithHistoryRecord;
import org.immutables.value.Value;

import java.util.Optional;
import java.util.UUID;

@Value.Immutable
public interface LineHeader
extends IHasPK<LineHeaderPK>,
Expand All @@ -21,6 +24,8 @@ public interface LineHeader

LinePK line();

Optional<UUID> jore4IdOfLine();

static LineHeader of(final LineHeaderPK pk,
final LinePK line,
final ExternalId externalId,
Expand All @@ -29,7 +34,8 @@ static LineHeader of(final LineHeaderPK pk,
final MultilingualString origin1,
final MultilingualString origin2,
final DateRange validTime,
final TimeRange systemTime) {
final TimeRange systemTime,
final Optional<UUID> jore4IfOfLine) {
return ImmutableLineHeader.builder()
.pk(pk)
.line(line)
Expand All @@ -40,11 +46,11 @@ static LineHeader of(final LineHeaderPK pk,
.origin2(origin2)
.validTime(validTime)
.systemTime(systemTime)
.jore4IdOfLine(jore4IfOfLine)
.build();
}

static LineHeader from(final NetworkLineHeadersRecord record,
final IJsonbConverter converter) {
static LineHeader from(final NetworkLineHeadersRecord record, final IJsonbConverter converter) {
return of(LineHeaderPK.of(record.getNetworkLineHeaderId()),
LinePK.of(record.getNetworkLineId()),
ExternalId.of(record.getNetworkLineHeaderExtId()),
Expand All @@ -53,12 +59,11 @@ static LineHeader from(final NetworkLineHeadersRecord record,
converter.fromJson(record.getNetworkLineHeaderOrigin_1(), MultilingualString.class),
converter.fromJson(record.getNetworkLineHeaderOrigin_2(), MultilingualString.class),
record.getNetworkLineHeaderValidDateRange(),
record.getNetworkLineHeaderSysPeriod());

record.getNetworkLineHeaderSysPeriod(),
Optional.ofNullable(record.getJore4LineId()));
}

static LineHeader from(final NetworkLineHeadersWithHistoryRecord record,
final IJsonbConverter converter) {
static LineHeader from(final NetworkLineHeadersWithHistoryRecord record, final IJsonbConverter converter) {
return of(LineHeaderPK.of(record.getNetworkLineHeaderId()),
LinePK.of(record.getNetworkLineId()),
ExternalId.of(record.getNetworkLineHeaderExtId()),
Expand All @@ -67,6 +72,7 @@ static LineHeader from(final NetworkLineHeadersWithHistoryRecord record,
converter.fromJson(record.getNetworkLineHeaderOrigin_1(), MultilingualString.class),
converter.fromJson(record.getNetworkLineHeaderOrigin_2(), MultilingualString.class),
record.getNetworkLineHeaderValidDateRange(),
record.getNetworkLineHeaderSysPeriod());
record.getNetworkLineHeaderSysPeriod(),
Optional.ofNullable(record.getJore4LineId()));
}
}
Loading

0 comments on commit a1f5e0b

Please sign in to comment.