Search in sources :

Example 6 with TariffZone

use of org.rutebanken.tiamat.model.TariffZone in project tiamat by entur.

the class TariffZoneTerminator method terminateTariffZone.

public TariffZone terminateTariffZone(String tariffZoneId, Instant suggestedTimeOfTermination, String versionComment) {
    return mutateLock.executeInLock(() -> {
        String usernameForAuthenticatedUser = usernameFetcher.getUserNameForAuthenticatedUser();
        logger.warn("About to terminate tariff zone by ID {}. User: {}", tariffZoneId, usernameForAuthenticatedUser);
        DataManagedObjectStructure resolved = referenceResolver.resolve(new VersionOfObjectRefStructure(tariffZoneId));
        authorizationService.assertAuthorized(ROLE_EDIT_STOPS, Collections.singletonList(resolved));
        Instant now = Instant.now();
        Instant timeOfTermination;
        if (suggestedTimeOfTermination.isBefore(now)) {
            logger.warn("Termination date {} cannot be before now {}. Setting now as time of termination for {}", suggestedTimeOfTermination, now, tariffZoneId);
            timeOfTermination = now;
        } else {
            timeOfTermination = suggestedTimeOfTermination;
        }
        logger.info("User {} is terminating tariff zone {} at {} with comment '{}'", usernameFetcher.getUserNameForAuthenticatedUser(), tariffZoneId, timeOfTermination, versionComment);
        final TariffZone tariffZone = tariffZoneRepository.findFirstByNetexIdOrderByVersionDesc(tariffZoneId);
        if (tariffZone != null) {
            // or to_date after future date, this is to avoid duplicated tariff zones.
            if (tariffZone.getValidBetween() != null && tariffZone.getValidBetween().getToDate() != null && tariffZone.getValidBetween().getToDate().isBefore(timeOfTermination)) {
                throw new IllegalArgumentException("The tariff zone " + tariffZoneId + ", version " + tariffZone.getVersion() + " is already terminated at " + tariffZone.getValidBetween().getToDate());
            }
            logger.debug("End previous version {} of tariff zone {} at {} (timeOfTermination)", tariffZone.getVersion(), tariffZone.getNetexId(), timeOfTermination);
            tariffZone.getValidBetween().setToDate(timeOfTermination);
            final TariffZone result = tariffZoneRepository.save(tariffZone);
            // Start updating stop_place tariff zone ref
            backgroundJobs.triggerStopPlaceUpdate();
            return result;
        } else {
            throw new IllegalArgumentException("Cannot find tariff zone to terminate: " + tariffZoneId + ". No changes executed.");
        }
    });
}
Also used : TariffZone(org.rutebanken.tiamat.model.TariffZone) DataManagedObjectStructure(org.rutebanken.tiamat.model.DataManagedObjectStructure) VersionOfObjectRefStructure(org.rutebanken.tiamat.model.VersionOfObjectRefStructure) Instant(java.time.Instant)

Example 7 with TariffZone

use of org.rutebanken.tiamat.model.TariffZone in project tiamat by entur.

the class TariffZonesFromStopsExporterTest method avoidDuplicateTariffZones.

@Test
public void avoidDuplicateTariffZones() {
    TariffZone tariffZone = new TariffZone();
    tariffZone.setNetexId("VKT:TariffZone:201");
    tariffZone.setVersion(1L);
    tariffZoneRepository.save(tariffZone);
    // Two stops with reference to the same tariffzone
    StopPlace netexStopPlace = new StopPlace();
    netexStopPlace.setId("NSR:StopPlace:1");
    netexStopPlace.withTariffZones(new TariffZoneRefs_RelStructure().withTariffZoneRef(new TariffZoneRef().withRef(tariffZone.getNetexId()).withVersion("1")));
    StopPlace netexStopPlace2 = new StopPlace();
    netexStopPlace2.setId("NSR:StopPlace:2");
    netexStopPlace2.withTariffZones(new TariffZoneRefs_RelStructure().withTariffZoneRef(new TariffZoneRef().withRef(tariffZone.getNetexId()).withVersion("1")));
    SiteFrame siteFrame = new SiteFrame();
    tariffZonesFromStopsExporter.resolveTariffZones(Arrays.asList(netexStopPlace, netexStopPlace2), siteFrame);
    assertThat(siteFrame.getTariffZones().getTariffZone()).as("Number of tariffzones returned").hasSize(1);
}
Also used : TariffZoneRefs_RelStructure(org.rutebanken.netex.model.TariffZoneRefs_RelStructure) TariffZone(org.rutebanken.tiamat.model.TariffZone) StopPlace(org.rutebanken.netex.model.StopPlace) SiteFrame(org.rutebanken.netex.model.SiteFrame) TariffZoneRef(org.rutebanken.netex.model.TariffZoneRef) Test(org.junit.Test) TiamatIntegrationTest(org.rutebanken.tiamat.TiamatIntegrationTest)

