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