1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.mobility.push.controllers;
17
18 import java.sql.Timestamp;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import net.sf.json.JSONException;
25 import net.sf.json.JSONObject;
26 import net.sf.json.JSONSerializer;
27
28 import org.kuali.mobility.push.entity.Preference;
29 import org.kuali.mobility.push.entity.Sender;
30 import org.kuali.mobility.push.service.PreferenceService;
31 import org.kuali.mobility.push.service.SenderService;
32 import org.kuali.mobility.security.user.api.User;
33 import org.kuali.mobility.security.user.api.UserDao;
34 import org.kuali.mobility.security.user.entity.UserAttribute;
35 import org.kuali.mobility.shared.Constants;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.beans.factory.annotation.Qualifier;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.ui.Model;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RequestMethod;
44 import org.springframework.web.bind.annotation.RequestParam;
45 import org.springframework.web.bind.annotation.ResponseBody;
46
47
48
49
50
51
52
53 @Controller
54 @RequestMapping("/pushprefs")
55 public class PreferencesController {
56
57
58
59
60 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PushController.class);
61
62
63
64
65 @Autowired
66 private SenderService senderService;
67
68
69
70
71
72
73 public void setSenderService(SenderService senderService) {
74 this.senderService = senderService;
75 }
76
77
78
79
80 @Autowired
81 @Qualifier("kmeUserDao")
82 private UserDao userDao;
83
84
85
86
87
88
89 public void setUserDao(UserDao userDao) {
90 this.userDao = userDao;
91 }
92
93
94
95
96 @Autowired
97 private PreferenceService preferenceService;
98
99
100
101
102
103
104 public void setPreferenceService(PreferenceService preferenceService) {
105 this.preferenceService = preferenceService;
106 }
107
108
109
110
111
112
113
114
115
116 @RequestMapping(value = "/pushprefs", method = RequestMethod.GET)
117 public String preferences(HttpServletRequest request, Model uiModel, @RequestParam(value="key", required=false) String key) {
118 List<Sender> senders = senderService.findAllUnhiddenSenders();
119 uiModel.addAttribute("senders", senders);
120 User user = (User) request.getSession(true).getAttribute(Constants.KME_USER_KEY);
121 if(!user.isPublicUser()){
122 uiModel.addAttribute("username", user.getLoginName());
123
124 List<UserAttribute> list = user.getAttributes();
125 Iterator<UserAttribute> it = list.iterator();
126 while(it.hasNext()){
127 UserAttribute ua = (UserAttribute) it.next();
128 LOG.info(ua.getAttributeName() + "/" + ua.getAttributeValue());
129 }
130 }else{
131 uiModel.addAttribute("username", "");
132 }
133 return "push/preferences";
134 }
135
136
137
138
139
140
141
142
143
144 @RequestMapping(value = "/login", method = RequestMethod.GET)
145 public String login(HttpServletRequest request, Model uiModel, @RequestParam(value="key", required=false) String key) {
146 List<Sender> senders = senderService.findAllUnhiddenSenders();
147 uiModel.addAttribute("senders", senders);
148
149 return "redirect:/pushprefs";
150 }
151
152
153
154
155
156
157
158
159
160 @RequestMapping(value = "/get", method = RequestMethod.GET)
161 @ResponseBody
162 public String getPreferences(Model uiModel, HttpServletRequest request, @RequestParam(value="username", required=false) String username) {
163 List<Preference> preferences = preferenceService.findPreferencesByUsername(username);
164
165
166
167
168 if(preferences == null){
169 return "{\"preferences\":[]}";
170 }else{
171 return "{\"preferences\":" + new JSONSerializer().toJSON(preferences).toString() + "}";
172 }
173 }
174
175
176
177
178
179
180
181 @RequestMapping(value = "/save", method = RequestMethod.GET)
182 public ResponseEntity<String> register(@RequestParam(value="data", required=true) String data) {
183 LOG.info("-----Register-----");
184 if(data == null){
185 return new ResponseEntity<String>(HttpStatus.NO_CONTENT);
186 }
187
188 JSONObject queryParams;
189 try{
190 queryParams = (JSONObject) JSONSerializer.toJSON(data);
191 LOG.info(queryParams.toString());
192 }catch(JSONException je){
193 LOG.error("JSONException in :" + data + " : " + je.getMessage());
194 return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
195 }
196
197 Preference pref = new Preference();
198 pref.setEnabled(queryParams.getBoolean("enabled"));
199 pref.setUsername(queryParams.getString("username"));
200 pref.setPushSenderID(queryParams.getLong("senderId"));
201 pref.setPostedTimestamp(new Timestamp(System.currentTimeMillis()));
202 LOG.info("\n--- New Preference---" + pref.toString());
203
204 Preference temp = preferenceService.findPreference(pref.getUsername(), senderService.findSenderById(pref.getPushSenderID()));
205 if(temp != null){
206 LOG.info("--- Pref Exists --- UPDATE");
207 temp.setEnabled(queryParams.getBoolean("enabled"));
208 temp.setUsername(queryParams.getString("username"));
209 temp.setPushSenderID(queryParams.getLong("senderId"));
210 temp.setPostedTimestamp(new Timestamp(System.currentTimeMillis()));
211
212 if(!temp.isEnabled()){
213 preferenceService.savePreference(temp);
214 }else{
215 preferenceService.removePreference(temp);
216 }
217 }else{
218 LOG.info("--- Pref Doesn't Exist --- ADD");
219 if(!pref.isEnabled()){
220 preferenceService.savePreference(pref);
221 }
222 }
223
224 return new ResponseEntity<String>(HttpStatus.OK);
225 }
226
227
228
229
230
231
232 @RequestMapping(value = "/ping", method = RequestMethod.GET)
233 public ResponseEntity<String> ping() {
234 return new ResponseEntity<String>(HttpStatus.OK);
235 }
236
237 }