001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Sun Seng David TAN (a.k.a. sunix) <stan@nuxeo.com>
016 *     Stephane Lacoin at Nuxeo (aka matic) <slacoin@nuxeo.com>
017 */
018package org.nuxeo.ecm.webapp.locale;
019
020import java.io.Serializable;
021import java.util.Locale;
022
023import javax.faces.context.FacesContext;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.jboss.seam.Component;
028import org.jboss.seam.ScopeType;
029import org.jboss.seam.annotations.In;
030import org.jboss.seam.annotations.Name;
031import org.jboss.seam.annotations.Observer;
032import org.jboss.seam.annotations.Scope;
033import org.jboss.seam.annotations.Startup;
034import org.jboss.seam.contexts.Contexts;
035import org.jboss.seam.international.LocaleSelector;
036import org.jboss.seam.international.TimeZoneSelector;
037import org.nuxeo.ecm.core.api.CoreSession;
038import org.nuxeo.ecm.core.api.DocumentModel;
039import org.nuxeo.ecm.platform.web.common.locale.LocaleProvider;
040import org.nuxeo.ecm.webapp.helpers.EventNames;
041import org.nuxeo.runtime.api.Framework;
042
043/**
044 * Initialize the locale when the user session is entered. Enables client to send their timezone id through AJAX (not
045 * yet implemented).
046 *
047 * @since 5.6
048 */
049@Name("clientLocaleInitializer")
050@Scope(ScopeType.SESSION)
051@Startup
052public class LocaleStartup implements Serializable {
053
054    private static final long serialVersionUID = 1L;
055
056    public static final Log log = LogFactory.getLog(LocaleStartup.class);
057
058    public static LocaleStartup instance() {
059        if (!Contexts.isSessionContextActive()) {
060            return null;
061        }
062        return (LocaleStartup) Component.getInstance(LocaleStartup.class, ScopeType.SESSION);
063    }
064
065    @In(create = true)
066    protected CoreSession documentManager;
067
068    protected String tzId;
069
070    public String getTzId() {
071        return tzId;
072    }
073
074    public void setTzId(String id) {
075        tzId = id;
076    }
077
078    @Observer(EventNames.USER_SESSION_STARTED)
079    public void handleUserSessionStarted(CoreSession session) {
080        setupTimeZone(session);
081        setupLocale(session);
082    }
083
084    /**
085     * Getting the timezone from the cookies and initialize Seam timezone. The nxtimezone.js contains methods to set the
086     * cookie with the browser timezone.
087     */
088    public void setupTimeZone(CoreSession session) {
089        // Not using LocaleProvider to get persisted timezone because it is too
090        // hard to make it works with OpenSocialGadgets.
091        // and changing a timezone for a Date in javascript is not trivial.
092        TimeZoneSelector tzSelector = TimeZoneSelector.instance();
093        tzSelector.setCookieEnabled(true);
094        tzSelector.initTimeZone();
095    }
096
097    public void setupLocale(CoreSession session) {
098        Locale locale = Framework.getService(LocaleProvider.class).getLocale(session);
099        setupLocale(locale);
100    }
101
102    /**
103     * @since 5.9.5
104     */
105    public void setupLocale(DocumentModel userProfileDoc) {
106        Locale locale = Framework.getService(LocaleProvider.class).getLocale(userProfileDoc);
107        setupLocale(locale);
108    }
109
110    protected void setupLocale(Locale locale) {
111        if (locale == null) {
112            log.debug("Locale not set, falling back to request locale");
113            locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
114        }
115        if (locale == null) {
116            log.debug("Locale not set, falling back to default locale");
117            locale = Locale.getDefault();
118        }
119        LocaleSelector localeSelector = LocaleSelector.instance();
120        localeSelector.setLocale(locale);
121        localeSelector.setCookieEnabled(true);
122        localeSelector.select();
123    }
124
125}