Skip to content

Commit

Permalink
Merge branch 'master' of github.com:conveyal/gtfs-editor
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Oct 14, 2015
2 parents e0acd55 + 701639e commit 31815cb
Show file tree
Hide file tree
Showing 35 changed files with 1,092 additions and 890 deletions.
2 changes: 1 addition & 1 deletion INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Configure gtfs-editor application.conf

Install dependencies

[path to play1.2.5]/play dependencies
[path to play1.2.5]/play dependencies


Run application
Expand Down
49 changes: 24 additions & 25 deletions app/controllers/Application.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
package controllers;

import com.google.common.collect.Maps;
import datastore.AgencyTx;
import datastore.GlobalTx;
import datastore.VersionedDataStore;
import jobs.GisExport;
import play.*;
import jobs.ProcessGtfsSnapshotExport;
import jobs.ProcessGtfsSnapshotMerge;
import models.Account;
import models.OAuthToken;
import models.transit.*;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import play.Play;
import play.i18n.Lang;
import play.i18n.Messages;
import play.mvc.*;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.Http.Request;
import play.mvc.Scope.Session;
import play.mvc.With;

import java.io.File;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;

import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

import datastore.VersionedDataStore;
import datastore.AgencyTx;
import datastore.GlobalTx;
import jobs.ProcessGtfsSnapshotExport;
import jobs.ProcessGtfsSnapshotMerge;
import models.*;
import models.transit.RouteType;
import models.transit.Agency;
import models.transit.StopTime;
import models.transit.Trip;
import models.transit.TripPattern;
import models.transit.TripPatternStop;

