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