001
014
015 package com.liferay.portal.action;
016
017 import com.liferay.portal.events.EventsProcessorUtil;
018 import com.liferay.portal.exception.LayoutTypeException;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.util.Constants;
022 import com.liferay.portal.kernel.util.HttpUtil;
023 import com.liferay.portal.kernel.util.ParamUtil;
024 import com.liferay.portal.kernel.util.PropsKeys;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.kernel.util.WebKeys;
029 import com.liferay.portal.model.Layout;
030 import com.liferay.portal.model.LayoutConstants;
031 import com.liferay.portal.model.LayoutPrototype;
032 import com.liferay.portal.model.LayoutType;
033 import com.liferay.portal.security.permission.ActionKeys;
034 import com.liferay.portal.service.LayoutLocalServiceUtil;
035 import com.liferay.portal.service.LayoutPrototypeServiceUtil;
036 import com.liferay.portal.service.LayoutServiceUtil;
037 import com.liferay.portal.service.ServiceContext;
038 import com.liferay.portal.service.ServiceContextFactory;
039 import com.liferay.portal.service.permission.GroupPermissionUtil;
040 import com.liferay.portal.service.permission.LayoutPermissionUtil;
041 import com.liferay.portal.struts.JSONAction;
042 import com.liferay.portal.theme.ThemeDisplay;
043 import com.liferay.portal.util.PortalUtil;
044 import com.liferay.sites.kernel.util.SitesUtil;
045
046 import javax.servlet.http.HttpServletRequest;
047 import javax.servlet.http.HttpServletResponse;
048
049 import org.apache.struts.action.ActionForm;
050 import org.apache.struts.action.ActionMapping;
051
052
056 public class EditLayoutAction extends JSONAction {
057
058 @Override
059 public String getJSON(
060 ActionMapping actionMapping, ActionForm actionForm,
061 HttpServletRequest request, HttpServletResponse response)
062 throws Exception {
063
064 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
065 WebKeys.THEME_DISPLAY);
066
067 String cmd = ParamUtil.getString(request, Constants.CMD);
068
069 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
070
071 try {
072 if (cmd.equals("add")) {
073 String[] array = addPage(themeDisplay, request, response);
074
075 jsonObject.put("deletable", Boolean.valueOf(array[2]));
076 jsonObject.put("layoutId", array[0]);
077 jsonObject.put("sortable", Boolean.valueOf(array[3]));
078 jsonObject.put("updateable", Boolean.valueOf(array[4]));
079 jsonObject.put("url", array[1]);
080 }
081 else if (cmd.equals("delete")) {
082 SitesUtil.deleteLayout(request, response);
083 }
084 else if (cmd.equals("display_order")) {
085 updateDisplayOrder(request);
086 }
087 else if (cmd.equals("name")) {
088 updateName(request);
089 }
090 else if (cmd.equals("parent_layout_id")) {
091 updateParentLayoutId(request);
092 }
093 else if (cmd.equals("priority")) {
094 updatePriority(request);
095 }
096
097 jsonObject.put("status", HttpServletResponse.SC_OK);
098 }
099 catch (LayoutTypeException lte) {
100 jsonObject.put(
101 "message",
102 getLayoutTypeExceptionMessage(themeDisplay, lte, cmd));
103
104 long plid = ParamUtil.getLong(request, "plid");
105
106 if ((lte.getType() == LayoutTypeException.FIRST_LAYOUT) &&
107 (plid > 0)) {
108
109 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
110
111 jsonObject.put("groupId", layout.getGroupId());
112 jsonObject.put("layoutId", layout.getLayoutId());
113 jsonObject.put(
114 "originalParentLayoutId", layout.getParentLayoutId());
115 jsonObject.put("originalParentPlid", layout.getParentPlid());
116 jsonObject.put("originalPriority", layout.getPriority());
117 jsonObject.put("plid", plid);
118
119 jsonObject.put("status", HttpServletResponse.SC_BAD_REQUEST);
120 }
121 }
122
123 return jsonObject.toString();
124 }
125
126 protected String[] addPage(
127 ThemeDisplay themeDisplay, HttpServletRequest request,
128 HttpServletResponse response)
129 throws Exception {
130
131 String doAsUserId = ParamUtil.getString(request, "doAsUserId");
132 String doAsUserLanguageId = ParamUtil.getString(
133 request, "doAsUserLanguageId");
134
135 long groupId = ParamUtil.getLong(request, "groupId");
136 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
137 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
138 String name = ParamUtil.getString(request, "name", "New Page");
139 String title = StringPool.BLANK;
140 String description = StringPool.BLANK;
141 String type = LayoutConstants.TYPE_PORTLET;
142 boolean hidden = false;
143 String friendlyURL = StringPool.BLANK;
144 long layoutPrototypeId = ParamUtil.getLong(
145 request, "layoutPrototypeId");
146
147 ServiceContext serviceContext = ServiceContextFactory.getInstance(
148 request);
149
150 Layout layout = null;
151
152 if (layoutPrototypeId > 0) {
153 LayoutPrototype layoutPrototype =
154 LayoutPrototypeServiceUtil.getLayoutPrototype(
155 layoutPrototypeId);
156
157 serviceContext.setAttribute(
158 "layoutPrototypeUuid", layoutPrototype.getUuid());
159
160 layout = LayoutServiceUtil.addLayout(
161 groupId, privateLayout, parentLayoutId, name, title,
162 description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
163 serviceContext);
164 }
165 else {
166 layout = LayoutServiceUtil.addLayout(
167 groupId, privateLayout, parentLayoutId, name, title,
168 description, type, hidden, friendlyURL, serviceContext);
169 }
170
171 LayoutType layoutType = layout.getLayoutType();
172
173 EventsProcessorUtil.process(
174 PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
175 layoutType.getConfigurationActionUpdate(), request, response);
176
177 String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
178
179 if (Validator.isNotNull(doAsUserId)) {
180 layoutURL = HttpUtil.addParameter(
181 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
182 }
183
184 if (Validator.isNotNull(doAsUserLanguageId)) {
185 layoutURL = HttpUtil.addParameter(
186 layoutURL, "doAsUserLanguageId",
187 themeDisplay.getDoAsUserLanguageId());
188 }
189
190 boolean deleteable = LayoutPermissionUtil.contains(
191 themeDisplay.getPermissionChecker(), layout, ActionKeys.DELETE);
192 boolean sortable = GroupPermissionUtil.contains(
193 themeDisplay.getPermissionChecker(), layout.getGroupId(),
194 ActionKeys.MANAGE_LAYOUTS) &&
195 SitesUtil.isLayoutSortable(layout);
196 boolean updateable = LayoutPermissionUtil.contains(
197 themeDisplay.getPermissionChecker(), layout, ActionKeys.UPDATE);
198
199 return new String[] {
200 String.valueOf(layout.getLayoutId()), layoutURL,
201 String.valueOf(deleteable), String.valueOf(sortable),
202 String.valueOf(updateable)
203 };
204 }
205
206 protected String getLayoutTypeExceptionMessage(
207 ThemeDisplay themeDisplay, LayoutTypeException lte, String cmd) {
208
209 if (Validator.isNotNull(cmd)) {
210 if (cmd.equals("delete") &&
211 (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
212
213 return themeDisplay.translate(
214 "you-cannot-delete-this-page-because-the-next-page-is-of-" +
215 "type-x-and-so-cannot-be-the-first-page",
216 "layout.types." + lte.getLayoutType());
217 }
218
219 if (cmd.equals("delete") &&
220 (lte.getType() ==
221 LayoutTypeException.FIRST_LAYOUT_PERMISSION)) {
222
223 return themeDisplay.translate(
224 "you-cannot-delete-this-page-because-the-next-page-is-" +
225 "not-vieweable-by-unathenticated-users-and-so-cannot-" +
226 "be-the-first-page");
227 }
228
229 if ((cmd.equals("display_order") || cmd.equals("priority")) &&
230 (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
231
232 return themeDisplay.translate(
233 "you-cannot-move-this-page-because-the-resulting-order-" +
234 "would-place-a-page-of-type-x-as-the-first-page",
235 "layout.types." + lte.getLayoutType());
236 }
237
238 if (cmd.equals("parent_layout_id") &&
239 (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
240
241 return themeDisplay.translate(
242 "you-cannot-move-this-page-because-the-resulting-order-" +
243 "would-place-a-page-of-type-x-as-the-first-page",
244 "layout.types." + lte.getLayoutType());
245 }
246 }
247
248 if (lte.getType() == LayoutTypeException.FIRST_LAYOUT ) {
249 return themeDisplay.translate(
250 "the-first-page-cannot-be-of-type-x",
251 "layout.types." + lte.getLayoutType());
252 }
253 else if (lte.getType() == LayoutTypeException.NOT_PARENTABLE) {
254 return themeDisplay.translate(
255 "a-page-cannot-become-a-child-of-a-page-that-is-not-" +
256 "parentable");
257 }
258
259 return StringPool.BLANK;
260 }
261
262 protected void updateDisplayOrder(HttpServletRequest request)
263 throws Exception {
264
265 long groupId = ParamUtil.getLong(request, "groupId");
266 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
267 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
268 long[] layoutIds = StringUtil.split(
269 ParamUtil.getString(request, "layoutIds"), 0L);
270
271 ServiceContext serviceContext = ServiceContextFactory.getInstance(
272 request);
273
274 LayoutServiceUtil.setLayouts(
275 groupId, privateLayout, parentLayoutId, layoutIds, serviceContext);
276 }
277
278 protected void updateName(HttpServletRequest request) throws Exception {
279 long plid = ParamUtil.getLong(request, "plid");
280
281 long groupId = ParamUtil.getLong(request, "groupId");
282 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
283 long layoutId = ParamUtil.getLong(request, "layoutId");
284 String name = ParamUtil.getString(request, "name");
285 String languageId = ParamUtil.getString(request, "languageId");
286
287 if (plid <= 0) {
288 LayoutServiceUtil.updateName(
289 groupId, privateLayout, layoutId, name, languageId);
290 }
291 else {
292 LayoutServiceUtil.updateName(plid, name, languageId);
293 }
294 }
295
296 protected void updateParentLayoutId(HttpServletRequest request)
297 throws Exception {
298
299 long plid = ParamUtil.getLong(request, "plid");
300 long parentPlid = ParamUtil.getLong(request, "parentPlid");
301 int priority = ParamUtil.getInteger(request, "priority");
302
303 LayoutServiceUtil.updateParentLayoutIdAndPriority(
304 plid, parentPlid, priority);
305 }
306
307 protected void updatePriority(HttpServletRequest request) throws Exception {
308 long plid = ParamUtil.getLong(request, "plid");
309
310 long groupId = ParamUtil.getLong(request, "groupId");
311 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
312 long layoutId = ParamUtil.getLong(request, "layoutId");
313 long nextLayoutId = ParamUtil.getLong(request, "nextLayoutId");
314 long previousLayoutId = ParamUtil.getLong(request, "previousLayoutId");
315 int priority = ParamUtil.getInteger(request, "priority");
316
317 if (plid <= 0) {
318 LayoutServiceUtil.updatePriority(
319 groupId, privateLayout, layoutId, nextLayoutId,
320 previousLayoutId);
321 }
322 else {
323 LayoutServiceUtil.updatePriority(plid, priority);
324 }
325 }
326
327 }