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.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            @Override
051            public void processAction(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String redirect = ParamUtil.getString(actionRequest, "redirect");
058    
059                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
060                    boolean privateLayout = ParamUtil.getBoolean(
061                            actionRequest, "privateLayout");
062    
063                    Layout layout = null;
064    
065                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
066                            groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
067                            false, 0, 1);
068    
069                    if (layouts.size() > 0) {
070                            layout = layouts.get(0);
071                    }
072                    else {
073                            long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
074                            String name = "New Page";
075                            String title = StringPool.BLANK;
076                            String description = StringPool.BLANK;
077                            String type = LayoutConstants.TYPE_PORTLET;
078                            boolean hidden = false;
079                            String friendlyURL = StringPool.BLANK;
080    
081                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
082                                    Layout.class.getName(), actionRequest);
083    
084                            layout = LayoutServiceUtil.addLayout(
085                                    groupId, privateLayout, parentLayoutId, name, title,
086                                    description, type, hidden, friendlyURL, serviceContext);
087                    }
088    
089                    if (layout == null) {
090                            return;
091                    }
092    
093                    String tabs1 = "public-pages";
094    
095                    if (privateLayout) {
096                            tabs1 = "private-pages";
097                    }
098    
099                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
100                            actionRequest);
101    
102                    PortletURL portletURL = new PortletURLImpl(
103                            request, PortletKeys.LAYOUTS_ADMIN, layout.getPlid(),
104                            PortletRequest.RENDER_PHASE);
105    
106                    portletURL.setParameter("struts_action", "/layouts_admin/edit_layouts");
107                    portletURL.setParameter("tabs1", tabs1);
108                    portletURL.setParameter("redirect", redirect);
109                    portletURL.setParameter("groupId", String.valueOf(groupId));
110                    portletURL.setPortletMode(PortletMode.VIEW);
111                    portletURL.setWindowState(WindowState.MAXIMIZED);
112    
113                    actionResponse.sendRedirect(portletURL.toString());
114            }
115    
116    }