001package jmri.jmrit.operations.trains; 002 003import java.io.*; 004import java.nio.charset.StandardCharsets; 005import java.text.MessageFormat; 006import java.util.List; 007 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011import jmri.InstanceManager; 012import jmri.jmrit.operations.locations.Location; 013import jmri.jmrit.operations.rollingstock.cars.Car; 014import jmri.jmrit.operations.rollingstock.engines.Engine; 015import jmri.jmrit.operations.routes.Route; 016import jmri.jmrit.operations.routes.RouteLocation; 017import jmri.jmrit.operations.setup.Setup; 018import jmri.jmrit.operations.trains.schedules.TrainSchedule; 019import jmri.jmrit.operations.trains.schedules.TrainScheduleManager; 020import jmri.jmrit.operations.trains.trainbuilder.TrainCommon; 021 022/** 023 * Builds a train's manifest. User has the ability to modify the text of the 024 * messages which can cause an IllegalArgumentException. Some messages have more 025 * arguments than the default message allowing the user to customize the message 026 * to their liking. 027 * 028 * @author Daniel Boudreau Copyright (C) 2011, 2012, 2013, 2015, 2024 029 */ 030public class TrainManifest extends TrainCommon { 031 032 private static final Logger log = LoggerFactory.getLogger(TrainManifest.class); 033 034 String messageFormatText = ""; // the text being formated in case there's an exception 035 036 public TrainManifest(Train train) throws BuildFailedException { 037 // create manifest file 038 File file = InstanceManager.getDefault(TrainManagerXml.class).createTrainManifestFile(train.getName()); 039 PrintWriter fileOut; 040 041 try { 042 fileOut = new PrintWriter( 043 new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)), 044 true); 045 } catch (IOException e) { 046 log.error("Can not open train manifest file: {}", e.getLocalizedMessage()); 047 throw new BuildFailedException(e); 048 } 049 050 try { 051 // build header 052 if (!train.getRailroadName().equals(Train.NONE)) { 053 newLine(fileOut, train.getRailroadName()); 054 } else { 055 newLine(fileOut, Setup.getRailroadName()); 056 } 057 newLine(fileOut); // empty line 058 newLine(fileOut, MessageFormat.format(messageFormatText = TrainManifestText.getStringManifestForTrain(), 059 new Object[]{train.getName(), train.getDescription()})); 060 061 String valid = MessageFormat.format(messageFormatText = TrainManifestText.getStringValid(), 062 new Object[]{getDate(true)}); 063 064 String schName = ""; 065 066 if (Setup.isPrintTrainScheduleNameEnabled()) { 067 TrainSchedule sch = InstanceManager.getDefault(TrainScheduleManager.class).getActiveSchedule(); 068 if (sch != null) { 069 schName = "(" + sch.getName() + ")"; 070 } 071 } 072 if (Setup.isPrintValidEnabled()) { 073 newLine(fileOut, valid + " " + schName); 074 } else { 075 newLine(fileOut, schName); 076 } 077 if (!train.getCommentWithColor().equals(Train.NONE)) { 078 newLine(fileOut, train.getCommentWithColor()); 079 } 080 if (Setup.isPrintRouteCommentsEnabled() && !train.getRoute().getComment().equals(Route.NONE)) { 081 newLine(fileOut, train.getRoute().getComment()); 082 } 083 084 List<Engine> engineList = engineManager.getByTrainBlockingList(train); 085 List<Car> carList = carManager.getByTrainDestinationList(train); 086 log.debug("Train has {} cars assigned to it", carList.size()); 087 088 boolean hadWork = false; 089 String previousRouteLocationName = null; 090 List<RouteLocation> routeList = train.getRoute().getLocationsBySequenceList(); 091 092 /* 093 * Go through the train's route and print out the work for each 094 * location. Locations with "similar" names are combined to look 095 * like one location. 096 */ 097 for (RouteLocation rl : routeList) { 098 boolean printHeader = false; 099 boolean hasWork = isThereWorkAtLocation(carList, engineList, rl); 100 // print info only if new location 101 String routeLocationName = rl.getSplitName(); 102 if (!routeLocationName.equals(previousRouteLocationName) || (hasWork && !hadWork)) { 103 if (hasWork) { 104 newLine(fileOut); 105 hadWork = true; 106 printHeader = true; 107 108 // add arrival message 109 arrivalMessage(fileOut, train, rl); 110 111 // add route location comment 112 if (!rl.getComment().trim().equals(RouteLocation.NONE)) { 113 newLine(fileOut, rl.getCommentWithColor()); 114 } 115 116 // add location comment 117 if (Setup.isPrintLocationCommentsEnabled() && 118 !rl.getLocation().getCommentWithColor().equals(Location.NONE)) { 119 newLine(fileOut, rl.getLocation().getCommentWithColor()); 120 } 121 } 122 } 123 // remember location name 124 previousRouteLocationName = routeLocationName; 125 126 // add track comments 127 printTrackComments(fileOut, rl, carList, IS_MANIFEST); 128 129 // engine change or helper service? 130 if (train.getSecondLegOptions() != Train.NO_CABOOSE_OR_FRED) { 131 if (rl == train.getSecondLegStartRouteLocation()) { 132 printChange(fileOut, rl, train, train.getSecondLegOptions()); 133 } 134 if (rl == train.getSecondLegEndRouteLocation() && 135 train.getSecondLegOptions() == Train.HELPER_ENGINES) { 136 newLine(fileOut, 137 MessageFormat.format(messageFormatText = TrainManifestText.getStringRemoveHelpers(), 138 new Object[]{rl.getSplitName(), train.getName(), 139 train.getDescription(), train.getSecondLegNumberEngines(), 140 train.getSecondLegEngineModel(), train.getSecondLegEngineRoad()})); 141 } 142 } 143 if (train.getThirdLegOptions() != Train.NO_CABOOSE_OR_FRED) { 144 if (rl == train.getThirdLegStartRouteLocation()) { 145 printChange(fileOut, rl, train, train.getThirdLegOptions()); 146 } 147 if (rl == train.getThirdLegEndRouteLocation() && 148 train.getThirdLegOptions() == Train.HELPER_ENGINES) { 149 newLine(fileOut, 150 MessageFormat.format(messageFormatText = TrainManifestText.getStringRemoveHelpers(), 151 new Object[]{rl.getSplitName(), train.getName(), 152 train.getDescription(), train.getThirdLegNumberEngines(), 153 train.getThirdLegEngineModel(), train.getThirdLegEngineRoad()})); 154 } 155 } 156 157 setCarPickupAndSetoutTimes(train, rl, carList); 158 159 if (Setup.getManifestFormat().equals(Setup.STANDARD_FORMAT)) { 160 pickupEngines(fileOut, engineList, rl, IS_MANIFEST); 161 // if switcher show loco drop at end of list 162 if (train.isLocalSwitcher() || Setup.isPrintLocoLastEnabled()) { 163 blockCarsByTrack(fileOut, train, carList, rl, printHeader, IS_MANIFEST); 164 dropEngines(fileOut, engineList, rl, IS_MANIFEST); 165 } else { 166 dropEngines(fileOut, engineList, rl, IS_MANIFEST); 167 blockCarsByTrack(fileOut, train, carList, rl, printHeader, IS_MANIFEST); 168 } 169 } else if (Setup.getManifestFormat().equals(Setup.TWO_COLUMN_FORMAT)) { 170 blockLocosTwoColumn(fileOut, engineList, rl, IS_MANIFEST); 171 blockCarsTwoColumn(fileOut, train, carList, rl, printHeader, IS_MANIFEST); 172 } else { 173 blockLocosTwoColumn(fileOut, engineList, rl, IS_MANIFEST); 174 blockCarsByTrackNameTwoColumn(fileOut, train, carList, rl, printHeader, IS_MANIFEST); 175 } 176 177 if (rl != train.getTrainTerminatesRouteLocation()) { 178 // Is the next location the same as the current? 179 RouteLocation rlNext = train.getRoute().getNextRouteLocation(rl); 180 if (routeLocationName.equals(rlNext.getSplitName())) { 181 continue; 182 } 183 departureMessage(fileOut, train, rl, hadWork); 184 hadWork = false; 185 186 } else { 187 // last location in the train's route, print train terminates message 188 if (!hadWork) { 189 newLine(fileOut); 190 } else { 191 printHorizontalLine3(fileOut, IS_MANIFEST); 192 } 193 newLine(fileOut, MessageFormat.format(messageFormatText = TrainManifestText 194 .getStringTrainTerminates(), 195 new Object[]{routeLocationName, train.getName(), 196 train.getDescription(), rl.getLocation().getDivisionName()})); 197 } 198 } 199 // Are there any cars that need to be found? 200 addCarsLocationUnknown(fileOut, IS_MANIFEST); 201 202 } catch (IllegalArgumentException e) { 203 newLine(fileOut, Bundle.getMessage("ErrorIllegalArgument", 204 Bundle.getMessage("TitleManifestText"), e.getLocalizedMessage())); 205 newLine(fileOut, messageFormatText); 206 log.error("Illegal argument", e); 207 } 208 fileOut.flush(); 209 fileOut.close(); 210 train.setModified(false); 211 } 212 213 private void arrivalMessage(PrintWriter fileOut, Train train, RouteLocation rl) { 214 newLine(fileOut, getTrainMessage(train, rl)); 215 } 216 217 private void departureMessage(PrintWriter fileOut, Train train, RouteLocation rl, boolean hadWork) { 218 String routeLocationName = rl.getSplitName(); 219 if (!hadWork) { 220 newLine(fileOut); 221 // No work at {0} 222 String s = MessageFormat.format(messageFormatText = TrainManifestText 223 .getStringNoScheduledWork(), 224 new Object[]{routeLocationName, train.getName(), 225 train.getDescription(), rl.getLocation().getDivisionName()}); 226 // if a route comment, then only use location name and route comment, useful for passenger 227 // trains 228 if (!rl.getComment().equals(RouteLocation.NONE)) { 229 s = routeLocationName; 230 if (!rl.getComment().isBlank()) { 231 s = MessageFormat.format(messageFormatText = TrainManifestText 232 .getStringNoScheduledWorkWithRouteComment(), 233 new Object[]{routeLocationName, rl.getCommentWithColor(), train.getName(), 234 train.getDescription(), rl.getLocation().getDivisionName()}); 235 } 236 } 237 // append arrival or departure time if enabled 238 if (train.isShowArrivalAndDepartureTimesEnabled()) { 239 if (rl == train.getTrainDepartsRouteLocation()) { 240 s += MessageFormat.format(messageFormatText = TrainManifestText 241 .getStringDepartTime(), new Object[]{train.getFormatedDepartureTime()}); 242 } else if (!rl.getDepartureTime().equals(RouteLocation.NONE)) { 243 s += MessageFormat.format(messageFormatText = TrainManifestText 244 .getStringDepartTime(), new Object[]{train.getExpectedDepartureTime(rl)}); 245 } else if (Setup.isUseDepartureTimeEnabled() && 246 !rl.getComment().equals(RouteLocation.NONE)) { 247 s += MessageFormat 248 .format(messageFormatText = TrainManifestText.getStringDepartTime(), 249 new Object[]{train.getExpectedDepartureTime(rl)}); 250 } 251 } 252 newLine(fileOut, s); 253 254 // add location comment 255 if (Setup.isPrintLocationCommentsEnabled() && 256 !rl.getLocation().getCommentWithColor().equals(Location.NONE)) { 257 newLine(fileOut, rl.getLocation().getCommentWithColor()); 258 } 259 } else { 260 printHorizontalLine3(fileOut, IS_MANIFEST); 261 } 262 if (Setup.isPrintLoadsAndEmptiesEnabled()) { 263 int emptyCars = train.getNumberEmptyCarsInTrain(rl); 264 // Message format: Train departs Boston Westbound with 4 loads, 8 empties, 450 feet, 3000 tons 265 newLine(fileOut, MessageFormat.format(messageFormatText = TrainManifestText 266 .getStringTrainDepartsLoads(), 267 new Object[]{routeLocationName, 268 rl.getTrainDirectionString(), train.getNumberCarsInTrain(rl) - emptyCars, 269 emptyCars, 270 train.getTrainLength(rl), Setup.getLengthUnit().toLowerCase(), 271 train.getTrainWeight(rl), train.getTrainTerminatesName(), train.getName()})); 272 } else { 273 // Message format: Train departs Boston Westbound with 12 cars, 450 feet, 3000 tons 274 newLine(fileOut, MessageFormat.format(messageFormatText = TrainManifestText 275 .getStringTrainDepartsCars(), 276 new Object[]{routeLocationName, 277 rl.getTrainDirectionString(), train.getNumberCarsInTrain(rl), 278 train.getTrainLength(rl), 279 Setup.getLengthUnit().toLowerCase(), train.getTrainWeight(rl), 280 train.getTrainTerminatesName(), train.getName()})); 281 } 282 } 283 284 private void printChange(PrintWriter fileOut, RouteLocation rl, Train train, int legOptions) 285 throws IllegalArgumentException { 286 if ((legOptions & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) { 287 // assume 2nd leg for helper change 288 String numberEngines = train.getSecondLegNumberEngines(); 289 String endLocationName = train.getSecondLegEndLocationName(); 290 String engineModel = train.getSecondLegEngineModel(); 291 String engineRoad = train.getSecondLegEngineRoad(); 292 if (rl == train.getThirdLegStartRouteLocation()) { 293 numberEngines = train.getThirdLegNumberEngines(); 294 endLocationName = train.getThirdLegEndLocationName(); 295 engineModel = train.getThirdLegEngineModel(); 296 engineRoad = train.getThirdLegEngineRoad(); 297 } 298 newLine(fileOut, 299 MessageFormat.format(messageFormatText = TrainManifestText.getStringAddHelpers(), 300 new Object[]{rl.getSplitName(), train.getName(), train.getDescription(), 301 numberEngines, endLocationName, engineModel, engineRoad})); 302 } else if ((legOptions & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES && 303 ((legOptions & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE || 304 (legOptions & Train.ADD_CABOOSE) == Train.ADD_CABOOSE)) { 305 newLine(fileOut, MessageFormat.format( 306 messageFormatText = TrainManifestText.getStringLocoAndCabooseChange(), new Object[]{ 307 rl.getSplitName(), train.getName(), train.getDescription(), 308 rl.getLocation().getDivisionName()})); 309 } else if ((legOptions & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) { 310 newLine(fileOut, MessageFormat.format(messageFormatText = TrainManifestText.getStringLocoChange(), 311 new Object[]{rl.getSplitName(), train.getName(), train.getDescription(), 312 rl.getLocation().getDivisionName()})); 313 } else if ((legOptions & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE || 314 (legOptions & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) { 315 newLine(fileOut, MessageFormat.format(messageFormatText = TrainManifestText.getStringCabooseChange(), 316 new Object[]{rl.getSplitName(), train.getName(), train.getDescription(), 317 rl.getLocation().getDivisionName()})); 318 } 319 } 320 321 private void newLine(PrintWriter file, String string) { 322 if (!string.isEmpty()) { 323 newLine(file, string, IS_MANIFEST); 324 } 325 } 326}