001    /**
002     * Copyright (c) 2000-2010 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.portlet.myplaces.action;
016    
017    import com.liferay.portal.kernel.util.ParamUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.LayoutConstants;
021    import com.liferay.portal.service.LayoutLocalServiceUtil;
022    import com.liferay.portal.service.LayoutServiceUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portlet.PortletURLImpl;
029    
030    import java.util.List;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.PortletMode;
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletURL;
038    import javax.portlet.WindowState;
039    
040    import javax.servlet.http.HttpServletRequest;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class EditPagesAction extends PortletAction {
049    
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String redirect = ParamUtil.getString(actionRequest, "redirect");
056    
057                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
058                    boolean privateLayout = ParamUtil.getBoolean(
059                            actionRequest, "privateLayout");
060    
061                    Layout layout = null;
062    
063                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
064                            groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0,
065                            1);
066    
067                    if (layouts.size() > 0) {
068                            layout = layouts.get(0);
069                    }
070                    else {
071                            long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
072                            String name = "New Page";
073                            String title = StringPool.BLANK;
074                            String description = StringPool.BLANK;
075                            String type = LayoutConstants.TYPE_PORTLET;
076                            boolean hidden = false;
077                            String friendlyURL = StringPool.BLANK;
078    
079                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
080                                    Layout.class.getName(), actionRequest);
081    
082                            layout = LayoutServiceUtil.addLayout(
083                                    groupId, privateLayout, parentLayoutId, name, title,
084                                    description, type, hidden, friendlyURL, serviceContext);
085                    }
086    
087                    if (layout != null) {
088                            String tabs1 = "public-pages";
089    
090                            if (privateLayout) {
091                                    tabs1 = "private-pages";
092                            }
093    
094                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
095                                    actionRequest);
096    
097                            PortletURL portletURL = new PortletURLImpl(
098                                    request, PortletKeys.LAYOUT_MANAGEMENT, layout.getPlid(),
099                                    PortletRequest.RENDER_PHASE);
100    
101                            portletURL.setWindowState(WindowState.MAXIMIZED);
102                            portletURL.setPortletMode(PortletMode.VIEW);
103    
104                            portletURL.setParameter(
105                                    "struts_action", "/layout_management/edit_pages");
106                            portletURL.setParameter("tabs1", tabs1);
107                            portletURL.setParameter("redirect", redirect);
108                            portletURL.setParameter("groupId", String.valueOf(groupId));
109    
110                            actionResponse.sendRedirect(portletURL.toString());
111                    }
112            }
113    
114    }