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;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.ResourceBundleUtil;
020    import com.liferay.portal.model.LayoutSet;
021    import com.liferay.portal.model.LayoutSetPrototype;
022    import com.liferay.portal.service.LayoutLocalServiceUtil;
023    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
024    import com.liferay.portal.service.ServiceContext;
025    
026    import java.util.HashMap;
027    import java.util.List;
028    import java.util.Locale;
029    import java.util.Map;
030    import java.util.ResourceBundle;
031    
032    /**
033     * @author Sergio Gonz??lez
034     */
035    public class DefaultLayoutSetPrototypesUtil {
036    
037            public static LayoutSet addLayoutSetPrototype(
038                            long companyId, long defaultUserId, String nameKey,
039                            String descriptionKey, List<LayoutSetPrototype> layoutSetPrototypes,
040                            ClassLoader classLoader)
041                    throws Exception {
042    
043                    ResourceBundle resourceBundle = ResourceBundleUtil.getBundle(
044                            "content.Language", LocaleUtil.getDefault(), classLoader);
045    
046                    String name = LanguageUtil.get(resourceBundle, nameKey);
047                    String description = LanguageUtil.get(resourceBundle, descriptionKey);
048    
049                    for (LayoutSetPrototype layoutSetPrototype : layoutSetPrototypes) {
050                            String curName = layoutSetPrototype.getName(
051                                    LocaleUtil.getDefault());
052                            String curDescription = layoutSetPrototype.getDescription(
053                                    LocaleUtil.getDefault());
054    
055                            if (name.equals(curName) && description.equals(curDescription)) {
056                                    return null;
057                            }
058                    }
059    
060                    Map<Locale, String> nameMap = new HashMap<>();
061                    Map<Locale, String> descriptionMap = new HashMap<>();
062    
063                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
064                            resourceBundle = ResourceBundleUtil.getBundle(
065                                    "content.Language", locale, classLoader);
066    
067                            nameMap.put(locale, LanguageUtil.get(resourceBundle, nameKey));
068                            descriptionMap.put(
069                                    locale, LanguageUtil.get(resourceBundle, descriptionKey));
070                    }
071    
072                    LayoutSetPrototype layoutSetPrototype =
073                            LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
074                                    defaultUserId, companyId, nameMap, descriptionMap, true, true,
075                                    new ServiceContext());
076    
077                    LayoutSet layoutSet = layoutSetPrototype.getLayoutSet();
078    
079                    ServiceContext serviceContext = new ServiceContext();
080    
081                    LayoutLocalServiceUtil.deleteLayouts(
082                            layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
083                            serviceContext);
084    
085                    return layoutSetPrototype.getLayoutSet();
086            }
087    
088    }