View Javadoc
1   /**
2    * Copyright 2011-2014 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  package org.kuali.mobility.dining.service;
16  
17  import org.apache.commons.collections.CollectionUtils;
18  import org.kuali.mobility.dining.dao.DiningDao;
19  import org.kuali.mobility.dining.entity.DiningHall;
20  import org.kuali.mobility.dining.util.DiningHallBuildingPredicate;
21  import org.kuali.mobility.dining.util.DiningHallCampusPredicate;
22  import org.kuali.mobility.dining.util.DiningHallComparitor;
23  import org.kuali.mobility.dining.util.DiningHallNamePredicate;
24  import org.kuali.mobility.shared.entity.ErrorMessage;
25  
26  import javax.annotation.Resource;
27  import javax.jws.WebService;
28  import javax.ws.rs.GET;
29  import javax.ws.rs.Path;
30  import javax.ws.rs.QueryParam;
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.List;
35  
36  /**
37   * @author Kuali Mobility Team (mobility.dev@kuali.org)
38   */
39  @WebService(endpointInterface = "org.kuali.mobility.dining.service.DiningService")
40  public class DiningServiceImpl implements DiningService {
41  
42  	private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DiningServiceImpl.class);
43  
44      @Resource(name="diningDao")
45  	private DiningDao dao;
46  
47      @GET
48      @Path("/diningHalls")
49      @Override
50      public List<DiningHall> getDiningHalls() {
51          List<DiningHall> diningHalls = new ArrayList<DiningHall>();
52          try {
53              diningHalls = getDao().getDiningHalls();
54          } catch( Exception e ) {
55              DiningHall diningHall = new DiningHall();
56              ErrorMessage error = new ErrorMessage();
57              error.setName("No Dining Halls Found");
58              error.setDescription("An error occurred while retrieving dining halls.");
59              List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
60              diningHall.setErrors(errors);
61              diningHalls.add(diningHall);
62          }
63          Collections.sort( diningHalls, new DiningHallComparitor() );
64          return diningHalls;
65      }
66  
67      @GET
68      @Path("/diningHallsByCampus")
69      @Override
70      public List<DiningHall> getDiningHallsByCampus(@QueryParam(value="campus") String campus) {
71          List<DiningHall> diningHalls = new ArrayList<DiningHall>();
72          try {
73              CollectionUtils.select(getDao().getDiningHalls(), new DiningHallCampusPredicate(campus), diningHalls);
74          } catch( Exception e ) {
75              DiningHall diningHall = new DiningHall();
76              ErrorMessage error = new ErrorMessage();
77              error.setName("No Dining Halls Found");
78              error.setDescription("No dining hall found for "+campus+".");
79              List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
80              diningHall.setErrors(errors);
81              diningHalls.add(diningHall);
82          }
83          Collections.sort( diningHalls, new DiningHallComparitor() );
84          return diningHalls;
85      }
86  
87      @GET
88      @Path("/diningHallsByBuilding")
89      @Override
90      public List<DiningHall> getDiningHallsByBuilding(@QueryParam(value="buildingName") String buildingName) {
91          List<DiningHall> diningHalls = new ArrayList<DiningHall>();
92          try {
93              CollectionUtils.select(getDao().getDiningHalls(), new DiningHallBuildingPredicate(buildingName), diningHalls);
94          } catch( Exception e ) {
95              DiningHall diningHall = new DiningHall();
96              ErrorMessage error = new ErrorMessage();
97              error.setName("No Dining Halls Found");
98              error.setDescription("No dining hall found for "+buildingName+".");
99              List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
100             diningHall.setErrors(errors);
101             diningHalls.add(diningHall);
102         }
103         Collections.sort( diningHalls, new DiningHallComparitor() );
104         return diningHalls;
105     }
106 
107     @GET
108     @Path("/diningHallsNearby")
109     @Override
110     public List<DiningHall> getDiningHallsNearby(
111             @QueryParam(value="latitude") String latitude,
112             @QueryParam(value="longitude") String longitude,
113             @QueryParam(value="radius") int radius,
114             @QueryParam(value="unit") String unit) {
115         List<DiningHall> diningHalls = new ArrayList<DiningHall>();
116 
117         DiningHall diningHall = new DiningHall();
118         ErrorMessage error = new ErrorMessage();
119         error.setName("Not Yet Implemented");
120         error.setDescription("Searching for dining halls by your location is not yet implemented.");
121         List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
122         diningHall.setErrors(errors);
123         diningHalls.add(diningHall);
124 
125         return diningHalls;
126     }
127 
128     @GET
129     @Path("/diningHall")
130     @Override
131 	public DiningHall getDiningHall(@QueryParam(value = "name") String name) {
132         DiningHall diningHall = null;
133         Collection <? extends DiningHall> diningHalls = CollectionUtils.select(getDao().getDiningHalls(), new DiningHallNamePredicate(name));
134         if( diningHalls.size() > 1 ) {
135             ErrorMessage error = new ErrorMessage();
136             error.setName("Ambiguous request");
137             error.setDescription("Multiple dining halls found for request.");
138             diningHall = new DiningHall();
139             List<ErrorMessage> errors = new ArrayList<ErrorMessage>();
140             diningHall.setErrors(errors);
141         } else {
142             diningHall = (DiningHall)((diningHalls.toArray())[0]);
143         }
144         return diningHall;
145 	}
146 
147     public DiningDao getDao() {
148         return dao;
149     }
150 
151     public void setDao(DiningDao dao) {
152         this.dao = dao;
153     }
154 }
155 
156