001package jmri.jmrit.operations.rollingstock.cars.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.jmrit.XmlFile; 011import jmri.jmrit.operations.rollingstock.cars.Car; 012import jmri.jmrit.operations.setup.OperationsSetupXml; 013import jmri.jmrit.operations.setup.Setup; 014import jmri.util.swing.JmriJOptionPane; 015 016/** 017 * Exports the car roster into a comma delimited file (CSV). 018 * 019 * @author Daniel Boudreau Copyright (C) 2010, 2011, 2016, 2024, 2025 020 */ 021public class ExportCars extends XmlFile { 022 023 protected static final String LOCATION_TRACK_SEPARATOR = "-"; 024 List<Car> _carList; 025 026 public ExportCars(List<Car> carList) { 027 _carList = carList; 028 } 029 030 /** 031 * Create CSV file based on the car list. 032 */ 033 public void writeOperationsCarFile() { 034 makeBackupFile(defaultOperationsFilename()); 035 try { 036 if (!checkFile(defaultOperationsFilename())) { 037 // The file does not exist, create it before writing 038 java.io.File file = new java.io.File(defaultOperationsFilename()); 039 java.io.File parentDir = file.getParentFile(); 040 if (!parentDir.exists()) { 041 if (!parentDir.mkdir()) { 042 log.error("Directory wasn't created"); 043 } 044 } 045 if (file.createNewFile()) { 046 log.debug("File created"); 047 } 048 } 049 writeFile(defaultOperationsFilename()); 050 } catch (IOException e) { 051 log.error("Exception while writing the new CSV operations file, may not be complete: {}", 052 e.getLocalizedMessage()); 053 } 054 } 055 056 /** 057 * Any changes to the column order should also be made to the ImportCars.java 058 * file. 059 * 060 * @param name file name 061 */ 062 private void writeFile(String name) { 063 log.debug("writeFile {}", name); 064 // This is taken in large part from "Java and XML" page 368 065 File file = findFile(name); 066 if (file == null) { 067 file = new File(name); 068 } 069 070 try (CSVPrinter fileOut = new CSVPrinter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), 071 CSVFormat.DEFAULT)) { 072 073 // create header 074 fileOut.printRecord(Bundle.getMessage("Number"), 075 Bundle.getMessage("Road"), 076 Bundle.getMessage("Type"), 077 Bundle.getMessage("Length"), 078 Bundle.getMessage("Weight"), 079 Bundle.getMessage("Color"), 080 Bundle.getMessage("Owner"), 081 Bundle.getMessage("Built"), 082 Bundle.getMessage("Location"), 083 LOCATION_TRACK_SEPARATOR, 084 Bundle.getMessage("Track"), 085 Bundle.getMessage("Load"), 086 Bundle.getMessage("Kernel"), 087 Bundle.getMessage("Moves"), 088 Setup.getValueLabel(), 089 Bundle.getMessage("Comment"), 090 Bundle.getMessage("Miscellaneous"), 091 Bundle.getMessage("Extensions"), 092 Bundle.getMessage("Wait"), 093 Bundle.getMessage("Pickup"), 094 Bundle.getMessage("Last"), 095 Bundle.getMessage("RWELocation"), 096 LOCATION_TRACK_SEPARATOR, 097 Bundle.getMessage("Track"), 098 Bundle.getMessage("RWELoad"), 099 Bundle.getMessage("RWLLocation"), 100 LOCATION_TRACK_SEPARATOR, 101 Bundle.getMessage("Track"), 102 Bundle.getMessage("RWLLoad"), 103 Bundle.getMessage("Division"), 104 Bundle.getMessage("Train"), 105 Bundle.getMessage("Destination"), 106 LOCATION_TRACK_SEPARATOR, 107 Bundle.getMessage("Track"), 108 Bundle.getMessage("FinalDestination"), 109 LOCATION_TRACK_SEPARATOR, 110 Bundle.getMessage("Track"), 111 Bundle.getMessage("SchId"), 112 Bundle.getMessage("RFID_Tag"), 113 Bundle.getMessage("RoutePath"), 114 Bundle.getMessage("LastLocation"), 115 LOCATION_TRACK_SEPARATOR, 116 Bundle.getMessage("Track"), 117 Bundle.getMessage("LastTrain")); 118 119 // store car attributes 120 for (Car car : _carList) { 121 fileOut.printRecord(car.getNumber(), 122 car.getRoadName(), 123 car.getTypeName(), 124 car.getLength(), 125 car.getWeight(), 126 car.getColor(), 127 car.getOwnerName(), 128 car.getBuilt(), 129 car.getLocationName(), 130 LOCATION_TRACK_SEPARATOR, 131 car.getTrackName(), 132 car.getLoadName(), 133 car.getKernelName(), 134 car.getMoves(), 135 car.getValue(), 136 car.getComment(), 137 car.isOutOfService() ? Bundle.getMessage("OutOfService") : "", 138 car.getTypeExtensions(), 139 car.getWait(), 140 car.getPickupScheduleName(), 141 car.getSortDate(), 142 car.getReturnWhenEmptyDestinationName(), 143 LOCATION_TRACK_SEPARATOR, 144 car.getReturnWhenEmptyDestTrackName(), 145 car.getReturnWhenEmptyLoadName(), 146 car.getReturnWhenLoadedDestinationName(), 147 LOCATION_TRACK_SEPARATOR, 148 car.getReturnWhenLoadedDestTrackName(), 149 car.getReturnWhenLoadedLoadName(), 150 car.getDivisionName(), 151 car.getTrainName(), 152 car.getDestinationName(), 153 LOCATION_TRACK_SEPARATOR, 154 car.getDestinationTrackName(), 155 car.getFinalDestinationName(), 156 LOCATION_TRACK_SEPARATOR, 157 car.getFinalDestinationTrackName(), 158 car.getScheduleItemId(), 159 car.getRfid(), 160 car.getRoutePath(), 161 car.getLastLocationName(), 162 LOCATION_TRACK_SEPARATOR, 163 car.getLastTrackName(), 164 car.getLastTrainName()); 165 } 166 fileOut.flush(); 167 fileOut.close(); 168 log.info("Exported {} cars to file {}", _carList.size(), defaultOperationsFilename()); 169 JmriJOptionPane.showMessageDialog(null, Bundle.getMessage("ExportedCarsToFile", 170 _carList.size(), defaultOperationsFilename()), Bundle.getMessage("ExportComplete"), 171 JmriJOptionPane.INFORMATION_MESSAGE); 172 } catch (IOException e) { 173 log.error("Can not open export cars CSV file: {}", e.getLocalizedMessage()); 174 JmriJOptionPane.showMessageDialog(null, 175 Bundle.getMessage("ExportedCarsToFile", 176 0, defaultOperationsFilename()), 177 Bundle.getMessage("ExportFailed"), JmriJOptionPane.ERROR_MESSAGE); 178 } 179 } 180 181 // Operation files always use the same directory 182 public static String defaultOperationsFilename() { 183 return OperationsSetupXml.getFileLocation() 184 + OperationsSetupXml.getOperationsDirectoryName() 185 + File.separator 186 + getOperationsFileName(); 187 } 188 189 public static void setOperationsFileName(String name) { 190 operationsFileName = name; 191 } 192 193 public static String getOperationsFileName() { 194 return operationsFileName; 195 } 196 197 private static String operationsFileName = "ExportOperationsCarRoster.csv"; // NOI18N 198 199 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ExportCars.class); 200 201}