use of org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean in project onebusaway-application-modules by camsys.
the class TwitterServiceImpl method toTweet.
/**
* convert a service alert to a tweet. @See NotificationStrategy to change
* behaviour.
*/
public static String toTweet(ServiceAlertBean bean, NotificationStrategy strategy) {
if (bean == null)
return null;
if (bean.getSummaries() == null)
return null;
if (bean.getSummaries().isEmpty())
return null;
List<String> routes = new ArrayList<>();
List<String> stops = new ArrayList<>();
boolean foundClause = false;
// this is making a big assumption
String tweet = bean.getSummaries().get(0).getValue();
if (bean.getAllAffects() != null && !bean.getAllAffects().isEmpty()) {
for (SituationAffectsBean allAffects : bean.getAllAffects()) {
if (allAffects != null && StringUtils.isNotBlank(allAffects.getRouteId())) {
// agency id is optional -- if route_id has it already do not prepend
if (allAffects.getAgencyId() == null || "null".equals(allAffects.getAgencyId())) {
routes.add(strategy.summarizeRoute(allAffects.getRouteId()));
foundClause = true;
} else {
AgencyAndId routeId = new AgencyAndId(allAffects.getAgencyId(), allAffects.getRouteId());
routes.add(strategy.summarizeRoute(routeId.toString()));
foundClause = true;
}
} else if (allAffects != null && StringUtils.isNotBlank(allAffects.getStopId())) {
AgencyAndId stopId = null;
try {
stopId = AgencyAndId.convertFromString(allAffects.getStopId());
} catch (IllegalStateException ise) {
stopId = new AgencyAndId(allAffects.getAgencyId(), allAffects.getStopId());
}
stops.add(strategy.summarizeStop(stopId.toString()));
foundClause = true;
} else if (allAffects != null && StringUtils.isBlank(allAffects.getRouteId()) && StringUtils.isBlank(allAffects.getStopId()) && StringUtils.isBlank(allAffects.getTripId()) && StringUtils.isNotBlank(allAffects.getAgencyId())) {
// agency wide service alert
foundClause = true;
}
}
} else {
// nothing to do -- no affects
_log.info("tweet rejected -- no affects clauses");
return null;
}
if (!foundClause) {
_log.info("no route/stop specified, rejecting tweet");
return null;
}
if (!routes.isEmpty()) {
tweet += " affecting route(s) ";
for (String r : routes) {
tweet += r + ", ";
}
tweet = tweet.substring(0, tweet.length() - 2);
if (!stops.isEmpty()) {
tweet += " and";
}
}
if (!stops.isEmpty()) {
if (routes.isEmpty()) {
tweet += " affecting stop(s) ";
} else {
tweet += " stop(s) ";
}
for (String s : stops) {
tweet += s + ", ";
}
tweet = tweet.substring(0, tweet.length() - 2);
}
// now add description and assume user is watching length
if (bean.getDescriptions() != null && !bean.getDescriptions().isEmpty()) {
tweet += " -- " + bean.getDescriptions().get(0).getValue();
}
return tweet;
}
use of org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean in project onebusaway-application-modules by camsys.
the class ServiceAlertsHelperV2 method handleAffects.
/*
public Date serviceAlertTimeToDate(long time) {
if (time == 0)
return null;
return new Date(time);
}
@SuppressWarnings("unused")
private void handlReasons(PtSituationElementStructure ptSituation,
ServiceAlertBean serviceAlert) {
throw new RuntimeException("handleReasons not implemented");
}
*/
private void handleAffects(ServiceAlertBean serviceAlert, PtSituationElementStructure ptSituation) {
if (serviceAlert.getAllAffects() == null)
return;
boolean hasOperators = false;
AffectsScopeStructure affectsStructure = new AffectsScopeStructure();
VehicleJourneys vehicleJourneys = new VehicleJourneys();
for (SituationAffectsBean affects : serviceAlert.getAllAffects()) {
String agencyId = affects.getAgencyId();
if (agencyId != null) {
Operators operators = new Operators();
if (StringUtils.equals(agencyId, "__ALL_OPERATORS__")) {
operators.setAllOperators("");
} else {
AffectedOperatorStructure affectedOperator = new AffectedOperatorStructure();
affectedOperator.getOperatorName().add(createDefaultedTextStructure(agencyId));
operators.getAffectedOperator().add(affectedOperator);
}
affectsStructure.setOperators(operators);
hasOperators = true;
}
// LineRef
String routeId = affects.getRouteId();
String directionId = affects.getDirectionId();
if (!StringUtils.isBlank(routeId)) {
AffectedVehicleJourneyStructure affectedVehicleJourneyStructure = new AffectedVehicleJourneyStructure();
LineRefStructure lineRefStructure = new LineRefStructure();
lineRefStructure.setValue(routeId);
affectedVehicleJourneyStructure.setLineRef(lineRefStructure);
if (!StringUtils.isBlank(directionId)) {
DirectionRefStructure directionRefStructure = new DirectionRefStructure();
directionRefStructure.setValue(directionId);
affectedVehicleJourneyStructure.setDirectionRef(directionRefStructure);
}
vehicleJourneys.getAffectedVehicleJourney().add(affectedVehicleJourneyStructure);
}
}
if (vehicleJourneys.getAffectedVehicleJourney().size() > 0) {
affectsStructure.setVehicleJourneys(vehicleJourneys);
}
if ((vehicleJourneys.getAffectedVehicleJourney().size() > 0) || hasOperators) {
ptSituation.setAffects(affectsStructure);
}
}
use of org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean in project onebusaway-application-modules by camsys.
the class AlertsForAgencyActionTest method test.
@Test
public void test() {
long now = System.currentTimeMillis();
List<ServiceAlertBean> alerts = new ArrayList<ServiceAlertBean>();
{
ServiceAlertBean alert = new ServiceAlertBean();
alerts.add(alert);
TimeRangeBean range = new TimeRangeBean(1234 * 1000, 5678 * 1000);
alert.setActiveWindows(Arrays.asList(range));
SituationAffectsBean affects = new SituationAffectsBean();
affects.setAgencyId("1");
affects.setRouteId("1_r0");
affects.setStopId("1_s0");
affects.setTripId("1_t0");
SituationAffectsBean alsoAffects = new SituationAffectsBean();
alsoAffects.setAgencyId("2");
alert.setAllAffects(Arrays.asList(affects, alsoAffects));
alert.setSummaries(Arrays.asList(new NaturalLanguageStringBean("Name", "en"), new NaturalLanguageStringBean("Nombre", "es")));
alert.setDescriptions(Arrays.asList(new NaturalLanguageStringBean("Description", "en"), new NaturalLanguageStringBean("DescripciĆ³n", "es")));
}
{
ServiceAlertBean alert = new ServiceAlertBean();
alerts.add(alert);
TimeRangeBean range = new TimeRangeBean(5678 * 1000, 1234 * 1000);
alert.setActiveWindows(Arrays.asList(range));
SituationAffectsBean affects = new SituationAffectsBean();
affects.setAgencyId("2");
affects.setRouteId("1_r1");
affects.setStopId("1_s1");
affects.setTripId("1_t1");
alert.setAllAffects(Arrays.asList(affects));
alert.setSummaries(Arrays.asList(new NaturalLanguageStringBean("Name", "en")));
alert.setDescriptions(Arrays.asList(new NaturalLanguageStringBean("Description", "en")));
}
ListBean<ServiceAlertBean> bean = new ListBean<ServiceAlertBean>();
bean.setList(alerts);
Mockito.when(_service.getAllServiceAlertsForAgencyId("1")).thenReturn(bean);
_action.setId("1");
_action.setTime(new Date(now));
_action.show();
ResponseBean model = _action.getModel();
FeedMessage feed = (FeedMessage) model.getData();
assertEquals(now / 1000, feed.getHeader().getTimestamp());
assertEquals(2, feed.getEntityCount());
{
FeedEntity entity = feed.getEntity(0);
assertEquals("1", entity.getId());
Alert alert = entity.getAlert();
assertEquals(1, alert.getActivePeriodCount());
TimeRange range = alert.getActivePeriod(0);
assertEquals(1234, range.getStart());
assertEquals(5678, range.getEnd());
assertEquals(2, alert.getInformedEntityCount());
{
EntitySelector affects = alert.getInformedEntity(0);
assertEquals("1", affects.getAgencyId());
assertEquals("r0", affects.getRouteId());
assertEquals("t0", affects.getTrip().getTripId());
assertEquals("s0", affects.getStopId());
}
{
EntitySelector affects = alert.getInformedEntity(1);
assertEquals("2", affects.getAgencyId());
}
TranslatedString header = alert.getHeaderText();
assertEquals(2, header.getTranslationCount());
{
Translation translation = header.getTranslation(0);
assertEquals("Name", translation.getText());
assertEquals("en", translation.getLanguage());
}
{
Translation translation = header.getTranslation(1);
assertEquals("Nombre", translation.getText());
assertEquals("es", translation.getLanguage());
}
TranslatedString description = alert.getDescriptionText();
assertEquals(2, description.getTranslationCount());
{
Translation translation = description.getTranslation(0);
assertEquals("Description", translation.getText());
assertEquals("en", translation.getLanguage());
}
{
Translation translation = description.getTranslation(1);
assertEquals("DescripciĆ³n", translation.getText());
assertEquals("es", translation.getLanguage());
}
}
{
FeedEntity entity = feed.getEntity(1);
assertEquals("2", entity.getId());
Alert alert = entity.getAlert();
assertEquals(1, alert.getActivePeriodCount());
TimeRange range = alert.getActivePeriod(0);
assertEquals(5678, range.getStart());
assertEquals(1234, range.getEnd());
assertEquals(1, alert.getInformedEntityCount());
{
EntitySelector affects = alert.getInformedEntity(0);
assertEquals("2", affects.getAgencyId());
assertEquals("r1", affects.getRouteId());
assertEquals("t1", affects.getTrip().getTripId());
assertEquals("s1", affects.getStopId());
}
}
}
use of org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean in project onebusaway-application-modules by camsys.
the class ServiceAlertsTestSupport method createServiceAlertBean.
public static ServiceAlertBean createServiceAlertBean(String id) {
ServiceAlertBean serviceAlertBean = new ServiceAlertBean();
serviceAlertBean.setId(id);
List<NaturalLanguageStringBean> summaries = new ArrayList<NaturalLanguageStringBean>();
summaries.add(createNaturalLanguageStringBean("summary"));
serviceAlertBean.setSummaries(summaries);
List<NaturalLanguageStringBean> descriptions = new ArrayList<NaturalLanguageStringBean>();
descriptions.add(createNaturalLanguageStringBean("description"));
serviceAlertBean.setDescriptions(descriptions);
List<SituationAffectsBean> allAffects = new ArrayList<SituationAffectsBean>();
serviceAlertBean.setAllAffects(allAffects);
allAffects.add(addAffects("1_100277", "0"));
allAffects.add(addAffects("1_100277", "1"));
allAffects.add(addAffects("1_100194", "0"));
allAffects.add(addAffects("1_100194", "1"));
return serviceAlertBean;
}
use of org.onebusaway.transit_data.model.service_alerts.SituationAffectsBean in project onebusaway-application-modules by camsys.
the class ServiceAlertAction method addAffects.
public String addAffects() {
if (_model.getId() == null) {
return INPUT;
}
try {
_model = _transitDataService.getServiceAlertForId(_model.getId());
List<SituationAffectsBean> allAffects = _model.getAllAffects();
if (allAffects == null) {
allAffects = new ArrayList<SituationAffectsBean>();
_model.setAllAffects(allAffects);
}
allAffects.add(new SituationAffectsBean());
_transitDataService.updateServiceAlert(_model);
} catch (RuntimeException e) {
_log.error("Error updating Service Alert Affects clause", e);
throw e;
}
return "submitSuccess";
}
Aggregations