001package jmri.jmrit.operations.automation.actions;
002
003import java.io.File;
004
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007
008import jmri.InstanceManager;
009import jmri.jmrit.operations.locations.Location;
010import jmri.jmrit.operations.locations.LocationManager;
011import jmri.jmrit.operations.setup.Setup;
012import jmri.jmrit.operations.trains.Train;
013import jmri.jmrit.operations.trains.TrainCsvSwitchLists;
014import jmri.jmrit.operations.trains.TrainManager;
015import jmri.jmrit.operations.trains.TrainSwitchLists;
016
017public class GenerateSwitchListChangesAction extends Action {
018
019    private static final int _code = ActionCodes.GENERATE_SWITCHLIST_CHANGES;
020    protected static final boolean IS_CHANGED = true;
021
022    @Override
023    public int getCode() {
024        return _code;
025    }
026
027    @Override
028    public String getName() {
029        return Bundle.getMessage("GenerateSwitchListChanges");
030    }
031
032    @Override
033    public void doAction() {
034        doAction(IS_CHANGED);
035    }
036
037    /**
038     * Generates the CSV file switch list for each location that is selected and
039     * there's new work for that location.
040     * <p>
041     * common code see GenerateSwitchListAction.java
042     *
043     * @param isChanged if set true only locations with changes will get a custom
044     *                  switch list.
045     */
046    protected void doAction(boolean isChanged) {
047        if (getAutomationItem() != null) {
048            if (!Setup.isGenerateCsvSwitchListEnabled()) {
049                log.warn("Generate CSV Switch List isn't enabled!");
050                finishAction(false);
051                return;
052            }
053            setRunning(true);
054            TrainSwitchLists trainSwitchLists = new TrainSwitchLists();
055            TrainCsvSwitchLists trainCsvSwitchLists = new TrainCsvSwitchLists();
056            for (Location location : InstanceManager.getDefault(LocationManager.class).getUniqueLocationsByNameList()) {
057                if (location.isSwitchListEnabled() &&
058                        (!isChanged || location.getStatus().equals(Location.MODIFIED))) {
059                    File csvFile = trainCsvSwitchLists.buildSwitchList(location);
060                    // also build the regular switch lists so they can be used
061                    trainSwitchLists.buildSwitchList(location);
062                    if (csvFile == null || !csvFile.exists()) {
063                        log.error("CSV switch list file was not created for location {}", location.getName());
064                        finishAction(false);
065                        return;
066                    }
067                }
068            }
069            // set trains switch lists printed
070            InstanceManager.getDefault(TrainManager.class).setTrainsSwitchListStatus(Train.PRINTED);
071            finishAction(true);
072        }
073    }
074
075    @Override
076    public void cancelAction() {
077        // no cancel for this action
078    }
079
080    private final static Logger log = LoggerFactory.getLogger(GenerateSwitchListChangesAction.class);
081
082}