View Javadoc
1   /******************************************************************************
2    * WebEditController.java - The Spring controller for the naked AJAX calls
3    * 
4    * PicMan - The BuckoSoft Picture Manager in Java
5    * Copyright(c) 2008 - Dick Balaska
6    * 
7    */
8   package com.buckosoft.PicMan.web;
9   
10  import java.io.IOException;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.springframework.web.servlet.ModelAndView;
18  import org.springframework.web.servlet.mvc.Controller;
19  
20  import com.buckosoft.BSAccount.web.BSAccountPageController;
21  import com.buckosoft.BSAccount.web.BSAccountUserWebSession;
22  import com.buckosoft.PicMan.business.PicManFacade;
23  import com.buckosoft.PicMan.domain.User;
24  
25  /**
26   * The Spring controller for the naked AJAX calls. <br>
27   * This controller just returns a status. Typically, 200 for OK or 5xx for an error.
28   * @author Dick Balaska
29   * @since 2008/05/28
30   * @see <a href="http://cvs.buckosoft.com/Projects/java/PicMan/PicMan/src/com/buckosoft/PicMan/web/WebEditController.java">WebEditController.java</a>
31   */
32  public class WebEditController  extends BSAccountPageController implements Controller {
33  	private final static boolean DEBUG = false;
34  	protected final Log logger = LogFactory.getLog(getClass());
35  
36  	private PicManFacade pmf;
37  	/** Set the PicManFacade reference
38  	 * @param pmf The PicManFacade API
39  	 */
40  	public void setPicMan(PicManFacade pmf) { this.pmf = pmf; }
41  
42  
43  	/** Spring standard http request handler.
44  	 * @param request The http request.
45  	 * @param response The http response.
46  	 * @return null to return just an http status because we pushed the DOM out the response.
47  	 */ 
48  	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
49  		String cid = request.getParameter("cid");
50  		String c = request.getParameter("c");
51  		String command = request.getParameter("command");
52  		if (DEBUG) {
53  			if (!command.equals("PicManStatus"))
54  				logger.info("XML Request com=" + command + " c=" + c + " cid=" + cid);
55  		}
56  		response.setContentType("text/plain");
57  		response.addHeader("Cache-Control", "max-age=0");
58  		response.addHeader("Cache-Control", "no-cache");
59  		response.addHeader("expires", "0");
60  		response.addHeader("Expires", "Tue, 01 Jan 1980 1:00:00 GMT");
61  		response.addHeader("Pragma", "no-cache");
62  
63  //		User user = null;
64  //		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
65  //		if (userWebSession != null) 
66  //			user = (User)userWebSession.getUser();
67  
68  
69  		try {
70  			if (command == null) {
71  				response.sendError(550, "No command in request");
72  				return(null);
73  			}
74  			else if (command.equals("DeleteMosaicTiles"))
75  				pmf.getDB().deleteMosaicTiles(Integer.parseInt(c));
76  			else if (command.equals("DeleteVirgin"))
77  				pmf.getDB().deleteVirgin(c);
78  			else if (command.equals("UpdateOptions")) {
79  				if (!updateOptions(request, response))
80  					response.sendError(251, "Failed to update Options");
81  					
82  			} else {
83  				response.sendError(551, "Unknown command '" + command + "' in request");
84  			}
85  		} catch (NumberFormatException e) {
86  			logger.info(e);
87  			pmf.addError(e);
88  		} catch (IOException e) {
89  			logger.info(e);
90  			pmf.addError(e);
91  		}
92  
93  		return null;
94  	}
95  
96  	private boolean updateOptions(HttpServletRequest request, HttpServletResponse response) {
97  		User user = null;
98  		BSAccountUserWebSession userWebSession = this.bsAccountMan.getUserWebSession(request);
99  		if (userWebSession != null) 
100 			user = (User)userWebSession.getUser();
101 		if (user == null) {
102 			logger.warn("Can't update options for unknown user");
103 			return(false);
104 		}
105 		String showFileDates = request.getHeader("showFileDates");
106 		String showChangedFilters = request.getHeader("showChangedFilters");
107 		if (showFileDates == null || showChangedFilters == null)
108 			return(false);
109 		user.setEditorShowFileDates(showFileDates.equals("1") ? true : false);
110 		user.setEditorShowChangedFilters(showChangedFilters.equals("1") ? true : false);
111 		pmf.getDB().storeUser(user);
112 		return(true);
113 	}
114 
115 }