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.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                            _companyId.set(CompanyConstants.SYSTEM);
069    
070                            LocaleThreadLocal.setDefaultLocale(null);
071                            TimeZoneThreadLocal.setDefaultTimeZone(null);
072                    }
073            }
074    
075            public static void setDeleteInProcess(boolean deleteInProcess) {
076                    _deleteInProcess.set(deleteInProcess);
077            }
078    
079            private static final Log _log = LogFactoryUtil.getLog(
080                    CompanyThreadLocal.class);
081    
082            private static final ThreadLocal<Long> _companyId =
083                    new AutoResetThreadLocal<Long>(
084                            CompanyThreadLocal.class + "._companyId", CompanyConstants.SYSTEM);
085            private static final ThreadLocal<Boolean> _deleteInProcess =
086                    new AutoResetThreadLocal<Boolean>(
087                            CompanyThreadLocal.class + "._deleteInProcess", false);
088    
089    }