001package jmri.jmrit.operations.trains; 002 003import java.io.IOException; 004import java.util.List; 005 006import org.apache.commons.csv.CSVPrinter; 007 008import jmri.InstanceManager; 009import jmri.jmrit.operations.locations.Location; 010import jmri.jmrit.operations.locations.Track; 011import jmri.jmrit.operations.rollingstock.cars.Car; 012import jmri.jmrit.operations.rollingstock.cars.CarManager; 013import jmri.jmrit.operations.rollingstock.engines.Engine; 014import jmri.jmrit.operations.routes.RouteLocation; 015import jmri.jmrit.operations.setup.Setup; 016import jmri.util.FileUtil; 017 018/** 019 * Contains the csv operators for manifests and switch lists 020 * 021 * @author Daniel Boudreau Copyright (C) 2011, 2013, 2015, 2022 022 */ 023public class TrainCsvCommon extends TrainCommon { 024 025 protected final void printDepartureTime(CSVPrinter printer, String time) throws IOException { 026 printer.printRecord("DT", Bundle.getMessage("csvDepartureTime"), time); // NOI18N 027 } 028 029 protected final void printHeader(CSVPrinter printer) throws IOException { 030 printer.printRecord(Bundle.getMessage("csvOperator"), Bundle.getMessage("csvDescription"), 031 Bundle.getMessage("csvParameters")); // NOI18N 032 } 033 034 protected final void printLocationSwitchListComment(CSVPrinter printer, String comment, String textColorName) 035 throws IOException { 036 printer.printRecord("SWLC", Bundle.getMessage("csvSwitchListComment"), comment, textColorName); // NOI18N 037 } 038 039 protected final void printLocationComment(CSVPrinter printer, String comment, String textColorName) 040 throws IOException { 041 printer.printRecord("LC", Bundle.getMessage("csvLocationComment"), comment, textColorName); // NOI18N 042 } 043 044 protected final void printLocationName(CSVPrinter printer, String name) throws IOException { 045 printer.printRecord("LN", Bundle.getMessage("csvLocationName"), name); // NOI18N 046 } 047 048 protected final void printPrinterName(CSVPrinter printer, String name) throws IOException { 049 printer.printRecord("PRNTR", Bundle.getMessage("csvPrinterName"), name); // NOI18N 050 } 051 052 protected final void printRailroadName(CSVPrinter printer, String name) throws IOException { 053 printer.printRecord("RN", Bundle.getMessage("csvRailroadName"), name); // NOI18N 054 } 055 056 protected final void printRemoveHelpers(CSVPrinter printer) throws IOException { 057 printer.printRecord("RH", Bundle.getMessage("csvRemoveHelpers")); // NOI18N 058 } 059 060 protected final void printRouteLocationComment(CSVPrinter printer, String comment, String textColorName) 061 throws IOException { 062 printer.printRecord("RLC", Bundle.getMessage("csvRouteLocationComment"), comment, textColorName); // NOI18N 063 } 064 065 protected final void printTrainDeparts(CSVPrinter printer, String name, String direction) throws IOException { 066 printer.printRecord("TD", Bundle.getMessage("csvTrainDeparts"), name, direction); // NOI18N 067 } 068 069 protected final void printTrainDescription(CSVPrinter printer, String description) throws IOException { 070 printer.printRecord("TM", Bundle.getMessage("csvTrainManifestDescription"), description); // NOI18N 071 } 072 073 protected final void printTrainLength(CSVPrinter printer, int length, int empty, int total) throws IOException { 074 printer.printRecord("TL", Bundle.getMessage("csvTrainLengthEmptiesCars"), length, empty, total); // NOI18N 075 } 076 077 protected final void printTrainName(CSVPrinter printer, String name) throws IOException { 078 printer.printRecord("TN", Bundle.getMessage("csvTrainName"), name); // NOI18N 079 } 080 081 protected final void printTrainTerminates(CSVPrinter printer, String name) throws IOException { 082 printer.printRecord("TT", Bundle.getMessage("csvTrainTerminates"), name); 083 } 084 085 protected final void printTrainWeight(CSVPrinter printer, int weight) throws IOException { 086 printer.printRecord("TW", Bundle.getMessage("csvTrainWeight"), weight); // NOI18N 087 } 088 089 protected final void printValidity(CSVPrinter printer, String date) throws IOException { 090 printer.printRecord("VT", Bundle.getMessage("csvValid"), date); // NOI18N 091 } 092 093 protected final void printLocationSwitchListComment(CSVPrinter fileOut, Location location) throws IOException { 094 String textColorName = TrainCommon.getTextColorName(location.getSwitchListCommentWithColor()); 095 // comment can have multiple lines 096 String[] comments = location.getSwitchListComment().split(NEW_LINE); // NOI18N 097 for (String comment : comments) { 098 printLocationSwitchListComment(fileOut, comment, textColorName); 099 } 100 } 101 102 protected final void printLocationComment(CSVPrinter fileOut, Location location) throws IOException { 103 if (!location.getComment().equals(Location.NONE)) { 104 String textColorName = TrainCommon.getTextColorName(location.getCommentWithColor()); 105 // comment can have multiple lines 106 String[] comments = location.getComment().split(NEW_LINE); // NOI18N 107 for (String comment : comments) { 108 printLocationComment(fileOut, comment, textColorName); 109 } 110 } 111 } 112 113 protected final void printRouteLocationComment(CSVPrinter fileOut, RouteLocation rl) throws IOException { 114 if (!rl.getComment().equals(RouteLocation.NONE)) { 115 String textColorName = rl.getCommentTextColor(); 116 // comment can have multiple lines 117 String[] comments = rl.getComment().split(NEW_LINE); // NOI18N 118 for (String comment : comments) { 119 printRouteLocationComment(fileOut, comment, textColorName); 120 } 121 } 122 } 123 124 protected final void printTrainComment(CSVPrinter fileOut, Train train) throws IOException { 125 if (!train.getComment().equals(Train.NONE)) { 126 String textColorName = TrainCommon.getTextColorName(train.getCommentWithColor()); 127 String[] comments = train.getComment().split(NEW_LINE); 128 for (String comment : comments) { 129 fileOut.printRecord("TC", Bundle.getMessage("csvTrainComment"), comment, textColorName); // NOI18N 130 } 131 } 132 } 133 134 protected final void printRouteComment(CSVPrinter fileOut, Train train) throws IOException { 135 fileOut.printRecord("RC", Bundle.getMessage("csvRouteComment"), train.getRoute().getComment()); // NOI18N 136 } 137 138 protected void printLogoURL(CSVPrinter fileOut, Train train) throws IOException { 139 String logoURL = FileUtil.getExternalFilename(Setup.getManifestLogoURL()); 140 if (!train.getManifestLogoPathName().equals(Train.NONE)) { 141 logoURL = FileUtil.getExternalFilename(train.getManifestLogoPathName()); 142 } 143 if (!logoURL.isEmpty()) { 144 fileOut.printRecord("LOGO", Bundle.getMessage("csvLogoFilePath"), logoURL); 145 } 146 } 147 148 protected void printCar(CSVPrinter fileOut, Car car, String code, String message, int count) throws IOException { 149 fileOut.printRecord(code, 150 message, 151 car.getRoadName(), 152 car.getNumber(), 153 car.getTypeName(), 154 car.getLength(), 155 car.getLoadName(), 156 car.getColor(), 157 car.getLocationName(), 158 car.getTrackName(), 159 car.getDestinationName(), 160 car.getDestinationTrackName(), 161 car.getOwnerName(), 162 car.getKernelName(), 163 car.getComment(), 164 car.getPickupComment(), 165 car.getDropComment(), 166 car.isCaboose() ? "C" : "", 167 car.hasFred() ? "F" : "", 168 car.isHazardous() ? "H" : "", 169 car.getRfid(), 170 car.getReturnWhenEmptyDestinationName(), 171 car.getReturnWhenEmptyDestTrackName(), 172 car.isUtility() ? "U" : "", 173 count, 174 car.getFinalDestinationName(), 175 car.getFinalDestinationTrackName(), 176 car.getLoadType()); 177 } 178 179 protected void printEngine(CSVPrinter fileOut, Engine engine, String code, String message) throws IOException { 180 fileOut.printRecord(code, 181 message, 182 engine.getRoadName(), 183 engine.getNumber(), 184 engine.getModel(), 185 engine.getLength(), 186 engine.getTypeName(), 187 engine.getHp(), 188 engine.getLocationName(), 189 engine.getTrackName(), 190 engine.getDestinationName(), 191 engine.getDestinationTrackName(), 192 engine.getOwnerName(), 193 engine.getConsistName(), 194 engine.isLead() ? "Lead loco" : "", // NOI18N 195 engine.getComment(), 196 engine.getRfid(), 197 engine.getDccAddress()); 198 } 199 200 protected final void checkForEngineOrCabooseChange(CSVPrinter fileOut, Train train, RouteLocation rl) 201 throws IOException { 202 if (train.getSecondLegOptions() != Train.NO_CABOOSE_OR_FRED) { 203 if (rl == train.getSecondLegStartRouteLocation()) { 204 engineCsvChange(fileOut, rl, train.getSecondLegOptions()); 205 } 206 if (rl == train.getSecondLegEndRouteLocation()) { 207 printRemoveHelpers(fileOut); 208 } 209 } 210 if (train.getThirdLegOptions() != Train.NO_CABOOSE_OR_FRED) { 211 if (rl == train.getThirdLegStartRouteLocation()) { 212 engineCsvChange(fileOut, rl, train.getThirdLegOptions()); 213 } 214 if (rl == train.getThirdLegEndRouteLocation()) { 215 printRemoveHelpers(fileOut); 216 } 217 } 218 } 219 220 protected void engineCsvChange(CSVPrinter fileOut, RouteLocation rl, int legOptions) throws IOException { 221 if ((legOptions & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) { 222 fileOut.printRecord("AH", Bundle.getMessage("csvAddHelpers")); 223 } 224 if ((legOptions & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE || 225 (legOptions & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) { 226 fileOut.printRecord("CC", Bundle.getMessage("csvChangeCaboose")); 227 } 228 if ((legOptions & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) { 229 fileOut.printRecord("CL", Bundle.getMessage("csvChangeLocos")); 230 } 231 } 232 233 protected void printTrackComments(CSVPrinter fileOut, RouteLocation rl, List<Car> carList) throws IOException { 234 Location location = rl.getLocation(); 235 if (location != null) { 236 List<Track> tracks = location.getTracksByNameList(null); 237 for (Track track : tracks) { 238 // any pick ups or set outs to this track? 239 boolean pickup = false; 240 boolean setout = false; 241 for (Car car : carList) { 242 if (car.getRouteLocation() == rl && car.getTrack() != null && car.getTrack() == track) { 243 pickup = true; 244 } 245 if (car.getRouteDestination() == rl && 246 car.getDestinationTrack() != null && 247 car.getDestinationTrack() == track) { 248 setout = true; 249 } 250 } 251 // print the appropriate comment if there's one 252 // each comment can have multiple lines 253 if (pickup && setout && !track.getCommentBoth().equals(Track.NONE)) { 254 String textColorName = TrainCommon.getTextColorName(track.getCommentBothWithColor()); 255 String[] comments = track.getCommentBoth().split(NEW_LINE); 256 for (String comment : comments) { 257 fileOut.printRecord("TKCB", Bundle.getMessage("csvTrackCommentBoth"), comment, textColorName); // NOI18N 258 } 259 } else if (pickup && !setout && !track.getCommentPickup().equals(Track.NONE)) { 260 String textColorName = TrainCommon.getTextColorName(track.getCommentPickupWithColor()); 261 String[] comments = track.getCommentPickup().split(NEW_LINE); 262 for (String comment : comments) { 263 fileOut.printRecord("TKCP", Bundle.getMessage("csvTrackCommentPickUp"), comment, textColorName); // NOI18N 264 } 265 } else if (!pickup && setout && !track.getCommentSetout().equals(Track.NONE)) { 266 String textColorName = TrainCommon.getTextColorName(track.getCommentSetoutWithColor()); 267 String[] comments = track.getCommentSetout().split(NEW_LINE); 268 for (String comment : comments) { 269 fileOut.printRecord("TKCS", Bundle.getMessage("csvTrackCommentSetOut"), comment, textColorName); // NOI18N 270 } 271 } 272 } 273 } 274 } 275 276 protected void listCarsLocationUnknown(CSVPrinter fileOut) throws IOException { 277 List<Car> cars = InstanceManager.getDefault(CarManager.class).getCarsLocationUnknown(); 278 if (cars.isEmpty()) { 279 return; // no cars to search for! 280 } 281 fileOut.printRecord("SMCM", Bundle.getMessage("csvSearchMiaMessage"), Setup.getMiaComment()); 282 for (Car car : cars) { 283 printCar(fileOut, car, "SMC", Bundle.getMessage("csvSearchMissingCar"), 0); 284 } 285 } 286}