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: {}", 043 e.getLocalizedMessage()); 044 } 045 } 046 047 public void writeFile(String name) { 048 log.debug("writeFile {}", name); 049 File file = findFile(name); 050 if (file == null) { 051 file = new File(name); 052 } 053 054 try (CSVPrinter fileOut = new CSVPrinter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), 055 CSVFormat.DEFAULT)) { 056 057 // create header 058 fileOut.printRecord(Bundle.getMessage("ScheduleName"), 059 Bundle.getMessage("Id"), 060 Bundle.getMessage("Type"), 061 Bundle.getMessage("Random"), 062 Bundle.getMessage("Delivery"), 063 Bundle.getMessage("Road"), 064 Bundle.getMessage("Receive"), 065 Bundle.getMessage("Ship"), 066 Bundle.getMessage("Destination"), 067 Bundle.getMessage("Track"), 068 Bundle.getMessage("Pickup"), 069 Bundle.getMessage("Count"), 070 Bundle.getMessage("Wait"), 071 Bundle.getMessage("Hits"), 072 Bundle.getMessage("Comment")); 073 074 List<Schedule> schedules = InstanceManager.getDefault(ScheduleManager.class).getSchedulesByNameList(); 075 for (Schedule schedule : schedules) { 076 for (ScheduleItem scheduleItem : schedule.getItemsBySequenceList()) { 077 fileOut.printRecord(schedule.getName(), 078 scheduleItem.getId(), 079 scheduleItem.getTypeName(), 080 scheduleItem.getRandom(), 081 scheduleItem.getSetoutTrainScheduleName(), 082 scheduleItem.getRoadName(), 083 scheduleItem.getReceiveLoadName(), 084 scheduleItem.getShipLoadName(), 085 scheduleItem.getDestinationName(), 086 scheduleItem.getDestinationTrackName(), 087 scheduleItem.getPickupTrainScheduleName(), 088 scheduleItem.getCount(), 089 scheduleItem.getWait(), 090 scheduleItem.getHits(), 091 schedule.getComment()); 092 } 093 094 } 095 fileOut.flush(); 096 fileOut.close(); 097 log.info("Exported {} schedules to file {}", schedules.size(), defaultOperationsFilename()); 098 JmriJOptionPane.showMessageDialog(null, 099 Bundle.getMessage("ExportedSchedulesToFile", schedules.size(), defaultOperationsFilename()), 100 Bundle.getMessage("ExportComplete"), JmriJOptionPane.INFORMATION_MESSAGE); 101 } catch (IOException e) { 102 log.error("Can not open export schedules CSV file: {}", e.getLocalizedMessage()); 103 JmriJOptionPane.showMessageDialog(null, 104 Bundle.getMessage("ExportedSchedulesToFile", 0, defaultOperationsFilename()), 105 Bundle.getMessage("ExportFailed"), JmriJOptionPane.ERROR_MESSAGE); 106 } 107 } 108 109 // Operation files always use the same directory 110 public static String defaultOperationsFilename() { 111 return OperationsSetupXml.getFileLocation() + 112 OperationsSetupXml.getOperationsDirectoryName() + 113 File.separator + 114 getOperationsFileName(); 115 } 116 117 public static void setOperationsFileName(String name) { 118 operationsFileName = name; 119 } 120 121 public static String getOperationsFileName() { 122 return operationsFileName; 123 } 124 125 private static String operationsFileName = "ExportOperationsSchedules.csv"; // NOI18N 126 127 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExportSchedules.class); 128 129}