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.util.test;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.Accessor;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.PrefsPropsUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.PropsUtil;
025    import com.liferay.portal.kernel.util.TimeZoneUtil;
026    import com.liferay.portal.model.Company;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.security.auth.CompanyThreadLocal;
029    import com.liferay.portal.service.CompanyLocalServiceUtil;
030    import com.liferay.portal.service.UserLocalServiceUtil;
031    
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, Locale[] locales, Locale defaultLocale)
057                    throws Exception {
058    
059                    String defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
060    
061                    String languageIds = ArrayUtil.toString(locales, _accessor);
062    
063                    resetCompanyLocales(companyId, languageIds, defaultLanguageId);
064            }
065    
066            public static void resetCompanyLocales(
067                            long companyId, String languageIds, String defaultLanguageId)
068                    throws Exception {
069    
070                    // Reset company default locale and timezone
071    
072                    User user = UserLocalServiceUtil.loadGetDefaultUser(companyId);
073    
074                    user.setLanguageId(defaultLanguageId);
075                    user.setTimeZoneId(TimeZoneUtil.getDefault().getID());
076    
077                    UserLocalServiceUtil.updateUser(user);
078    
079                    // Reset company supported locales
080    
081                    PortletPreferences preferences = PrefsPropsUtil.getPreferences(
082                            companyId);
083    
084                    preferences.setValue(PropsKeys.LOCALES, languageIds);
085    
086                    preferences.store();
087    
088                    // Reset company locales cache
089    
090                    LanguageUtil.resetAvailableLocales(companyId);
091    
092                    // Reset thread locals
093    
094                    CompanyThreadLocal.setCompanyId(companyId);
095            }
096    
097            private static final Accessor<Locale, String> _accessor =
098                    new Accessor<Locale, String>() {
099    
100                            @Override
101                            public String get(Locale locale) {
102                                    return LocaleUtil.toLanguageId(locale);
103                            }
104    
105                            @Override
106                            public Class<String> getAttributeClass() {
107                                    return String.class;
108                            }
109    
110                            @Override
111                            public Class<Locale> getTypeClass() {
112                                    return Locale.class;
113                            }
114    
115                    };
116    
117    }