001
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
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 portletURL.setParameter(
088 getPrimaryKeyParameterName(), String.valueOf(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 if (plid != LayoutConstants.DEFAULT_PLID) {
123 try {
124 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
125
126 LayoutTypePortlet layoutTypePortlet =
127 (LayoutTypePortlet)layout.getLayoutType();
128
129 for (String portletId : _portletIds) {
130 if (layoutTypePortlet.hasPortletId(portletId)) {
131 return new Object[] {plid, portletId};
132 }
133 }
134 }
135 catch (NoSuchLayoutException nsle) {
136 }
137 }
138
139 long groupId = 0;
140
141 if (primaryKey > 0) {
142 try {
143 groupId = getGroupId(primaryKey);
144 }
145 catch (Exception e) {
146 if (_log.isDebugEnabled()) {
147 _log.debug(e, e);
148 }
149 }
150 }
151
152 if (groupId <= 0) {
153 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
154 WebKeys.THEME_DISPLAY);
155
156 groupId = themeDisplay.getScopeGroupId();
157 }
158
159 for (String portletId : _portletIds) {
160 plid = PortalUtil.getPlidFromPortletId(groupId, portletId);
161
162 if (plid != LayoutConstants.DEFAULT_PLID) {
163 return new Object[] {plid, portletId};
164 }
165 }
166
167 throw new NoSuchLayoutException();
168 }
169
170 protected abstract String getPrimaryKeyParameterName();
171
172 protected abstract String getStrutsAction(
173 HttpServletRequest request, String portletId);
174
175 protected abstract String[] initPortletIds();
176
177 protected PortletURL processPortletURL(
178 HttpServletRequest request, PortletURL portletURL)
179 throws Exception {
180
181 return portletURL;
182 }
183
184 private static Log _log = LogFactoryUtil.getLog(FindAction.class);
185
186 private String[] _portletIds;
187
188 }