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