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