001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Sun Seng David TAN (a.k.a. sunix) <stan@nuxeo.com> 018 * Stephane Lacoin at Nuxeo (aka matic) <slacoin@nuxeo.com> 019 */ 020package org.nuxeo.ecm.webapp.locale; 021 022import java.io.Serializable; 023import java.util.Locale; 024 025import javax.faces.context.FacesContext; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.jboss.seam.Component; 030import org.jboss.seam.ScopeType; 031import org.jboss.seam.annotations.In; 032import org.jboss.seam.annotations.Name; 033import org.jboss.seam.annotations.Observer; 034import org.jboss.seam.annotations.Scope; 035import org.jboss.seam.annotations.Startup; 036import org.jboss.seam.contexts.Contexts; 037import org.jboss.seam.international.LocaleSelector; 038import org.jboss.seam.international.TimeZoneSelector; 039import org.nuxeo.ecm.core.api.CoreSession; 040import org.nuxeo.ecm.core.api.DocumentModel; 041import org.nuxeo.ecm.platform.web.common.locale.LocaleProvider; 042import org.nuxeo.ecm.webapp.helpers.EventNames; 043import org.nuxeo.runtime.api.Framework; 044 045/** 046 * Initialize the locale when the user session is entered. Enables client to send their timezone id through AJAX (not 047 * yet implemented). 048 * 049 * @since 5.6 050 */ 051@Name("clientLocaleInitializer") 052@Scope(ScopeType.SESSION) 053@Startup 054public class LocaleStartup implements Serializable { 055 056 private static final long serialVersionUID = 1L; 057 058 public static final Log log = LogFactory.getLog(LocaleStartup.class); 059 060 public static LocaleStartup instance() { 061 if (!Contexts.isSessionContextActive()) { 062 return null; 063 } 064 return (LocaleStartup) Component.getInstance(LocaleStartup.class, ScopeType.SESSION); 065 } 066 067 @In(create = true) 068 protected CoreSession documentManager; 069 070 protected String tzId; 071 072 public String getTzId() { 073 return tzId; 074 } 075 076 public void setTzId(String id) { 077 tzId = id; 078 } 079 080 @Observer(EventNames.USER_SESSION_STARTED) 081 public void handleUserSessionStarted(CoreSession session) { 082 setupTimeZone(session); 083 setupLocale(session); 084 } 085 086 /** 087 * Getting the timezone from the cookies and initialize Seam timezone. The nxtimezone.js contains methods to set the 088 * cookie with the browser timezone. 089 */ 090 public void setupTimeZone(CoreSession session) { 091 // Not using LocaleProvider to get persisted timezone because it is too 092 // hard to make it works with OpenSocialGadgets. 093 // and changing a timezone for a Date in javascript is not trivial. 094 TimeZoneSelector tzSelector = TimeZoneSelector.instance(); 095 tzSelector.setCookieEnabled(true); 096 tzSelector.initTimeZone(); 097 } 098 099 public void setupLocale(CoreSession session) { 100 Locale locale = Framework.getService(LocaleProvider.class).getLocale(session); 101 setupLocale(locale); 102 } 103 104 /** 105 * @since 5.9.5 106 */ 107 public void setupLocale(DocumentModel userProfileDoc) { 108 Locale locale = Framework.getService(LocaleProvider.class).getLocale(userProfileDoc); 109 setupLocale(locale); 110 } 111 112 protected void setupLocale(Locale locale) { 113 if (locale == null) { 114 log.debug("Locale not set, falling back to request locale"); 115 locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale(); 116 } 117 if (locale == null) { 118 log.debug("Locale not set, falling back to default locale"); 119 locale = Locale.getDefault(); 120 } 121 LocaleSelector localeSelector = LocaleSelector.instance(); 122 localeSelector.setLocale(locale); 123 localeSelector.setCookieEnabled(true); 124 localeSelector.select(); 125 } 126 127}