1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.imagegallery.action;
24  
25  import com.liferay.portal.NoSuchLayoutException;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.LayoutConstants;
29  import com.liferay.portal.model.LayoutTypePortlet;
30  import com.liferay.portal.service.LayoutLocalServiceUtil;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PortletKeys;
33  import com.liferay.portlet.PortletURLImpl;
34  import com.liferay.portlet.imagegallery.model.IGFolder;
35  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
36  
37  import javax.portlet.PortletMode;
38  import javax.portlet.PortletRequest;
39  import javax.portlet.PortletURL;
40  import javax.portlet.WindowState;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import org.apache.struts.action.Action;
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="FindFolderAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   */
55  public class FindFolderAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          try {
63              long plid = ParamUtil.getLong(request, "p_l_id");
64              long folderId = ParamUtil.getLong(request, "folderId");
65  
66              plid = getPlid(plid, folderId);
67  
68              PortletURL portletURL = new PortletURLImpl(
69                  request, PortletKeys.IMAGE_GALLERY, plid,
70                  PortletRequest.RENDER_PHASE);
71  
72              portletURL.setWindowState(WindowState.NORMAL);
73              portletURL.setPortletMode(PortletMode.VIEW);
74  
75              portletURL.setParameter("struts_action", "/image_gallery/view");
76              portletURL.setParameter("folderId", String.valueOf(folderId));
77  
78              response.sendRedirect(portletURL.toString());
79  
80              return null;
81          }
82          catch (Exception e) {
83              PortalUtil.sendError(e, request, response);
84  
85              return null;
86          }
87      }
88  
89      protected long getPlid(long plid, long folderId) throws Exception {
90          if (plid != LayoutConstants.DEFAULT_PLID) {
91              try {
92                  Layout layout = LayoutLocalServiceUtil.getLayout(plid);
93  
94                  LayoutTypePortlet layoutTypePortlet =
95                      (LayoutTypePortlet)layout.getLayoutType();
96  
97                  if (layoutTypePortlet.hasPortletId(PortletKeys.IMAGE_GALLERY)) {
98                      return plid;
99                  }
100             }
101             catch (NoSuchLayoutException nsle) {
102             }
103         }
104 
105         IGFolder folder = IGFolderLocalServiceUtil.getFolder(folderId);
106 
107         plid = PortalUtil.getPlidFromPortletId(
108             folder.getGroupId(), PortletKeys.IMAGE_GALLERY);
109 
110         if (plid != LayoutConstants.DEFAULT_PLID) {
111             return plid;
112         }
113         else {
114             throw new NoSuchLayoutException(
115                 "No page was found with the Image Gallery portlet.");
116         }
117     }
118 
119 }