001
014
015 package com.liferay.portlet.communities.action;
016
017 import com.liferay.portal.events.EventsProcessorUtil;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.util.Constants;
021 import com.liferay.portal.kernel.util.HttpUtil;
022 import com.liferay.portal.kernel.util.ParamUtil;
023 import com.liferay.portal.kernel.util.PropsKeys;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.StringUtil;
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.security.permission.ActionKeys;
031 import com.liferay.portal.security.permission.PermissionChecker;
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.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.communities.util.CommunitiesUtil;
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
054 public class UpdatePageAction extends JSONAction {
055
056 public String getJSON(
057 ActionMapping mapping, ActionForm form, HttpServletRequest request,
058 HttpServletResponse response)
059 throws Exception {
060
061 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
062 WebKeys.THEME_DISPLAY);
063
064 PermissionChecker permissionChecker =
065 themeDisplay.getPermissionChecker();
066
067 long plid = ParamUtil.getLong(request, "plid");
068
069 long groupId = ParamUtil.getLong(request, "groupId");
070 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
071 long layoutId = ParamUtil.getLong(request, "layoutId");
072 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
073
074 Layout layout = null;
075
076 if (plid > 0) {
077 layout = LayoutLocalServiceUtil.getLayout(plid);
078 }
079 else if (layoutId > 0) {
080 layout = LayoutLocalServiceUtil.getLayout(
081 groupId, privateLayout, layoutId);
082 }
083 else if (parentLayoutId > 0) {
084 layout = LayoutLocalServiceUtil.getLayout(
085 groupId, privateLayout, parentLayoutId);
086 }
087
088 if (layout != null) {
089 if (!LayoutPermissionUtil.contains(
090 permissionChecker, layout, ActionKeys.UPDATE)) {
091
092 return null;
093 }
094 }
095 else {
096 if (!GroupPermissionUtil.contains(
097 permissionChecker, groupId, ActionKeys.MANAGE_LAYOUTS)) {
098
099 return null;
100 }
101 }
102
103 String cmd = ParamUtil.getString(request, Constants.CMD);
104
105 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
106
107 if (cmd.equals("add")) {
108 String[] array = addPage(themeDisplay, request, response);
109
110 jsonObj.put("layoutId", array[0]);
111 jsonObj.put("url", array[1]);
112 }
113 else if (cmd.equals("delete")) {
114 CommunitiesUtil.deleteLayout(request, response);
115 }
116 else if (cmd.equals("display_order")) {
117 updateDisplayOrder(request);
118 }
119 else if (cmd.equals("name")) {
120 updateName(request);
121 }
122 else if (cmd.equals("parent_layout_id")) {
123 updateParentLayoutId(request);
124 }
125 else if (cmd.equals("priority")) {
126 updatePriority(request);
127 }
128
129 return jsonObj.toString();
130 }
131
132 protected String[] addPage(
133 ThemeDisplay themeDisplay, HttpServletRequest request,
134 HttpServletResponse response)
135 throws Exception {
136
137 String doAsUserId = ParamUtil.getString(request, "doAsUserId");
138 String doAsUserLanguageId = ParamUtil.getString(
139 request, "doAsUserLanguageId");
140
141 long groupId = ParamUtil.getLong(request, "groupId");
142 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
143 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
144 String name = ParamUtil.getString(request, "name", "New Page");
145 String title = StringPool.BLANK;
146 String description = StringPool.BLANK;
147 String type = LayoutConstants.TYPE_PORTLET;
148 boolean hidden = false;
149 String friendlyURL = StringPool.BLANK;
150 long layoutPrototypeId = ParamUtil.getLong(
151 request, "layoutPrototypeId");
152
153 ServiceContext serviceContext = new ServiceContext();
154
155 Layout layout = null;
156
157 if (layoutPrototypeId > 0) {
158 LayoutPrototype layoutPrototype =
159 LayoutPrototypeServiceUtil.getLayoutPrototype(
160 layoutPrototypeId);
161
162 Layout layoutPrototypeLayout = layoutPrototype.getLayout();
163
164 layout = LayoutServiceUtil.addLayout(
165 groupId, privateLayout, parentLayoutId, name, title,
166 description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
167 serviceContext);
168
169 LayoutServiceUtil.updateLayout(
170 layout.getGroupId(), layout.isPrivateLayout(),
171 layout.getLayoutId(), layoutPrototypeLayout.getTypeSettings());
172
173 ActionUtil.copyPreferences(request, layout, layoutPrototypeLayout);
174 }
175 else {
176 layout = LayoutServiceUtil.addLayout(
177 groupId, privateLayout, parentLayoutId, name, title,
178 description, type, hidden, friendlyURL, serviceContext);
179 }
180
181 LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
182
183 EventsProcessorUtil.process(
184 PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
185 layoutSettings.getConfigurationActionUpdate(), request, response);
186
187 String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
188
189 if (Validator.isNotNull(doAsUserId)) {
190 layoutURL = HttpUtil.addParameter(
191 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
192 }
193
194 if (Validator.isNotNull(doAsUserLanguageId)) {
195 layoutURL = HttpUtil.addParameter(
196 layoutURL, "doAsUserLanguageId",
197 themeDisplay.getDoAsUserLanguageId());
198 }
199
200 return new String[] {String.valueOf(layout.getLayoutId()), layoutURL};
201 }
202
203 protected void updateDisplayOrder(HttpServletRequest request)
204 throws Exception {
205
206 long groupId = ParamUtil.getLong(request, "groupId");
207 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
208 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
209 long[] layoutIds = StringUtil.split(
210 ParamUtil.getString(request, "layoutIds"), 0L);
211
212 LayoutServiceUtil.setLayouts(
213 groupId, privateLayout, parentLayoutId, layoutIds);
214 }
215
216 protected void updateName(HttpServletRequest request) throws Exception {
217 long plid = ParamUtil.getLong(request, "plid");
218
219 long groupId = ParamUtil.getLong(request, "groupId");
220 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
221 long layoutId = ParamUtil.getLong(request, "layoutId");
222 String name = ParamUtil.getString(request, "name");
223 String languageId = ParamUtil.getString(request, "languageId");
224
225 if (plid <= 0) {
226 LayoutServiceUtil.updateName(
227 groupId, privateLayout, layoutId, name, languageId);
228 }
229 else {
230 LayoutServiceUtil.updateName(plid, name, languageId);
231 }
232 }
233
234 protected void updateParentLayoutId(HttpServletRequest request)
235 throws Exception {
236
237 long plid = ParamUtil.getLong(request, "plid");
238
239 long parentPlid = ParamUtil.getLong(request, "parentPlid");
240
241 long groupId = ParamUtil.getLong(request, "groupId");
242 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
243 long layoutId = ParamUtil.getLong(request, "layoutId");
244 long parentLayoutId = ParamUtil.getLong(
245 request, "parentLayoutId",
246 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
247
248 if (plid <= 0) {
249 LayoutServiceUtil.updateParentLayoutId(
250 groupId, privateLayout, layoutId, parentLayoutId);
251 }
252 else {
253 LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
254 }
255 }
256
257 protected void updatePriority(HttpServletRequest request) throws Exception {
258 long plid = ParamUtil.getLong(request, "plid");
259
260 long groupId = ParamUtil.getLong(request, "groupId");
261 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
262 long layoutId = ParamUtil.getLong(request, "layoutId");
263 int priority = ParamUtil.getInteger(request, "priority");
264
265 if (plid <= 0) {
266 LayoutServiceUtil.updatePriority(
267 groupId, privateLayout, layoutId, priority);
268 }
269 else {
270 LayoutServiceUtil.updatePriority(plid, priority);
271 }
272 }
273
274 }