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