View Javadoc
1   /**
2    * Copyright 2011-2014 The Kuali Foundation Licensed under the Educational
3    * Community License, Version 2.0 (the "License"); you may not use this file
4    * except in compliance with the License. You may obtain a copy of the License
5    * at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12   * License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  package org.kuali.mobility.bus.service;
16  
17  import org.apache.commons.collections.CollectionUtils;
18  import org.apache.commons.lang.StringUtils;
19  import org.slf4j.Logger;
20  import org.slf4j.LoggerFactory;
21  import org.kuali.mobility.bus.dao.BusDao;
22  import org.kuali.mobility.bus.entity.*;
23  import org.kuali.mobility.bus.service.util.BusStopDistanceUtil;
24  import org.kuali.mobility.bus.util.ScheduledStopComparitor;
25  import org.springframework.context.ApplicationContext;
26  import org.springframework.context.ApplicationContextAware;
27  
28  import javax.annotation.Resource;
29  import javax.ws.rs.GET;
30  import javax.ws.rs.Path;
31  import javax.ws.rs.PathParam;
32  import javax.ws.rs.QueryParam;
33  import java.util.*;
34  
35  /**
36   *
37   * @author Kuali Mobility Team <mobility.collab@kuali.org>
38   */
39  public class BusServiceImpl implements BusService, ApplicationContextAware {
40  
41      private static Logger LOG = LoggerFactory.getLogger(BusServiceImpl.class);
42      private ApplicationContext applicationContext;
43      @Resource(name="busDao")
44      private BusDao dao;
45  
46  	@GET
47      @Path("/route/lookup/{campus}")
48  	@Override
49  	public List<BusRoute> getRoutes(@QueryParam( value="campus" ) String campus) {
50  		List<BusRoute> routes = getDao().getBusRoutes();
51  		if (CollectionUtils.isEmpty(routes)) {
52  			getDao().loadRoutes();
53  			routes = getDao().getBusRoutes();
54  		}
55          return routes;
56  	}
57  
58  	@GET
59      @Path("/route/search/{campus}")
60  	@Override
61  	public BusRoute getRoute(@QueryParam( value="campus" ) String campus, @QueryParam( value="routeId" ) long id) {
62  		BusRoute route = null;
63  		List<BusRoute> routes = getDao().getBusRoutes();
64  		if (CollectionUtils.isEmpty(routes)) {
65  			getDao().loadRoutes();
66  			routes = getDao().getBusRoutes();
67  		}
68  		for (BusRoute r : routes) {
69  			if (r.getId() == id) {
70  				route = r;
71  				break;
72  			}
73  		}
74  		return route;
75  	}
76  
77  	@GET
78      @Path("/stop/lookup/{campus}")
79  	@Override
80  	public List<BusStop> getStops(@QueryParam( value="campus" ) String campus) {
81  		List<BusStop> stops = getDao().getBusStops();
82  		if (CollectionUtils.isEmpty(stops)) {
83  			getDao().loadRoutes();
84  			stops = getDao().getBusStops();
85  		}
86          return stops;
87  	}
88  	
89  	@GET
90  	@Path("/stop/routestopdistances")
91  	@Override
92  	public List<BusRoute> getRoutesWithDistance (
93  			@QueryParam( value="latitude" ) double lat1, 
94  			@QueryParam( value="longitude" ) double lon1, 
95  			@QueryParam( value="radius" ) double radius) {
96  		BusStopDistanceUtil bsutil = new BusStopDistanceUtil();
97  		// adds same stops but for different routes
98  		List<BusRoute> routesImpl = getRoutes("");
99  		for (BusRoute r : routesImpl) {
100 			for (BusStop s : r.getStops()) {
101 				double dist = bsutil.calculateDistance(s, lat1, lon1);
102 				s.setDistance(dist);
103 			}
104 		}
105 		return routesImpl;
106 	}
107 
108 	@GET
109     @Path("/stop/nearby")
110 	@Override
111 	public List<BusStop> getNearbyStops(
112 			@QueryParam( value="latitude" ) double lat1, 
113 			@QueryParam( value="longitude" ) double lon1, 
114 			@QueryParam( value="radius" ) double radius) {
115         List<BusStop> stops = new ArrayList<BusStop>();
116         Map<Double, BusStop> mapaTemp = new TreeMap<Double, BusStop>();
117         double dist = 0.0;
118         List<BusStop> busStops = getDao().getBusStops();
119         if (CollectionUtils.isEmpty(busStops)) {
120             getDao().loadRoutes();
121             busStops = getDao().getBusStops();
122         }
123         BusStopDistanceUtil bsutil = new BusStopDistanceUtil();
124         for (BusStop s : busStops) {
125             dist = bsutil.calculateDistance(s, lat1, lon1);
126             if (dist <= radius) {
127                 //distance in km
128                 mapaTemp.put(dist, s);
129             }
130         }
131         for (Iterator<Double> it = mapaTemp.keySet().iterator(); it.hasNext();) {
132             Double key = (Double) it.next();
133             BusStop stop = mapaTemp.get(key);
134             stop.setDistance(key);
135             stops.add(stop);
136         }
137 		return stops;
138     }
139 
140 	@GET
141     @Path("/stop/lookupbyid/{campus}")
142 	@Override
143 	public BusStop getStop(@QueryParam( value="campus" ) String campus, @QueryParam( value="stopId" ) long id) {
144 		BusStop stop = null;
145 		List<BusStop> stops = getStops("");
146 		
147 		for (BusStop s : stops) {
148 			if (s.getId() == id) {
149 				stop = s;
150 				break;
151 			}
152 		}
153 		return stop;
154 	}
155 
156 	@GET
157     @Path("/stop/lookupbyname/{campus}")
158     @Override
159     public BusStop getStopByName(
160             @QueryParam(value = "name") final String stopName,
161             @QueryParam(value = "campus") final String campus) {
162         BusStop busStop = null;
163         for (BusStop s : getStops("")) {
164             if (s.getName() != null
165                 && s.getName().equalsIgnoreCase(stopName)) {
166                 busStop = s;
167                 break;
168             }
169         }
170         LOG.debug("Bus stop found for "+stopName+" with "+(busStop.getScheduledStop()==null?"NULL":busStop.getScheduledStop().size())+" stops.");
171         return busStop;
172     }
173 
174     @SuppressWarnings("unchecked")
175 	@GET
176     @Path("/stop/schedule/{campus}")
177     @Override
178     public List<ScheduledStop> getArrivalData(
179             @QueryParam(value = "routeId") final String routeId,
180             @QueryParam(value = "stopId") final String stopId,
181             @QueryParam(value = "campus") final String campus) {
182 
183         List<ScheduledStop> arrivals;
184         BusStop stop = null;
185 
186         if (StringUtils.isNotBlank(routeId)) {
187             BusRoute route = getRoute(campus, Long.parseLong(routeId));
188 			if( null != route && null != route.getStops() && !route.getStops().isEmpty() ) {
189 	            for (BusStop s : route.getStops()) {
190 	                LOG.debug(route.getName() + " routeid: " + route.getId() + " stopid: " + s.getId() + " stop name " + s.getName());
191 	                if (s.getId() == Long.parseLong(stopId)) {
192 	                    stop = s;
193 	                    break;
194 	                }
195 	            }
196 			}
197         } else {
198             stop = getStop(campus, Long.parseLong(stopId));
199             LOG.debug("Unable to find route.");
200         }
201 
202         if (null != stop) {
203             arrivals = (List<ScheduledStop>) (List<?>) stop.getScheduledStop();
204         } else {
205             arrivals = new ArrayList<ScheduledStop>();
206         }
207         Collections.sort(arrivals, new ScheduledStopComparitor());
208         return arrivals;
209     }
210 
211     @GET
212     @Path("/locations")
213     @Override
214     public List<Bus> getAllBuses(@QueryParam("campus") String campus) {
215         if( null == getDao().getBuses() || getDao().getBuses().isEmpty() ) {
216             getDao().loadBusLocations();
217         }
218         return getDao().getBuses();
219     }
220 
221     @GET
222     @Path("/stops/{stopId}")
223     @Override
224     public BusStop getStopById(@PathParam("stopId") int stopId){
225         List<BusStop> busStops = (List<BusStop>) this.getStops("");
226         BusStop foundStop = null;
227         for (BusStop stop : busStops) {
228             if (stopId == stop.getId()) {
229                 foundStop = stop;
230                 break;
231             }
232         }
233         return foundStop;
234     }
235 
236     @GET
237     @Path("/alerts")
238     @Override
239     public List<BusAlert> getAlerts() {
240         return getDao().getBusAlerts();
241     }
242 
243     public void setDao(BusDao dao) {
244         this.dao = dao;
245     }
246 
247     public BusDao getDao() {
248         return dao;
249     }
250 
251     /**
252      * @return the applicationContext
253      */
254     public ApplicationContext getApplicationContext() {
255         return applicationContext;
256     }
257 
258     /**
259      * @param applicationContext the applicationContext to set
260      */
261     public void setApplicationContext(ApplicationContext applicationContext) {
262         this.applicationContext = applicationContext;
263     }
264 }