001
014
015 package com.liferay.portlet.layoutsadmin.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.ServiceContextFactory;
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 mapping, ActionForm form, HttpServletRequest request,
060 HttpServletResponse response)
061 throws Exception {
062
063 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
064 WebKeys.THEME_DISPLAY);
065
066 PermissionChecker permissionChecker =
067 themeDisplay.getPermissionChecker();
068
069 long plid = ParamUtil.getLong(request, "plid");
070
071 long groupId = ParamUtil.getLong(request, "groupId");
072 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
073 long layoutId = ParamUtil.getLong(request, "layoutId");
074 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
075
076 Layout layout = null;
077
078 if (plid > 0) {
079 layout = LayoutLocalServiceUtil.getLayout(plid);
080 }
081 else if (layoutId > 0) {
082 layout = LayoutLocalServiceUtil.getLayout(
083 groupId, privateLayout, layoutId);
084 }
085 else if (parentLayoutId > 0) {
086 layout = LayoutLocalServiceUtil.getLayout(
087 groupId, privateLayout, parentLayoutId);
088 }
089
090 if ((layout != null) &&
091 !LayoutPermissionUtil.contains(
092 permissionChecker, layout, ActionKeys.UPDATE)) {
093
094 return null;
095 }
096
097 String cmd = ParamUtil.getString(request, Constants.CMD);
098
099 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
100
101 if (cmd.equals("add")) {
102 String[] array = addPage(themeDisplay, request, response);
103
104 jsonObj.put("layoutId", array[0]);
105 jsonObj.put("url", array[1]);
106 }
107 else if (cmd.equals("delete")) {
108 SitesUtil.deleteLayout(request, response);
109 }
110 else if (cmd.equals("display_order")) {
111 updateDisplayOrder(request);
112 }
113 else if (cmd.equals("name")) {
114 updateName(request);
115 }
116 else if (cmd.equals("parent_layout_id")) {
117 updateParentLayoutId(request);
118 }
119 else if (cmd.equals("priority")) {
120 updatePriority(request);
121 }
122
123 return jsonObj.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 layout = LayoutServiceUtil.addLayout(
154 groupId, privateLayout, parentLayoutId, name, title,
155 description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
156 serviceContext);
157
158 LayoutPrototype layoutPrototype =
159 LayoutPrototypeServiceUtil.getLayoutPrototype(
160 layoutPrototypeId);
161
162 SitesUtil.applyLayoutPrototype(layoutPrototype, layout, true);
163 }
164 else {
165 layout = LayoutServiceUtil.addLayout(
166 groupId, privateLayout, parentLayoutId, name, title,
167 description, type, hidden, friendlyURL, serviceContext);
168 }
169
170 LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
171
172 EventsProcessorUtil.process(
173 PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
174 layoutSettings.getConfigurationActionUpdate(), request, response);
175
176 String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
177
178 if (Validator.isNotNull(doAsUserId)) {
179 layoutURL = HttpUtil.addParameter(
180 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
181 }
182
183 if (Validator.isNotNull(doAsUserLanguageId)) {
184 layoutURL = HttpUtil.addParameter(
185 layoutURL, "doAsUserLanguageId",
186 themeDisplay.getDoAsUserLanguageId());
187 }
188
189 return new String[] {String.valueOf(layout.getLayoutId()), layoutURL};
190 }
191
192 protected void updateDisplayOrder(HttpServletRequest request)
193 throws Exception {
194
195 long groupId = ParamUtil.getLong(request, "groupId");
196 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
197 long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
198 long[] layoutIds = StringUtil.split(
199 ParamUtil.getString(request, "layoutIds"), 0L);
200
201 ServiceContext serviceContext = ServiceContextFactory.getInstance(
202 request);
203
204 LayoutServiceUtil.setLayouts(
205 groupId, privateLayout, parentLayoutId, layoutIds, serviceContext);
206 }
207
208 protected void updateName(HttpServletRequest request) throws Exception {
209 long plid = ParamUtil.getLong(request, "plid");
210
211 long groupId = ParamUtil.getLong(request, "groupId");
212 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
213 long layoutId = ParamUtil.getLong(request, "layoutId");
214 String name = ParamUtil.getString(request, "name");
215 String languageId = ParamUtil.getString(request, "languageId");
216
217 LayoutLocalServiceUtil.updateScopedPortletNames(
218 groupId, privateLayout, layoutId, name, languageId);
219
220 if (plid <= 0) {
221 LayoutServiceUtil.updateName(
222 groupId, privateLayout, layoutId, name, languageId);
223 }
224 else {
225 LayoutServiceUtil.updateName(plid, name, languageId);
226 }
227 }
228
229 protected void updateParentLayoutId(HttpServletRequest request)
230 throws Exception {
231
232 long plid = ParamUtil.getLong(request, "plid");
233
234 long parentPlid = ParamUtil.getLong(request, "parentPlid");
235
236 long groupId = ParamUtil.getLong(request, "groupId");
237 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
238 long layoutId = ParamUtil.getLong(request, "layoutId");
239 long parentLayoutId = ParamUtil.getLong(
240 request, "parentLayoutId",
241 LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
242
243 if (plid <= 0) {
244 LayoutServiceUtil.updateParentLayoutId(
245 groupId, privateLayout, layoutId, parentLayoutId);
246 }
247 else {
248 LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
249 }
250
251 updatePriority(request);
252 }
253
254 protected void updatePriority(HttpServletRequest request) throws Exception {
255 long plid = ParamUtil.getLong(request, "plid");
256
257 long groupId = ParamUtil.getLong(request, "groupId");
258 boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
259 long layoutId = ParamUtil.getLong(request, "layoutId");
260 int priority = ParamUtil.getInteger(request, "priority");
261
262 if (plid <= 0) {
263 LayoutServiceUtil.updatePriority(
264 groupId, privateLayout, layoutId, priority);
265 }
266 else {
267 LayoutServiceUtil.updatePriority(plid, priority);
268 }
269 }
270
271 }