KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > website > actions > RollerPropertiesAction


1 /*
2  * RollerPropertiesAction.java
3  *
4  * Created on April 21, 2005, 2:48 PM
5  */

6
7 package org.roller.presentation.website.actions;
8
9 import java.io.IOException JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import javax.servlet.ServletException JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.apache.struts.action.ActionError;
18 import org.apache.struts.action.ActionErrors;
19 import org.apache.struts.action.ActionForm;
20 import org.apache.struts.action.ActionForward;
21 import org.apache.struts.action.ActionMapping;
22 import org.apache.struts.action.ActionMessage;
23 import org.apache.struts.action.ActionMessages;
24 import org.apache.struts.actions.DispatchAction;
25 import org.roller.RollerException;
26 import org.roller.RollerPermissionsException;
27 import org.roller.model.PropertiesManager;
28 import org.roller.model.Roller;
29 import org.roller.model.RollerFactory;
30 import org.roller.pojos.RollerPropertyData;
31 import org.roller.presentation.RollerRequest;
32
33
34
35 /**
36  * Struts Action class which handles requests to the Admin Properties page.
37  *
38  * @author Allen Gilliland
39  *
40  * @struts.action path="/admin/rollerConfig"
41  * scope="request" parameter="method"
42  *
43  * @struts.action-forward name="rollerProperties.page"
44  * path="/website/rollerProperties.jsp"
45  */

46 public class RollerPropertiesAction extends DispatchAction {
47     
48     private static Log mLogger =
49             LogFactory.getFactory().getInstance(RollerPropertiesAction.class);
50     
51     
52     public ActionForward unspecified(
53             ActionMapping mapping,
54             ActionForm actionForm,
55             HttpServletRequest JavaDoc request,
56             HttpServletResponse JavaDoc response)
57             throws IOException JavaDoc, ServletException JavaDoc {
58         
59         // make "edit" our default action
60
return this.edit(mapping, actionForm, request, response);
61     }
62     
63     
64     public ActionForward edit(
65             ActionMapping mapping,
66             ActionForm actionForm,
67             HttpServletRequest JavaDoc request,
68             HttpServletResponse JavaDoc response)
69             throws IOException JavaDoc, ServletException JavaDoc {
70         
71         mLogger.debug("Handling edit request");
72         
73         ActionForward forward = mapping.findForward("rollerProperties.page");
74         try {
75             RollerRequest rreq = RollerRequest.getRollerRequest(request);
76             if ( rreq.isUserAuthorizedToEdit() && rreq.isAdminUser() ) {
77                 
78                 // just grab our properties map and put it in the request
79
Roller mRoller = RollerFactory.getRoller();
80                 PropertiesManager propsManager = mRoller.getPropertiesManager();
81                 Map JavaDoc props = propsManager.getProperties();
82                 request.setAttribute("RollerProps", props);
83                 
84             } else {
85                 forward = mapping.findForward("access-denied");
86             }
87         } catch (Exception JavaDoc e) {
88             mLogger.error("ERROR in action",e);
89             throw new ServletException JavaDoc(e);
90         }
91         return forward;
92     }
93     
94     
95     public ActionForward update(
96             ActionMapping mapping,
97             ActionForm actionForm,
98             HttpServletRequest JavaDoc request,
99             HttpServletResponse JavaDoc response)
100             throws IOException JavaDoc, ServletException JavaDoc {
101         
102         mLogger.debug("Handling update request");
103         
104         ActionForward forward = mapping.findForward("rollerProperties.page");
105         ActionErrors errors = new ActionErrors();
106         try {
107             RollerRequest rreq = RollerRequest.getRollerRequest(request);
108             if ( rreq.isUserAuthorizedToEdit() && rreq.isAdminUser() ) {
109             
110                 // just grab our properties map and put it in the request
111
Roller mRoller = RollerFactory.getRoller();
112                 PropertiesManager propsManager = mRoller.getPropertiesManager();
113                 Map JavaDoc props = propsManager.getProperties();
114                 request.setAttribute("RollerProps", props);
115                 
116                 // only set values for properties that are already defined
117
String JavaDoc propName = null;
118                 RollerPropertyData updProp = null;
119                 String JavaDoc incomingProp = null;
120                 Iterator JavaDoc propsIT = props.keySet().iterator();
121                 while(propsIT.hasNext()) {
122                     propName = (String JavaDoc) propsIT.next();
123                     updProp = (RollerPropertyData) props.get(propName);
124                     incomingProp = request.getParameter(updProp.getName());
125                     
126                     mLogger.debug("Checking property ["+propName+"]");
127                     
128                     // some special treatment for booleans
129
// this is a bit hacky since we are assuming that any prop
130
// with a value of "true" or "false" is meant to be a boolean
131
// it may not always be the case, but we should be okay for now
132
if(updProp.getValue().equals("true") ||
133                             updProp.getValue().equals("false")) {
134                         
135                         if(incomingProp == null || !incomingProp.equals("on"))
136                             incomingProp = "false";
137                         else
138                             incomingProp = "true";
139                     }
140                     
141                     // only work on props that were submitted with the request
142
if(incomingProp != null) {
143                         mLogger.debug("Setting new value for ["+propName+"]");
144                         
145                         // NOTE: the old way had some locale sensitive way to do this??
146
updProp.setValue(incomingProp.trim());
147                     }
148                 }
149                 
150                 // save it
151
propsManager.store(props);
152                 mRoller.getRefererManager().applyRefererFilters();
153                 mRoller.commit();
154                 
155                 ActionMessages uiMessages = new ActionMessages();
156                 uiMessages.add(null, new ActionMessage("weblogEdit.changesSaved"));
157                 saveMessages(request, uiMessages);
158                 
159             } else {
160                 forward = mapping.findForward("access-denied");
161             }
162             
163         } catch (RollerPermissionsException e) {
164             errors.add(null, new ActionError("error.permissions.deniedSave"));
165             saveErrors(request, errors);
166             forward = mapping.findForward("access-denied");
167             
168         } catch (RollerException e) {
169             mLogger.error(e);
170             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
171                     "error.update.rollerConfig",e.getClass().getName()));
172             saveErrors(request,errors);
173         }
174         
175         return forward;
176     }
177     
178 }
179
Popular Tags