001package jmri.jmrit.operations.locations.schedules.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.locations.schedules.*; 013import jmri.jmrit.operations.setup.OperationsSetupXml; 014import jmri.util.swing.JmriJOptionPane; 015 016/** 017 * Exports the Operation Schedules into a comma delimited file (CSV). 018 * 019 * @author Daniel Boudreau Copyright (C) 2018 020 * 021 */ 022public class ExportSchedules extends XmlFile { 023 024 public void writeOperationsScheduleFile() { 025 makeBackupFile(defaultOperationsFilename()); 026 try { 027 if (!checkFile(defaultOperationsFilename())) { 028 // The file does not exist, create it before writing 029 java.io.File file = new java.io.File(defaultOperationsFilename()); 030 java.io.File parentDir = file.getParentFile(); 031 if (!parentDir.exists()) { 032 if (!parentDir.mkdir()) { 033 log.error("Directory wasn't created"); 034 } 035 } 036 if (file.createNewFile()) { 037 log.debug("File created"); 038 } 039 } 040 writeFile(defaultOperationsFilename()); 041 } catch (IOException e) { 042 log.error("Exception while writing the new CSV operations file, may not be complete", e); 043 } 044 } 045 046 public void writeFile(String name) { 047 log.debug("writeFile {}", name); 048 File file = findFile(name); 049 if (file == null) { 050 file = new File(name); 051 } 052 053 try (CSVPrinter fileOut = new CSVPrinter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), 054 CSVFormat.DEFAULT)) { 055 056 // create header 057 fileOut.printRecord(Bundle.getMessage("ScheduleName"), 058 Bundle.getMessage("Id"), 059 Bundle.getMessage("Type"), 060 Bundle.getMessage("Random"), 061 Bundle.getMessage("Delivery"), 062 Bundle.getMessage("Road"), 063 Bundle.getMessage("Receive"), 064 Bundle.getMessage("Ship"), 065 Bundle.getMessage("Destination"), 066 Bundle.getMessage("Track"), 067 Bundle.getMessage("Pickup"), 068 Bundle.getMessage("Count"), 069 Bundle.getMessage("Wait"), 070 Bundle.getMessage("Hits"), 071 Bundle.getMessage("Comment")); 072 073 List<Schedule> schedules = InstanceManager.getDefault(ScheduleManager.class).getSchedulesByNameList(); 074 for (Schedule schedule : schedules) { 075 for (ScheduleItem scheduleItem : schedule.getItemsBySequenceList()) { 076 fileOut.printRecord(schedule.getName(), 077 scheduleItem.getId(), 078 scheduleItem.getTypeName(), 079 scheduleItem.getRandom(), 080 scheduleItem.getSetoutTrainScheduleName(), 081 scheduleItem.getRoadName(), 082 scheduleItem.getReceiveLoadName(), 083 scheduleItem.getShipLoadName(), 084 scheduleItem.getDestinationName(), 085 scheduleItem.getDestinationTrackName(), 086 scheduleItem.getPickupTrainScheduleName(), 087 scheduleItem.getCount(), 088 scheduleItem.getWait(), 089 scheduleItem.getHits(), 090 schedule.getComment()); 091 } 092 093 } 094 fileOut.flush(); 095 fileOut.close(); 096 log.info("Exported {} schedules to file {}", schedules.size(), defaultOperationsFilename()); 097 JmriJOptionPane.showMessageDialog(null, 098 Bundle.getMessage("ExportedSchedulesToFile", schedules.size(), defaultOperationsFilename()), 099 Bundle.getMessage("ExportComplete"), JmriJOptionPane.INFORMATION_MESSAGE); 100 } catch (IOException e) { 101 log.error("Can not open export schedules CSV file: {}", file.getName()); 102 JmriJOptionPane.showMessageDialog(null, 103 Bundle.getMessage("ExportedSchedulesToFile", 0, defaultOperationsFilename()), 104 Bundle.getMessage("ExportFailed"), JmriJOptionPane.ERROR_MESSAGE); 105 } 106 } 107 108 // Operation files always use the same directory 109 public static String defaultOperationsFilename() { 110 return OperationsSetupXml.getFileLocation() + 111 OperationsSetupXml.getOperationsDirectoryName() + 112 File.separator + 113 getOperationsFileName(); 114 } 115 116 public static void setOperationsFileName(String name) { 117 operationsFileName = name; 118 } 119 120 public static String getOperationsFileName() { 121 return operationsFileName; 122 } 123 124 private static String operationsFileName = "ExportOperationsSchedules.csv"; // NOI18N 125 126 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExportSchedules.class); 127 128}