KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > mysettings > MySettingsEngine


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Developer of the Shared Modifications is Jahia Solution Sarl.
30  * Portions created by the Initial Developer are Copyright (C) 2002 by the
31  * Initial Developer. All Rights Reserved.
32  *
33  * Contributor(s):
34  * 21 nov. 2003, Jahia Solutions Sarl, Fulco Houkes : Initial version
35  *
36  * ----- END LICENSE BLOCK -----
37  */

38
39 package org.jahia.engines.mysettings;
40
41
42 import org.jahia.data.JahiaData;
43 import org.jahia.engines.EngineMessage;
44 import org.jahia.engines.EngineMessages;
45 import org.jahia.engines.EngineToolBox;
46 import org.jahia.engines.JahiaEngine;
47 import org.jahia.engines.core.Core_Engine;
48 import org.jahia.exceptions.JahiaException;
49 import org.jahia.exceptions.JahiaSessionExpirationException;
50 import org.jahia.params.ParamBean;
51 import org.jahia.registries.ServicesRegistry;
52 import org.jahia.services.usermanager.JahiaUser;
53 import org.jahia.services.usermanager.JahiaUserManagerService;
54 import org.jahia.services.usermanager.UserProperties;
55 import org.jahia.services.usermanager.UserProperty;
56
57 import java.util.Enumeration JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Iterator JavaDoc;
60
61
62 /**
63  * @author Fulco Houkes
64  * @version 1.0
65  * @since Jahia 4.0.2
66  */

67 public class MySettingsEngine implements JahiaEngine {
68
69     public static final String JavaDoc REQUEST_KEY_PREFIX = "mysettings-user-";
70     public static final String JavaDoc REQUEST_PASSWORD_KEY = REQUEST_KEY_PREFIX + "password";
71     public static final String JavaDoc REQUEST_PASSWORD_CONFIRMATION_KEY =
72             REQUEST_KEY_PREFIX + "passwordConfirmation";
73
74     public static final String JavaDoc SEPARATOR = "#";
75     public static final String JavaDoc USER_PROPERTY_PREFIX = REQUEST_KEY_PREFIX + "property" + SEPARATOR;
76     public static final String JavaDoc EDIT_TOKEN = "edit";
77     public static final String JavaDoc SAVE_TOKEN = "save";
78
79     /**
80      * The engine's name
81      */

82     public static final String JavaDoc ENGINE_NAME = "mysettings";
83
84     private static final String JavaDoc EDIT_JSP = "mysettings.jsp";
85     private static final String JavaDoc SUCCESS_JSP = "mysettingschanged.jsp";
86
87     /**
88      * logging
89      */

90     private static final org.apache.log4j.Logger logger =
91             org.apache.log4j.Logger.getLogger (MySettingsEngine.class);
92
93     /**
94      * The engine's unique instance.
95      */

96     private static MySettingsEngine instance;
97     private EngineToolBox toolbox;
98
99
100     /**
101      * Default constructor, creates a new <code>MySettingsEngine</code> object instance.
102      */

103     public MySettingsEngine () {
104         logger.debug ("***** Starting " + MySettingsEngine.class.getName () + " engine *****");
105         toolbox = EngineToolBox.getInstance ();
106     }
107
108
109     /**
110      * Retrieve the unique instance of this engine.
111      *
112      * @return the unique instance of this engine.
113      */

114     public static synchronized MySettingsEngine getInstance () {
115         if (instance == null)
116             instance = new MySettingsEngine ();
117         return instance;
118     }
119
120
121     public boolean authoriseRender (ParamBean jParams) {
122         return true;
123     }
124
125
126     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj) throws JahiaException {
127         String JavaDoc theUrl = jParams.composeEngineUrl (ENGINE_NAME, EMPTY_STRING);
128         if (theObj != null)
129             theUrl += theObj;
130         return jParams.getResponse ().encodeURL (theUrl);
131     }
132
133
134     public boolean needsJahiaData (ParamBean jParams) {
135         return true;
136     }
137
138
139     public void handleActions (ParamBean jParams, JahiaData jData)
140             throws JahiaException {
141
142         // initalizes the hashmap
143
HashMap JavaDoc engineMap = new HashMap JavaDoc ();
144         initEngineMap (jParams, engineMap);
145
146         processScreen (jParams, engineMap);
147
148         // displays the screen
149
toolbox.displayScreen (jParams, engineMap);
150
151     }
152
153
154     public final String JavaDoc getName () {
155         return ENGINE_NAME;
156     }
157
158
159     /**
160      * inits the engine map
161      *
162      * @param jParams a ParamBean object (with request and response)
163      */

