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