11
11
import com .conveyal .datatools .editor .models .transit .TripPatternStop ;
12
12
import com .conveyal .datatools .manager .DataManager ;
13
13
import com .conveyal .datatools .manager .models .FeedSource ;
14
+ import com .conveyal .datatools .manager .models .Snapshot ;
14
15
import com .conveyal .datatools .manager .persistence .Persistence ;
15
16
import com .conveyal .gtfs .GTFSFeed ;
16
17
import com .conveyal .gtfs .loader .FeedLoadResult ;
26
27
import java .sql .JDBCType ;
27
28
import java .sql .PreparedStatement ;
28
29
import java .sql .SQLException ;
30
+ import java .util .Iterator ;
31
+ import java .util .List ;
29
32
30
33
import static com .conveyal .gtfs .loader .DateField .GTFS_DATE_FORMATTER ;
34
+ import static com .mongodb .client .model .Filters .and ;
35
+ import static com .mongodb .client .model .Filters .eq ;
31
36
32
37
public class ConvertEditorMapDBToSQL extends MonitorableJob {
33
38
private final String feedId ;
@@ -53,6 +58,18 @@ public void jobLogic() {
53
58
LOG .warn ("Not converting snapshot. Feed source Id {} does not exist in application data" , feedId );
54
59
return ;
55
60
}
61
+ Snapshot matchingSnapshot = Persistence .snapshots .getOneFiltered (
62
+ and (
63
+ eq ("version" , versionNumber ),
64
+ eq (Snapshot .FEED_SOURCE_REF , feedId )
65
+ ),
66
+ null
67
+ );
68
+ boolean snapshotExists = true ;
69
+ if (matchingSnapshot == null ) {
70
+ snapshotExists = false ;
71
+ matchingSnapshot = new Snapshot ("Imported" , feedId , "mapdb_editor" );
72
+ }
56
73
FeedTx feedTx ;
57
74
// FIXME: This needs to share a connection with the snapshotter.
58
75
// Create connection for each snapshot
@@ -74,19 +91,19 @@ public void jobLogic() {
74
91
LOG .info ("Converting {}.{} to SQL" , feedId , versionNumber );
75
92
// Convert mapdb to SQL
76
93
FeedLoadResult convertFeedResult = convertFeed (feedId , versionNumber , feedTx );
77
- // Create manager snapshot for storing in feed source .
78
- com . conveyal . datatools . manager . models . Snapshot managerSnapshot =
79
- new com . conveyal . datatools . manager . models . Snapshot (
80
- feedId , versionNumber != null ? versionNumber : 0 , "mapdb_editor" , convertFeedResult ) ;
81
- // managerSnapshot.dateCreated =
82
- LOG . info ( "Storing snapshot {}" , managerSnapshot . id );
83
- Persistence .snapshots .create (managerSnapshot );
94
+ // Update manager snapshot with result details .
95
+ matchingSnapshot . snapshotOf = "mapdb_editor" ;
96
+ matchingSnapshot . namespace = convertFeedResult . uniqueIdentifier ;
97
+ matchingSnapshot . feedLoadResult = convertFeedResult ;
98
+ LOG . info ( "Storing snapshot {}" , matchingSnapshot . id );
99
+ if ( snapshotExists ) Persistence . snapshots . replace ( matchingSnapshot . id , matchingSnapshot );
100
+ else Persistence .snapshots .create (matchingSnapshot );
84
101
if (setEditorBuffer ) {
85
102
// If there is no version, that indicates that this was from the editor buffer for that feedId.
86
103
// Make this snapshot the editor namespace buffer.
87
- LOG .info ("Updating active snapshot to {}" , managerSnapshot .id );
104
+ LOG .info ("Updating active snapshot to {}" , matchingSnapshot .id );
88
105
FeedSource updatedFeedSource = Persistence .feedSources .updateField (
89
- feedSource .id , "editorNamespace" , managerSnapshot .namespace );
106
+ feedSource .id , "editorNamespace" , matchingSnapshot .namespace );
90
107
LOG .info ("Editor namespace: {}" , updatedFeedSource .editorNamespace );
91
108
}
92
109
connection .commit ();
0 commit comments