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