001    /**
002     * Copyright (c) 2000-2013 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.events;
016    
017    import com.liferay.portal.kernel.events.SimpleAction;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.LayoutConstants;
022    import com.liferay.portal.model.LayoutSet;
023    import com.liferay.portal.model.LayoutTypePortlet;
024    import com.liferay.portal.model.Portlet;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.service.PortletLocalServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.PortletPreferencesFactoryUtil;
030    
031    import java.util.Map;
032    
033    import javax.portlet.PortletPreferences;
034    
035    /**
036     * @author Eudaldo Alonso
037     */
038    public abstract class BaseDefaultLayoutPrototypesAction extends SimpleAction {
039    
040            protected Layout addLayout(
041                            LayoutSet layoutSet, String name, String friendlyURL,
042                            String layouteTemplateId)
043                    throws Exception {
044    
045                    Group group = layoutSet.getGroup();
046    
047                    ServiceContext serviceContext = new ServiceContext();
048    
049                    Layout layout = LayoutLocalServiceUtil.addLayout(
050                            group.getCreatorUserId(), group.getGroupId(),
051                            layoutSet.isPrivateLayout(),
052                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, name, StringPool.BLANK,
053                            StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
054                            serviceContext);
055    
056                    LayoutTypePortlet layoutTypePortlet =
057                            (LayoutTypePortlet)layout.getLayoutType();
058    
059                    layoutTypePortlet.setLayoutTemplateId(0, layouteTemplateId, false);
060    
061                    return layout;
062            }
063    
064            protected String addPortletId(
065                            Layout layout, String portletId, String columnId)
066                    throws Exception {
067    
068                    LayoutTypePortlet layoutTypePortlet =
069                            (LayoutTypePortlet)layout.getLayoutType();
070    
071                    portletId = layoutTypePortlet.addPortletId(
072                            0, portletId, columnId, -1, false);
073    
074                    updateLayout(layout);
075    
076                    addResourcePermissions(layout, portletId);
077    
078                    return portletId;
079            }
080    
081            protected void addResourcePermissions(Layout layout, String portletId)
082                    throws Exception {
083    
084                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
085                            layout.getCompanyId(), portletId);
086    
087                    PortalUtil.addPortletDefaultResource(
088                            layout.getCompanyId(), layout, portlet);
089            }
090    
091            protected void updateLayout(Layout layout) throws Exception {
092                    LayoutLocalServiceUtil.updateLayout(
093                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
094                            layout.getTypeSettings());
095            }
096    
097            protected PortletPreferences updatePortletSetup(
098                            Layout layout, String portletId, Map<String, String> preferences)
099                    throws Exception {
100    
101                    PortletPreferences portletSetup =
102                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
103                                    layout, portletId);
104    
105                    for (Map.Entry<String, String> entry : preferences.entrySet()) {
106                            String key = entry.getKey();
107                            String value = entry.getValue();
108    
109                            portletSetup.setValue(key, value);
110                    }
111    
112                    portletSetup.store();
113    
114                    return portletSetup;
115            }
116    
117    }