001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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.UnicodeProperties;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Group;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.model.LayoutConstants;
031    import com.liferay.portal.model.LayoutPrototype;
032    import com.liferay.portal.model.LayoutSet;
033    import com.liferay.portal.model.LayoutSetPrototype;
034    import com.liferay.portal.security.permission.ActionKeys;
035    import com.liferay.portal.security.permission.PermissionChecker;
036    import com.liferay.portal.service.GroupLocalServiceUtil;
037    import com.liferay.portal.service.LayoutLocalServiceUtil;
038    import com.liferay.portal.service.LayoutPrototypeServiceUtil;
039    import com.liferay.portal.service.LayoutServiceUtil;
040    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
041    import com.liferay.portal.service.LayoutSetPrototypeLocalServiceUtil;
042    import com.liferay.portal.service.ServiceContext;
043    import com.liferay.portal.service.ServiceContextFactory;
044    import com.liferay.portal.service.permission.LayoutPermissionUtil;
045    import com.liferay.portal.struts.JSONAction;
046    import com.liferay.portal.theme.ThemeDisplay;
047    import com.liferay.portal.util.LayoutSettings;
048    import com.liferay.portal.util.PortalUtil;
049    import com.liferay.portal.util.WebKeys;
050    import com.liferay.portlet.sites.action.ActionUtil;
051    import com.liferay.portlet.sites.util.SitesUtil;
052    
053    import java.util.List;
054    
055    import javax.servlet.http.HttpServletRequest;
056    import javax.servlet.http.HttpServletResponse;
057    
058    import org.apache.struts.action.ActionForm;
059    import org.apache.struts.action.ActionMapping;
060    
061    /**
062     * @author Ming-Gih Lam
063     * @author Hugo Huijser
064     */
065    public class UpdateLayoutAction extends JSONAction {
066    
067            @Override
068            public String getJSON(
069                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
070                            HttpServletResponse response)
071                    throws Exception {
072    
073                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
074                            WebKeys.THEME_DISPLAY);
075    
076                    PermissionChecker permissionChecker =
077                            themeDisplay.getPermissionChecker();
078    
079                    long plid = ParamUtil.getLong(request, "plid");
080    
081                    long groupId = ParamUtil.getLong(request, "groupId");
082                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
083                    long layoutId = ParamUtil.getLong(request, "layoutId");
084                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
085    
086                    Layout layout = null;
087    
088                    if (plid > 0) {
089                            layout = LayoutLocalServiceUtil.getLayout(plid);
090                    }
091                    else if (layoutId > 0) {
092                            layout = LayoutLocalServiceUtil.getLayout(
093                                    groupId, privateLayout, layoutId);
094                    }
095                    else if (parentLayoutId > 0) {
096                            layout = LayoutLocalServiceUtil.getLayout(
097                                    groupId, privateLayout, parentLayoutId);
098                    }
099    
100                    if ((layout != null) &&
101                            !LayoutPermissionUtil.contains(
102                                    permissionChecker, layout, ActionKeys.UPDATE)) {
103    
104                            return null;
105                    }
106    
107                    String cmd = ParamUtil.getString(request, Constants.CMD);
108    
109                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
110    
111                    if (cmd.equals("add")) {
112                            String[] array = addPage(themeDisplay, request, response);
113    
114                            jsonObj.put("layoutId", array[0]);
115                            jsonObj.put("url", array[1]);
116                    }
117                    else if (cmd.equals("delete")) {
118                            SitesUtil.deleteLayout(request, response);
119                    }
120                    else if (cmd.equals("display_order")) {
121                            updateDisplayOrder(request);
122                    }
123                    else if (cmd.equals("name")) {
124                            updateName(request);
125                    }
126                    else if (cmd.equals("parent_layout_id")) {
127                            updateParentLayoutId(request);
128                    }
129                    else if (cmd.equals("priority")) {
130                            updatePriority(request);
131                    }
132    
133                    return jsonObj.toString();
134            }
135    
136            protected String[] addPage(
137                            ThemeDisplay themeDisplay, HttpServletRequest request,
138                            HttpServletResponse response)
139                    throws Exception {
140    
141                    String doAsUserId = ParamUtil.getString(request, "doAsUserId");
142                    String doAsUserLanguageId = ParamUtil.getString(
143                            request, "doAsUserLanguageId");
144    
145                    long groupId = ParamUtil.getLong(request, "groupId");
146                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
147                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
148                    String name = ParamUtil.getString(request, "name", "New Page");
149                    String title = StringPool.BLANK;
150                    String description = StringPool.BLANK;
151                    String type = LayoutConstants.TYPE_PORTLET;
152                    boolean hidden = false;
153                    String friendlyURL = StringPool.BLANK;
154                    boolean locked = ParamUtil.getBoolean(request, "locked");
155                    long layoutPrototypeId = ParamUtil.getLong(
156                            request, "layoutPrototypeId");
157    
158                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
159                            request);
160    
161                    Layout layout = null;
162    
163                    if (layoutPrototypeId > 0) {
164                            LayoutPrototype layoutPrototype =
165                                    LayoutPrototypeServiceUtil.getLayoutPrototype(
166                                            layoutPrototypeId);
167    
168                            Layout layoutPrototypeLayout = layoutPrototype.getLayout();
169    
170                            layout = LayoutServiceUtil.addLayout(
171                                    groupId, privateLayout, parentLayoutId, name, title,
172                                    description, layoutPrototypeLayout.getType(),
173                                    false, friendlyURL, locked, serviceContext);
174    
175                            LayoutServiceUtil.updateLayout(
176                                    layout.getGroupId(), layout.isPrivateLayout(),
177                                    layout.getLayoutId(), layoutPrototypeLayout.getTypeSettings());
178    
179                            ActionUtil.copyLookAndFeel(layout, layoutPrototypeLayout);
180    
181                            ActionUtil.copyPortletPermissions(
182                                    request, layout, layoutPrototypeLayout);
183    
184                            ActionUtil.copyPreferences(request, layout, layoutPrototypeLayout);
185                    }
186                    else {
187                            layout = LayoutServiceUtil.addLayout(
188                                    groupId, privateLayout, parentLayoutId, name, title,
189                                    description, type, hidden, friendlyURL, false, serviceContext);
190    
191                            Group group = GroupLocalServiceUtil.getGroup(groupId);
192    
193                            if (group.isLayoutSetPrototype()) {
194                                    LayoutSetPrototype layoutSetPrototype =
195                                            LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(
196                                                    group.getClassPK());
197    
198                                    List<LayoutSet> layoutSets = LayoutSetLocalServiceUtil.
199                                            getLayoutSetsByLayoutSetPrototypeUuid(
200                                                    layoutSetPrototype.getUuid());
201    
202                                    for (LayoutSet layoutSet : layoutSets) {
203                                            Layout addedLayout = LayoutServiceUtil.addLayout(
204                                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
205                                                    parentLayoutId, name, title, description, type, hidden,
206                                                    friendlyURL, locked, serviceContext);
207    
208                                            addedLayout.setModifiedDate(layout.getModifiedDate());
209                                            addedLayout.setTemplateLayoutUuid(layout.getUuid());
210    
211                                            UnicodeProperties typeSettingsProperties =
212                                                    addedLayout.getTypeSettingsProperties();
213    
214                                            typeSettingsProperties.put(
215                                                    "layoutSetPrototypeLastCopyDate",
216                                                    String.valueOf(layout.getModifiedDate().getTime()));
217    
218                                            addedLayout.setTypeSettingsProperties(
219                                                    typeSettingsProperties);
220    
221                                            LayoutLocalServiceUtil.updateLayout(addedLayout);
222                                    }
223                            }
224                    }
225    
226                    LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
227    
228                    EventsProcessorUtil.process(
229                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
230                            layoutSettings.getConfigurationActionUpdate(), request, response);
231    
232                    String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
233    
234                    if (Validator.isNotNull(doAsUserId)) {
235                            layoutURL = HttpUtil.addParameter(
236                                    layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
237                    }
238    
239                    if (Validator.isNotNull(doAsUserLanguageId)) {
240                            layoutURL = HttpUtil.addParameter(
241                                    layoutURL, "doAsUserLanguageId",
242                                    themeDisplay.getDoAsUserLanguageId());
243                    }
244    
245                    return new String[] {String.valueOf(layout.getLayoutId()), layoutURL};
246            }
247    
248            protected void updateDisplayOrder(HttpServletRequest request)
249                    throws Exception {
250    
251                    long groupId = ParamUtil.getLong(request, "groupId");
252                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
253                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
254                    long[] layoutIds = StringUtil.split(
255                            ParamUtil.getString(request, "layoutIds"), 0L);
256    
257                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
258                            request);
259    
260                    LayoutServiceUtil.setLayouts(
261                            groupId, privateLayout, parentLayoutId, layoutIds, serviceContext);
262            }
263    
264            protected void updateName(HttpServletRequest request) throws Exception {
265                    long plid = ParamUtil.getLong(request, "plid");
266    
267                    long groupId = ParamUtil.getLong(request, "groupId");
268                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
269                    long layoutId = ParamUtil.getLong(request, "layoutId");
270                    String name = ParamUtil.getString(request, "name");
271                    String languageId = ParamUtil.getString(request, "languageId");
272    
273                    LayoutLocalServiceUtil.updateScopedPortletNames(
274                            groupId, privateLayout, layoutId, name, languageId);
275    
276                    if (plid <= 0) {
277                            LayoutServiceUtil.updateName(
278                                    groupId, privateLayout, layoutId, name, languageId);
279                    }
280                    else {
281                            LayoutServiceUtil.updateName(plid, name, languageId);
282                    }
283            }
284    
285            protected void updateParentLayoutId(HttpServletRequest request)
286                    throws Exception {
287    
288                    long plid = ParamUtil.getLong(request, "plid");
289    
290                    long parentPlid = ParamUtil.getLong(request, "parentPlid");
291    
292                    long groupId = ParamUtil.getLong(request, "groupId");
293                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
294                    long layoutId = ParamUtil.getLong(request, "layoutId");
295                    long parentLayoutId = ParamUtil.getLong(
296                            request, "parentLayoutId",
297                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
298    
299                    if (plid <= 0) {
300                            LayoutServiceUtil.updateParentLayoutId(
301                                    groupId, privateLayout, layoutId, parentLayoutId);
302                    }
303                    else {
304                            LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
305                    }
306    
307                    updatePriority(request);
308            }
309    
310            protected void updatePriority(HttpServletRequest request) throws Exception {
311                    long plid = ParamUtil.getLong(request, "plid");
312    
313                    long groupId = ParamUtil.getLong(request, "groupId");
314                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
315                    long layoutId = ParamUtil.getLong(request, "layoutId");
316                    int priority = ParamUtil.getInteger(request, "priority");
317    
318                    if (plid <= 0) {
319                            LayoutServiceUtil.updatePriority(
320                                    groupId, privateLayout, layoutId, priority);
321                    }
322                    else {
323                            LayoutServiceUtil.updatePriority(plid, priority);
324                    }
325            }
326    
327    }