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 java.util.Locale; 022import java.util.TimeZone; 023 024import org.apache.commons.lang.LocaleUtils; 025import org.apache.commons.logging.Log; 026import org.apache.commons.logging.LogFactory; 027import org.nuxeo.ecm.core.api.CoreSession; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.platform.web.common.locale.LocaleProvider; 030import org.nuxeo.ecm.user.center.profile.UserProfileConstants; 031import org.nuxeo.ecm.user.center.profile.UserProfileService; 032import org.nuxeo.runtime.api.Framework; 033 034/** 035 * Provides user local stored in profile doc model 036 * 037 * @since 5.6 038 */ 039public class UserLocaleProvider implements LocaleProvider { 040 public static final Log log = LogFactory.getLog(UserLocaleProvider.class); 041 042 @Override 043 public Locale getLocale(CoreSession repo) { 044 UserProfileService userProfileService = Framework.getLocalService(UserProfileService.class); 045 DocumentModel userProfileDoc = userProfileService.getUserProfileDocument(repo); 046 return getLocale(userProfileDoc); 047 } 048 049 @Override 050 public Locale getLocale(DocumentModel userProfileDoc) { 051 if (userProfileDoc == null) { 052 return null; 053 } 054 055 String locale = (String) userProfileDoc.getPropertyValue(UserProfileConstants.USER_PROFILE_LOCALE); 056 if (locale == null || locale.trim().length() == 0) { 057 // undefined if not set 058 return null; 059 } 060 try { 061 return LocaleUtils.toLocale(locale); 062 } catch (IllegalArgumentException e) { 063 log.error("Locale parse exception: \"" + locale + "\"", e); 064 } 065 return null; 066 } 067 068 @Override 069 public TimeZone getTimeZone(CoreSession repo) { 070 // the timezone is not retrieved from the user profile (cookie and Seam 071 // TimezoneSelector) 072 return null; 073 } 074 075}