Skip to content

Commit

Permalink
Merge pull request #111 from camsys/NYS-141-Sort-Services-in-standard…
Browse files Browse the repository at this point in the history
…-MTA-order

Added RouteSort configurable class and Implementation.
  • Loading branch information
sheldonabrown authored May 15, 2023
2 parents 6541bbb + 9072b96 commit c9dc040
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class BeanFactoryV2 {

private Locale _locale;

private RouteSort customRouteSort;

public BeanFactoryV2(boolean includeReferences) {
_includeReferences = includeReferences;
_locale = Locale.getDefault();
Expand All @@ -93,6 +95,11 @@ public void setApplicationKey(String applicationKey) {
_applicationKey = applicationKey;
}

public void setCustomRouteSort(RouteSort customRouteSort) {
this.customRouteSort = customRouteSort;
}


public void setLocale(Locale locale) {
_locale = locale;
}
Expand Down Expand Up @@ -124,11 +131,27 @@ public ListWithReferencesBean<StopV2Bean> getResponse(StopSearchResultBean input
// swap objects for ids and references
beans.add(getStop(stop));
}
return new ListWithRangeAndReferencesBean<>(
ListWithReferencesBean<StopV2Bean> response = new ListWithRangeAndReferencesBean<>(
beans,
input.getStopSuggestions().isLimitExceeded(),
false,
this._references);

String agencyId = response.getReferences().getRoutes().size() > 0 ?
response.getReferences().getRoutes().get(0).getAgencyId() :
"";

response.getReferences()
.getRoutes()
.sort((a,b) -> customRouteSort
.compareRoutes(
a.getShortName(),
b.getShortName(),
customRouteSort,
agencyId)
);

return response;
}

public ListWithReferencesBean<RouteV2Bean> getResponse(RouteSearchResultBean input) {
Expand All @@ -137,11 +160,24 @@ public ListWithReferencesBean<RouteV2Bean> getResponse(RouteSearchResultBean inp
// swap objects for ids and references
beans.add(getRoute(route));
}
return new ListWithRangeAndReferencesBean<>(
ListWithReferencesBean<RouteV2Bean> response = new ListWithRangeAndReferencesBean<>(
beans,
input.getRouteSuggestions().isLimitExceeded(),
false,
this._references);

String agencyId = response.getList().size() > 0 ?
response.getList().get(0).getAgencyId() :
"";
response
.getList().sort((a,b) ->
customRouteSort.compareRoutes(
a.getShortName(),
b.getShortName(),
customRouteSort,
agencyId));

return response;
}

public EntryWithReferencesBean<TripV2Bean> getResponse(TripBean trip) {
Expand All @@ -159,12 +195,47 @@ public EntryWithReferencesBean<BlockV2Bean> getBlockResponse(BlockBean block) {

public EntryWithReferencesBean<StopWithArrivalsAndDeparturesV2Bean> getResponse(
StopWithArrivalsAndDeparturesBean result) {
return entry(getStopWithArrivalAndDepartures(result));
EntryWithReferencesBean<StopWithArrivalsAndDeparturesV2Bean> response = entry(getStopWithArrivalAndDepartures(result));

String agencyId = response
.getEntry()
.getArrivalsAndDepartures().size() > 0 ?
AgencyAndIdLibrary.convertFromString(response.getEntry().getArrivalsAndDepartures().get(0)
.getRouteId()).getAgencyId() :
"";

response.getEntry().getArrivalsAndDepartures()
.sort((a,b) ->
customRouteSort
.compareRoutes(
a.getRouteShortName(),
b.getRouteShortName(),
customRouteSort,
agencyId)
);

return response;
}

public EntryWithReferencesBean<StopsWithArrivalsAndDeparturesV2Bean> getResponse(
StopsWithArrivalsAndDeparturesBean result) {
return entry(getStopsWithArrivalAndDepartures(result));

EntryWithReferencesBean<StopsWithArrivalsAndDeparturesV2Bean> response = entry(getStopsWithArrivalAndDepartures(result));

String agencyId = response
.getEntry()
.getArrivalsAndDepartures().size() > 0 ? AgencyAndIdLibrary.convertFromString(response.getEntry().getArrivalsAndDepartures().get(0)
.getRouteId()).getAgencyId() : "";

response.getEntry()
.getArrivalsAndDepartures()
.sort((a,b) -> customRouteSort
.compareRoutes(
a.getRouteShortName(),
b.getRouteShortName(),
customRouteSort,
agencyId));
return response;
}

public EntryWithReferencesBean<ArrivalAndDepartureV2Bean> getResponse(
Expand Down Expand Up @@ -1071,6 +1142,7 @@ public StopWithArrivalsAndDeparturesV2Bean getStopWithArrivalAndDepartures(
addToReferences(sad.getStop());

List<ArrivalAndDepartureV2Bean> ads = new ArrayList<ArrivalAndDepartureV2Bean>();

for (ArrivalAndDepartureBean ad : sad.getArrivalsAndDepartures())
ads.add(getArrivalAndDeparture(ad));
bean.setArrivalsAndDepartures(ads);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.onebusaway.api.impl.MaxCountSupport;
import org.onebusaway.api.impl.SearchBoundsFactory;
import org.onebusaway.api.model.transit.BeanFactoryV2;
import org.onebusaway.api.model.transit.EntryWithReferencesBean;
import org.onebusaway.api.model.transit.StopsWithArrivalsAndDeparturesV2Bean;
import org.onebusaway.exceptions.OutOfServiceAreaServiceException;
import org.onebusaway.exceptions.ServiceException;
import org.onebusaway.geospatial.model.CoordinateBounds;
Expand Down Expand Up @@ -53,6 +55,9 @@ public class ArrivalsAndDeparturesForLocationAction extends ApiActionSupport {
@Autowired
private ConfigurationService _configService;

@Autowired
private RouteSort customRouteSort;

@Autowired(required = false)
public void setFilterChain(FilterChain filterChain) {
_query.setSystemFilterChain(filterChain);
Expand Down Expand Up @@ -164,6 +169,7 @@ public DefaultHttpHeaders index() throws IOException, ServiceException {
if (adResult == null) {
return emptyResponse();
}
factory.setCustomRouteSort(customRouteSort);
return setOkResponse(factory.getResponse(adResult));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.onebusaway.api.actions.api.ApiActionSupport;
import org.onebusaway.api.model.transit.BeanFactoryV2;
import org.onebusaway.api.model.transit.EntryWithReferencesBean;
import org.onebusaway.api.model.transit.StopWithArrivalsAndDeparturesV2Bean;
import org.onebusaway.api.model.where.ArrivalAndDepartureBeanV1;
import org.onebusaway.api.model.where.StopWithArrivalsAndDeparturesBeanV1;
import org.onebusaway.exceptions.NoSuchStopServiceException;
Expand Down Expand Up @@ -48,6 +50,9 @@ public class ArrivalsAndDeparturesForStopAction extends ApiActionSupport {
@Autowired
private ConfigurationService _configService;

@Autowired
private RouteSort customRouteSort;

private String _id;

private ArrivalsAndDeparturesQueryBean _query = new ArrivalsAndDeparturesQueryBean();
Expand Down Expand Up @@ -90,7 +95,6 @@ public void setFilterChain(FilterChain filterChain) {
_query.setSystemFilterChain(filterChain);
}


public DefaultHttpHeaders show() throws ServiceException {
HashSet<String> agenciesExcludingScheduled = new HashSet<String>();
List<AgencyWithCoverageBean> allAgencies = _service.getAgenciesWithCoverage();
Expand Down Expand Up @@ -132,6 +136,7 @@ public DefaultHttpHeaders show() throws ServiceException {
return setOkResponse(v1);
} else if (isVersion(V2)) {
BeanFactoryV2 factory = getBeanFactoryV2();
factory.setCustomRouteSort(customRouteSort);
return setOkResponse(factory.getResponse(result));
} else {
return setUnknownVersionResponse();
Expand Down Expand Up @@ -169,6 +174,17 @@ private List<ArrivalAndDepartureBeanV1> getArrivalsAsV1(
v1s.add(v1);
}

String agencyId = v1s.size() > 0 ?
v1s.get(0).getRouteId().split("_")[0] :
"";
v1s.sort((a,b) -> customRouteSort.
compareRoutes(
a.getRouteShortName(),
b.getRouteShortName(),
customRouteSort,
agencyId)
);

return v1s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.onebusaway.api.model.transit.BeanFactoryV2;
import org.onebusaway.api.model.transit.ListWithReferencesBean;
import org.onebusaway.api.model.transit.RouteSearchResultBean;
import org.onebusaway.api.model.transit.RouteV2Bean;
import org.onebusaway.exceptions.ServiceException;
import org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean;
import org.onebusaway.transit_data.model.ListBean;
import org.onebusaway.transit_data.model.RouteBean;
import org.onebusaway.transit_data.model.RouteSort;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

Expand All @@ -31,6 +36,11 @@
*/
public class RouteAction extends ApiSearchAction {

private ArrivalsAndDeparturesQueryBean _query = new ArrivalsAndDeparturesQueryBean();

@Autowired
private RouteSort customRouteSort;

public RouteAction() {
super(V2);
}
Expand All @@ -44,6 +54,7 @@ public DefaultHttpHeaders index() throws IOException, ServiceException {
BeanFactoryV2 factory = getBeanFactoryV2();
RouteSearchResultBean result = new RouteSearchResultBean();
result.setSuggestions(routeSuggestions);
factory.setCustomRouteSort(customRouteSort);
return setOkResponse(factory.getResponse(result));
} else {
return setUnknownVersionResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.onebusaway.api.model.transit.BeanFactoryV2;
import org.onebusaway.api.model.transit.ListWithReferencesBean;
import org.onebusaway.api.model.transit.StopSearchResultBean;
import org.onebusaway.api.model.transit.StopV2Bean;
import org.onebusaway.exceptions.ServiceException;
import org.onebusaway.transit_data.model.ArrivalsAndDeparturesQueryBean;
import org.onebusaway.transit_data.model.ListBean;
import org.onebusaway.transit_data.model.RouteSort;
import org.onebusaway.transit_data.model.StopBean;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

Expand All @@ -29,12 +34,16 @@
* as used by autocomplete controls.
*/
public class StopAction extends ApiSearchAction {
private ArrivalsAndDeparturesQueryBean _query = new ArrivalsAndDeparturesQueryBean();

@Autowired
private RouteSort customRouteSort;

public StopAction() {
super(V2);
}


public DefaultHttpHeaders index() throws IOException, ServiceException {
if (isVersion(V2)) {
ListBean<StopBean> stopSuggestions = _service.getStopSuggestions(null, _input, maxCount);
Expand All @@ -44,6 +53,7 @@ public DefaultHttpHeaders index() throws IOException, ServiceException {
BeanFactoryV2 factory = getBeanFactoryV2();
StopSearchResultBean result = new StopSearchResultBean();
result.setStopSuggestions(stopSuggestions);
factory.setCustomRouteSort(customRouteSort);
return setOkResponse(factory.getResponse(result));
} else {
return setUnknownVersionResponse();
Expand Down
7 changes: 7 additions & 0 deletions onebusaway-api-webapp/src/main/resources/data-sources.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@
</props>
</property>
</bean>
<bean id="customRouteSort" class="org.onebusaway.transit_data.model.RouteSort">
<constructor-arg name="agencySortConfiguration" >
<map>
</map>
</constructor-arg>
</bean>

</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public void setRouteTypes(List<Integer> types) {
if (types == null || types.isEmpty()) return;
instanceFilterChain.add(new ArrivalAndDepartureFilterByRouteType(types));
}

public void setRouteType(String routeType) {
if (routeType == null) return;
ArrivalAndDepartureFilterByRouteType arrivalAndDepartureFilterByRouteType = new ArrivalAndDepartureFilterByRouteType(routeType);
Expand Down
Loading

0 comments on commit c9dc040

Please sign in to comment.