001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.auth;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
020    import com.liferay.portal.kernel.util.LocaleThreadLocal;
021    import com.liferay.portal.kernel.util.TimeZoneThreadLocal;
022    import com.liferay.portal.model.Company;
023    import com.liferay.portal.model.CompanyConstants;
024    import com.liferay.portal.service.CompanyLocalServiceUtil;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class CompanyThreadLocal {
030    
031            public static Long getCompanyId() {
032                    Long companyId = _companyId.get();
033    
034                    if (_log.isDebugEnabled()) {
035                            _log.debug("getCompanyId " + companyId);
036                    }
037    
038                    return companyId;
039            }
040    
041            public static void setCompanyId(int companyId) {
042                    setCompanyId(Long.valueOf(companyId));
043            }
044    
045            public static void setCompanyId(Long companyId) {
046                    if (_log.isDebugEnabled()) {
047                            _log.debug("setCompanyId " + companyId);
048                    }
049    
050                    if (companyId > 0) {
051                            try {
052                                    Company company = CompanyLocalServiceUtil.getCompany(companyId);
053    
054                                    LocaleThreadLocal.setDefaultLocale(company.getLocale());
055                                    TimeZoneThreadLocal.setDefaultTimeZone(company.getTimeZone());
056                            }
057                            catch (Exception e) {
058                                    _log.error(e, e);
059                            }
060    
061                            _companyId.set(companyId);
062                    }
063                    else {
064                            LocaleThreadLocal.setDefaultLocale(null);
065                            TimeZoneThreadLocal.setDefaultTimeZone(null);
066    
067                            _companyId.set(CompanyConstants.SYSTEM);
068                    }
069            }
070    
071            private static Log _log = LogFactoryUtil.getLog(CompanyThreadLocal.class);
072    
073            private static ThreadLocal<Long> _companyId =
074                    new AutoResetThreadLocal<Long>(
075                            CompanyThreadLocal.class + "._companyId", CompanyConstants.SYSTEM);
076    
077    }