001package jmri.jmrit.operations.rollingstock.engines.tools; 002 003import java.io.*; 004import java.nio.charset.StandardCharsets; 005import java.util.List; 006 007import org.apache.commons.csv.CSVFormat; 008import org.apache.commons.csv.CSVPrinter; 009 010import jmri.InstanceManager; 011import jmri.jmrit.XmlFile; 012import jmri.jmrit.operations.rollingstock.engines.Engine; 013import jmri.jmrit.operations.rollingstock.engines.EngineManager; 014import jmri.jmrit.operations.setup.OperationsSetupXml; 015import jmri.jmrit.operations.setup.Setup; 016import jmri.util.swing.JmriJOptionPane; 017 018/** 019 * Exports the Engine roster into a comma delimited file (CSV). Order stored: 020 * Number, Road, Model, Length, Owner, Built, Location, -, Track, Consist, 021 * Moves, Last, Value, HP, Weight, Type, Comment, Misc. 022 * 023 * @author Daniel Boudreau Copyright (C) 2010 024 * 025 */ 026public class ExportEngines extends XmlFile { 027 028 protected static final String LOCATION_TRACK_SEPARATOR = "-"; 029 030 public ExportEngines() { 031 // nothing to do 032 } 033 034 /** 035 * Store the all of the operation Engine objects in the default place, 036 * including making a backup if needed 037 */ 038 public void writeOperationsEngineFile() { 039 makeBackupFile(defaultOperationsFilename()); 040 try { 041 if (!checkFile(defaultOperationsFilename())) { 042 // The file does not exist, create it before writing 043 java.io.File file = new java.io.File(defaultOperationsFilename()); 044 java.io.File parentDir = file.getParentFile(); 045 if (!parentDir.exists()) { 046 if (!parentDir.mkdir()) { 047 log.error("Directory wasn't created"); 048 } 049 } 050 if (file.createNewFile()) { 051 log.debug("File created"); 052 } 053 } 054 writeFile(defaultOperationsFilename()); 055 } catch (IOException e) { 056 log.error("Exception while writing the new CSV operations file, may not be complete: {}", 057 e.getLocalizedMessage()); 058 } 059 } 060 061 public void writeFile(String name) { 062 log.debug("writeFile {}", name); 063 // This is taken in large part from "Java and XML" page 368 064 File file = findFile(name); 065 if (file == null) { 066 file = new File(name); 067 } 068 069 try (CSVPrinter fileOut = new CSVPrinter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), 070 CSVFormat.DEFAULT)) { 071 072 EngineManager manager = InstanceManager.getDefault(EngineManager.class); 073 List<Engine> engineList = manager.getByNumberList(); 074 075 // create header 076 fileOut.printRecord(Bundle.getMessage("Number"), 077 Bundle.getMessage("Road"), 078 Bundle.getMessage("Model"), 079 Bundle.getMessage("Length"), 080 Bundle.getMessage("Owner"), 081 Bundle.getMessage("Built"), 082 Bundle.getMessage("Location"), 083 LOCATION_TRACK_SEPARATOR, 084 Bundle.getMessage("Track"), 085 Bundle.getMessage("Consist"), 086 Bundle.getMessage("Moves"), 087 Bundle.getMessage("Last"), 088 Setup.getValueLabel(), 089 Bundle.getMessage("HP"), 090 Bundle.getMessage("WeightTons"), 091 Bundle.getMessage("Type"), 092 Bundle.getMessage("Comment"), 093 Bundle.getMessage("Miscellaneous")); 094 095 // store engine number, road, model, length, owner, built date, location - track 096 for (Engine engine : engineList) { 097 fileOut.printRecord(engine.getNumber(), 098 engine.getRoadName(), 099 engine.getModel(), 100 engine.getLength(), 101 engine.getOwnerName(), 102 engine.getBuilt(), 103 engine.getLocationName(), 104 LOCATION_TRACK_SEPARATOR, 105 engine.getTrackName(), 106 engine.getConsistName(), 107 engine.getMoves(), 108 engine.getSortDate(), 109 engine.getValue(), 110 engine.getHp(), 111 engine.getWeightTons(), 112 engine.getTypeName(), 113 engine.getComment(), 114 engine.isOutOfService() ? Bundle.getMessage("OutOfService") : ""); 115 } 116 fileOut.flush(); 117 fileOut.close(); 118 log.info("Exported {} engines to file {}", engineList.size(), defaultOperationsFilename()); 119 JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("ExportedEnginesToFile", 120 engineList.size(), defaultOperationsFilename()), Bundle.getMessage("ExportComplete"), 121 JmriJOptionPane.INFORMATION_MESSAGE); 122 } catch (IOException e) { 123 log.error("Can not open export engines CSV file: {}", e.getLocalizedMessage()); 124 JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("ExportedEnginesToFile", 125 0, defaultOperationsFilename()), Bundle.getMessage("ExportFailed"), 126 JmriJOptionPane.ERROR_MESSAGE); 127 } 128 } 129 130 // Operation files always use the same directory 131 public static String defaultOperationsFilename() { 132 return OperationsSetupXml.getFileLocation() 133 + OperationsSetupXml.getOperationsDirectoryName() 134 + File.separator 135 + getOperationsFileName(); 136 } 137 138 public static void setOperationsFileName(String name) { 139 operationsFileName = name; 140 } 141 142 public static String getOperationsFileName() { 143 return operationsFileName; 144 } 145 146 private static String operationsFileName = "ExportOperationsEngineRoster.csv"; // NOI18N 147 148 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExportEngines.class); 149 150}