1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.myplaces.action;
21  
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.model.Layout;
25  import com.liferay.portal.model.LayoutConstants;
26  import com.liferay.portal.service.LayoutLocalServiceUtil;
27  import com.liferay.portal.service.LayoutServiceUtil;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portlet.PortletURLImpl;
32  
33  import java.util.List;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.PortletMode;
39  import javax.portlet.PortletRequest;
40  import javax.portlet.PortletURL;
41  import javax.portlet.WindowState;
42  
43  import javax.servlet.http.HttpServletRequest;
44  
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="EditPagesAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class EditPagesAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          String redirect = ParamUtil.getString(actionRequest, "redirect");
62  
63          long groupId = ParamUtil.getLong(actionRequest, "groupId");
64          boolean privateLayout = ParamUtil.getBoolean(
65              actionRequest, "privateLayout");
66  
67          Layout layout = null;
68  
69          List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
70              groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, 0,
71              1);
72  
73          if (layouts.size() > 0) {
74              layout = layouts.get(0);
75          }
76          else {
77              long parentLayoutId = LayoutConstants.DEFAULT_PARENT_LAYOUT_ID;
78              String name = "New Page";
79              String title = StringPool.BLANK;
80              String description = StringPool.BLANK;
81              String type = LayoutConstants.TYPE_PORTLET;
82              boolean hidden = false;
83              String friendlyURL = StringPool.BLANK;
84  
85              layout = LayoutServiceUtil.addLayout(
86                  groupId, privateLayout, parentLayoutId, name, title,
87                  description, type, hidden, friendlyURL);
88          }
89  
90          if (layout != null) {
91              String tabs1 = "public-pages";
92  
93              if (privateLayout) {
94                  tabs1 = "private-pages";
95              }
96  
97              HttpServletRequest request = PortalUtil.getHttpServletRequest(
98                  actionRequest);
99  
100             PortletURL portletURL = new PortletURLImpl(
101                 request, PortletKeys.LAYOUT_MANAGEMENT, layout.getPlid(),
102                 PortletRequest.RENDER_PHASE);
103 
104             portletURL.setWindowState(WindowState.MAXIMIZED);
105             portletURL.setPortletMode(PortletMode.VIEW);
106 
107             portletURL.setParameter(
108                 "struts_action", "/layout_management/edit_pages");
109             portletURL.setParameter("tabs1", tabs1);
110             portletURL.setParameter("redirect", redirect);
111             portletURL.setParameter("groupId", String.valueOf(groupId));
112 
113             actionResponse.sendRedirect(portletURL.toString());
114         }
115     }
116 
117 }