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.kernel.util.StringPool;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutConstants;
024    import com.liferay.portal.model.LayoutPrototype;
025    import com.liferay.portal.model.LayoutSet;
026    import com.liferay.portal.model.LayoutTypePortlet;
027    import com.liferay.portal.model.Portlet;
028    import com.liferay.portal.service.LayoutLocalServiceUtil;
029    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
030    import com.liferay.portal.service.PortletLocalServiceUtil;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portlet.PortletPreferencesFactoryUtil;
033    
034    import java.util.HashMap;
035    import java.util.List;
036    import java.util.Locale;
037    import java.util.Map;
038    import java.util.ResourceBundle;
039    
040    import javax.portlet.PortletPreferences;
041    
042    /**
043     * @author Eudaldo Alonso
044     */
045    public class DefaultLayoutPrototypesUtil {
046    
047            public static Layout addLayout(
048                            LayoutSet layoutSet, String nameKey, String friendlyURL,
049                            String layouteTemplateId)
050                    throws Exception {
051    
052                    Group group = layoutSet.getGroup();
053    
054                    Map<Locale, String> nameMap = new HashMap<>();
055    
056                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
057                            nameMap.put(locale, LanguageUtil.get(locale, nameKey));
058                    }
059    
060                    Map<Locale, String> friendlyURLMap = new HashMap<>();
061    
062                    friendlyURLMap.put(LocaleUtil.getDefault(), friendlyURL);
063    
064                    ServiceContext serviceContext = new ServiceContext();
065    
066                    Layout layout = LayoutLocalServiceUtil.addLayout(
067                            group.getCreatorUserId(), group.getGroupId(),
068                            layoutSet.isPrivateLayout(),
069                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, nameMap, null, null, null,
070                            null, LayoutConstants.TYPE_PORTLET, StringPool.BLANK, false,
071                            friendlyURLMap, serviceContext);
072    
073                    LayoutTypePortlet layoutTypePortlet =
074                            (LayoutTypePortlet)layout.getLayoutType();
075    
076                    layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
077    
078                    return layout;
079            }
080    
081            public static Layout addLayoutPrototype(
082                            long companyId, long defaultUserId, String nameKey,
083                            String descriptionKey, String layouteTemplateId,
084                            List<LayoutPrototype> layoutPrototypes, ClassLoader classLoader)
085                    throws Exception {
086    
087                    ResourceBundle resourceBundle = ResourceBundleUtil.getBundle(
088                            "content.Language", LocaleUtil.getDefault(), classLoader);
089    
090                    String name = LanguageUtil.get(resourceBundle, nameKey);
091                    String description = LanguageUtil.get(resourceBundle, descriptionKey);
092    
093                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
094                            String curName = layoutPrototype.getName(LocaleUtil.getDefault());
095                            String curDescription = layoutPrototype.getDescription(
096                                    LocaleUtil.getDefault());
097    
098                            if (name.equals(curName) && description.equals(curDescription)) {
099                                    return null;
100                            }
101                    }
102    
103                    Map<Locale, String> nameMap = new HashMap<>();
104                    Map<Locale, String> descriptionMap = new HashMap<>();
105    
106                    for (Locale locale : LanguageUtil.getAvailableLocales()) {
107                            resourceBundle = ResourceBundleUtil.getBundle(
108                                    "content.Language", locale, classLoader);
109    
110                            nameMap.put(locale, LanguageUtil.get(resourceBundle, nameKey));
111                            descriptionMap.put(
112                                    locale, LanguageUtil.get(resourceBundle, descriptionKey));
113                    }
114    
115                    LayoutPrototype layoutPrototype =
116                            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
117                                    defaultUserId, companyId, nameMap, descriptionMap, true,
118                                    new ServiceContext());
119    
120                    Layout layout = layoutPrototype.getLayout();
121    
122                    LayoutTypePortlet layoutTypePortlet =
123                            (LayoutTypePortlet)layout.getLayoutType();
124    
125                    layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
126    
127                    return layout;
128            }
129    
130            public static String addPortletId(
131                            Layout layout, String portletId, String columnId)
132                    throws Exception {
133    
134                    LayoutTypePortlet layoutTypePortlet =
135                            (LayoutTypePortlet)layout.getLayoutType();
136    
137                    portletId = layoutTypePortlet.addPortletId(
138                            0, portletId, columnId, -1, false);
139    
140                    updateLayout(layout);
141    
142                    addResourcePermissions(layout, portletId);
143    
144                    return portletId;
145            }
146    
147            public static PortletPreferences updatePortletSetup(
148                            Layout layout, String portletId, Map<String, String> preferences)
149                    throws Exception {
150    
151                    PortletPreferences portletSetup =
152                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
153                                    layout, portletId);
154    
155                    for (Map.Entry<String, String> entry : preferences.entrySet()) {
156                            String key = entry.getKey();
157                            String value = entry.getValue();
158    
159                            portletSetup.setValue(key, value);
160                    }
161    
162                    portletSetup.store();
163    
164                    return portletSetup;
165            }
166    
167            protected static void addResourcePermissions(
168                            Layout layout, String portletId)
169                    throws Exception {
170    
171                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
172                            layout.getCompanyId(), portletId);
173    
174                    PortalUtil.addPortletDefaultResource(
175                            layout.getCompanyId(), layout, portlet);
176            }
177    
178            protected static void updateLayout(Layout layout) throws Exception {
179                    LayoutLocalServiceUtil.updateLayout(
180                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
181                            layout.getTypeSettings());
182            }
183    
184    }