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                            String tabs1 = "public-pages";
091    
092                            if (privateLayout) {
093                                    tabs1 = "private-pages";
094                            }
095    
096                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
097                                    actionRequest);
098    
099                            PortletURL portletURL = new PortletURLImpl(
100                                    request, PortletKeys.LAYOUTS_ADMIN, layout.getPlid(),
101                                    PortletRequest.RENDER_PHASE);
102    
103                            portletURL.setWindowState(WindowState.MAXIMIZED);
104                            portletURL.setPortletMode(PortletMode.VIEW);
105    
106                            portletURL.setParameter(
107                                    "struts_action", "/layouts_admin/edit_layouts");
108                            portletURL.setParameter("tabs1", tabs1);
109                            portletURL.setParameter("redirect", redirect);
110                            portletURL.setParameter("groupId", String.valueOf(groupId));
111    
112                            actionResponse.sendRedirect(portletURL.toString());
113                    }
114            }
115    
116    }