use of org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.ServiceAlert in project onebusaway-application-modules by camsys.
the class GtfsRealtimeAlertLibraryTest method testGetAlertAsServiceAlert.
@Test
public void testGetAlertAsServiceAlert() {
AgencyAndId alertId = new AgencyAndId("1", "A1");
Alert.Builder alert = Alert.newBuilder();
GtfsRealtime.TimeRange.Builder timeRange = GtfsRealtime.TimeRange.newBuilder();
timeRange.setStart(1L);
timeRange.setStart(2L);
alert.addActivePeriod(timeRange);
EntitySelector.Builder entitySelector = EntitySelector.newBuilder();
entitySelector.setAgencyId("agencyId");
entitySelector.setRouteId("routeId");
entitySelector.setStopId("stopId");
TripDescriptor.Builder tripDescriptor = TripDescriptor.newBuilder();
tripDescriptor.setTripId("tripId");
entitySelector.setTrip(tripDescriptor);
alert.addInformedEntity(entitySelector);
alert.setCause(Alert.Cause.ACCIDENT);
alert.setEffect(Alert.Effect.DETOUR);
TranslatedString.Builder headerTexts = TranslatedString.newBuilder();
Translation.Builder headerText = Translation.newBuilder();
headerText.setLanguage("en");
headerText.setText("Summary");
headerTexts.addTranslation(headerText);
alert.setHeaderText(headerTexts);
TranslatedString.Builder descriptionTexts = TranslatedString.newBuilder();
Translation.Builder descriptionText = Translation.newBuilder();
descriptionText.setLanguage("fr");
descriptionText.setText("Description");
descriptionTexts.addTranslation(descriptionText);
alert.setDescriptionText(descriptionTexts);
TranslatedString.Builder urls = TranslatedString.newBuilder();
Translation.Builder url = Translation.newBuilder();
url.setLanguage("es");
url.setText("http://something/");
urls.addTranslation(url);
alert.setUrl(urls);
Mockito.when(_entitySource.getRouteId("routeId")).thenReturn(ServiceAlertLibrary.id("1", "routeId"));
Mockito.when(_entitySource.getStopId("stopId")).thenReturn(ServiceAlertLibrary.id("2", "stopId"));
Mockito.when(_entitySource.getTripId("tripId")).thenReturn(ServiceAlertLibrary.id("3", "tripId"));
ServiceAlert.Builder serviceAlert = _library.getAlertAsServiceAlert(alertId, alert.build());
Id id = serviceAlert.getId();
assertEquals("1", id.getAgencyId());
assertEquals("A1", id.getId());
assertEquals(1, serviceAlert.getAffectsCount());
Affects affects = serviceAlert.getAffects(0);
assertEquals("agencyId", affects.getAgencyId());
assertEquals("1", affects.getRouteId().getAgencyId());
assertEquals("routeId", affects.getRouteId().getId());
assertEquals("2", affects.getStopId().getAgencyId());
assertEquals("stopId", affects.getStopId().getId());
assertEquals("3", affects.getTripId().getAgencyId());
assertEquals("tripId", affects.getTripId().getId());
assertEquals(ServiceAlert.Cause.ACCIDENT, serviceAlert.getCause());
assertEquals(1, serviceAlert.getConsequenceCount());
Consequence consequence = serviceAlert.getConsequence(0);
assertEquals(Effect.DETOUR, consequence.getEffect());
ServiceAlerts.TranslatedString summaries = serviceAlert.getSummary();
assertEquals(1, summaries.getTranslationCount());
ServiceAlerts.TranslatedString.Translation summary = summaries.getTranslation(0);
assertEquals("en", summary.getLanguage());
assertEquals("Summary", summary.getText());
ServiceAlerts.TranslatedString descriptions = serviceAlert.getDescription();
assertEquals(1, descriptions.getTranslationCount());
ServiceAlerts.TranslatedString.Translation description = descriptions.getTranslation(0);
assertEquals("fr", description.getLanguage());
assertEquals("Description", description.getText());
ServiceAlerts.TranslatedString alertUrls = serviceAlert.getUrl();
assertEquals(1, alertUrls.getTranslationCount());
ServiceAlerts.TranslatedString.Translation alertUrl = alertUrls.getTranslation(0);
assertEquals("es", alertUrl.getLanguage());
assertEquals("http://something/", alertUrl.getText());
}
use of org.onebusaway.transit_data_federation.services.service_alerts.ServiceAlerts.ServiceAlert in project onebusaway-application-modules by camsys.
the class GtfsRealtimeSource method handleAlerts.
private void handleAlerts(FeedMessage alerts) {
for (FeedEntity entity : alerts.getEntityList()) {
Alert alert = entity.getAlert();
if (alert == null) {
_log.warn("epxected a FeedEntity with an Alert");
continue;
}
AgencyAndId id = createId(entity.getId());
if (entity.getIsDeleted()) {
_alertsById.remove(id);
_serviceAlertService.removeServiceAlert(id);
} else {
ServiceAlert.Builder serviceAlertBuilder = _alertLibrary.getAlertAsServiceAlert(id, alert, _alertAgencyIdMap);
ServiceAlert serviceAlert = serviceAlertBuilder.build();
ServiceAlert existingAlert = _alertsById.get(id);
if (existingAlert == null || !existingAlert.equals(serviceAlert)) {
_alertsById.put(id, serviceAlert);
ServiceAlertRecord serviceAlertRecord = new ServiceAlertRecord();
serviceAlertRecord.setAgencyId(_agencyIds.get(0));
serviceAlertRecord.setActiveWindows(new HashSet<ServiceAlertTimeRange>());
if (serviceAlert.getActiveWindowList() != null) {
for (ServiceAlerts.TimeRange timeRange : serviceAlert.getActiveWindowList()) {
ServiceAlertTimeRange serviceAlertTimeRange = new ServiceAlertTimeRange();
serviceAlertTimeRange.setFromValue(timeRange.getStart());
serviceAlertTimeRange.setToValue(timeRange.getEnd());
serviceAlertRecord.getActiveWindows().add(serviceAlertTimeRange);
}
}
serviceAlertRecord.setAllAffects(new HashSet<ServiceAlertsSituationAffectsClause>());
if (serviceAlert.getAffectsList() != null) {
for (ServiceAlerts.Affects affects : serviceAlertBuilder.getAffectsList()) {
ServiceAlertsSituationAffectsClause serviceAlertsSituationAffectsClause = new ServiceAlertsSituationAffectsClause();
serviceAlertsSituationAffectsClause.setAgencyId(affects.getAgencyId());
serviceAlertsSituationAffectsClause.setApplicationId(affects.getApplicationId());
serviceAlertsSituationAffectsClause.setDirectionId(affects.getDirectionId());
serviceAlertsSituationAffectsClause.setRouteId(affects.getRouteId().getId());
serviceAlertsSituationAffectsClause.setStopId(affects.getStopId().getId());
serviceAlertsSituationAffectsClause.setTripId(affects.getTripId().getId());
serviceAlertRecord.getAllAffects().add(serviceAlertsSituationAffectsClause);
}
}
serviceAlertRecord.setCause(getECause(serviceAlert.getCause()));
serviceAlertRecord.setConsequences(new HashSet<ServiceAlertSituationConsequenceClause>());
if (serviceAlert.getConsequenceList() != null) {
for (ServiceAlerts.Consequence consequence : serviceAlert.getConsequenceList()) {
ServiceAlertSituationConsequenceClause serviceAlertSituationConsequenceClause = new ServiceAlertSituationConsequenceClause();
serviceAlertSituationConsequenceClause.setDetourPath(consequence.getDetourPath());
serviceAlertSituationConsequenceClause.setDetourStopIds(new HashSet<String>());
if (consequence.getDetourStopIdsList() != null) {
for (ServiceAlerts.Id stopId : consequence.getDetourStopIdsList()) {
serviceAlertSituationConsequenceClause.getDetourStopIds().add(stopId.getId());
}
}
serviceAlertRecord.getConsequences().add(serviceAlertSituationConsequenceClause);
}
}
serviceAlertRecord.setCreationTime(serviceAlert.getCreationTime());
serviceAlertRecord.setDescriptions(new HashSet<ServiceAlertLocalizedString>());
if (serviceAlert.getDescription() != null) {
for (ServiceAlerts.TranslatedString.Translation translation : serviceAlert.getDescription().getTranslationList()) {
ServiceAlertLocalizedString string = new ServiceAlertLocalizedString();
string.setValue(translation.getText());
string.setLanguage(translation.getLanguage());
serviceAlertRecord.getDescriptions().add(string);
}
}
serviceAlertRecord.setModifiedTime(serviceAlert.getModifiedTime());
serviceAlertRecord.setPublicationWindows(new HashSet<ServiceAlertTimeRange>());
serviceAlertRecord.setServiceAlertId(serviceAlert.getId().getId());
serviceAlertRecord.setSeverity(getESeverity(serviceAlert.getSeverity()));
serviceAlertRecord.setSummaries(new HashSet<ServiceAlertLocalizedString>());
if (serviceAlert.getSummary() != null) {
for (ServiceAlerts.TranslatedString.Translation translation : serviceAlert.getSummary().getTranslationList()) {
ServiceAlertLocalizedString string = new ServiceAlertLocalizedString();
string.setValue(translation.getText());
string.setLanguage(translation.getLanguage());
serviceAlertRecord.getSummaries().add(string);
}
}
serviceAlertRecord.setUrls(new HashSet<ServiceAlertLocalizedString>());
if (serviceAlert.getUrl() != null) {
for (ServiceAlerts.TranslatedString.Translation translation : serviceAlert.getUrl().getTranslationList()) {
ServiceAlertLocalizedString string = new ServiceAlertLocalizedString();
string.setValue(translation.getText());
string.setLanguage(translation.getLanguage());
serviceAlertRecord.getUrls().add(string);
}
}
_serviceAlertService.createOrUpdateServiceAlert(serviceAlertRecord);
}
}
}
}
Aggregations