Example 8 with TariffZone

use of org.rutebanken.tiamat.model.TariffZone in project tiamat by entur.

the class TiamatSiteFrameExporterTest method exportTariffZonesInSiteFrame.

@Test
public void exportTariffZonesInSiteFrame() {
    org.rutebanken.tiamat.model.SiteFrame siteFrame = new org.rutebanken.tiamat.model.SiteFrame();
    TariffZone tariffZone = new TariffZone();
    tariffZone.setName(new EmbeddableMultilingualString("name"));
    tariffZoneRepository.save(tariffZone);
    tiamatSiteFrameExporter.addAllTariffZones(siteFrame);
    assertThat(siteFrame.getTariffZones()).isNotNull();
    assertThat(siteFrame.getTariffZones().getTariffZone()).hasSize(1);
}
Also used : SiteFrame(org.rutebanken.tiamat.model.SiteFrame) TariffZone(org.rutebanken.tiamat.model.TariffZone) SiteFrame(org.rutebanken.tiamat.model.SiteFrame) EmbeddableMultilingualString(org.rutebanken.tiamat.model.EmbeddableMultilingualString) Test(org.junit.Test) TiamatIntegrationTest(org.rutebanken.tiamat.TiamatIntegrationTest)

Example 9 with TariffZone

use of org.rutebanken.tiamat.model.TariffZone in project tiamat by entur.

the class GenericObjectDifferTest method diffStopPlaceWithTariffZone.

@Test
public void diffStopPlaceWithTariffZone() throws IllegalAccessException {
    StopPlace oldStopPlace = new StopPlace();
    StopPlace newStopPlace = new StopPlace();
    newStopPlace.getTariffZones().add(new TariffZoneRef(new TariffZone()));
    String diffString = compareObjectsAndPrint(oldStopPlace, newStopPlace);
    assertThat(diffString).contains("tariffZones");
}
Also used : StopPlace(org.rutebanken.tiamat.model.StopPlace) TariffZone(org.rutebanken.tiamat.model.TariffZone) TariffZoneRef(org.rutebanken.tiamat.model.TariffZoneRef) EmbeddableMultilingualString(org.rutebanken.tiamat.model.EmbeddableMultilingualString) Test(org.junit.Test)

Example 10 with TariffZone

use of org.rutebanken.tiamat.model.TariffZone in project tiamat by entur.

the class TariffZoneSaverService method saveNewVersion.

public TariffZone saveNewVersion(TariffZone existingVersion, TariffZone newVersion) {
    versionValidator.validate(existingVersion, newVersion);
    TariffZone saved = defaultVersionedSaverService.saveNewVersion(existingVersion, newVersion, tariffZoneRepository);
    tariffZonesLookupService.resetTariffZone();
    return saved;
}
Also used : TariffZone(org.rutebanken.tiamat.model.TariffZone)

Aggregations

TariffZone (org.rutebanken.tiamat.model.TariffZone)25 Test (org.junit.Test)18 TiamatIntegrationTest (org.rutebanken.tiamat.TiamatIntegrationTest)16 EmbeddableMultilingualString (org.rutebanken.tiamat.model.EmbeddableMultilingualString)14 StopPlace (org.rutebanken.tiamat.model.StopPlace)8 Instant (java.time.Instant)5 Coordinate (org.locationtech.jts.geom.Coordinate)5 Geometry (org.locationtech.jts.geom.Geometry)5 LinearRing (org.locationtech.jts.geom.LinearRing)5 CoordinateArraySequence (org.locationtech.jts.geom.impl.CoordinateArraySequence)5 TariffZoneSearch (org.rutebanken.tiamat.exporter.params.TariffZoneSearch)5 TariffZoneRef (org.rutebanken.tiamat.model.TariffZoneRef)5 ValidBetween (org.rutebanken.tiamat.model.ValidBetween)4 SiteFrame (org.rutebanken.netex.model.SiteFrame)3 StopPlace (org.rutebanken.netex.model.StopPlace)3 TariffZoneRef (org.rutebanken.netex.model.TariffZoneRef)3 TariffZoneRefs_RelStructure (org.rutebanken.netex.model.TariffZoneRefs_RelStructure)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2