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