Skip to content

Commit

Permalink
Add TranslatedAlert to help serializing of alert translations
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesj committed Feb 17, 2015
1 parent 1fd0340 commit e18e0c0
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 24 deletions.
13 changes: 9 additions & 4 deletions src/main/java/org/opentripplanner/api/model/Leg.java
Expand Up @@ -16,12 +16,14 @@ the License, or (at your option) any later version.
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;

import org.opentripplanner.api.model.alertpatch.LocalizedAlert;
import org.opentripplanner.routing.alertpatch.Alert;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.util.model.EncodedPolylineBean;
Expand Down Expand Up @@ -228,7 +230,7 @@ public class Leg {

@XmlElement
@JsonSerialize
public List<Alert> alerts;
public List<LocalizedAlert> alerts;

@XmlAttribute
@JsonSerialize
Expand Down Expand Up @@ -272,13 +274,16 @@ public double getDuration() {
return endTime.getTimeInMillis()/1000.0 - startTime.getTimeInMillis()/1000.0;
}

public void addAlert(Alert alert) {
public void addAlert(Alert alert, Locale locale) {
if (alerts == null) {
alerts = new ArrayList<>();
}
if (!alerts.contains(alert)) {
alerts.add(alert);
for (LocalizedAlert a : alerts) {
if (a.alert.equals(alert)) {
return;
}
}
alerts.add(new LocalizedAlert(alert, locale));
}

public void setTimeZone(TimeZone timeZone) {
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/opentripplanner/api/model/WalkStep.java
Expand Up @@ -17,11 +17,13 @@ the License, or (at your option) any later version.
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.opentripplanner.api.model.alertpatch.LocalizedAlert;
import org.opentripplanner.common.model.P2;
import org.opentripplanner.routing.alertpatch.Alert;

Expand Down Expand Up @@ -115,7 +117,8 @@ public class WalkStep {
public List<P2<Double>> elevation;

@XmlElement
public List<Alert> alerts;
@JsonSerialize
public List<LocalizedAlert> alerts;

public transient double angle;

Expand Down Expand Up @@ -173,18 +176,20 @@ public void setAbsoluteDirection(double thisAngle) {
absoluteDirection = AbsoluteDirection.values()[octant];
}

public void addAlerts(Collection<Alert> newAlerts) {
public void addAlerts(Collection<Alert> newAlerts, Locale locale) {
if (newAlerts == null) {
return;
}
if (alerts == null) {
alerts = new ArrayList<Alert>(newAlerts);
return;
alerts = new ArrayList<>();
}
for (Alert alert : newAlerts) {
if (!alerts.contains(alert)) {
alerts.add(alert);
ALERT: for (Alert newAlert : newAlerts) {
for (LocalizedAlert alert : alerts) {
if (alert.alert.equals(newAlert)) {
break ALERT;
}
}
alerts.add(new LocalizedAlert(newAlert, locale));
}
}

Expand Down
@@ -0,0 +1,66 @@
package org.opentripplanner.api.model.alertpatch;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.opentripplanner.routing.alertpatch.Alert;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.util.Date;
import java.util.Locale;

@XmlRootElement(name = "Alert")
public class LocalizedAlert {

@XmlTransient
@JsonIgnore
public Alert alert;

@XmlTransient
@JsonIgnore
private Locale locale;

public LocalizedAlert(Alert alert, Locale locale) {
this.alert = alert;
this.locale = locale;
}

public LocalizedAlert(){
}

@XmlAttribute
@JsonSerialize
public String getAlertHeaderText() {
if (alert.alertHeaderText == null) {
return null;
}
return alert.alertHeaderText.toString(locale);
}

@XmlAttribute
@JsonSerialize
public String getAlertDescriptionText() {
if (alert.alertDescriptionText == null) {
return null;
}
return alert.alertDescriptionText.toString(locale);
}

@XmlAttribute
@JsonSerialize
public String getAlertUrl() {
if (alert.alertUrl == null) {
return null;
}
return alert.alertUrl.toString(locale);
}

//null means unknown
@XmlElement
@JsonSerialize
public Date getEffectiveStartDate() {
return alert.effectiveStartDate;
}
}
Expand Up @@ -19,7 +19,6 @@ the License, or (props, at your option) any later version.
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TimeZone;

Expand All @@ -33,6 +32,7 @@ the License, or (props, at your option) any later version.
import org.opentripplanner.api.model.RelativeDirection;
import org.opentripplanner.api.model.TripPlan;
import org.opentripplanner.api.model.WalkStep;
import org.opentripplanner.api.model.alertpatch.LocalizedAlert;
import org.opentripplanner.common.geometry.DirectionUtils;
import org.opentripplanner.common.geometry.GeometryUtils;
import org.opentripplanner.common.geometry.PackedCoordinateSequence;
Expand Down Expand Up @@ -660,13 +660,13 @@ private void addModeAndAlerts(Graph graph, Leg leg, State[] states) {

if (alerts != null) {
for (Alert alert : alerts) {
leg.addAlert(alert);
leg.addAlert(alert, requestedLocale);
}
}

for (AlertPatch alertPatch : graph.getAlertPatches(edge)) {
if (alertPatch.displayDuring(state)) {
leg.addAlert(alertPatch.getAlert());
leg.addAlert(alertPatch.getAlert(), requestedLocale);
}
}
}
Expand Down Expand Up @@ -1088,7 +1088,7 @@ public static List<WalkStep> generateWalkSteps(Graph graph, State[] states, Walk

// increment the total length for this step
step.distance += edge.getDistance();
step.addAlerts(graph.streetNotesService.getNotes(forwardState));
step.addAlerts(graph.streetNotesService.getNotes(forwardState), wantedLocale);
lastAngle = DirectionUtils.getLastAngle(geom);
}
return steps;
Expand Down Expand Up @@ -1119,7 +1119,7 @@ private static WalkStep createWalkStep(Graph graph, State s, Locale wantedLocale
step.lat = en.getFromVertex().getY();
step.elevation = encodeElevationProfile(s.getBackEdge(), 0);
step.bogusName = en.hasBogusName();
step.addAlerts(graph.streetNotesService.getNotes(s));
step.addAlerts(graph.streetNotesService.getNotes(s), wantedLocale);
step.angle = DirectionUtils.getFirstAngle(s.getBackEdge().getGeometry());
if (s.getBackEdge() instanceof AreaEdge) {
step.area = true;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/opentripplanner/util/LocalizedString.java
Expand Up @@ -17,6 +17,7 @@ the License, or (at your option) any later version.
import com.google.common.collect.ListMultimap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -114,6 +115,13 @@ private List<String> getTagNames() {
return tag_names;
}

@Override
public boolean equals(Object other){
return other instanceof LocalizedString &&
key.equals(((LocalizedString) other).key) &&
Arrays.equals(params, ((LocalizedString) other).params);
}

/**
* Returns translated string in default locale
* with tag_names replaced with values
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/opentripplanner/GtfsTest.java
Expand Up @@ -179,7 +179,7 @@ public void validateLeg(Leg leg, long startTime, long endTime, String toStopId,
if (alert != null) {
assertNotNull(leg.alerts);
assertEquals(1, leg.alerts.size());
assertEquals(alert, leg.alerts.get(0).alertHeaderText.toString());
assertEquals(alert, leg.alerts.get(0).getAlertHeaderText());
} else {
assertNull(leg.alerts);
}
Expand Down
Expand Up @@ -1096,7 +1096,7 @@ private void compareLegs(Leg[] legs, Type type) {
assertEquals("http://www.ferry.org/", legs[4].agencyUrl);
assertEquals(2, legs[4].agencyTimeZoneOffset);
assertEquals(1, legs[4].alerts.size());
assertEquals(Alert.createSimpleAlerts(alertsExample), legs[4].alerts.get(0));
assertEquals(Alert.createSimpleAlerts(alertsExample), legs[4].alerts.get(0).alert);
assertEquals("C", legs[4].route);
assertEquals("C", legs[4].routeId);
assertEquals("C", legs[4].routeShortName);
Expand Down Expand Up @@ -1199,7 +1199,7 @@ private void compareLegs(Leg[] legs, Type type) {
assertNull(legs[7].agencyUrl);
assertEquals(2, legs[7].agencyTimeZoneOffset);
assertEquals(1, legs[7].alerts.size());
assertEquals(Alert.createSimpleAlerts(alertsExample), legs[7].alerts.get(0));
assertEquals(Alert.createSimpleAlerts(alertsExample), legs[7].alerts.get(0).alert);
assertEquals("", legs[7].route);
assertNull(legs[7].routeId);
assertNull(legs[7].routeShortName);
Expand Down Expand Up @@ -1353,7 +1353,7 @@ private void compareSteps(WalkStep[][] steps, Type type) {
* the arctic regions. Of course, longitude becomes meaningless at the poles themselves, but
* walking towards the pole, past it, and then back again will now yield correct results.
*/
assertEquals(alertsExample, steps[7][0].alerts.get(0).alertHeaderText.toString());
assertEquals(alertsExample, steps[7][0].alerts.get(0).getAlertHeaderText());
assertEquals(AbsoluteDirection.SOUTH, steps[7][0].absoluteDirection);
assertEquals(RelativeDirection.CONTINUE, steps[7][0].relativeDirection);
assertEquals(SOUTH, steps[7][0].angle, EPSILON);
Expand Down
Expand Up @@ -75,14 +75,12 @@ the License, or (at your option) any later version.
import org.opentripplanner.routing.impl.MemoryGraphSource;
import org.opentripplanner.routing.services.AlertPatchService;
import org.opentripplanner.routing.services.GraphService;
import org.opentripplanner.routing.spt.GraphPath;
import org.opentripplanner.routing.vertextype.IntersectionVertex;
import org.opentripplanner.routing.vertextype.StreetVertex;
import org.opentripplanner.routing.vertextype.TransitStationStop;
import org.opentripplanner.standalone.CommandLineParameters;
import org.opentripplanner.standalone.OTPConfigurator;
import org.opentripplanner.standalone.OTPServer;
import org.opentripplanner.standalone.Router;
import org.opentripplanner.updater.stoptime.TimetableSnapshotSource;

import com.google.transit.realtime.GtfsRealtime.TripDescriptor;
Expand Down Expand Up @@ -283,10 +281,10 @@ public void testAlerts() throws Exception {

assertNotNull(step0.alerts);
assertEquals(1, step0.alerts.size());
assertEquals("SE", step0.alerts.get(0).alertHeaderText.toString());
assertEquals("SE", step0.alerts.get(0).getAlertHeaderText());

assertEquals(1, step1.alerts.size());
assertEquals("NE", step1.alerts.get(0).alertHeaderText.toString());
assertEquals("NE", step1.alerts.get(0).getAlertHeaderText());
}

public void testIntermediate() throws Exception {
Expand Down

0 comments on commit e18e0c0

Please sign in to comment.