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.test.util;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.PrefsPropsUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.TimeZoneUtil;
023    import com.liferay.portal.model.Company;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.security.auth.CompanyThreadLocal;
026    import com.liferay.portal.service.CompanyLocalServiceUtil;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    
029    import java.util.Collection;
030    import java.util.Locale;
031    
032    import javax.portlet.PortletPreferences;
033    
034    /**
035     * @author Manuel de la Pe??a
036     */
037    public class CompanyTestUtil {
038    
039            public static Company addCompany() throws Exception {
040                    return addCompany(RandomTestUtil.randomString());
041            }
042    
043            public static Company addCompany(String name) throws Exception {
044                    String virtualHostname = name + "." + RandomTestUtil.randomString(3);
045    
046                    return CompanyLocalServiceUtil.addCompany(
047                            name, virtualHostname, virtualHostname, false, 0, true);
048            }
049    
050            public static void resetCompanyLocales(
051                            long companyId, Collection<Locale> locales, Locale defaultLocale)
052                    throws Exception {
053    
054                    String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
055    
056                    String languageIds = StringUtil.merge(
057                            LocaleUtil.toLanguageIds(locales));
058    
059                    resetCompanyLocales(companyId, languageIds, defaultLanguageId);
060            }
061    
062            public static void resetCompanyLocales(
063                            long companyId, String languageIds, String defaultLanguageId)
064                    throws Exception {
065    
066                    // Reset company default locale and timezone
067    
068                    User user = UserLocalServiceUtil.loadGetDefaultUser(companyId);
069    
070                    user.setLanguageId(defaultLanguageId);
071                    user.setTimeZoneId(TimeZoneUtil.getDefault().getID());
072    
073                    UserLocalServiceUtil.updateUser(user);
074    
075                    // Reset company supported locales
076    
077                    PortletPreferences preferences = PrefsPropsUtil.getPreferences(
078                            companyId);
079    
080                    preferences.setValue(PropsKeys.LOCALES, languageIds);
081    
082                    preferences.store();
083    
084                    // Reset company locales cache
085    
086                    LanguageUtil.resetAvailableLocales(companyId);
087    
088                    // Reset thread locals
089    
090                    CompanyThreadLocal.setCompanyId(companyId);
091            }
092    
093    }