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.model.PortletConstants;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.PortletURLFactoryUtil;
031    
032    import javax.portlet.PortletMode;
033    import javax.portlet.PortletRequest;
034    import javax.portlet.PortletURL;
035    import javax.portlet.WindowState;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    import org.apache.struts.action.Action;
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public abstract class FindAction extends Action {
049    
050            public FindAction() {
051                    _portletIds = initPortletIds();
052    
053                    if ((_portletIds == null) || (_portletIds.length == 0)) {
054                            throw new RuntimeException("Portlet IDs cannot be null or empty");
055                    }
056            }
057    
058            @Override
059            public ActionForward execute(
060                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
061                            HttpServletResponse response)
062                    throws Exception {
063    
064                    try {
065                            long plid = ParamUtil.getLong(request, "p_l_id");
066                            long primaryKey = ParamUtil.getLong(
067                                    request, getPrimaryKeyParameterName());
068    
069                            Object[] plidAndPortletId = getPlidAndPortletId(
070                                    request, plid, primaryKey);
071    
072                            plid = (Long)plidAndPortletId[0];
073    
074                            String portletId = (String)plidAndPortletId[1];
075    
076                            PortletURL portletURL = PortletURLFactoryUtil.create(
077                                    request, portletId, plid, PortletRequest.RENDER_PHASE);
078    
079                            portletURL.setParameter(
080                                    "struts_action", getStrutsAction(request, portletId));
081    
082                            String redirect = ParamUtil.getString(request, "redirect");
083    
084                            if (Validator.isNotNull(redirect)) {
085                                    portletURL.setParameter("redirect", redirect);
086                            }
087    
088                            setPrimaryKeyParameter(portletURL, primaryKey);
089    
090                            portletURL.setPortletMode(PortletMode.VIEW);
091                            portletURL.setWindowState(WindowState.NORMAL);
092    
093                            portletURL = processPortletURL(request, portletURL);
094    
095                            response.sendRedirect(portletURL.toString());
096    
097                            return null;
098                    }
099                    catch (Exception e) {
100                            String noSuchEntryRedirect = ParamUtil.getString(
101                                    request, "noSuchEntryRedirect");
102    
103                            if (Validator.isNotNull(noSuchEntryRedirect) &&
104                                    (e instanceof NoSuchLayoutException)) {
105    
106                                    response.sendRedirect(noSuchEntryRedirect);
107                            }
108                            else {
109                                    PortalUtil.sendError(e, request, response);
110                            }
111    
112                            return null;
113                    }
114            }
115    
116            protected abstract long getGroupId(long primaryKey) throws Exception;
117    
118            protected Object[] getPlidAndPortletId(
119                            HttpServletRequest request, long plid, long primaryKey)
120                    throws Exception {
121    
122                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
123                            WebKeys.THEME_DISPLAY);
124    
125                    long groupId = ParamUtil.getLong(
126                            request, "groupId", themeDisplay.getScopeGroupId());
127    
128                    if (primaryKey > 0) {
129                            try {
130                                    groupId = getGroupId(primaryKey);
131                            }
132                            catch (Exception e) {
133                                    if (_log.isDebugEnabled()) {
134                                            _log.debug(e, e);
135                                    }
136                            }
137                    }
138    
139                    if ((plid != LayoutConstants.DEFAULT_PLID) &&
140                            (groupId == themeDisplay.getScopeGroupId())) {
141    
142                            try {
143                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
144    
145                                    LayoutTypePortlet layoutTypePortlet =
146                                            (LayoutTypePortlet)layout.getLayoutType();
147    
148                                    for (String portletId : _portletIds) {
149                                            if (layoutTypePortlet.hasPortletId(portletId)) {
150                                                    return new Object[] {plid, portletId};
151                                            }
152                                    }
153                            }
154                            catch (NoSuchLayoutException nsle) {
155                            }
156                    }
157    
158                    for (String portletId : _portletIds) {
159                            plid = PortalUtil.getPlidFromPortletId(groupId, portletId);
160    
161                            if (plid != LayoutConstants.DEFAULT_PLID) {
162                                    Layout layout = LayoutLocalServiceUtil.getLayout(plid);
163    
164                                    LayoutTypePortlet layoutTypePortlet =
165                                            (LayoutTypePortlet)layout.getLayoutType();
166    
167                                    for (String curPortletId : layoutTypePortlet.getPortletIds()) {
168                                            String curRootPortletId = PortletConstants.getRootPortletId(
169                                                    curPortletId);
170    
171                                            if (portletId.equals(curRootPortletId)) {
172                                                    portletId = curPortletId;
173    
174                                                    break;
175                                            }
176                                    }
177    
178                                    return new Object[] {plid, portletId};
179                            }
180                    }
181    
182                    throw new NoSuchLayoutException();
183            }
184    
185            protected abstract String getPrimaryKeyParameterName();
186    
187            protected abstract String getStrutsAction(
188                    HttpServletRequest request, String portletId);
189    
190            protected abstract String[] initPortletIds();
191    
192            protected PortletURL processPortletURL(
193                            HttpServletRequest request, PortletURL portletURL)
194                    throws Exception {
195    
196                    return portletURL;
197            }
198    
199            protected void setPrimaryKeyParameter(
200                            PortletURL portletURL, long primaryKey)
201                    throws Exception {
202    
203                    portletURL.setParameter(
204                            getPrimaryKeyParameterName(), String.valueOf(primaryKey));
205            }
206    
207            private static Log _log = LogFactoryUtil.getLog(FindAction.class);
208    
209            private String[] _portletIds;
210    
211    }