001package jmri.jmrit.operations.routes.tools; 002 003import java.io.*; 004import java.nio.charset.StandardCharsets; 005 006import org.apache.commons.csv.CSVFormat; 007import org.apache.commons.csv.CSVPrinter; 008 009import jmri.InstanceManager; 010import jmri.jmrit.XmlFile; 011import jmri.jmrit.operations.routes.*; 012import jmri.jmrit.operations.setup.OperationsSetupXml; 013import jmri.jmrit.operations.setup.Setup; 014import jmri.util.swing.JmriJOptionPane; 015 016/** 017 * Export Routes to CSV file 018 */ 019public class ExportRoutes extends XmlFile { 020 021 public ExportRoutes() { 022 // nothing to do 023 } 024 025 public void writeOperationsRoutesFile() { 026 makeBackupFile(defaultOperationsFilename()); 027 try { 028 if (!checkFile(defaultOperationsFilename())) { 029 // The file does not exist, create it before writing 030 java.io.File file = new java.io.File(defaultOperationsFilename()); 031 java.io.File parentDir = file.getParentFile(); 032 if (!parentDir.exists()) { 033 if (!parentDir.mkdir()) { 034 log.error("Directory wasn't created"); 035 } 036 } 037 if (file.createNewFile()) { 038 log.debug("File created"); 039 } 040 } 041 writeFile(defaultOperationsFilename()); 042 } catch (IOException e) { 043 log.error("Exception while writing the new CSV operations file, may not be complete: {}", 044 e.getLocalizedMessage()); 045 } 046 } 047 048 public void writeFile(String name) { 049 log.debug("writeFile {}", name); 050 // This is taken in large part from "Java and XML" page 368 051 File file = findFile(name); 052 if (file == null) { 053 file = new File(name); 054 } 055 056 int count = 0; 057 try (CSVPrinter fileOut = new CSVPrinter( 058 new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), 059 CSVFormat.DEFAULT)) { 060 061 loadHeader(fileOut); 062 063 for (Route route : InstanceManager.getDefault(RouteManager.class).getRoutesByNameList()) { 064 count++; 065 fileOut.printRecord(route.getName(), 066 "", 067 route.getComment()); 068 for (RouteLocation rl : route.getLocationsBySequenceList()) { 069 if (rl.getLocation() != null) { 070 fileOut.printRecord("", 071 rl.getLocation().getName(), 072 rl.getTrainDirectionString(), 073 rl.getMaxCarMoves(), 074 rl.getRandomControl(), 075 rl.isPickUpAllowed() ? Bundle.getMessage("yes") : Bundle.getMessage("no"), 076 rl.isDropAllowed() ? Bundle.getMessage("yes") : Bundle.getMessage("no"), 077 rl.getWait() + Setup.getTravelTime(), 078 rl.getFormatedDepartureTime(), 079 rl.getMaxTrainLength(), 080 rl.getGrade(), 081 rl.getTrainIconX(), 082 rl.getTrainIconY(), 083 rl.getComment().replace("\n", "<LF>"), 084 rl.getCommentTextColor()); 085 } else { 086 fileOut.printRecord("", 087 Bundle.getMessage("ErrorTitle")); 088 } 089 } 090 } 091 092 JmriJOptionPane.showMessageDialog(null, 093 Bundle.getMessage("ExportedRoutesToFile", 094 count, defaultOperationsFilename()), 095 Bundle.getMessage("ExportComplete"), JmriJOptionPane.INFORMATION_MESSAGE); 096 097 fileOut.flush(); 098 fileOut.close(); 099 } catch (IOException e) { 100 log.error("Can not open export Routes CSV file: {}", e.getLocalizedMessage()); 101 JmriJOptionPane.showMessageDialog(null, 102 Bundle.getMessage("ExportedRoutesToFile", 103 0, defaultOperationsFilename()), 104 Bundle.getMessage("ExportFailed"), JmriJOptionPane.ERROR_MESSAGE); 105 } 106 } 107 108 private void loadHeader(CSVPrinter fileOut) throws IOException { 109 fileOut.printRecord(Bundle.getMessage("Route"), 110 Bundle.getMessage("Location"), 111 Bundle.getMessage("TrainDirection"), 112 Bundle.getMessage("Moves"), 113 Bundle.getMessage("Random"), 114 Bundle.getMessage("Pickups"), 115 Bundle.getMessage("Drops"), 116 Bundle.getMessage("Travel"), 117 Bundle.getMessage("DepartTime"), 118 Bundle.getMessage("MaxLength"), 119 Bundle.getMessage("Grade"), 120 Bundle.getMessage("X"), 121 Bundle.getMessage("Y"), 122 Bundle.getMessage("Comment"), 123 Bundle.getMessage("TextColor")); 124 } 125 126 public File getExportFile() { 127 return findFile(defaultOperationsFilename()); 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 = "ExportOperationsRoutes.csv"; // NOI18N 147 148 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExportRoutes.class); 149 150}