164     private void initEngineMap (ParamBean jParams, HashMap JavaDoc engineMap)
165             throws JahiaException {
166         // init map
167
String JavaDoc theScreen = jParams.getRequest ().getParameter ("screen");
168         if (theScreen == null) {
169             theScreen = EDIT_TOKEN;
170         }
171
172         engineMap.put ("screen", theScreen);
173         if (!theScreen.equals (SAVE_TOKEN)) {
174             engineMap.put ("jspSource", EDIT_JSP);
175             engineMap.put (ENGINE_URL_PARAM, jParams.composeEngineUrl (ENGINE_NAME, EMPTY_STRING));
176             engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME);
177
178         } else {
179             engineMap.put (ENGINE_NAME_PARAM, Core_Engine.ENGINE_NAME);
180             engineMap.put (ENGINE_URL_PARAM, jParams.composePageUrl (jParams.getPageID ()));
181             engineMap.put ("jspSource", "close");
182         }
183         engineMap.put (RENDER_TYPE_PARAM, new Integer JavaDoc (JahiaEngine.RENDERTYPE_FORWARD));
184         engineMap.put ("jahiaBuild", new Integer JavaDoc (jParams.settings ().getBuildNumber ()));
185         engineMap.put ("javascriptUrl", jParams.settings ().getJsHttpPath ());
186
187
188         if (logger.isDebugEnabled ())
189             logger.debug ("fetch user properties");
190
191         // get the user properties
192
JahiaUser user = jParams.getUser ();
193         UserProperties userProps = user.getUserProperties ();
194
195         // get the properties names, which will be used to store them into the request
196
Iterator JavaDoc propNameIter = userProps.propertyNameIterator();
197         while (propNameIter.hasNext()) {
198             String JavaDoc key = (String JavaDoc) propNameIter.next();
199
200             // get the property value
201
UserProperty value = userProps.getUserProperty(key);
202
203             // store the property into the request
204
engineMap.put (USER_PROPERTY_PREFIX + key, value);
205             logger.debug ("Adding property ["+ key +"] = ["+ value +"]");
206         }
207
208         if (logger.isDebugEnabled ())
209             logger.debug ("done with properties");
210
211
212         jParams.getRequest ().setAttribute ("engineTitle", "MySettings");
213         jParams.getRequest().setAttribute( "engineMap", engineMap );
214     }
215
216
217     /**
218      * prepares the screen requested by the user
219      *
220      * @param jParams a ParamBean object
221      */

