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.user.center.profile.UserProfileService;
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
051        // if the profile does not belong to the current user
052        // => no need to sync Seam session
053        UserWorkspaceService uws = Framework.getLocalService(UserWorkspaceService.class);
054        DocumentModel userWorkspace = uws.getCurrentUserPersonalWorkspace(ctx.getCoreSession(), userProfileDocument);
055        if (!userProfileDocument.getPathAsString().startsWith(userWorkspace.getPathAsString())) {
056            return;
057        }
058
059        // performing the locale update
060        LocaleStartup localeStartup = LocaleStartup.instance();
061        if (localeStartup == null) {
062            log.warn("Locale Startup not available. Can't set locale");
063            return;
064        }
065        localeStartup.setupLocale(userProfileDocument);
066
067    }
068
069}