001
014
015 package com.liferay.portlet.layoutsadmin.action;
016
017 import com.liferay.portal.LayoutTypeException;
018 import com.liferay.portal.events.EventsProcessorUtil;
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.model.Layout;
029 import com.liferay.portal.model.LayoutConstants;
030 import com.liferay.portal.model.LayoutPrototype;
031 import com.liferay.portal.security.permission.ActionKeys;
032 import com.liferay.portal.service.LayoutLocalServiceUtil;
033 import com.liferay.portal.service.LayoutPrototypeServiceUtil;
034 import com.liferay.portal.service.LayoutServiceUtil;
035 import com.liferay.portal.service.ServiceContext;
036 import com.liferay.portal.service.ServiceContextFactory;
037 import com.liferay.portal.service.permission.GroupPermissionUtil;
038 import com.liferay.portal.service.permission.LayoutPermissionUtil;
039 import com.liferay.portal.struts.JSONAction;
040 import com.liferay.portal.theme.ThemeDisplay;
041 import com.liferay.portal.util.LayoutSettings;
042 import com.liferay.portal.util.PortalUtil;
043 import com.liferay.portal.util.WebKeys;
044 import com.liferay.portlet.sites.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 UpdateLayoutAction 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 LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
172
173 EventsProcessorUtil.process(
174 PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
175 layoutSettings.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 =
193 GroupPermissionUtil.contains(
194 themeDisplay.getPermissionChecker(), layout.getGroupId(),
195 ActionKeys.MANAGE_LAYOUTS) &&
196 SitesUtil.isLayoutSortable(layout);
197 boolean updateable = LayoutPermissionUtil.contains(
198 themeDisplay.getPermissionChecker(), layout, ActionKeys.UPDATE);
199
200 return new String[] {
201 String.valueOf(layout.getLayoutId()), layoutURL,
202 String.valueOf(deleteable), String.valueOf(sortable),
203 String.valueOf(updateable)
204 };
205 }
206
207 protected String getLayoutTypeExceptionMessage(
208 ThemeDisplay themeDisplay, LayoutTypeException lte, String cmd) {
209
210 if (Validator.isNotNull(cmd)) {
211 if (cmd.equals("delete") &&
212 (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
213
214 return themeDisplay.translate(
215 "you-cannot-delete-this-page-because-the-next-page-is-of-" +
216 "type-x-and-so-cannot-be-the-first-page",
217 "layout.types." + lte.getLayoutType());
218 }
219
220 if (cmd.equals("delete") &&
221 (lte.getType() ==
222 LayoutTypeException.FIRST_LAYOUT_PERMISSION)) {
223
224 return themeDisplay.translate(
225 "you-cannot-delete-this-page-because-the-next-page-is-" +
226 "not-vieweable-by-unathenticated-users-and-so-cannot-" +
227 "be-the-first-page");
228 }
229
230 if ((cmd.equals("display_order") || cmd.equals("priority")) &&
231 (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
232
233 return themeDisplay.translate(
234 "you-cannot-move-this-page-because-the-resulting-order-" +
235 "would-place-a-page-of-type-x-as-the-first-page",
236 "layout.types." + lte.getLayoutType());
237 }
238 }
239
240 if (lte.getType() == LayoutTypeException.FIRST_LAYOUT ) {
241 return themeDisplay.translate(
242 "the-first-page-cannot-be-of-type-x",
243 "layout.types." + lte.getLayoutType());
244 }
245 else if (lte.getType() == LayoutTypeException.NOT_PARENTABLE) {
246 return themeDisplay.translate(
247 "a-page-cannot-become-a-child-of-a-page-that-is-not-" +
248 "parentable");
249 }
250
251 return StringPool.BLANK;
252 }
253
254 protected void updateDisplayOrder(HttpServletRequest request)
255 throws Exception {
256
257 long groupId = ParamUtil.getLong(request, "groupId");
258 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
259 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
260 long[] layoutIds = StringUtil.split(
261 ParamUtil.getString(request, "layoutIds"), 0L);
262
263 ServiceContext serviceContext = ServiceContextFactory.getInstance(
264 request);
265
266 LayoutServiceUtil.setLayouts(
267 groupId, privateLayout, parentLayoutId, layoutIds, serviceContext);
268 }
269
270 protected void updateName(HttpServletRequest request) throws Exception {
271 long plid = ParamUtil.getLong(request, "plid");
272
273 long groupId = ParamUtil.getLong(request, "groupId");
274 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
275 long layoutId = ParamUtil.getLong(request, "layoutId");
276 String name = ParamUtil.getString(request, "name");
277 String languageId = ParamUtil.getString(request, "languageId");
278
279 if (plid <= 0) {
280 LayoutServiceUtil.updateName(
281 groupId, privateLayout, layoutId, name, languageId);
282 }
283 else {
284 LayoutServiceUtil.updateName(plid, name, languageId);
285 }
286 }
287
288 protected void updateParentLayoutId(HttpServletRequest request)
289 throws Exception {
290
291 long plid = ParamUtil.getLong(request, "plid");
292 long parentPlid = ParamUtil.getLong(request, "parentPlid");
293 int priority = ParamUtil.getInteger(request, "priority");
294
295 LayoutServiceUtil.updateParentLayoutIdAndPriority(
296 plid, parentPlid, priority);
297 }
298
299 protected void updatePriority(HttpServletRequest request) throws Exception {
300 long plid = ParamUtil.getLong(request, "plid");
301
302 long groupId = ParamUtil.getLong(request, "groupId");
303 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
304 long layoutId = ParamUtil.getLong(request, "layoutId");
305 long nextLayoutId = ParamUtil.getLong(request, "nextLayoutId");
306 long previousLayoutId = ParamUtil.getLong(request, "previousLayoutId");
307 int priority = ParamUtil.getInteger(request, "priority");
308
309 if (plid <= 0) {
310 LayoutServiceUtil.updatePriority(
311 groupId, privateLayout, layoutId, nextLayoutId,
312 previousLayoutId);
313 }
314 else {
315 LayoutServiceUtil.updatePriority(plid, priority);
316 }
317 }
318
319 }