001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.struts;
016    
017    import com.liferay.portal.NoSuchLayoutException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutConstants;
025    import com.liferay.portal.model.LayoutTypePortlet;
026    import com.liferay.portal.service.LayoutLocalServiceUtil;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.PortletURLFactoryUtil;
030    
031    import javax.portlet.PortletMode;
032    import javax.portlet.PortletRequest;
033    import javax.portlet.PortletURL;
034    import javax.portlet.WindowState;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    
039    import org.apache.struts.action.Action;
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public abstract class FindAction extends Action {
048    
049            public FindAction() {
050                    _portletIds = initPortletIds();
051    
052                    if ((_portletIds == null) || (_portletIds.length == 0)) {
053                            throw new RuntimeException("Portlet IDs cannot be null or empty");
054                    }
055            }
056    
057            @Override
058            public ActionForward execute(
059                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
060                            HttpServletResponse response)
061                    throws Exception {
062    
063                    try {
064                            long plid = ParamUtil.getLong(request, "p_l_id");
065                            long primaryKey = ParamUtil.getLong(
066                                    request, getPrimaryKeyParameterName());
067    
068                            Object[] plidAndPortletId = getPlidAndPortletId(
069                                    request, plid, primaryKey);
070    
071                            plid = (Long)plidAndPortletId[0];
072    
073                            String portletId = (String)plidAndPortletId[1];
074    
075                            PortletURL portletURL = PortletURLFactoryUtil.create(
076                                    request, portletId, plid, PortletRequest.RENDER_PHASE);
077    
078                            portletURL.setParameter(
079                                    "struts_action", getStrutsAction(request, portletId));
080    
081                            String redirect = ParamUtil.getString(request, "redirect");
082    
083                            if (Validator.isNotNull(redirect)) {
084                                    portletURL.setParameter("redirect", redirect);
085                            }
086    
087                            setPrimaryKeyParameter(portletURL, primaryKey);
088    
089                            portletURL.setPortletMode(PortletMode.VIEW);
090                            portletURL.setWindowState(WindowState.NORMAL);
091    
092                            portletURL = processPortletURL(request, portletURL);
093    
094                            response.sendRedirect(portletURL.toString());
095    
096                            return null;
097                    }
098                    catch (Exception e) {
099                            String noSuchEntryRedirect = ParamUtil.getString(
100                                    request, "noSuchEntryRedirect");
101    
102                            if (Validator.isNotNull(noSuchEntryRedirect) &&
103                                    (e instanceof NoSuchLayoutException)) {
104    
105                                    response.sendRedirect(noSuchEntryRedirect);
106                            }
107                            else {
108                                    PortalUtil.sendError(e, request, response);
109                            }
110    
111                            return null;
112                    }
113            }
114    
115            protected abstract long getGroupId(long primaryKey) throws Exception;
116    
117            protected Object[] getPlidAndPortletId(
118                            HttpServletRequest request, long plid, long primaryKey)
119                    throws Exception {
120    
121                    if (plid != LayoutConstants.DEFAULT_PLID) {
122                            try {
123                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
124    
125                                    LayoutTypePortlet layoutTypePortlet =
126                                            (LayoutTypePortlet)layout.getLayoutType();
127    
128                                    for (String portletId : _portletIds) {
129                                            if (layoutTypePortlet.hasPortletId(portletId)) {
130                                                    return new Object[] {plid, portletId};
131                                            }
132                                    }
133                            }
134                            catch (NoSuchLayoutException nsle) {
135                            }
136                    }
137    
138                    long groupId = 0;
139    
140                    if (primaryKey > 0) {
141                            try {
142                                    groupId = getGroupId(primaryKey);
143                            }
144                            catch (Exception e) {
145                                    if (_log.isDebugEnabled()) {
146                                            _log.debug(e, e);
147                                    }
148                            }
149                    }
150    
151                    if (groupId <= 0) {
152                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
153                                    WebKeys.THEME_DISPLAY);
154    
155                            groupId = themeDisplay.getScopeGroupId();
156                    }
157    
158                    for (String portletId : _portletIds) {
159                            plid = PortalUtil.getPlidFromPortletId(groupId, portletId);
160    
161                            if (plid != LayoutConstants.DEFAULT_PLID) {
162                                    return new Object[] {plid, portletId};
163                            }
164                    }
165    
166                    throw new NoSuchLayoutException();
167            }
168    
169            protected abstract String getPrimaryKeyParameterName();
170    
171            protected abstract String getStrutsAction(
172                    HttpServletRequest request, String portletId);
173    
174            protected abstract String[] initPortletIds();
175    
176            protected PortletURL processPortletURL(
177                            HttpServletRequest request, PortletURL portletURL)
178                    throws Exception {
179    
180                    return portletURL;
181            }
182    
183            protected void setPrimaryKeyParameter(
184                            PortletURL portletURL, long primaryKey)
185                    throws Exception {
186    
187                    portletURL.setParameter(
188                            getPrimaryKeyParameterName(), String.valueOf(primaryKey));
189            }
190    
191            private static Log _log = LogFactoryUtil.getLog(FindAction.class);
192    
193            private String[] _portletIds;
194    
195    }