Skip to content

Commit

Permalink
Flag agencies for review. #15.
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Conway committed Aug 29, 2012
1 parent 282d8fc commit e4909cb
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/models/BikeRentalSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public class BikeRentalSystem extends Model {
*/
public String name;

/**
* Machine readable problem type requiring human review - picked up in the admin interface.
*/
@Enumerated(EnumType.STRING)
public ReviewType review;

/**
* The type of this bike rental system.
* One of KeolisRennes, Static, CityBik.es or Bixi
Expand Down
38 changes: 38 additions & 0 deletions app/models/GtfsFeed.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package models;

import javax.persistence.*;

import java.util.*;
import java.math.BigInteger;

import play.Logger;
import play.db.jpa.*;
Expand Down Expand Up @@ -40,6 +42,12 @@ public class GtfsFeed extends Model implements Cloneable {
/** Is this feed disabled? */
public boolean disabled;

/**
* Machine readable problem type requiring human review - picked up in the admin interface.
*/
@Enumerated(EnumType.STRING)
public ReviewType review;

/**
* The URL where this feed may be found. This may be the feed itself, or may be a
* developer site (e.g. http://developer.metro.net).
Expand Down Expand Up @@ -209,4 +217,34 @@ public GtfsFeed clone () {
return ret;

}

/**
* Attempt to automatically find and link this feed to agencies.
* @return true if matching was successful, false otherwise
*/
public boolean findAgency() {
Query q = JPA.em().createNativeQuery("SELECT a.id FROM NtdAgency a WHERE regexp_replace(LOWER(?), " +
// that regular expression strips the protocol,
// strips pathinfo,
// and strips www. to get a hopefully LCD domain name
"'(?:\\W*)(?:https?://)?(?:www\\.)?([a-zA-Z0-9\\-_\\.]*)(?:/.*)?(?:\\W*)'," +
"'\\1') = " +
"regexp_replace(LOWER(a.url)," +
"'(?:\\W*)(?:https?://)?(?:www\\.)?([a-zA-Z0-9\\-_\\.]*)(?:/.*)?(?:\\W*)'," +
"'\\1');");
q.setParameter(1, this.agencyUrl);
List<Object> results = q.getResultList();

if (results.size() == 0)
return false;
else {
NtdAgency agency;
for (Object result : results) {
agency = NtdAgency.findById(((BigInteger) result).longValue());
agency.feeds.add(this);
agency.save();
}
return true;
}
}
}
6 changes: 6 additions & 0 deletions app/models/MetroArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public class MetroArea extends Model {
*/
public String note;

/**
* Machine readable problem type requiring human review - picked up in the admin interface.
*/
@Enumerated(EnumType.STRING)
public ReviewType review;

/**
* The source of this metro area
*/
Expand Down
6 changes: 6 additions & 0 deletions app/models/NtdAgency.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class NtdAgency extends Model {
/** Where the data for this agency came from */
@Enumerated(EnumType.STRING)
public AgencySource source;

/**
* Machine readable problem type requiring human review - picked up in the admin interface.
*/
@Enumerated(EnumType.STRING)
public ReviewType review;

/** Does this agency provide GTFS to Google? */
public boolean googleGtfs;
Expand Down
20 changes: 20 additions & 0 deletions app/models/ReviewType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package models;
/**
* A ReviewType handles the options for when the action is ambiguous and must be reviewed by a human.
* @author matthewc
*
*/
public enum ReviewType {
NO_AGENCY ("Feed matches no agency"),
AGENCY_MULTIPLE_AREAS ("Agency matches multiple metro areas");

private String description;

private ReviewType (String options) {
this.description = options;
}

public String toString () {
return description;
}
}
15 changes: 14 additions & 1 deletion app/updaters/GtfsDataExchangeUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import models.GtfsFeed;
import models.MetroArea;
import models.NtdAgency;
import models.ReviewType;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
Expand Down Expand Up @@ -97,12 +98,16 @@ public Set<MetroArea> update () {
}

GtfsFeed newFeed;
boolean isNew;
// copy over all the data.
if (originalFeed != null)
if (originalFeed != null) {
isNew = false;
newFeed = originalFeed.clone();
}
else {
newFeed = new GtfsFeed();
newFeed.note = "new feed";
isNew = true;
}

// update all fields
Expand Down Expand Up @@ -137,6 +142,14 @@ public Set<MetroArea> update () {
stats.apply(newFeed);
newFeed.status = FeedParseStatus.SUCCESSFUL;
newFeed.save();

// if it's a new feed, find an agency.
if (isNew) {
if (!newFeed.findAgency())
newFeed.review = ReviewType.NO_AGENCY;

newFeed.save();
}

if (originalFeed != null) {
originalFeed.supersededBy = newFeed;
Expand Down
2 changes: 2 additions & 0 deletions app/utils/DbUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import models.MetroArea;
import models.MetroAreaSource;
import models.NtdAgency;
import models.ReviewType;
import play.db.jpa.JPA;

public class DbUtils {
Expand Down Expand Up @@ -98,6 +99,7 @@ else if (metros.size() > 1) {
else {
feed.disabled = true;
agency.note = "Too many metro areas";
agency.review = ReviewType.AGENCY_MULTIPLE_AREAS;
feed.save();
agency.save();
}
Expand Down

0 comments on commit e4909cb

Please sign in to comment.