@With(Secure.class)
public class Application extends Controller {
/** used to give almost-friendly names to exported files */
private static AtomicLong nextExportId = new AtomicLong(1);
public static AtomicLong nextExportId = new AtomicLong(1);

@Before
static void initSession() throws Throwable {
Expand Down Expand Up @@ -358,7 +351,7 @@ public static void createGtfs(List<String> agencySelect, Long calendarFrom, Long

File out = new File(Play.configuration.getProperty("application.publicDataDirectory"), "gtfs_" + nextExportId.incrementAndGet() + ".zip");

new ProcessGtfsSnapshotExport(agencySelect, out, startDate, endDate).run();
new ProcessGtfsSnapshotExport(agencySelect, out, startDate, endDate, false).run();

redirect(Play.configuration.getProperty("application.appBase") + "/public/data/" + out.getName());
}
Expand Down Expand Up @@ -413,16 +406,22 @@ public static void uploadGtfs(File gtfsUpload) {
importGtfs();
}
else {
ProcessGtfsSnapshotMerge merge;
try {
new ProcessGtfsSnapshotMerge(gtfsUpload).run();
merge = new ProcessGtfsSnapshotMerge(gtfsUpload);
merge.run();
}
catch (Exception e) {
e.printStackTrace();
validation.addError("gtfsUpload", "Unable to process file.");
params.flash();
validation.keep();
importGtfs();
return;
}

// if there are multiple agencies this will pick one randomly
search(merge.agencyId);
}
}

Expand Down
90 changes: 82 additions & 8 deletions app/controllers/api/SnapshotController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package controllers.api;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import jobs.ProcessGtfsSnapshotExport;
import models.Snapshot;
import models.transit.Stop;

Expand All @@ -16,6 +18,8 @@
import controllers.Security;
import datastore.GlobalTx;
import datastore.VersionedDataStore;
import play.Logger;
import play.Play;
import play.mvc.Before;
import play.mvc.Controller;
import play.mvc.With;
Expand Down Expand Up @@ -46,13 +50,18 @@ public static void getSnapshot(String agencyId, String id) throws IOException {
else {
if (agencyId == null)
agencyId = session.get("agencyId");


Collection<Snapshot> snapshots;
if (agencyId == null) {
badRequest();
return;
// if it's still null just give them everything
// this is used in GTFS Data Manager to get snapshots in bulk
// TODO this allows any authenticated user to fetch GTFS data for any agency
snapshots = gtx.snapshots.values();
}

Collection<Snapshot> snapshots = gtx.snapshots.subMap(new Tuple2(agencyId, null), new Tuple2(agencyId, Fun.HI)).values();
else {
snapshots = gtx.snapshots.subMap(new Tuple2(agencyId, null), new Tuple2(agencyId, Fun.HI)).values();
}

renderJSON(Base.toJson(snapshots, false));
}
} finally {
Expand All @@ -63,8 +72,11 @@ public static void getSnapshot(String agencyId, String id) throws IOException {
public static void createSnapshot () {
GlobalTx gtx = null;
try {
Snapshot s = Base.mapper.readValue(params.get("body"), Snapshot.class);
s = VersionedDataStore.takeSnapshot(s.agencyId, s.name);
// create a dummy snapshot from which to get values
Snapshot original = Base.mapper.readValue(params.get("body"), Snapshot.class);
Snapshot s = VersionedDataStore.takeSnapshot(original.agencyId, original.name, original.comment);
s.validFrom = original.validFrom;
s.validTo = original.validTo;
gtx = VersionedDataStore.getGlobalTx();

// the snapshot we have just taken is now current; make the others not current
Expand All @@ -81,10 +93,42 @@ public static void createSnapshot () {

renderJSON(Base.toJson(s, false));
} catch (IOException e) {
e.printStackTrace();
badRequest();
if (gtx != null) gtx.rollbackIfOpen();
}
}

public static void updateSnapshot (String id) {
GlobalTx gtx = null;
try {
Snapshot s = Base.mapper.readValue(params.get("body"), Snapshot.class);

Tuple2<String, Integer> sid = JacksonSerializers.Tuple2IntDeserializer.deserialize(id);

if (s == null || s.id == null || !s.id.equals(sid)) {
Logger.warn("snapshot ID not matched, not updating: %s, %s", s.id, id);
badRequest();
}

gtx = VersionedDataStore.getGlobalTx();

if (!gtx.snapshots.containsKey(s.id)) {
gtx.rollback();
notFound();
}

gtx.snapshots.put(s.id, s);

gtx.commit();

renderJSON(Base.toJson(s, false));
} catch (IOException e) {
e.printStackTrace();
if (gtx != null) gtx.rollbackIfOpen();
badRequest();
}
}

public static void restoreSnapshot (String id) {
Tuple2<String, Integer> decodedId;
Expand All @@ -107,7 +151,7 @@ public static void restoreSnapshot (String id) {

List<Stop> stops = VersionedDataStore.restore(local);

// the snapshot we have just taken is now current; make the others not current
// the snapshot we have just restored is now current; make the others not current
for (Snapshot o : gtx.snapshots.subMap(new Tuple2(local.agencyId, null), new Tuple2(local.agencyId, Fun.HI)).values()) {
if (o.id.equals(local.id))
continue;
Expand All @@ -130,4 +174,34 @@ public static void restoreSnapshot (String id) {
gtx.rollbackIfOpen();
}
}

/** Export a snapshot as GTFS */
public static void exportSnapshot (String id) {
Tuple2<String, Integer> decodedId;
try {
decodedId = JacksonSerializers.Tuple2IntDeserializer.deserialize(id);
} catch (IOException e1) {
badRequest();
return;
}

GlobalTx gtx = VersionedDataStore.getGlobalTx();
Snapshot local;
try {
if (!gtx.snapshots.containsKey(decodedId)) {
notFound();
return;
}

local = gtx.snapshots.get(decodedId);

File out = new File(Play.configuration.getProperty("application.publicDataDirectory"), "gtfs_" + Application.nextExportId.incrementAndGet() + ".zip");

new ProcessGtfsSnapshotExport(local, out).run();

redirect(Play.configuration.getProperty("application.appBase") + "/public/data/" + out.getName());
} finally {
gtx.rollbackIfOpen();
}
}
}
43 changes: 16 additions & 27 deletions app/datastore/AgencyTx.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
package datastore;

import java.util.Collection;
import java.util.Iterator;
import java.util.NavigableSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentMap;

import models.transit.Agency;
import models.transit.Route;
import models.transit.ScheduleException;
import models.transit.ServiceCalendar;
import models.transit.Stop;
import models.transit.Trip;
import models.transit.TripPattern;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterators;
import models.transit.*;
import org.joda.time.LocalDate;
import org.mapdb.Atomic;
import org.mapdb.BTreeMap;
import org.mapdb.Bind;
import org.mapdb.Bind.MapWithModificationListener;
import org.mapdb.DB;
import org.mapdb.Fun;
import org.mapdb.*;
import org.mapdb.Fun.Function2;
import org.mapdb.Fun.Tuple2;

import play.i18n.Messages;
import utils.BindUtils;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Iterators;
import java.util.*;
import java.util.concurrent.ConcurrentMap;

/** a transaction in an agency database */
public class AgencyTx extends DatabaseTx {
Expand Down Expand Up @@ -94,7 +76,12 @@ public class AgencyTx extends DatabaseTx {
/**
* Create an agency tx.
*/
AgencyTx (DB tx) {
public AgencyTx (DB tx) {
this(tx, true);
}

/** Create an agency tx, optionally without secondary indices */
public AgencyTx (DB tx, boolean buildSecondaryIndices) {
super(tx);

tripPatterns = getMap("tripPatterns");
Expand All @@ -104,7 +91,9 @@ public class AgencyTx extends DatabaseTx {
exceptions = getMap("exceptions");
snapshotVersion = tx.getAtomicInteger("snapshotVersion");
stops = getMap("stops");
buildSecondaryIndices();

if (buildSecondaryIndices)
buildSecondaryIndices();
}

public void buildSecondaryIndices () {
Expand Down
Loading

0 comments on commit 31815cb

Please sign in to comment.