001    /**
002     * Copyright (c) 2000-2010 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.imagegallery.action;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.LayoutConstants;
021    import com.liferay.portal.model.LayoutTypePortlet;
022    import com.liferay.portal.service.LayoutLocalServiceUtil;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.PortletURLImpl;
026    import com.liferay.portlet.imagegallery.model.IGFolder;
027    import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
028    
029    import javax.portlet.PortletMode;
030    import javax.portlet.PortletRequest;
031    import javax.portlet.PortletURL;
032    import javax.portlet.WindowState;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    import org.apache.struts.action.Action;
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class FindFolderAction extends Action {
046    
047            public ActionForward execute(
048                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
049                            HttpServletResponse response)
050                    throws Exception {
051    
052                    try {
053                            long plid = ParamUtil.getLong(request, "p_l_id");
054                            long folderId = ParamUtil.getLong(request, "folderId");
055    
056                            plid = getPlid(plid, folderId);
057    
058                            PortletURL portletURL = new PortletURLImpl(
059                                    request, PortletKeys.IMAGE_GALLERY, plid,
060                                    PortletRequest.RENDER_PHASE);
061    
062                            portletURL.setWindowState(WindowState.NORMAL);
063                            portletURL.setPortletMode(PortletMode.VIEW);
064    
065                            portletURL.setParameter("struts_action", "/image_gallery/view");
066                            portletURL.setParameter("folderId", String.valueOf(folderId));
067    
068                            response.sendRedirect(portletURL.toString());
069    
070                            return null;
071                    }
072                    catch (Exception e) {
073                            PortalUtil.sendError(e, request, response);
074    
075                            return null;
076                    }
077            }
078    
079            protected long getPlid(long plid, long folderId) throws Exception {
080                    if (plid != LayoutConstants.DEFAULT_PLID) {
081                            try {
082                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
083    
084                                    LayoutTypePortlet layoutTypePortlet =
085                                            (LayoutTypePortlet)layout.getLayoutType();
086    
087                                    if (layoutTypePortlet.hasPortletId(PortletKeys.IMAGE_GALLERY)) {
088                                            return plid;
089                                    }
090                            }
091                            catch (NoSuchLayoutException nsle) {
092                            }
093                    }
094    
095                    IGFolder folder = IGFolderLocalServiceUtil.getFolder(folderId);
096    
097                    plid = PortalUtil.getPlidFromPortletId(
098                            folder.getGroupId(), PortletKeys.IMAGE_GALLERY);
099    
100                    if (plid != LayoutConstants.DEFAULT_PLID) {
101                            return plid;
102                    }
103                    else {
104                            throw new NoSuchLayoutException(
105                                    "No page was found with the Image Gallery portlet.");
106                    }
107            }
108    
109    }