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.service.util.test;
016    
017    import com.liferay.portal.kernel.test.util.TestPropsValues;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.Portlet;
021    import com.liferay.portal.model.PortletPreferences;
022    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
023    import com.liferay.portal.util.PortletKeys;
024    
025    /**
026     * @author Cristina Gonz??lez
027     */
028    public class PortletPreferencesTestUtil {
029    
030            public static PortletPreferences addGroupPortletPreferences(
031                            Layout layout, Portlet portlet)
032                    throws Exception {
033    
034                    return addGroupPortletPreferences(layout, portlet, null);
035            }
036    
037            public static PortletPreferences addGroupPortletPreferences(
038                            Layout layout, Portlet portlet, String defaultPreferences)
039                    throws Exception {
040    
041                    return PortletPreferencesLocalServiceUtil.addPortletPreferences(
042                            layout.getCompanyId(), layout.getGroupId(),
043                            PortletKeys.PREFS_OWNER_TYPE_GROUP, layout.getPlid(),
044                            portlet.getPortletId(), portlet, defaultPreferences);
045            }
046    
047            public static PortletPreferences addLayoutPortletPreferences(
048                            Layout layout, Portlet portlet)
049                    throws Exception {
050    
051                    return addLayoutPortletPreferences(layout, portlet, null);
052            }
053    
054            public static PortletPreferences addLayoutPortletPreferences(
055                            Layout layout, Portlet portlet, String defaultPreferences)
056                    throws Exception {
057    
058                    return PortletPreferencesLocalServiceUtil.addPortletPreferences(
059                            TestPropsValues.getCompanyId(), PortletKeys.PREFS_OWNER_ID_DEFAULT,
060                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT, layout.getPlid(),
061                            portlet.getPortletId(), portlet, defaultPreferences);
062            }
063    
064            public static javax.portlet.PortletPreferences
065                            fetchLayoutJxPortletPreferences(
066                                    Layout layout, Portlet portlet)
067                    throws Exception {
068    
069                    return PortletPreferencesLocalServiceUtil.fetchPreferences(
070                            TestPropsValues.getCompanyId(), PortletKeys.PREFS_OWNER_ID_DEFAULT,
071                            PortletKeys.PREFS_OWNER_TYPE_LAYOUT, layout.getPlid(),
072                            portlet.getPortletId());
073            }
074    
075            public static String getPortletPreferencesXML() {
076                    return getPortletPreferencesXML(null, null);
077            }
078    
079            public static String getPortletPreferencesXML(String name) {
080                    return getPortletPreferencesXML(name, null);
081            }
082    
083            public static String getPortletPreferencesXML(
084                    String name, String[] values) {
085    
086                    StringBundler sb = new StringBundler();
087    
088                    sb.append("<portlet-preferences>");
089    
090                    if ((name != null) || (values != null)) {
091                            sb.append("<preference>");
092    
093                            if (name != null) {
094                                    sb.append("<name>");
095                                    sb.append(name);
096                                    sb.append("</name>");
097                            }
098    
099                            if (values != null) {
100                                    for (String value : values) {
101                                            sb.append("<value>");
102                                            sb.append(value);
103                                            sb.append("</value>");
104                                    }
105                            }
106    
107                            sb.append("</preference>");
108                    }
109    
110                    sb.append("</portlet-preferences>");
111    
112                    return sb.toString();
113            }
114    
115            public static String getPortletPreferencesXML(String[] values) {
116                    return getPortletPreferencesXML(null, values);
117            }
118    
119    }