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.test;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
019    import com.liferay.portal.kernel.util.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.UnicodeProperties;
022    import com.liferay.portal.model.CustomizedPages;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.LayoutConstants;
026    import com.liferay.portal.model.LayoutPrototype;
027    import com.liferay.portal.model.LayoutSetPrototype;
028    import com.liferay.portal.model.LayoutTemplate;
029    import com.liferay.portal.model.LayoutTypePortlet;
030    import com.liferay.portal.model.Portlet;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portal.service.LayoutPrototypeLocalServiceUtil;
033    import com.liferay.portal.service.LayoutServiceUtil;
034    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
035    import com.liferay.portal.service.ServiceContext;
036    import com.liferay.portal.test.randomizerbumpers.FriendlyURLRandomizerBumper;
037    import com.liferay.portal.test.randomizerbumpers.NumericStringRandomizerBumper;
038    import com.liferay.portal.test.randomizerbumpers.UniqueStringRandomizerBumper;
039    import com.liferay.portlet.PortletPreferencesFactoryUtil;
040    
041    import java.util.HashMap;
042    import java.util.List;
043    import java.util.Locale;
044    import java.util.Map;
045    
046    import javax.portlet.PortletPreferences;
047    
048    /**
049     * @author Manuel de la Pe??a
050     * @author Mate Thurzo
051     */
052    public class LayoutTestUtil {
053    
054            public static Layout addLayout(Group group) throws Exception {
055                    return addLayout(group.getGroupId());
056            }
057    
058            public static Layout addLayout(Group group, boolean privateLayout)
059                    throws Exception {
060    
061                    return addLayout(group.getGroupId(), privateLayout);
062            }
063    
064            public static Layout addLayout(
065                            Group group, boolean privateLayout, LayoutPrototype layoutPrototype,
066                            boolean linkEnabled)
067                    throws Exception {
068    
069                    return addLayout(
070                            group.getGroupId(), privateLayout, layoutPrototype, linkEnabled);
071            }
072    
073            public static Layout addLayout(Group group, long parentLayoutPlid)
074                    throws Exception {
075    
076                    return addLayout(group.getGroupId(), parentLayoutPlid);
077            }
078    
079            public static Layout addLayout(long groupId) throws Exception {
080                    return addLayout(groupId, false);
081            }
082    
083            public static Layout addLayout(long groupId, boolean privateLayout)
084                    throws Exception {
085    
086                    return addLayout(groupId, privateLayout, null, false);
087            }
088    
089            public static Layout addLayout(
090                            long groupId, boolean privateLayout,
091                            LayoutPrototype layoutPrototype, boolean linkEnabled)
092                    throws Exception {
093    
094                    return addLayout(
095                            groupId,
096                            RandomTestUtil.randomString(
097                                    FriendlyURLRandomizerBumper.INSTANCE,
098                                    NumericStringRandomizerBumper.INSTANCE,
099                                    UniqueStringRandomizerBumper.INSTANCE),
100                            privateLayout, layoutPrototype, linkEnabled);
101            }
102    
103            public static Layout addLayout(
104                            long groupId, boolean privateLayout, Map<Locale, String> nameMap,
105                            Map<Locale, String> friendlyURLMap)
106                    throws Exception {
107    
108                    ServiceContext serviceContext =
109                            ServiceContextTestUtil.getServiceContext(groupId);
110    
111                    return LayoutLocalServiceUtil.addLayout(
112                            serviceContext.getUserId(), groupId, privateLayout,
113                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, nameMap, nameMap,
114                            new HashMap<Locale, String>(), new HashMap<Locale, String>(),
115                            new HashMap<Locale, String>(), LayoutConstants.TYPE_PORTLET,
116                            StringPool.BLANK, false, friendlyURLMap, serviceContext);
117            }
118    
119            public static Layout addLayout(long groupId, long parentLayoutPlid)
120                    throws Exception {
121    
122                    Layout layout = addLayout(groupId, false);
123    
124                    LayoutLocalServiceUtil.updateParentLayoutId(
125                            layout.getPlid(), parentLayoutPlid);
126    
127                    return LayoutLocalServiceUtil.fetchLayout(layout.getPlid());
128            }
129    
130            public static Layout addLayout(
131                            long groupId, String name, boolean privateLayout)
132                    throws Exception {
133    
134                    return addLayout(groupId, name, privateLayout, null, false);
135            }
136    
137            public static Layout addLayout(
138                            long groupId, String name, boolean privateLayout,
139                            LayoutPrototype layoutPrototype, boolean linkEnabled)
140                    throws Exception {
141    
142                    String friendlyURL =
143                            StringPool.SLASH + FriendlyURLNormalizerUtil.normalize(name);
144    
145                    Layout layout = null;
146    
147                    try {
148                            layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
149                                    groupId, false, friendlyURL);
150    
151                            return layout;
152                    }
153                    catch (NoSuchLayoutException nsle) {
154                    }
155    
156                    String description = "This is a test page.";
157    
158                    ServiceContext serviceContext =
159                            ServiceContextTestUtil.getServiceContext();
160    
161                    if (layoutPrototype != null) {
162                            serviceContext.setAttribute(
163                                    "layoutPrototypeLinkEnabled", linkEnabled);
164                            serviceContext.setAttribute(
165                                    "layoutPrototypeUuid", layoutPrototype.getUuid());
166                    }
167    
168                    return LayoutLocalServiceUtil.addLayout(
169                            TestPropsValues.getUserId(), groupId, privateLayout,
170                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, name, null, description,
171                            LayoutConstants.TYPE_PORTLET, false, friendlyURL, serviceContext);
172            }
173    
174            public static Layout addLayout(
175                            long groupId, String name, long parentLayoutPlid)
176                    throws Exception {
177    
178                    Layout layout = addLayout(groupId, name, false);
179    
180                    LayoutLocalServiceUtil.updateParentLayoutId(
181                            layout.getPlid(), parentLayoutPlid);
182    
183                    return LayoutLocalServiceUtil.fetchLayout(layout.getPlid());
184            }
185    
186            public static LayoutPrototype addLayoutPrototype(String name)
187                    throws Exception {
188    
189                    HashMap<Locale, String> nameMap = new HashMap<Locale, String>();
190    
191                    nameMap.put(LocaleUtil.getDefault(), name);
192    
193                    return LayoutPrototypeLocalServiceUtil.addLayoutPrototype(
194                            TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
195                            nameMap, (Map<Locale, String>)null, true,
196                            ServiceContextTestUtil.getServiceContext());
197            }
198    
199            public static LayoutSetPrototype addLayoutSetPrototype(String name)
200                    throws Exception {
201    
202                    HashMap<Locale, String> nameMap = new HashMap<Locale, String>();
203    
204                    nameMap.put(LocaleUtil.getDefault(), name);
205    
206                    return LayoutSetPrototypeLocalServiceUtil.addLayoutSetPrototype(
207                            TestPropsValues.getUserId(), TestPropsValues.getCompanyId(),
208                            nameMap, (Map<Locale, String>)null, true, true,
209                            ServiceContextTestUtil.getServiceContext());
210            }
211    
212            public static String addPortletToLayout(Layout layout, String portletId)
213                    throws Exception {
214    
215                    Map<String, String[]> preferenceMap = null;
216    
217                    return addPortletToLayout(layout, portletId, preferenceMap);
218            }
219    
220            public static String addPortletToLayout(
221                            Layout layout, String portletId,
222                            Map<String, String[]> preferenceMap)
223                    throws Exception {
224    
225                    long userId = TestPropsValues.getUserId();
226    
227                    LayoutTypePortlet layoutTypePortlet =
228                            (LayoutTypePortlet)layout.getLayoutType();
229    
230                    LayoutTemplate layoutTemplate = layoutTypePortlet.getLayoutTemplate();
231    
232                    List<String> columns = layoutTemplate.getColumns();
233    
234                    String columnId = columns.get(0);
235    
236                    return addPortletToLayout(
237                            userId, layout, portletId, columnId, preferenceMap);
238            }
239    
240            public static String addPortletToLayout(
241                            long userId, Layout layout, String portletId, String columnId,
242                            Map<String, String[]> preferenceMap)
243                    throws Exception {
244    
245                    LayoutTypePortlet layoutTypePortlet =
246                            (LayoutTypePortlet)layout.getLayoutType();
247    
248                    String newPortletId = layoutTypePortlet.addPortletId(
249                            userId, portletId, columnId, -1);
250    
251                    LayoutLocalServiceUtil.updateLayout(
252                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
253                            layout.getTypeSettings());
254    
255                    if (preferenceMap == null) {
256                            return newPortletId;
257                    }
258    
259                    PortletPreferences portletPreferences = getPortletPreferences(
260                            layout, newPortletId);
261    
262                    for (String key : preferenceMap.keySet()) {
263                            portletPreferences.setValues(key, preferenceMap.get(key));
264                    }
265    
266                    portletPreferences.store();
267    
268                    return newPortletId;
269            }
270    
271            public static Layout addTypeArticleLayout(long groupId, String articleId)
272                    throws Exception {
273    
274                    Layout layout = addLayout(groupId, false);
275    
276                    UnicodeProperties typeSettingsProperties =
277                            layout.getTypeSettingsProperties();
278    
279                    typeSettingsProperties.setProperty("article-id", articleId);
280    
281                    layout.setType(LayoutConstants.TYPE_ARTICLE);
282    
283                    LayoutLocalServiceUtil.updateLayout(layout);
284    
285                    return layout;
286            }
287    
288            public static Layout addTypeLinkToLayoutLayout(
289                            long groupId, long linkedToLayoutId)
290                    throws Exception {
291    
292                    Layout layout = addLayout(groupId, false);
293    
294                    UnicodeProperties typeSettingsProperties =
295                            layout.getTypeSettingsProperties();
296    
297                    typeSettingsProperties.setProperty(
298                            "linkToLayoutId", String.valueOf(linkedToLayoutId));
299    
300                    layout.setType(LayoutConstants.TYPE_LINK_TO_LAYOUT);
301    
302                    LayoutLocalServiceUtil.updateLayout(layout);
303    
304                    return layout;
305            }
306    
307            public static String getLayoutTemplateId(Layout layout) {
308                    LayoutTypePortlet layoutTypePortlet =
309                            (LayoutTypePortlet)layout.getLayoutType();
310    
311                    return layoutTypePortlet.getLayoutTemplateId();
312            }
313    
314            public static PortletPreferences getPortletPreferences(
315                            Layout layout, String portletId)
316                    throws Exception {
317    
318                    return PortletPreferencesFactoryUtil.getPortletSetup(
319                            layout, portletId, null);
320            }
321    
322            public static PortletPreferences getPortletPreferences(
323                            long plid, String portletId)
324                    throws Exception {
325    
326                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
327    
328                    return getPortletPreferences(layout, portletId);
329            }
330    
331            public static List<Portlet> getPortlets(Layout layout) throws Exception {
332                    LayoutTypePortlet layoutTypePortlet =
333                            (LayoutTypePortlet)layout.getLayoutType();
334    
335                    return layoutTypePortlet.getPortlets();
336            }
337    
338            public static boolean isLayoutColumnCustomizable(
339                    Layout layout, String columnId) {
340    
341                    LayoutTypePortlet layoutTypePortlet =
342                            (LayoutTypePortlet)layout.getLayoutType();
343    
344                    return layoutTypePortlet.isColumnCustomizable(columnId);
345            }
346    
347            public static Layout updateLayoutColumnCustomizable(
348                            Layout layout, String columnId, boolean customizable)
349                    throws Exception {
350    
351                    LayoutTypePortlet layoutTypePortlet =
352                            (LayoutTypePortlet)layout.getLayoutType();
353    
354                    layoutTypePortlet.setTypeSettingsProperty(
355                            CustomizedPages.namespaceColumnId(columnId),
356                            String.valueOf(customizable));
357    
358                    layoutTypePortlet.setUpdatePermission(customizable);
359    
360                    return LayoutServiceUtil.updateLayout(
361                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
362                            layout.getTypeSettings());
363            }
364    
365            public static Layout updateLayoutPortletPreference(
366                            Layout layout, String portletId, String portletPreferenceName,
367                            String portletPreferenceValue)
368                    throws Exception {
369    
370                    PortletPreferences layoutPortletPreferences = getPortletPreferences(
371                            layout, portletId);
372    
373                    layoutPortletPreferences.setValue(
374                            portletPreferenceName, portletPreferenceValue);
375    
376                    layoutPortletPreferences.store();
377    
378                    return LayoutLocalServiceUtil.getLayout(layout.getPlid());
379            }
380    
381            public static Layout updateLayoutPortletPreferences(
382                            Layout layout, String portletId,
383                            Map<String, String> portletPreferences)
384                    throws Exception {
385    
386                    PortletPreferences layoutPortletPreferences = getPortletPreferences(
387                            layout, portletId);
388    
389                    for (Map.Entry<String, String> entry : portletPreferences.entrySet()) {
390                            layoutPortletPreferences.setValue(entry.getKey(), entry.getValue());
391                    }
392    
393                    layoutPortletPreferences.store();
394    
395                    return LayoutLocalServiceUtil.getLayout(layout.getPlid());
396            }
397    
398            public static Layout updateLayoutTemplateId(
399                            Layout layout, String layoutTemplateId)
400                    throws Exception {
401    
402                    LayoutTypePortlet layoutTypePortlet =
403                            (LayoutTypePortlet)layout.getLayoutType();
404    
405                    layoutTypePortlet.setLayoutTemplateId(
406                            TestPropsValues.getUserId(), layoutTemplateId);
407    
408                    return LayoutServiceUtil.updateLayout(
409                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
410                            layout.getTypeSettings());
411            }
412    
413    }