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 <stan@nuxeo.com> 018 */ 019package org.nuxeo.ecm.user.center.profile.localeProvider; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023import org.nuxeo.ecm.core.api.DocumentModel; 024import org.nuxeo.ecm.core.api.SystemPrincipal; 025import org.nuxeo.ecm.core.event.Event; 026import org.nuxeo.ecm.core.event.EventListener; 027import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 028import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService; 029import org.nuxeo.ecm.webapp.locale.LocaleStartup; 030import org.nuxeo.runtime.api.Framework; 031 032/** 033 * Refresh Faces locale and timezone when the userProfileDocument is updated (and created). 034 * 035 * @since 5.6 036 */ 037public class UserLocaleSelectorListener implements EventListener { 038 039 public static final Log log = LogFactory.getLog(UserLocaleSelectorListener.class); 040 041 @Override 042 public void handleEvent(Event event) { 043 DocumentEventContext ctx = (DocumentEventContext) event.getContext(); 044 DocumentModel userProfileDocument = ctx.getSourceDocument(); 045 046 // The document should be the current user profile doc 047 if (!userProfileDocument.hasFacet("UserProfile")) { 048 return; 049 } 050 // No need to sync Seam session for system user 051 if (ctx.getPrincipal() instanceof SystemPrincipal) { 052 log.debug("Skip locale update for system user"); 053 return; 054 } 055 056 // if the profile does not belong to the current user 057 // => no need to sync Seam session 058 UserWorkspaceService uws = Framework.getService(UserWorkspaceService.class); 059 DocumentModel userWorkspace = uws.getCurrentUserPersonalWorkspace(ctx.getCoreSession(), userProfileDocument); 060 if (!userProfileDocument.getPathAsString().startsWith(userWorkspace.getPathAsString())) { 061 return; 062 } 063 064 // performing the locale update 065 LocaleStartup localeStartup = LocaleStartup.instance(); 066 if (localeStartup == null) { 067 log.warn("Locale Startup not available. Can't set locale"); 068 return; 069 } 070 localeStartup.setupLocale(userProfileDocument); 071 072 } 073 074}