222     public void processScreen (ParamBean jParams, HashMap JavaDoc engineMap)
223             throws JahiaException,
224             JahiaSessionExpirationException {
225
226         if (logger.isDebugEnabled ())
227             logger.debug ("started");
228
229         // gets the actual screen
230
// screen can be "edit" or "save"
231
String JavaDoc theScreen = jParams.getRequest ().getParameter ("screen");
232         if (theScreen == null) {
233             theScreen = EDIT_TOKEN;
234         }
235
236         JahiaUser user = jParams.getUser ();
237         if (user == null)
238             return;
239
240         // by default, consider the request as successful, and change it to *false* when
241
// any error occurs.
242
boolean ok = true;
243
244         // When the user clicked on the "save" button
245
if (theScreen.equals (SAVE_TOKEN)) {
246             ok = processSave (jParams, user);
247         }
248
249         if (!ok) {
250             //TODO: The process did not succeeded, process the error here.
251
}
252
253         if (logger.isDebugEnabled ())
254             logger.debug ("end");
255
256         String JavaDoc targetJSP = EDIT_JSP;
257         if ("save".equals(theScreen) && ok) {
258             targetJSP = SUCCESS_JSP;
259         }
260
261         String JavaDoc jspSiteMapFileName = jParams.getPage ().getPageTemplate ().getSourcePath ();
262         jspSiteMapFileName = jspSiteMapFileName.substring (0,
263                 jspSiteMapFileName.lastIndexOf ("/") + 1) +
264                 targetJSP;
265         engineMap.put (ENGINE_OUTPUT_FILE_PARAM, jspSiteMapFileName);
266     }
267
268     private boolean processSave (ParamBean jParams, JahiaUser user) {
269         if (logger.isDebugEnabled ())
270             logger.debug ("save button clicked");
271
272         EngineMessages resultMessages = new EngineMessages();
273         ServicesRegistry registry = ServicesRegistry.getInstance ();
274         if (registry != null) {
275             JahiaUserManagerService service = registry.getJahiaUserManagerService ();
276             if (service != null) {
277
278                 // retrieve the user password and set it if it is not null
279
String JavaDoc password = jParams.getRequest ().getParameter (REQUEST_PASSWORD_KEY);
280                 if ((password != null) && (!"".equals(password))) {
281                     if (logger.isDebugEnabled ())
282                         logger.debug ("password changed, check for password confirmation");
283
284                     // the password is not null, retrieve the password confirmation
285
String JavaDoc passwordConfirmation = jParams.getRequest ().getParameter (REQUEST_PASSWORD_CONFIRMATION_KEY);
286                     if (password.equals (passwordConfirmation)) {
287                         if (logger.isDebugEnabled ())
288                             logger.debug ("password and password confirmation match!");
289                         user.setPassword (password);
290                         registry.getJahiaSiteUserManagerService().refreshUser(jParams.getSiteID(),user.getUsername());
291                     } else {
292                         if (logger.isDebugEnabled ())
293                             logger.debug ("password and password confirmation do not match!");
294                         EngineMessage errorMessage = new EngineMessage(
295                             "org.jahia.engines.mysettings.passwordsDontMatch");
296                         resultMessages.add("mySettings", errorMessage);
297                         resultMessages.saveMessages(jParams.getRequest());
298                         return false;
299                     }
300                     if (password.length() < 6) {
301                         EngineMessage errorMessage = new EngineMessage("org.jahia.engines.mysettings.passwordTooShort");
302                         resultMessages.add("newUserRegistration", errorMessage);
303                         resultMessages.saveMessages(jParams.getRequest());
304                         return false;
305                     }
306                 }
307
308                 // pick out all the user properties parameters, and set it into the
309
// user properties
310
Enumeration JavaDoc names = jParams.getRequest ().getParameterNames ();
311                 if (names != null) {
312                     while (names.hasMoreElements ()) {
313                         String JavaDoc name = (String JavaDoc) names.nextElement ();
314                         if (name != null && name.startsWith (USER_PROPERTY_PREFIX)) {
315                             String JavaDoc newValue = jParams.getRequest ().getParameter (name);
316                             int index = name.indexOf (SEPARATOR);
317                             String JavaDoc key = name.substring (index + 1);
318                             UserProperty currentProp = user.getUserProperty (key);
319                             if (newValue == null) {
320                                 continue;
321                             }
322                             if (currentProp == null) {
323                                 user.setProperty (key, newValue);
324                             } else if (!currentProp.getValue().equals (newValue)) {
325                                 //TODO: The new data should be validated here!!
326
if (!currentProp.isReadOnly()) {
327                                     user.setProperty(key, newValue);
328                                 }
329                             }
330                         }
331                     }
332                 }
333
334             } else {
335                 return false;
336             }
337         } else {
338             return false;
339         }
340
341         if (logger.isDebugEnabled ())
342             logger.debug ("save process ended");
343         return true;
344     }
345
346
347 }
348
Popular Tags