001
014
015 package com.liferay.portal.struts;
016
017 import com.liferay.portal.NoSuchLayoutException;
018 import com.liferay.portal.kernel.util.ParamUtil;
019 import com.liferay.portal.kernel.util.Validator;
020 import com.liferay.portal.kernel.util.WebKeys;
021 import com.liferay.portal.model.Layout;
022 import com.liferay.portal.model.LayoutConstants;
023 import com.liferay.portal.model.LayoutTypePortlet;
024 import com.liferay.portal.service.LayoutLocalServiceUtil;
025 import com.liferay.portal.theme.ThemeDisplay;
026 import com.liferay.portal.util.PortalUtil;
027 import com.liferay.portlet.PortletURLFactoryUtil;
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
045 public abstract class FindAction extends Action {
046
047 public FindAction() {
048 _portletIds = initPortletIds();
049
050 if ((_portletIds == null) || (_portletIds.length == 0)) {
051 throw new RuntimeException("Portlet IDs cannot be null or empty");
052 }
053 }
054
055 @Override
056 public ActionForward execute(
057 ActionMapping mapping, ActionForm form, HttpServletRequest request,
058 HttpServletResponse response)
059 throws Exception {
060
061 try {
062 long plid = ParamUtil.getLong(request, "p_l_id");
063 long primaryKey = ParamUtil.getLong(
064 request, getPrimaryKeyParameterName());
065
066 Object[] plidAndPortletId = getPlidAndPortletId(
067 request, plid, primaryKey);
068
069 plid = (Long)plidAndPortletId[0];
070
071 String portletId = (String)plidAndPortletId[1];
072
073 PortletURL portletURL = PortletURLFactoryUtil.create(
074 request, portletId, plid, PortletRequest.RENDER_PHASE);
075
076 portletURL.setParameter(
077 "struts_action", getStrutsAction(request, portletId));
078
079 String redirect = ParamUtil.getString(request, "redirect");
080
081 if (Validator.isNotNull(redirect)) {
082 portletURL.setParameter("redirect", redirect);
083 }
084
085 portletURL.setParameter(
086 getPrimaryKeyParameterName(), String.valueOf(primaryKey));
087
088 portletURL.setPortletMode(PortletMode.VIEW);
089 portletURL.setWindowState(WindowState.NORMAL);
090
091 portletURL = processPortletURL(request, portletURL);
092
093 response.sendRedirect(portletURL.toString());
094
095 return null;
096 }
097 catch (Exception e) {
098 String noSuchEntryRedirect = ParamUtil.getString(
099 request, "noSuchEntryRedirect");
100
101 if (Validator.isNotNull(noSuchEntryRedirect) &&
102 (e instanceof NoSuchLayoutException)) {
103
104 response.sendRedirect(noSuchEntryRedirect);
105 }
106 else {
107 PortalUtil.sendError(e, request, response);
108 }
109
110 return null;
111 }
112 }
113
114 protected abstract long getGroupId(long primaryKey) throws Exception;
115
116 protected Object[] getPlidAndPortletId(
117 HttpServletRequest request, long plid, long primaryKey)
118 throws Exception {
119
120 if (plid != LayoutConstants.DEFAULT_PLID) {
121 try {
122 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
123
124 LayoutTypePortlet layoutTypePortlet =
125 (LayoutTypePortlet)layout.getLayoutType();
126
127 for (String portletId : _portletIds) {
128 if (layoutTypePortlet.hasPortletId(portletId)) {
129 return new Object[] {plid, portletId};
130 }
131 }
132 }
133 catch (NoSuchLayoutException nsle) {
134 }
135 }
136
137 long groupId = getGroupId(primaryKey);
138
139 if (groupId == 0) {
140 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
141 WebKeys.THEME_DISPLAY);
142
143 groupId = themeDisplay.getScopeGroupId();
144 }
145
146 for (String portletId : _portletIds) {
147 plid = PortalUtil.getPlidFromPortletId(groupId, portletId);
148
149 if (plid != LayoutConstants.DEFAULT_PLID) {
150 return new Object[] {plid, portletId};
151 }
152 }
153
154 throw new NoSuchLayoutException();
155 }
156
157 protected abstract String getPrimaryKeyParameterName();
158
159 protected abstract String getStrutsAction(
160 HttpServletRequest request, String portletId);
161
162 protected abstract String[] initPortletIds();
163
164 protected PortletURL processPortletURL(
165 HttpServletRequest request, PortletURL portletURL)
166 throws Exception {
167
168 return portletURL;
169 }
170
171 private String[] _portletIds;
172
173 }