All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.netex.loader.mapping.CalendarMapper Maven / Gradle / Ivy

package org.opentripplanner.netex.loader.mapping;

import org.opentripplanner.graph_builder.DataImportIssue;
import org.opentripplanner.graph_builder.DataImportIssueStore;
import org.opentripplanner.graph_builder.issues.ServiceCodeDoesNotContainServiceDates;
import org.opentripplanner.model.calendar.ServiceCalendarDate;
import org.opentripplanner.model.calendar.ServiceDate;
import org.opentripplanner.netex.loader.util.ReadOnlyHierarchicalMap;
import org.opentripplanner.netex.loader.util.ReadOnlyHierarchicalMapById;
import org.opentripplanner.netex.support.DayTypeRefsToServiceIdAdapter;
import org.rutebanken.netex.model.DayType;
import org.rutebanken.netex.model.DayTypeAssignment;
import org.rutebanken.netex.model.OperatingPeriod;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

import static org.opentripplanner.model.calendar.ServiceCalendarDate.EXCEPTION_TYPE_ADD;
import static org.opentripplanner.model.calendar.ServiceCalendarDate.EXCEPTION_TYPE_REMOVE;

// TODO OTP2 - Add Unit tests
//           - JavaDoc needed
class CalendarMapper {
    private final DataImportIssueStore issueStore;

    private final FeedScopedIdFactory idFactory;
    private final ReadOnlyHierarchicalMap> dayTypeAssignmentByDayTypeId;
    private final ReadOnlyHierarchicalMapById operatingPeriodById;
    private final ReadOnlyHierarchicalMapById dayTypeById;


    CalendarMapper(
            FeedScopedIdFactory idFactory,
            ReadOnlyHierarchicalMap> dayTypeAssignmentByDayTypeId,
            ReadOnlyHierarchicalMapById operatingPeriodById,
            ReadOnlyHierarchicalMapById dayTypeById,
            DataImportIssueStore issueStore
    ) {
        this.idFactory = idFactory;
        this.dayTypeAssignmentByDayTypeId = dayTypeAssignmentByDayTypeId;
        this.operatingPeriodById = operatingPeriodById;
        this.dayTypeById = dayTypeById;
        this.issueStore = issueStore;
    }

    protected void addDataImportIssue(DataImportIssue issue) {
        issueStore.add(issue);
    }

    Collection mapToCalendarDates(DayTypeRefsToServiceIdAdapter dayTypeRefs) {
        String serviceId = dayTypeRefs.getServiceId();

        // The mapper store intermediate results and need to be initialized every time
        DayTypeAssignmentMapper dayTypeAssignmentMapper = new DayTypeAssignmentMapper(dayTypeById, operatingPeriodById);

        for (String dayTypeId : dayTypeRefs.getDayTypeRefs()) {
            dayTypeAssignmentMapper.mapAll(dayTypeId, dayTypeAssignments(dayTypeId));
        }

        Set dates = dayTypeAssignmentMapper.mergeDates();

        if (dates.isEmpty()) {
            addDataImportIssue(new ServiceCodeDoesNotContainServiceDates(serviceId));
            // Add one date exception when list is empty to ensure serviceId is not lost
            LocalDateTime today = LocalDate.now().atStartOfDay();
            return Collections.singleton(
                    newServiceCalendarDate(today, serviceId, EXCEPTION_TYPE_REMOVE)
            );
        }
        return dates.stream()
                .map(it -> newServiceCalendarDate(it, serviceId, EXCEPTION_TYPE_ADD))
                .collect(Collectors.toList());
    }

    private Collection dayTypeAssignments(String dayTypeId) {
        return dayTypeAssignmentByDayTypeId.lookup(dayTypeId);
    }

    private ServiceCalendarDate newServiceCalendarDate(
            LocalDateTime date, String serviceId, Integer exceptionType
    ) {
        return new ServiceCalendarDate(
                idFactory.createId(serviceId),
                new ServiceDate(date.getYear(), date.getMonthValue(), date.getDayOfMonth()),
                exceptionType
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy