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                car.getReturnWhenLoadedDestinationName(),
178                car.getReturnWhenLoadedDestTrackName(),
179                car.getRoutePath());
180    }
181
182    protected void printEngine(CSVPrinter fileOut, Engine engine, String code, String message) throws IOException {
183        fileOut.printRecord(code,
184                message,
185                engine.getRoadName(),
186                engine.getNumber(),
187                engine.getModel(),
188                engine.getLength(),
189                engine.getTypeName(),
190                engine.getHp(),
191                engine.getLocationName(),
192                engine.getTrackName(),
193                engine.getDestinationName(),
194                engine.getDestinationTrackName(),
195                engine.getOwnerName(),
196                engine.getConsistName(),
197                engine.isLead() ? "Lead loco" : "", // NOI18N
198                engine.getComment(),
199                engine.getRfid(),
200                engine.getDccAddress());
201    }
202
203    protected final void checkForEngineOrCabooseChange(CSVPrinter fileOut, Train train, RouteLocation rl)
204            throws IOException {
205        if (train.getSecondLegOptions() != Train.NO_CABOOSE_OR_FRED) {
206            if (rl == train.getSecondLegStartRouteLocation()) {
207                engineCsvChange(fileOut, rl, train.getSecondLegOptions());
208            }
209            if (rl == train.getSecondLegEndRouteLocation()) {
210                printRemoveHelpers(fileOut);
211            }
212        }
213        if (train.getThirdLegOptions() != Train.NO_CABOOSE_OR_FRED) {
214            if (rl == train.getThirdLegStartRouteLocation()) {
215                engineCsvChange(fileOut, rl, train.getThirdLegOptions());
216            }
217            if (rl == train.getThirdLegEndRouteLocation()) {
218                printRemoveHelpers(fileOut);
219            }
220        }
221    }
222
223    protected void engineCsvChange(CSVPrinter fileOut, RouteLocation rl, int legOptions) throws IOException {
224        if ((legOptions & Train.HELPER_ENGINES) == Train.HELPER_ENGINES) {
225            fileOut.printRecord("AH", Bundle.getMessage("csvAddHelpers"));
226        }
227        if ((legOptions & Train.REMOVE_CABOOSE) == Train.REMOVE_CABOOSE ||
228                (legOptions & Train.ADD_CABOOSE) == Train.ADD_CABOOSE) {
229            fileOut.printRecord("CC", Bundle.getMessage("csvChangeCaboose"));
230        }
231        if ((legOptions & Train.CHANGE_ENGINES) == Train.CHANGE_ENGINES) {
232            fileOut.printRecord("CL", Bundle.getMessage("csvChangeLocos"));
233        }
234    }
235
236    protected void printTrackComments(CSVPrinter fileOut, RouteLocation rl, List<Car> carList) throws IOException {
237        Location location = rl.getLocation();
238        if (location != null) {
239            List<Track> tracks = location.getTracksByNameList(null);
240            for (Track track : tracks) {
241                // any pick ups or set outs to this track?
242                boolean pickup = false;
243                boolean setout = false;
244                for (Car car : carList) {
245                    if (car.getRouteLocation() == rl && car.getTrack() != null && car.getTrack() == track) {
246                        pickup = true;
247                    }
248                    if (car.getRouteDestination() == rl &&
249                            car.getDestinationTrack() != null &&
250                            car.getDestinationTrack() == track) {
251                        setout = true;
252                    }
253                }
254                // print the appropriate comment if there's one
255                // each comment can have multiple lines
256                if (pickup && setout && !track.getCommentBoth().equals(Track.NONE)) {
257                    String textColorName = TrainCommon.getTextColorName(track.getCommentBothWithColor());
258                    String[] comments = track.getCommentBoth().split(NEW_LINE);
259                    for (String comment : comments) {
260                        fileOut.printRecord("TKCB", Bundle.getMessage("csvTrackCommentBoth"), comment, textColorName); // NOI18N
261                    }
262                } else if (pickup && !setout && !track.getCommentPickup().equals(Track.NONE)) {
263                    String textColorName = TrainCommon.getTextColorName(track.getCommentPickupWithColor());
264                    String[] comments = track.getCommentPickup().split(NEW_LINE);
265                    for (String comment : comments) {
266                        fileOut.printRecord("TKCP", Bundle.getMessage("csvTrackCommentPickUp"), comment, textColorName); // NOI18N
267                    }
268                } else if (!pickup && setout && !track.getCommentSetout().equals(Track.NONE)) {
269                    String textColorName = TrainCommon.getTextColorName(track.getCommentSetoutWithColor());
270                    String[] comments = track.getCommentSetout().split(NEW_LINE);
271                    for (String comment : comments) {
272                        fileOut.printRecord("TKCS", Bundle.getMessage("csvTrackCommentSetOut"), comment, textColorName); // NOI18N
273                    }
274                }
275            }
276        }
277    }
278
279    protected void listCarsLocationUnknown(CSVPrinter fileOut) throws IOException {
280        List<Car> cars = InstanceManager.getDefault(CarManager.class).getCarsLocationUnknown();
281        if (cars.isEmpty()) {
282            return; // no cars to search for!
283        }
284        fileOut.printRecord("SMCM", Bundle.getMessage("csvSearchMiaMessage"), Setup.getMiaComment());
285        for (Car car : cars) {
286            printCar(fileOut, car, "SMC", Bundle.getMessage("csvSearchMissingCar"), 0);
287        }
288    }
289}