View Javadoc

1   /**
2    * Copyright 2011 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  
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   * Controller for managing preferences.
49   * 
50   * @author Kuali Mobility Team (mobility.dev@kuali.org)
51   * @since 2.0.0
52   */
53  @Controller 
54  @RequestMapping("/pushprefs")
55  public class PreferencesController {
56  
57  	/**
58  	 * A reference to the Logger. 
59  	 */
60  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PushController.class);		
61  	
62  	/**
63  	 * A reference to the SenderService
64  	 */
65      @Autowired
66      private SenderService senderService;
67  
68      /**
69       * A method for setting the <code>SenderService<code> for the Controller object. 
70       * 
71       * @param senderService
72       */
73      public void setSenderService(SenderService senderService) {
74          this.senderService = senderService;
75      }
76  
77      /**
78       * A reference to the UserDao
79       */
80      @Autowired 
81  	@Qualifier("kmeUserDao")
82      private UserDao userDao;
83  
84      /**
85       * A method for setting the <code>UserDao</code> for the controller object. 
86       * 
87       * @param userDao
88       */
89      public void setUserDao(UserDao userDao) {
90  		this.userDao = userDao;
91  	}
92  
93      /**
94       * A reference to the PreferenceService 
95       */
96  	@Autowired
97      private PreferenceService preferenceService;
98  
99  	/**
100 	 * A method for setting the <code>PreferenceService</code> for the controller object. 
101 	 * 
102 	 * @param preferenceService
103 	 */
104 	public void setPreferenceService(PreferenceService preferenceService) {
105         this.preferenceService = preferenceService;
106     }    
107     
108 	/**
109 	 * A controller method for accessing a displaying all the push preferences for a given user. 
110 	 * 
111 	 * @param request
112 	 * @param uiModel
113 	 * @param key
114 	 * @return
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      * A controller method for redirecting the user to a login page.  
138      * 
139      * @param request
140      * @param uiModel
141      * @param key
142      * @return
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 //    	return "push/preferences";
149     	return "redirect:/pushprefs";
150     }  
151     
152     /**
153      * A controller method for getting the preferences for a given username and returning to the accessing page via a JSON formatted string. 
154      * 
155      * @param uiModel
156      * @param request
157      * @param username Username of the preferences requested. 
158      * @return String A JSON formatted string containing the preferences for a given username.
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 //		User currentUser = userDao.loadUserByLoginName(username);
165 //		LOG.info(currentUser);
166 		
167 		
168 		if(preferences == null){
169 			return "{\"preferences\":[]}";			
170 		}else{
171 			return "{\"preferences\":" + new JSONSerializer().toJSON(preferences).toString() + "}";
172 		}
173 	}  
174     
175     /**
176      * A controller method for saving prefernces for a given username after they have been changed by the user. 
177      * 
178      * @param data A JSON formatted string containing the end configuration of the preference values for a given user. 
179      * @return
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 	 * Simple controller which responds with 200.
229 	 * This controller can be used by devices to check if the server is online
230 	 * @return ResponseEntity<String>
231 	 */
232 	@RequestMapping(value = "/ping", method = RequestMethod.GET)
233 	public ResponseEntity<String> ping() {
234 		return new ResponseEntity<String>(HttpStatus.OK);
235 	}	
236     
237 }