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 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                            try {
056                                    Company company = CompanyLocalServiceUtil.getCompany(companyId);
057    
058                                    LocaleThreadLocal.setDefaultLocale(company.getLocale());
059                                    TimeZoneThreadLocal.setDefaultTimeZone(company.getTimeZone());
060                            }
061                            catch (Exception e) {
062                                    _log.error(e, e);
063                            }
064    
065                            _companyId.set(companyId);
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    }