1
22
23 package com.liferay.portlet.myplaces.action;
24
25 import com.liferay.portal.NoSuchLayoutSetException;
26 import com.liferay.portal.kernel.servlet.SessionErrors;
27 import com.liferay.portal.kernel.util.GetterUtil;
28 import com.liferay.portal.kernel.util.ParamUtil;
29 import com.liferay.portal.model.Layout;
30 import com.liferay.portal.model.LayoutConstants;
31 import com.liferay.portal.security.permission.ActionKeys;
32 import com.liferay.portal.security.permission.PermissionChecker;
33 import com.liferay.portal.service.LayoutLocalServiceUtil;
34 import com.liferay.portal.service.permission.LayoutPermissionUtil;
35 import com.liferay.portal.struts.PortletAction;
36 import com.liferay.portal.theme.ThemeDisplay;
37 import com.liferay.portal.util.PortalUtil;
38 import com.liferay.portal.util.WebKeys;
39
40 import java.util.List;
41
42 import javax.portlet.ActionRequest;
43 import javax.portlet.ActionResponse;
44 import javax.portlet.PortletConfig;
45 import javax.portlet.RenderRequest;
46 import javax.portlet.RenderResponse;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50
51 import org.apache.struts.action.ActionForm;
52 import org.apache.struts.action.ActionForward;
53 import org.apache.struts.action.ActionMapping;
54
55
60 public class ViewAction extends PortletAction {
61
62 public ActionForward strutsExecute(
63 ActionMapping mapping, ActionForm form, HttpServletRequest request,
64 HttpServletResponse response)
65 throws Exception {
66
67 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
68 WebKeys.THEME_DISPLAY);
69
70 long groupId = ParamUtil.getLong(request, "groupId");
71 String privateLayoutParam = request.getParameter("privateLayout");
72
73 String redirect = getRedirect(
74 themeDisplay, groupId, privateLayoutParam);
75
76 if (redirect == null) {
77 redirect = ParamUtil.getString(request, "redirect");
78
79 SessionErrors.add(
80 request, NoSuchLayoutSetException.class.getName(),
81 new NoSuchLayoutSetException(
82 "{groupId=" + groupId + ",privateLayout=" +
83 privateLayoutParam + "}"));
84 }
85
86 response.sendRedirect(redirect);
87
88 return null;
89 }
90
91 public void processAction(
92 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93 ActionRequest actionRequest, ActionResponse actionResponse)
94 throws Exception {
95
96 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
97 WebKeys.THEME_DISPLAY);
98
99 long groupId = ParamUtil.getLong(actionRequest, "groupId");
100 String privateLayoutParam = actionRequest.getParameter("privateLayout");
101
102 String redirect = getRedirect(
103 themeDisplay, groupId, privateLayoutParam);
104
105 if (redirect == null) {
106 redirect = ParamUtil.getString(actionRequest, "redirect");
107
108 SessionErrors.add(
109 actionRequest, NoSuchLayoutSetException.class.getName(),
110 new NoSuchLayoutSetException(
111 "{groupId=" + groupId + ",privateLayout=" +
112 privateLayoutParam + "}"));
113 }
114
115 actionResponse.sendRedirect(redirect);
116 }
117
118 public ActionForward render(
119 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
120 RenderRequest renderRequest, RenderResponse renderResponse)
121 throws Exception {
122
123 return mapping.findForward("portlet.my_places.view");
124 }
125
126 protected List<Layout> getLayouts(long groupId, boolean privateLayout)
127 throws Exception {
128
129 return LayoutLocalServiceUtil.getLayouts(
130 groupId, privateLayout, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
131 }
132
133 protected String getRedirect(
134 ThemeDisplay themeDisplay, long groupId, String privateLayoutParam)
135 throws Exception {
136
137 List<Layout> layouts = null;
138
139 if (privateLayoutParam == null) {
140 layouts = getLayouts(groupId, false);
141
142 if (layouts.size() == 0) {
143 layouts = getLayouts(groupId, true);
144 }
145 }
146 else {
147 boolean privateLayout = GetterUtil.getBoolean(privateLayoutParam);
148
149 layouts = getLayouts(groupId, privateLayout);
150 }
151
152 String redirect = null;
153
154 for (Layout layout : layouts) {
155 PermissionChecker permissionChecker =
156 themeDisplay.getPermissionChecker();
157
158 if (LayoutPermissionUtil.contains(
159 permissionChecker, layout, ActionKeys.VIEW)){
160
161 redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
162
163 break;
164 }
165 }
166
167 return redirect;
168 }
169
170 protected boolean isCheckMethodOnProcessAction() {
171 return _CHECK_METHOD_ON_PROCESS_ACTION;
172 }
173
174 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
175
176 }