3636import org .mtransit .parser .mt .data .MStop ;
3737import org .mtransit .parser .mt .data .MDirection ;
3838import org .mtransit .parser .mt .data .MDirectionStop ;
39+ import org .mtransit .parser .mt .data .MString ;
40+ import org .mtransit .parser .mt .data .MStrings ;
3941
4042import java .io .BufferedWriter ;
4143import java .io .File ;
@@ -176,7 +178,8 @@ public static MSpec generateMSpec(@NotNull GSpec gtfs, @NotNull GAgencyTools age
176178 MTLog .log ("- Trips: %d" , mTripsList .size ());
177179 MTLog .log ("- Trip stops: %d" , mDirectionStopsList .size ());
178180 MTLog .log ("- Stops: %d" , mStopsList .size ());
179- MTLog .log ("- Service Ids: %d" , MServiceIds .getAll ().size ());
181+ MTLog .log ("- Service Ids: %d" , MServiceIds .count ());
182+ MTLog .log ("- Strings: %d" , MStrings .count ());
180183 MTLog .log ("- Service Dates: %d" , mServiceDatesList .size ());
181184 MTLog .log ("- Route with Frequencies: %d" , mRouteFrequencies .size ());
182185 MTLog .log ("- First timestamp: %s" , MTLog .formatDateTime (firstTimestamp ));
@@ -201,6 +204,7 @@ private static void logMerging(@NotNull String msg, long routeId) {
201204 MTLog .logDebug ("%s: Generating routes, trips, trip stops & stops objects... (merging %s)" , routeId , msg );
202205 }
203206
207+ private static final String GTFS_STRINGS = "gtfs_strings" ;
204208 private static final String GTFS_SCHEDULE = "gtfs_schedule" ;
205209 private static final String GTFS_SCHEDULE_SERVICE_DATES = GTFS_SCHEDULE + "_service_dates" ; // DB
206210 private static final String GTFS_SCHEDULE_SERVICE_IDS = GTFS_SCHEDULE + "_service_ids" ; // DB
@@ -282,6 +286,8 @@ public static void dumpFiles(@NotNull GAgencyTools gAgencyTools,
282286 dumpFrequencyRoutes (gAgencyTools , mSpec , fileBase , deleteAll , rawDirF );
283287 // SERVICE IDS
284288 dumpServiceIds (mSpec , fileBase , deleteAll , dataDirF , rawDirF , dbConnection ); // AFTER SCHEDULE STOPS & FREQUENCY ROUTES
289+ // STRINGS
290+ dumpStrings (mSpec , fileBase , deleteAll , dataDirF , rawDirF , dbConnection ); // AFTER ALL OTHER TABLE W/ STRINGS
285291 if (deleteAll ) {
286292 dumpValues (rawDirF , fileBase , null , null , null , null , null , -1 , -1 , null , true );
287293 } else {
@@ -568,6 +574,53 @@ private static void dumpServiceIds(
568574 }
569575 }
570576
577+ private static void dumpStrings (
578+ @ Nullable MSpec mSpec ,
579+ @ NotNull String fileBase ,
580+ boolean deleteAll ,
581+ @ NotNull File dataDirF ,
582+ @ NotNull File rawDirF ,
583+ @ Nullable Connection dbConnection ) {
584+ if (!FeatureFlags .F_EXPORT_SERVICE_ID_INTS ) return ;
585+ if (!deleteAll
586+ && (mSpec == null || !mSpec .isValid () || (F_PRE_FILLED_DB && dbConnection == null ))) {
587+ throw new MTLog .Fatal ("Generated data invalid (agencies: %s)!" , mSpec );
588+ }
589+ if (F_PRE_FILLED_DB ) {
590+ FileUtils .deleteIfExist (new File (rawDirF , fileBase + GTFS_STRINGS )); // migration from src/main/res/raw to data
591+ }
592+ File file = new File (F_PRE_FILLED_DB ? dataDirF : rawDirF , fileBase + GTFS_STRINGS );
593+ FileUtils .deleteIfExist (file ); // delete previous
594+ try (BufferedWriter ow = new BufferedWriter (new FileWriter (file ))) {
595+ if (!deleteAll ) {
596+ MTLog .logPOINT (); // LOG
597+ Statement dbStatement = null ;
598+ String sqlInsert = null ;
599+ if (F_PRE_FILLED_DB ) {
600+ SQLUtils .setAutoCommit (dbConnection , false ); // START TRANSACTION
601+ dbStatement = dbConnection .createStatement ();
602+ sqlInsert = GTFSCommons .getT_SERVICE_IDS_SQL_INSERT ();
603+ }
604+ for (MString mString : MStrings .getAll ()) {
605+ final String stringInsert = mString .toFile ();
606+ if (F_PRE_FILLED_DB ) {
607+ SQLUtils .executeUpdate (
608+ dbStatement ,
609+ String .format (sqlInsert , stringInsert )
610+ );
611+ }
612+ ow .write (stringInsert );
613+ ow .write (Constants .NEW_LINE );
614+ }
615+ if (F_PRE_FILLED_DB ) {
616+ SQLUtils .setAutoCommit (dbConnection , true ); // END TRANSACTION == commit()
617+ }
618+ }
619+ } catch (Exception ioe ) {
620+ throw new MTLog .Fatal (ioe , "I/O Error while writing strings file!" );
621+ }
622+ }
623+
571624 @ NotNull
572625 private static Pair <Integer , Integer > dumpScheduleServiceDates (@ NotNull GAgencyTools gAgencyTools ,
573626 @ Nullable MSpec mSpec ,
0 commit comments