View Javadoc

1   /***
2    Copyright (C) 2005 The Java Community
3   
4    This program is free software; you can redistribute it and/or modify  it under
5    the terms of the GNU General Public License as published by  the Free Software
6    Foundation; either version 2 of the License, or  (at your option) any later
7    version.
8   
9    This program is distributed in the hope that it will be useful,  but WITHOUT
10   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13   You should have received a copy of the GNU General Public License  along with
14   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15   Place, Suite 330, Boston, MA 02111-1307 USA.
16  
17   */
18  
19  package org.bejug.javacareers.jobs.view.jsf.util;
20  
21  import net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.bejug.javacareers.common.view.jsf.constants.JSFConstants;
25  import org.bejug.javacareers.jobs.model.Country;
26  import org.bejug.javacareers.jobs.model.Item;
27  import org.bejug.javacareers.jobs.model.Region;
28  import org.bejug.javacareers.jobs.service.AdminService;
29  import org.springframework.context.ApplicationContext;
30  import org.springframework.web.jsf.FacesContextUtils;
31  
32  import javax.faces.application.Application;
33  import javax.faces.context.ExternalContext;
34  import javax.faces.context.FacesContext;
35  import javax.faces.el.ValueBinding;
36  import javax.faces.el.VariableResolver;
37  import javax.faces.model.SelectItem;
38  import javax.servlet.http.HttpSession;
39  import java.io.IOException;
40  import java.util.*;
41  
42  /***
43   * @author Peter Symoens (last modified by $Author: shally $)
44   * @version $Revision: 1.32 $ - $Date: 2005/12/20 15:36:47 $
45   */
46  public class Utils implements JSFConstants {
47  
48      /***
49       * The adminservice AdminService.
50       */
51      private AdminService adminService;
52  
53      /***
54       * String representing administrator role.
55       */
56      public static final String ROLE_ADMIN = "ROLE_ADMIN";
57  
58      /***
59       * String representing user role.
60       */
61      public static final String ROLE_USER = "ROLE_USER";
62  
63      /***
64       * the logger.
65       */
66      private static final Log LOG = LogFactory.getLog(Utils.class);
67  
68  
69      /***
70       * the list of locales supported by the application.
71       */
72      private List localeItems;
73  
74      /***
75       * the list of salutations for presentation in a popup.
76       */
77      private List salutations;
78  
79      /***
80       * the list of security roles for presentation in a popup.
81       */
82      private List roles;
83  
84      /***
85       * locations.
86       */
87      private static List locations = new ArrayList();
88  
89      /***
90       * userProfiles.
91       */
92      private static List userProfiles = new ArrayList();
93  
94      /***
95       * the organisation and username used by the RSS feeder.
96       */
97      public static final String RSSFEEDER = "RssFeeder";
98  
99  
100     /***
101      * Get the bundle as configured in the Faces configuration.
102      * In the absence of a context, revert to the default.
103      *
104      * @return the resource bundle to use.
105      */
106     public ResourceBundle getMessageBundle() {
107         FacesContext context = getFacesContext();
108         Locale locale;
109         String bundleName;
110         //use "Locale" argument to I18Nize
111         if (null == context) {
112             bundleName =
113                     "org.bejug.javacareers.common.view.jsf.resources.messages";
114             locale = Locale.getDefault();
115         } else {
116             bundleName = context.getApplication().getMessageBundle();
117             locale = context.getViewRoot().getLocale();
118         }
119         ResourceBundle bundle = ResourceBundle.getBundle(bundleName,
120                 locale);
121         return bundle;
122     }
123 
124     /***
125      * @return Returns the locations.
126      */
127     public List getLocations() {
128         if (locations.size() == 0) {
129             ResourceBundle messages = getMessageBundle();
130             try {
131                 Enumeration enumer = messages.getKeys();
132                 while (enumer.hasMoreElements()) {
133                     String key = enumer.nextElement().toString();
134                     if (key.startsWith("item_region")) {
135                         locations.add(new SelectItem(messages.getString(key)));
136                     }
137                 }
138             } catch (MissingResourceException e) {
139                 LOG.error("exception caught loading resource from " + messages);
140             }
141         }
142 
143         return locations;
144     }
145 
146     /***
147      * Retrieve the list of region types for display in a popup.
148      *
149      * @return list of menu items representing organisation types.
150      */
151     public List getRegionItems() {
152 
153         Set types = adminService.getRegions("Belgium");
154 
155         List items = new ArrayList();
156         if (types.size() == 0) {
157             items.add("empty");
158         } else {
159             for (Iterator it = types.iterator(); it.hasNext();) {
160                 Region reg = (Region) it.next();
161                 items.add(new SelectItem(reg.getName()));
162             }
163         }
164         return items;
165     }
166 
167     /***
168      * Retrieve the list of countries for display in a popup.
169      *
170      * @return list of select items representing the available countries.
171      */
172     public List getCountryItems() {
173         //TODO: select the country from a list using ajax
174         List countryList = adminService.getCountries();
175         List items = new ArrayList();
176         for (Iterator iterator = countryList.iterator(); iterator.hasNext();) {
177             Country country = (Country) iterator.next();
178             items.add(new SelectItem(country.getName()));
179         }
180         return items;
181     }
182 
183     /***
184      * @return Returns the user profiles.
185      */
186     public List getUserTypes() {
187         if (userProfiles.size() == 0) {
188             ResourceBundle messages = getMessageBundle();
189             try {
190                 Enumeration enumer = messages.getKeys();
191                 while (enumer.hasMoreElements()) {
192                     String key = enumer.nextElement().toString();
193 
194                     if (key.startsWith("item_user_type")) {
195                         userProfiles.add(new SelectItem(
196                                                     messages.getString(key)));
197                     }
198                 }
199             } catch (MissingResourceException e) {
200                 LOG.error("exception caught loading resource from " + messages);
201             }
202         }
203         return userProfiles;
204     }
205 
206     /***
207      * Retrieve the list of salutations to use for this region of the world.
208      *
209      * @return list of strings
210      */
211     public List getSalutationItems() {
212         if (salutations == null) {
213             salutations = new ArrayList();
214             ResourceBundle messages = getMessageBundle();
215             try {
216                 Enumeration keys = messages.getKeys();
217                 while (keys.hasMoreElements()) {
218                     String key = keys.nextElement().toString();
219                     if (key.startsWith("item_salutation")) {
220                         salutations.add(new SelectItem(messages.getString(key)));
221                     }
222                 }
223             } catch (MissingResourceException e) {
224                 LOG.error("exception caught loading resource from " + messages);
225             }
226         }
227 
228         return salutations;
229     }
230 
231     // TODO move locale related methods to view/jsf/preferences
232     /***
233      * Retrieve the list of locales supported by the Faces application.
234      *
235      * @return list of SelectItems representing the locale list.
236      */
237     public List getSupportedLocaleItems() {
238         if (localeItems == null) {
239             localeItems = new ArrayList();
240             Application app = Utils.getApplication();
241             for (Iterator i = app.getSupportedLocales(); i.hasNext();) {
242                 Locale locale = (Locale) i.next();
243                 SelectItem item = new SelectItem(locale.toString(), locale
244                         .getDisplayName().substring(0, 2));
245                 localeItems.add(item);
246             }
247             if (localeItems.size() == 0) {
248                 Locale defaultLocale = app.getDefaultLocale();
249                 localeItems.add(new SelectItem(defaultLocale.toString(),
250                         defaultLocale.getDisplayName().substring(0, 2)));
251             }
252         }
253         return localeItems;
254     }
255 
256     /***
257      * Retrieve the locale object by specifying the 2-letter ISO language code
258      * or the combination of the 2-letter ISO language code + 3-letter country
259      * code.
260      *
261      * @param name the locale name
262      * @return loc Locale
263      */
264     public static Locale getLocaleByName(String name) {
265         Locale loc = null;
266 
267         if (name.length() == 5) {
268             String language = name.substring(0, 2);
269             String country = name.substring(3, 5);
270             loc = new Locale(language, country);
271         } else if (name.length() == 2) {
272             loc = new Locale(name);
273         }
274 
275         return loc;
276     }
277 
278     /***
279      * @param defaultObject Object.
280      * @return Returns the classLoader.
281      */
282     protected static ClassLoader getCurrentClassLoader(Object defaultObject) {
283         ClassLoader loader = Thread.currentThread().getContextClassLoader();
284         if (null == loader) {
285             loader = defaultObject.getClass().getClassLoader();
286         }
287         return loader;
288     }
289 
290     /***
291      * @return the HttpSession.
292      */
293     public static HttpSession getSession() {
294         return (HttpSession) getFacesContext().getExternalContext().
295                 getSession(true);
296     }
297 
298     /***
299      * Retrieve the current Faces context.
300      *
301      * @return Returns the FacesContext.
302      */
303     public static FacesContext getFacesContext() {
304         return FacesContext.getCurrentInstance();
305     }
306 
307     /***
308      * @return Returns the ExternalContext
309      */
310     public static ExternalContext getExternalContext() {
311         return getFacesContext().getExternalContext();
312     }
313 
314     /***
315      * Retrieve the current Faces application.
316      *
317      * @return the current application.
318      */
319     public static Application getApplication() {
320         return getFacesContext().getApplication();
321     }
322 
323     /***
324      * Retrieve the locale as string for the current view.
325      *
326      * @return locale for current view.
327      */
328     public static String getLocale() {
329         return getFacesContext().getViewRoot().getLocale().toString();
330     }
331 
332     /***
333      * @return ServletContextRoot
334      */
335     public static String getServletContextRoot() {
336         return getFacesContext().getExternalContext().getRequestContextPath();
337     }
338 
339     /***
340      * @param id String
341      * @return Returns the message.
342      */
343     public static String getMessage(String id) {
344         String msg = "";
345         ClassLoader loader = getCurrentClassLoader(id);
346         FacesContext context = getFacesContext();
347         ResourceBundle bundle = null;
348         String bundleName;
349         Locale locale;
350 
351         /* if there is no faces context, revert to some default behaviour */
352         if (null == context) {
353             bundleName =
354                   "org.bejug.javacareers.common.view.jsf.resources.messages_en";
355             locale = Locale.getDefault();
356         } else {
357             bundleName = context.getApplication().getMessageBundle();
358             locale = context.getViewRoot().getLocale();
359         }
360 
361         if (bundleName != null) {
362             bundle = ResourceBundle.getBundle(bundleName, locale, loader);
363             if (null != bundle) {
364                 try {
365                     msg = bundle.getString(id);
366                 } catch (MissingResourceException ex) {
367                     msg = "";
368                 }
369             }
370         }
371         return msg;
372     }
373 
374     /***
375      * @return list of possible roles for registered users.
376      */
377     public List getRoleItems() {
378         if (roles == null) {
379             roles = new ArrayList();
380             roles.add(new SelectItem(ROLE_ADMIN, Utils
381                     .getMessage("label_administrator")));
382             roles.add(new SelectItem(ROLE_USER, Utils
383                     .getMessage("label_registered_user")));
384         }
385         return roles;
386     }
387 
388     /***
389      * Bind a item instance to a variable name
390      *
391      * @param item The Item to bind
392      */
393     public static void createValueBinding(Item item) {
394         FacesContext ctx = FacesContext.getCurrentInstance();
395         Application app = ctx.getApplication();
396 
397         // Used a session managed bean 'currentItem' instead of the
398         // predefined sessionScope object which doesn't seem to work with
399         // JSF-Spring v3.0.0M1  !?
400          
401         ValueBinding binding = app.createValueBinding("#{" + CURRENT_ITEM + ".item}");
402         binding.setValue(ctx, item);
403     }
404 
405     /***
406      * @param variable The variable to resolve
407      * @return the resolved object
408      */
409     public static Object resolveVariable(String variable) {
410         FacesContext ctx = FacesContext.getCurrentInstance();
411         ctx.getViewRoot().getAttributes();
412         Application app = ctx.getApplication();
413         VariableResolver variableResolver = app.getVariableResolver();
414         Object o = variableResolver.resolveVariable(ctx, variable);
415         LOG.info("Debug: resolved variable = " + o);
416         return o;
417     }
418 
419     /***
420      * Change the default target url.
421      *
422      * @param url The default target url
423      */
424     public static void setDefaultTargetUrl(String url) {
425         //static method, so Spring injection cannot be used.
426         ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
427 
428         AuthenticationProcessingFilter processingFilter =
429                 (AuthenticationProcessingFilter)
430                 ctx.getBean("authenticationProcessingFilter");
431 
432         // Instead of changing the default target url the attribute
433         // ACEGI_SECURITY_TARGET_URL_KEY should be set on the httpSession to
434         // redirect after login, but this doesn't work.
435         // Could be a bug in Acegi ?!
436         processingFilter.setDefaultTargetUrl(url);
437         try {
438             processingFilter.afterPropertiesSet();
439         } catch (Exception e) {
440             LOG.error(e);
441         }
442     }
443 
444     /***
445      * redirect to the login page
446      */
447     public static void redirectToLogin() {
448         ExternalContext externalContext = getExternalContext();
449 
450         try {
451             externalContext.redirect(externalContext
452                     .encodeResourceURL(externalContext.getRequestContextPath().
453                     concat("/login.jsf")));
454         } catch (IOException e) {
455             LOG.error(e);
456         }
457     }
458 
459     /***
460      * @return the VariableResolver.
461      */
462     public static VariableResolver getVariableResolver() {
463         return getFacesContext().getApplication().getVariableResolver();
464     }
465 
466     /***
467      * @return Returns the adminservice.
468      */
469     public AdminService getAdminService() {
470         return adminService;
471     }
472 
473     /***
474      * @param adminService The adminservice to set.
475      */
476     public void setAdminService(AdminService adminService) {
477         this.adminService = adminService;
478     }
479     
480     /***
481      * Store an object in a binding on a certain scope.
482      * @param scope the scope for the binding.
483      * @param key the key for the binding.
484      * @param value the object to store in the binding.
485      */
486     public static void saveBinding(String scope, String key, Object value) {
487         FacesContext ctx = Utils.getFacesContext();
488         Application app = ctx.getApplication();
489         ValueBinding binding = app.createValueBinding("#{" + scope + '.' + key + "}");
490         binding.setValue(ctx, value);
491     }
492     
493     /***
494      * Get the value of a binding on a certain scope e.g. sessionScope.
495      * @param scope the scope of the binding.
496      * @param key the key of the binding.
497      * @return the object stored at the binding.
498      */
499     public static Object getBinding(String scope, String key) {
500         FacesContext ctx = Utils.getFacesContext();
501         Application app = ctx.getApplication();
502         ValueBinding binding = app.createValueBinding("#{" + scope + '.' + key + "}");
503         return binding.getValue(ctx);
504     }
505 
506 }
507 
508 /***
509  * $Log: Utils.java,v $
510  * Revision 1.32  2005/12/20 15:36:47  shally
511  * CheckStyle and PMD changes.
512  *
513  * Revision 1.31  2005/11/15 14:35:05  kristofvb
514  * improve the profile form
515  *
516  * Revision 1.30  2005/10/11 09:59:23  stephan_janssen
517  * Code cleanup.
518  *
519  * Revision 1.29  2005/09/30 14:38:08  bavo_jcs
520  * Fixed URL
521  *
522  * Revision 1.28  2005/09/23 07:29:48  schauwvliege
523  * delete content refactory for interview
524  *
525  * Revision 1.27  2005/09/19 14:16:51  bavo_jcs
526  * Refactored comments to use Items
527  *
528  * Revision 1.26  2005/09/16 08:36:45  bavo_jcs
529  * Added JPG methods
530  *
531  * Revision 1.25  2005/09/16 08:12:15  schauwvliege
532  * Wizard post interview
533  *
534  * Revision 1.24  2005/09/15 14:26:10  bavo_jcs
535  * Added JPG methods
536  *
537  * Revision 1.23  2005/09/15 13:58:30  bavo_jcs
538  * Added JPG methods
539  *
540  * Revision 1.22  2005/09/15 12:48:39  bavo_jcs
541  * Added JPG methods
542  *
543  * Revision 1.21  2005/09/14 10:46:22  schauwvliege
544  * Upload files
545  *
546  * Revision 1.20  2005/09/14 08:57:49  schauwvliege
547  * Made dummy method for resizing images GO Bavo GO
548  *
549  * Revision 1.19  2005/09/09 15:11:52  bavo_jcs
550  * Added CommercialEducationOffer
551  *
552  * Revision 1.18  2005/09/09 13:09:13  schauwvliege
553  * added Commercial education offer
554  *
555  * Revision 1.17  2005/09/05 09:20:07  schauwvliege
556  * Removed CurrentJobOffer is now CurrentItem
557  *
558  * Revision 1.16  2005/08/24 16:30:00  schauwvliege
559  * introduction of mixed list of all items
560  *
561  * Revision 1.15  2005/08/24 12:32:13  psong09
562  * Added spinner component and renamed commentAction
563  *
564  * Revision 1.14  2005/08/23 10:18:47  bme_jcs
565  * resolved checkstyle and pmd errors
566  *
567  * Revision 1.13  2005/08/23 08:50:38  ge0ffrey
568  * JAVACAREERS-284 part 3: resolver
569  *
570  * Revision 1.12  2005/08/22 14:07:34  psong09
571  * JAVACAREERS_276
572  *
573  * Revision 1.11  2005/08/19 13:13:54  psong09
574  * introduced synchroniser token
575  *
576  * Revision 1.10  2005/08/17 16:50:32  psong09
577  * JAVACAREERS-276
578  *
579  * Revision 1.9  2005/08/17 09:09:30  schauwvliege
580  * Fixed all unit tests
581  *
582  * Revision 1.8  2005/08/10 09:04:50  bavo_jcs
583  * Optimized imports according to checkstyle
584  *
585  * Revision 1.7  2005/08/09 12:59:56  bavo_jcs
586  * Optimized imports
587  *
588  * Revision 1.6  2005/08/08 09:38:23  bme_jcs
589  * resolved checkstyle errors
590  *
591  * Revision 1.5  2005/08/04 14:41:37  psong09
592  * Added createValueBinding and resolveVariable methods
593  *
594  * Revision 1.4  2005/07/08 15:47:27  psong09
595  * modified comment component, removed subject field
596  *
597  * Revision 1.3  2005/06/14 12:05:53  schauwvliege
598  * CheckStyle and fixing tests
599  *
600  * Revision 1.2  2005/06/09 08:18:53  bejug_cc
601  * Fix initial import
602  *
603  * Revision 1.48  2005/06/04 11:04:58  sja
604  * Corrected item_user_type prefix in getUserTypes.
605  *
606  * Revision 1.47  2005/06/03 15:37:23  PSONG09
607  * added getUserTypes
608  *
609  * Revision 1.46  2005/06/02 19:49:57  kva
610  * filter RssFeeder
611  *
612  * Revision 1.45  2005/06/02 19:26:29  kva
613  * corrected registered user menu item
614  *
615  * Revision 1.44  2005/05/31 13:47:55  PSONG09
616  * removed some unused variables
617  *
618  * Revision 1.43  2005/05/31 07:53:21  kva
619  * use new resource bundle
620  *
621  * Revision 1.42  2005/05/29 21:54:42  sja
622  * Uses merged resource bundle.
623  *
624  * Revision 1.41  2005/05/26 09:35:35  kva
625  * make getMessage work in the absence of a faces context (e.g. unit testing)
626  *
627  * Revision 1.40  2005/05/25 19:55:53  sja
628  * Added todo comment.
629  *
630  * Revision 1.39  2005/05/18 19:22:12  kva
631  * checkstyle
632  *
633  * Revision 1.38  2005/05/18 15:06:22  PSONG09
634  * removed test methods
635  *
636  * Revision 1.36  2005/05/18 07:53:04  PSONG09
637  * update getLocations
638  *
639  * Revision 1.35  2005/05/18 07:37:30  PSONG09
640  * modified profile and location lists
641  *
642  * Revision 1.34  2005/05/18 07:19:00  PSONG09
643  * replaced previous update
644  *
645  * Revision 1.31  2005/05/17 14:23:08  ssc
646  * checkstyle
647  *
648  * Revision 1.30  2005/05/17 13:39:25  kva
649  * fixed typo in getLocations
650  *
651  * Revision 1.29  2005/05/17 12:14:48  kva
652  * clean up + more utility functions
653  *
654  * Revision 1.28  2005/05/17 11:59:56  ssc
655  * Refactored User and Publisher class to User Class added cvAdded Boolean, added Address to user, Fixed test to work with this setup
656  *
657  * Revision 1.27  2005/05/17 07:42:47  kva
658  * removed duplicate comment
659  *
660  * Revision 1.26  2005/05/17 07:29:03  PSONG09
661  * merge done
662  *
663  * Revision 1.25  2005/05/17 07:24:43  kva
664  * fix merge problem
665  *
666  * Revision 1.24  2005/05/17 07:08:13  PSONG09
667  * merge done
668  *Revision 1.23  2005/05/12 14:45:12  kva
669  * added locale to profiles and organisations
670  *
671  * Revision 1.22 2005/05/11 13:13:54 kva added salutation
672  * user profile and user registration use common bean class
673  *
674  * Revision 1.22 2005/05/11 13:13:54 kva added salutation
675  * user profile and user registration use common bean class
676  *
677  * Revision 1.21 2005/05/11 11:29:32 ssc Checstyle errors
678  *
679  * Revision 1.20 2005/05/11 08:50:22 kva change password functionality
680  *
681  * Revision 1.19 2005/05/11 07:04:51 kva more utility methods
682  *
683  * Revision 1.18 2005/05/10 15:14:48 kva added more utility methods
684  *
685  * Revision 1.17 2005/05/10 11:02:03 kva added getCurrentUser method
686  *
687  * Revision 1.16 2005/05/09 14:54:45 ssc checkstyle
688  *
689  * Revision 1.15 2005/05/04 15:27:09 PSONG09 methods modified
690  *
691  * Revision 1.14 2005/05/03 14:54:37 PSONG09 javadoc added
692  *
693  * Revision 1.13 2005/05/02 14:33:22 kva added some utility functions
694  *
695  * Revision 1.12 2005/05/01 19:41:49 sja Corrected typeo.
696  *
697  * Revision 1.11 2005/05/01 17:46:18 sja PMD corrections.
698  *
699  * Revision 1.10 2005/05/01 17:34:09 sja Added extra joboffer for sort view
700  * testing.
701  *
702  * Revision 1.9 2005/04/29 18:17:34 sja General cleanup (naming conv., javadoc
703  * and CVS tags)
704  *
705  * Revision 1.21 2005/05/11 11:29:32 ssc Checstyle errors
706  *
707  * Revision 1.20 2005/05/11 08:50:22 kva change password functionality
708  *
709  * Revision 1.19 2005/05/11 07:04:51 kva more utility methods
710  *
711  * Revision 1.18 2005/05/10 15:14:48 kva added more utility methods
712  *
713  * Revision 1.17 2005/05/10 11:02:03 kva added getCurrentUser method
714  *
715  * Revision 1.16 2005/05/09 14:54:45 ssc checkstyle
716  *
717  * Revision 1.15 2005/05/04 15:27:09 PSONG09 methods modified
718  *
719  * Revision 1.14 2005/05/03 14:54:37 PSONG09 javadoc added
720  *
721  * Revision 1.13 2005/05/02 14:33:22 kva added some utility functions
722  *
723  * Revision 1.12 2005/05/01 19:41:49 sja Corrected typeo.
724  *
725  * Revision 1.11 2005/05/01 17:46:18 sja PMD corrections.
726  *
727  * Revision 1.10 2005/05/01 17:34:09 sja Added extra joboffer for sort view
728  * testing.
729  *
730  * Revision 1.9 2005/04/29 18:17:34 sja General cleanup (naming conv., javadoc
731  * and CVS tags)
732  *
733  */