001package jmri.jmrit.timetable;
002
003/**
004 * Define the content of a Schedule record.
005 *
006 * @author Dave Sand Copyright (C) 2018
007 */
008public class Schedule {
009
010    /**
011     * Create a new schedule with default values.
012     * @param layoutId The parent layout id.
013     * @throws IllegalArgumentException SCHEDULE_ADD_FAIL
014     */
015    public Schedule(int layoutId) {
016        if (_dm.getLayout(layoutId) == null) {
017            throw new IllegalArgumentException(TimeTableDataManager.SCHEDULE_ADD_FAIL);
018        }
019        _scheduleId = _dm.getNextId("Schedule");  // NOI18N
020        _layoutId = layoutId;
021        _dm.addSchedule(_scheduleId, this);
022    }
023
024    public Schedule(int scheduleId, int layoutId, String scheduleName, String effDate, int startHour, int duration) {
025        _scheduleId = scheduleId;
026        _layoutId = layoutId;
027        setScheduleName(scheduleName);
028        setEffDate(effDate);
029        setStartHour(startHour);
030        setDuration(duration);
031    }
032
033    TimeTableDataManager _dm = TimeTableDataManager.getDataManager();
034
035    private int _scheduleId = 0;
036    private int _layoutId = 0;
037    private String _scheduleName = Bundle.getMessage("NewScheduleName");  // NOI18N
038    private String _effDate = Bundle.getMessage("NewScheduleDate");  // NOI18N
039    private int _startHour = 0;
040    private int _duration = 24;
041
042    /**
043     * Make a copy of the schedule.
044     * @param layoutId The new layoutId, if zero use the current layout id.
045     * @return a new schedule instance.
046     */
047    public Schedule getCopy(int layoutId) {
048        if (layoutId == 0) layoutId = getLayoutId();
049        Schedule copy = new Schedule(layoutId);
050        copy.setScheduleName(Bundle.getMessage("DuplicateCopyName", _scheduleName));
051        copy.setEffDate(_effDate);
052        copy.setStartHour(_startHour);
053        copy.setDuration(_duration);
054        return copy;
055    }
056
057    public int getScheduleId() {
058        return _scheduleId;
059    }
060
061    public int getLayoutId() {
062        return _layoutId;
063    }
064
065    public String getScheduleName() {
066        return _scheduleName;
067    }
068
069    public void setScheduleName(String newName) {
070        _scheduleName = newName;
071    }
072
073    public String getEffDate() {
074        return _effDate;
075    }
076
077    public void setEffDate(String newDate) {
078        _effDate = newDate;
079    }
080
081    public int getStartHour() {
082        return _startHour;
083    }
084
085    /**
086     * Set the start hour, 0 - 23.
087     * @param newStartHour The start hour in the range of 0 to 23.
088     * @throws IllegalArgumentException (START_HOUR_RANGE).
089     */
090    public void setStartHour(int newStartHour) {
091        if (newStartHour < 0 || newStartHour > 23) {
092            throw new IllegalArgumentException(TimeTableDataManager.START_HOUR_RANGE);
093        }
094        int oldStartHour = _startHour;
095        _startHour = newStartHour;
096
097        try {
098            _dm.calculateScheduleTrains(getScheduleId(), false);
099            _dm.calculateScheduleTrains(getScheduleId(), true);
100        } catch (IllegalArgumentException ex) {
101            _startHour = oldStartHour;  // Roll back start hour change
102            throw ex;
103        }
104    }
105
106    public int getDuration() {
107        return _duration;
108    }
109
110    /**
111     * Set the duration, 1 - 24 hours.
112     * @param newDuration The duration in the range of 1 to 24.
113     * @throws IllegalArgumentException (DURATION_RANGE).
114     */
115    public void setDuration(int newDuration) {
116        if (newDuration < 1 || newDuration > 24) {
117            throw new IllegalArgumentException(TimeTableDataManager.DURATION_RANGE);
118        }
119        int oldDuration = _duration;
120        _duration = newDuration;
121
122        try {
123            _dm.calculateScheduleTrains(getScheduleId(), false);
124            _dm.calculateScheduleTrains(getScheduleId(), true);
125        } catch (IllegalArgumentException ex) {
126            _duration = oldDuration;  // Roll back duration change
127            throw ex;
128        }
129    }
130
131    @Override
132    public String toString() {
133        return _scheduleName;
134    }
135
136//     private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(Schedule.class);
137}