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