001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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                            boolean locked = false;
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, locked, 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    }