001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 boolean isDeleteInProcess() {
042                    return _deleteInProcess.get();
043            }
044    
045            public static void setCompanyId(int companyId) {
046                    setCompanyId(Long.valueOf(companyId));
047            }
048    
049            public static void setCompanyId(Long companyId) {
050                    if (_log.isDebugEnabled()) {
051                            _log.debug("setCompanyId " + companyId);
052                    }
053    
054                    if (companyId > 0) {
055                            _companyId.set(companyId);
056    
057                            try {
058                                    Company company = CompanyLocalServiceUtil.getCompany(companyId);
059    
060                                    LocaleThreadLocal.setDefaultLocale(company.getLocale());
061                                    TimeZoneThreadLocal.setDefaultTimeZone(company.getTimeZone());
062                            }
063                            catch (Exception e) {
064                                    _log.error(e, e);
065                            }
066                    }
067                    else {
068                            LocaleThreadLocal.setDefaultLocale(null);
069                            TimeZoneThreadLocal.setDefaultTimeZone(null);
070    
071                            _companyId.set(CompanyConstants.SYSTEM);
072                    }
073            }
074    
075            public static void setDeleteInProcess(boolean deleteInProcess) {
076                    _deleteInProcess.set(deleteInProcess);
077            }
078    
079            private static Log _log = LogFactoryUtil.getLog(CompanyThreadLocal.class);
080    
081            private static ThreadLocal<Long> _companyId =
082                    new AutoResetThreadLocal<Long>(
083                            CompanyThreadLocal.class + "._companyId", CompanyConstants.SYSTEM);
084            private static ThreadLocal<Boolean> _deleteInProcess =
085                    new AutoResetThreadLocal<Boolean>(
086                            CompanyThreadLocal.class + "._deleteInProcess", false);
087    
088    }