1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.academics.service;
16
17 import org.apache.cxf.jaxrs.ext.MessageContext;
18 import org.apache.cxf.phase.PhaseInterceptorChain;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.kuali.mobility.academics.dao.AcademicsDao;
22 import org.kuali.mobility.academics.entity.Section;
23 import org.kuali.mobility.academics.entity.Term;
24 import org.kuali.mobility.academics.util.AcademicsConstants;
25 import org.kuali.mobility.push.dao.DeviceDao;
26 import org.kuali.mobility.push.entity.Device;
27 import org.kuali.mobility.push.entity.Push;
28 import org.kuali.mobility.push.service.PushService;
29 import org.kuali.mobility.security.authn.util.AuthenticationConstants;
30 import org.kuali.mobility.security.user.api.User;
31 import org.kuali.mobility.security.user.api.UserDao;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Qualifier;
34 import org.springframework.context.ApplicationContext;
35 import org.springframework.context.ApplicationContextAware;
36 import org.springframework.stereotype.Service;
37
38 import javax.annotation.Resource;
39 import javax.jws.WebService;
40 import javax.servlet.http.HttpServletRequest;
41 import javax.servlet.http.HttpSession;
42 import javax.ws.rs.GET;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.QueryParam;
45 import javax.ws.rs.core.Context;
46 import java.sql.Timestamp;
47 import java.util.*;
48
49
50
51
52
53 @Service
54 @WebService(endpointInterface = "org.kuali.mobility.academics.service.AcademicsAuthService")
55 public class AcademicsAuthServiceImpl implements AcademicsAuthService, ApplicationContextAware {
56 private static final Logger LOG = LoggerFactory.getLogger(AcademicsAuthServiceImpl.class);
57
58 @Resource(name="academicsDao")
59 private AcademicsDao dao;
60
61 @Resource(name="kmeUserDao")
62 private UserDao userDao;
63
64 @Resource(name="deviceDao")
65 private DeviceDao deviceDao;
66
67 private ApplicationContext applicationContext;
68
69 @Context
70 private MessageContext messageContext;
71
72 @Autowired
73 private PushService pushService;
74
75 @Autowired
76 @Qualifier("academicsProperties")
77 private Properties academicsProperties;
78
79 @Override
80 @GET
81 @Path("/getTerms")
82 public List<Term> getTerms() {
83 LOG.debug("Entering getTerms.");
84
85 HttpServletRequest request;
86 if( getMessageContext() != null ) {
87 request = (HttpServletRequest) getMessageContext().getHttpServletRequest();
88 } else {
89 request = (HttpServletRequest)PhaseInterceptorChain.getCurrentMessage().get("HTTP.REQUEST");
90 }
91 return getTerms( request );
92 }
93
94 public List<Term> getTerms(HttpServletRequest request) {
95 LOG.debug( "Entering getTerms." );
96 List<Term> terms = new ArrayList<Term>();
97 Map<String, String> query = new HashMap<String, String>();
98
99 String remoteUser = null;
100 if (null != request) {
101 remoteUser = request.getRemoteUser();
102 }
103 else {
104 LOG.error( "HttpServletRequest is null, not able to get the RemoteUser !!!");
105 }
106
107 query.put(AcademicsConstants.USER_ID, remoteUser);
108
109 terms = getDao().getTerms(query);
110 return terms;
111 }
112
113 @Override
114 @GET
115 @Path("/getClassSchedule")
116 public List<Section> getClassSchedule(
117 @QueryParam("termId") final String termId,
118 @QueryParam("careerId") final String careerId) {
119
120 HttpServletRequest request;
121 if( getMessageContext() != null ) {
122 request = (HttpServletRequest) getMessageContext().getHttpServletRequest();
123 } else {
124 request = (HttpServletRequest)PhaseInterceptorChain.getCurrentMessage().get("HTTP.REQUEST");
125 }
126 return getClassSchedule(request, termId, careerId);
127 }
128
129 public List<Section> getClassSchedule(
130 HttpServletRequest request,
131 @QueryParam("termId") final String termId,
132 @QueryParam("careerId") final String careerId) {
133 List<Section> sections = new ArrayList<Section>();
134
135 Map<String, String> query = new HashMap<String, String>();
136 query.put(AcademicsConstants.CAREER_ID, careerId);
137 query.put(AcademicsConstants.TERM_ID, termId);
138
139 String remoteUser = null;
140 if (null != request) {
141 remoteUser = request.getRemoteUser();
142 }
143 else {
144 LOG.error( "HttpServletRequest is null, not able to get the RemoteUser !!!");
145 }
146
147 query.put(AcademicsConstants.USER_ID, remoteUser);
148
149 sections = getDao().getMyClassSchedule(query);
150
151 return sections;
152 }
153
154 @Override
155 @Path("updateGradeAlertOpt")
156 @GET
157 public boolean updateGradeAlertOpt(@QueryParam("opt") final String opt) {
158 boolean updateResult = false;
159
160 HttpServletRequest request;
161 if( getMessageContext() != null ) {
162 request = (HttpServletRequest) getMessageContext().getHttpServletRequest();
163 } else {
164 request = (HttpServletRequest)PhaseInterceptorChain.getCurrentMessage().get("HTTP.REQUEST");
165 }
166 if (null == request || null == request.getSession()) {
167 LOG.error("request==null, quit!");
168 } else {
169 HttpSession session = request.getSession();
170 User user = (User)session.getAttribute(AuthenticationConstants.KME_USER_KEY);
171 String remoteUser = request.getRemoteUser();
172 LOG.info("request.getRemoteUser() = " + request.getRemoteUser());
173 if( user == null ) {
174 LOG.error("User is null in session. This should NEVER occur!");
175
176 if (remoteUser == null || remoteUser.isEmpty()) {
177 LOG.error("====== Can't get remote user, quit!");
178 } else {
179 user = getUserDao().loadUserByLoginName(remoteUser);
180 }
181 }
182 else if ( user.isPublicUser() ) {
183 LOG.error(user.getLoginName() + "====== Public user found when opting in. This should never happen ======");
184 LOG.error("Mismatch to RemoteUser[" + remoteUser + "], reset to RemoteUser");
185 user.setLoginName(remoteUser);
186 }
187
188 if( user != null ) {
189 LOG.debug("====== User exists, attempting to update grade alert with "+opt);
190 updateResult = user.removeAttribute(AcademicsConstants.USER_ATTR_GRADEALERT);
191 LOG.debug("====== removeAttribute on grade alerts returned "+updateResult);
192 user.addAttribute(AcademicsConstants.USER_ATTR_GRADEALERT, opt);
193
194 if( user.isPublicUser() ) {
195 LOG.error("====== Public user found when opting in. This should never happen ======");
196 } else {
197 if (null != getUserDao().saveUser(user)) {
198 updateResult = true;
199 }
200 }
201 if( session != null ) {
202 String deviceId = (String)session.getAttribute(AuthenticationConstants.DEVICE_ID);
203 if( null == deviceId ) {
204 LOG.debug("====== No device id stored in session. ======");
205 } else {
206 Device device = getDeviceDao().findDeviceByDeviceId(deviceId);
207 if( device == null ) {
208 LOG.error("====== Unable to find device for id in session ======");
209 } else {
210 device.setUsername(user.getLoginName());
211 getDeviceDao().saveDevice(device);
212 LOG.debug("Updating device "+deviceId+" to belong to "+user.getLoginName());
213 }
214 }
215 }
216 }
217 }
218 return updateResult;
219 }
220
221 @Override
222 @Path("testGradeAlert")
223 @GET
224 public String testGradeAlert () {
225 String result = "0";
226 HttpServletRequest request;
227 if( getMessageContext() != null ) {
228 request = (HttpServletRequest) getMessageContext().getHttpServletRequest();
229 } else {
230 request = (HttpServletRequest)PhaseInterceptorChain.getCurrentMessage().get("HTTP.REQUEST");
231 }
232
233 if (null == request || null == request.getSession()) {
234 LOG.error("request==null, quit!");
235 } else {
236 HttpSession session = request.getSession();
237 if( session != null ) {
238 String deviceId = (String)session.getAttribute(AuthenticationConstants.DEVICE_ID);
239 LOG.info("device id stored in session is " + deviceId);
240
241 List<Device> devices = getDeviceDao().findDevicesByUsername(request.getRemoteUser());
242 LOG.info(devices.size() + " devices in list");
243
244 if ( devices==null || devices.isEmpty() ) {
245 result = "1";
246 }
247 else {
248 Push push = new Push();
249 push.setTitle("[TEST] " + getAcademicsProperties().getProperty("academics.grade.alert.push.title"));
250 push.setMessage("[TEST] " + getAcademicsProperties().getProperty("academics.grade.alert.push.message"));
251 push.setPostedTimestamp(new Timestamp(Calendar.getInstance().getTimeInMillis()));
252 push.setEmergency(true);
253 push.setSender("RS5XcyVYoHSgnLVY2ZZw");
254 push.setUrl(null);
255
256 pushService.savePush(push, devices);
257 int pushed = pushService.sendPush(push,devices);
258 push.setRecipients(pushed);
259 pushService.savePush(push);
260 LOG.info("Post Push:" + push.toString());
261 result = "2";
262 }
263
264 }
265 }
266 return result;
267 }
268
269 public UserDao getUserDao() {
270 return userDao;
271 }
272
273 public void setUserDao(UserDao userDao) {
274 this.userDao = userDao;
275 }
276
277
278
279
280
281
282 public AcademicsDao getDao() {
283 return dao;
284 }
285
286
287
288
289 public void setDao(AcademicsDao dao) {
290 this.dao = dao;
291 }
292
293
294
295
296 public ApplicationContext getApplicationContext() {
297 return applicationContext;
298 }
299
300
301
302
303 public void setApplicationContext(ApplicationContext context) {
304 this.applicationContext = context;
305 }
306
307
308
309
310 public MessageContext getMessageContext() {
311 return messageContext;
312 }
313
314
315
316
317 public void setMessageContext(MessageContext messageContext) {
318 this.messageContext = messageContext;
319 }
320
321 public DeviceDao getDeviceDao() {
322 return deviceDao;
323 }
324
325 public void setDeviceDao(DeviceDao deviceDao) {
326 this.deviceDao = deviceDao;
327 }
328
329 public Properties getAcademicsProperties() {
330 return academicsProperties;
331 }
332
333 public void setAcademicsProperties(Properties academicsProperties) {
334 this.academicsProperties = academicsProperties;
335 }
336
337 public PushService getPushService() {
338 return pushService;
339 }
340
341 public void setPushService(PushService pushService) {
342 this.pushService = pushService;
343 }
344
345 }