001
014
015 package com.liferay.portal.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.Validator;
027 import com.liferay.portal.model.Layout;
028 import com.liferay.portal.model.LayoutConstants;
029 import com.liferay.portal.model.LayoutPrototype;
030 import com.liferay.portal.model.LayoutType;
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.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 EditLayoutAction 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("name")) {
084 updateName(request);
085 }
086 else if (cmd.equals("parent_layout_id")) {
087 updateParentLayoutId(request);
088 }
089
090 jsonObject.put("status", HttpServletResponse.SC_OK);
091 }
092 catch (LayoutTypeException lte) {
093 jsonObject.put(
094 "message",
095 getLayoutTypeExceptionMessage(themeDisplay, lte, cmd));
096
097 long plid = ParamUtil.getLong(request, "plid");
098
099 if ((lte.getType() == LayoutTypeException.FIRST_LAYOUT) &&
100 (plid > 0)) {
101
102 Layout layout = LayoutLocalServiceUtil.getLayout(plid);
103
104 jsonObject.put("groupId", layout.getGroupId());
105 jsonObject.put("layoutId", layout.getLayoutId());
106 jsonObject.put(
107 "originalParentLayoutId", layout.getParentLayoutId());
108 jsonObject.put("originalParentPlid", layout.getParentPlid());
109 jsonObject.put("originalPriority", layout.getPriority());
110 jsonObject.put("plid", plid);
111
112 jsonObject.put("status", HttpServletResponse.SC_BAD_REQUEST);
113 }
114 }
115
116 return jsonObject.toString();
117 }
118
119 protected String[] addPage(
120 ThemeDisplay themeDisplay, HttpServletRequest request,
121 HttpServletResponse response)
122 throws Exception {
123
124 String doAsUserId = ParamUtil.getString(request, "doAsUserId");
125 String doAsUserLanguageId = ParamUtil.getString(
126 request, "doAsUserLanguageId");
127
128 long groupId = ParamUtil.getLong(request, "groupId");
129 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
130 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
131 String name = ParamUtil.getString(request, "name", "New Page");
132 String title = StringPool.BLANK;
133 String description = StringPool.BLANK;
134 String type = LayoutConstants.TYPE_PORTLET;
135 boolean hidden = false;
136 String friendlyURL = StringPool.BLANK;
137 long layoutPrototypeId = ParamUtil.getLong(
138 request, "layoutPrototypeId");
139
140 ServiceContext serviceContext = ServiceContextFactory.getInstance(
141 request);
142
143 Layout layout = null;
144
145 if (layoutPrototypeId > 0) {
146 LayoutPrototype layoutPrototype =
147 LayoutPrototypeServiceUtil.getLayoutPrototype(
148 layoutPrototypeId);
149
150 serviceContext.setAttribute(
151 "layoutPrototypeUuid", layoutPrototype.getUuid());
152
153 layout = LayoutServiceUtil.addLayout(
154 groupId, privateLayout, parentLayoutId, name, title,
155 description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
156 serviceContext);
157 }
158 else {
159 layout = LayoutServiceUtil.addLayout(
160 groupId, privateLayout, parentLayoutId, name, title,
161 description, type, hidden, friendlyURL, serviceContext);
162 }
163
164 LayoutType layoutType = layout.getLayoutType();
165
166 EventsProcessorUtil.process(
167 PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
168 layoutType.getConfigurationActionUpdate(), request, response);
169
170 String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
171
172 if (Validator.isNotNull(doAsUserId)) {
173 layoutURL = HttpUtil.addParameter(
174 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
175 }
176
177 if (Validator.isNotNull(doAsUserLanguageId)) {
178 layoutURL = HttpUtil.addParameter(
179 layoutURL, "doAsUserLanguageId",
180 themeDisplay.getDoAsUserLanguageId());
181 }
182
183 boolean deleteable = LayoutPermissionUtil.contains(
184 themeDisplay.getPermissionChecker(), layout, ActionKeys.DELETE);
185 boolean sortable = GroupPermissionUtil.contains(
186 themeDisplay.getPermissionChecker(), layout.getGroupId(),
187 ActionKeys.MANAGE_LAYOUTS) &&
188 SitesUtil.isLayoutSortable(layout);
189 boolean updateable = LayoutPermissionUtil.contains(
190 themeDisplay.getPermissionChecker(), layout, ActionKeys.UPDATE);
191
192 return new String[] {
193 String.valueOf(layout.getLayoutId()), layoutURL,
194 String.valueOf(deleteable), String.valueOf(sortable),
195 String.valueOf(updateable)
196 };
197 }
198
199 protected String getLayoutTypeExceptionMessage(
200 ThemeDisplay themeDisplay, LayoutTypeException lte, String cmd) {
201
202 if (Validator.isNotNull(cmd)) {
203 if (cmd.equals("delete") &&
204 (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
205
206 return themeDisplay.translate(
207 "you-cannot-delete-this-page-because-the-next-page-is-of-" +
208 "type-x-and-so-cannot-be-the-first-page",
209 "layout.types." + lte.getLayoutType());
210 }
211
212 if (cmd.equals("delete") &&
213 (lte.getType() ==
214 LayoutTypeException.FIRST_LAYOUT_PERMISSION)) {
215
216 return themeDisplay.translate(
217 "you-cannot-delete-this-page-because-the-next-page-is-" +
218 "not-vieweable-by-unathenticated-users-and-so-cannot-" +
219 "be-the-first-page");
220 }
221
222 if (cmd.equals("parent_layout_id") &&
223 (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
224
225 return themeDisplay.translate(
226 "you-cannot-move-this-page-because-the-resulting-order-" +
227 "would-place-a-page-of-type-x-as-the-first-page",
228 "layout.types." + lte.getLayoutType());
229 }
230 }
231
232 if (lte.getType() == LayoutTypeException.FIRST_LAYOUT ) {
233 return themeDisplay.translate(
234 "the-first-page-cannot-be-of-type-x",
235 "layout.types." + lte.getLayoutType());
236 }
237 else if (lte.getType() == LayoutTypeException.NOT_PARENTABLE) {
238 return themeDisplay.translate(
239 "a-page-cannot-become-a-child-of-a-page-that-is-not-" +
240 "parentable");
241 }
242
243 return StringPool.BLANK;
244 }
245
246 protected void updateName(HttpServletRequest request) throws Exception {
247 long plid = ParamUtil.getLong(request, "plid");
248
249 long groupId = ParamUtil.getLong(request, "groupId");
250 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
251 long layoutId = ParamUtil.getLong(request, "layoutId");
252 String name = ParamUtil.getString(request, "name");
253 String languageId = ParamUtil.getString(request, "languageId");
254
255 if (plid <= 0) {
256 LayoutServiceUtil.updateName(
257 groupId, privateLayout, layoutId, name, languageId);
258 }
259 else {
260 LayoutServiceUtil.updateName(plid, name, languageId);
261 }
262 }
263
264 protected void updateParentLayoutId(HttpServletRequest request)
265 throws Exception {
266
267 long plid = ParamUtil.getLong(request, "plid");
268 long parentPlid = ParamUtil.getLong(request, "parentPlid");
269 int priority = ParamUtil.getInteger(request, "priority");
270
271 LayoutServiceUtil.updateParentLayoutIdAndPriority(
272 plid, parentPlid, priority);
273 }
274
275 }