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