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