1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.imagegallery.action;
16  
17  import com.liferay.portal.NoSuchLayoutException;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.model.Layout;
20  import com.liferay.portal.model.LayoutConstants;
21  import com.liferay.portal.model.LayoutTypePortlet;
22  import com.liferay.portal.service.LayoutLocalServiceUtil;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portlet.PortletURLImpl;
26  import com.liferay.portlet.imagegallery.model.IGFolder;
27  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
28  
29  import javax.portlet.PortletMode;
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletURL;
32  import javax.portlet.WindowState;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  import org.apache.struts.action.Action;
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="FindFolderAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class FindFolderAction extends Action {
48  
49      public ActionForward execute(
50              ActionMapping mapping, ActionForm form, HttpServletRequest request,
51              HttpServletResponse response)
52          throws Exception {
53  
54          try {
55              long plid = ParamUtil.getLong(request, "p_l_id");
56              long folderId = ParamUtil.getLong(request, "folderId");
57  
58              plid = getPlid(plid, folderId);
59  
60              PortletURL portletURL = new PortletURLImpl(
61                  request, PortletKeys.IMAGE_GALLERY, plid,
62                  PortletRequest.RENDER_PHASE);
63  
64              portletURL.setWindowState(WindowState.NORMAL);
65              portletURL.setPortletMode(PortletMode.VIEW);
66  
67              portletURL.setParameter("struts_action", "/image_gallery/view");
68              portletURL.setParameter("folderId", String.valueOf(folderId));
69  
70              response.sendRedirect(portletURL.toString());
71  
72              return null;
73          }
74          catch (Exception e) {
75              PortalUtil.sendError(e, request, response);
76  
77              return null;
78          }
79      }
80  
81      protected long getPlid(long plid, long folderId) throws Exception {
82          if (plid != LayoutConstants.DEFAULT_PLID) {
83              try {
84                  Layout layout = LayoutLocalServiceUtil.getLayout(plid);
85  
86                  LayoutTypePortlet layoutTypePortlet =
87                      (LayoutTypePortlet)layout.getLayoutType();
88  
89                  if (layoutTypePortlet.hasPortletId(PortletKeys.IMAGE_GALLERY)) {
90                      return plid;
91                  }
92              }
93              catch (NoSuchLayoutException nsle) {
94              }
95          }
96  
97          IGFolder folder = IGFolderLocalServiceUtil.getFolder(folderId);
98  
99          plid = PortalUtil.getPlidFromPortletId(
100             folder.getGroupId(), PortletKeys.IMAGE_GALLERY);
101 
102         if (plid != LayoutConstants.DEFAULT_PLID) {
103             return plid;
104         }
105         else {
106             throw new NoSuchLayoutException(
107                 "No page was found with the Image Gallery portlet.");
108         }
109     }
110 
111 }