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