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                    String description = descriptionMap.get(LocaleUtil.getDefault());
085    
086                    for (LayoutPrototype layoutPrototype : layoutPrototypes) {
087                            String curName = layoutPrototype.getName(LocaleUtil.getDefault());
088                            String curDescription = layoutPrototype.getDescription(
089                                    LocaleUtil.getDefault());
090    
091                            if (name.equals(curName) && description.equals(curDescription)) {
092                                    return null;
093                            }
094                    }
095    
096                    LayoutPrototype layoutPrototype =
097                            LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
098                                    defaultUserId, companyId, nameMap, descriptionMap, true,
099                                    new ServiceContext());
100    
101                    Layout layout = layoutPrototype.getLayout();
102    
103                    LayoutTypePortlet layoutTypePortlet =
104                            (LayoutTypePortlet)layout.getLayoutType();
105    
106                    layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
107    
108                    return layout;
109            }
110    
111            public static String addPortletId(
112                            Layout layout, String portletId, String columnId)
113                    throws Exception {
114    
115                    LayoutTypePortlet layoutTypePortlet =
116                            (LayoutTypePortlet)layout.getLayoutType();
117    
118                    portletId = layoutTypePortlet.addPortletId(
119                            0, portletId, columnId, -1, false);
120    
121                    updateLayout(layout);
122    
123                    addResourcePermissions(layout, portletId);
124    
125                    return portletId;
126            }
127    
128            public static PortletPreferences updatePortletSetup(
129                            Layout layout, String portletId, Map<String, String> preferences)
130                    throws Exception {
131    
132                    PortletPreferences portletSetup =
133                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
134                                    layout, portletId);
135    
136                    for (Map.Entry<String, String> entry : preferences.entrySet()) {
137                            String key = entry.getKey();
138                            String value = entry.getValue();
139    
140                            portletSetup.setValue(key, value);
141                    }
142    
143                    portletSetup.store();
144    
145                    return portletSetup;
146            }
147    
148            protected static void addResourcePermissions(
149                            Layout layout, String portletId)
150                    throws Exception {
151    
152                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
153                            layout.getCompanyId(), portletId);
154    
155                    PortalUtil.addPortletDefaultResource(
156                            layout.getCompanyId(), layout, portlet);
157            }
158    
159            protected static void updateLayout(Layout layout) throws Exception {
160                    LayoutLocalServiceUtil.updateLayout(
161                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
162                            layout.getTypeSettings());
163            }
164    
165    }