001    /**
002     * Copyright (c) 2000-present 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.kernel.security.auth;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.model.Company;
022    import com.liferay.portal.kernel.model.CompanyConstants;
023    import com.liferay.portal.kernel.service.CompanyLocalServiceUtil;
024    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
025    import com.liferay.portal.kernel.util.LocaleThreadLocal;
026    import com.liferay.portal.kernel.util.TimeZoneThreadLocal;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    @ProviderType
032    public class CompanyThreadLocal {
033    
034            public static Long getCompanyId() {
035                    Long companyId = _companyId.get();
036    
037                    if (_log.isDebugEnabled()) {
038                            _log.debug("getCompanyId " + companyId);
039                    }
040    
041                    return companyId;
042            }
043    
044            public static boolean isDeleteInProcess() {
045                    return _deleteInProcess.get();
046            }
047    
048            public static void setCompanyId(Long companyId) {
049                    if (_log.isDebugEnabled()) {
050                            _log.debug("setCompanyId " + companyId);
051                    }
052    
053                    if (companyId > 0) {
054                            _companyId.set(companyId);
055    
056                            try {
057                                    Company company = CompanyLocalServiceUtil.getCompany(companyId);
058    
059                                    LocaleThreadLocal.setDefaultLocale(company.getLocale());
060                                    TimeZoneThreadLocal.setDefaultTimeZone(company.getTimeZone());
061                            }
062                            catch (Exception e) {
063                                    _log.error(e, e);
064                            }
065                    }
066                    else {
067                            _companyId.set(CompanyConstants.SYSTEM);
068    
069                            LocaleThreadLocal.setDefaultLocale(null);
070                            TimeZoneThreadLocal.setDefaultTimeZone(null);
071                    }
072            }
073    
074            public static void setDeleteInProcess(boolean deleteInProcess) {
075                    _deleteInProcess.set(deleteInProcess);
076            }
077    
078            private static final Log _log = LogFactoryUtil.getLog(
079                    CompanyThreadLocal.class);
080    
081            private static final ThreadLocal<Long> _companyId =
082                    new AutoResetThreadLocal<>(
083                            CompanyThreadLocal.class + "._companyId", CompanyConstants.SYSTEM);
084            private static final ThreadLocal<Boolean> _deleteInProcess =
085                    new AutoResetThreadLocal<>(
086                            CompanyThreadLocal.class + "._deleteInProcess", false);
087    
088    }