001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.sites.action;
016    
017    import com.liferay.portal.DuplicateGroupException;
018    import com.liferay.portal.GroupFriendlyURLException;
019    import com.liferay.portal.GroupNameException;
020    import com.liferay.portal.LayoutSetVirtualHostException;
021    import com.liferay.portal.NoSuchGroupException;
022    import com.liferay.portal.NoSuchLayoutException;
023    import com.liferay.portal.RemoteExportException;
024    import com.liferay.portal.RemoteOptionsException;
025    import com.liferay.portal.RequiredGroupException;
026    import com.liferay.portal.kernel.dao.orm.QueryUtil;
027    import com.liferay.portal.kernel.exception.PortalException;
028    import com.liferay.portal.kernel.exception.SystemException;
029    import com.liferay.portal.kernel.servlet.SessionErrors;
030    import com.liferay.portal.kernel.servlet.SessionMessages;
031    import com.liferay.portal.kernel.staging.StagingUtil;
032    import com.liferay.portal.kernel.util.Constants;
033    import com.liferay.portal.kernel.util.GetterUtil;
034    import com.liferay.portal.kernel.util.HttpUtil;
035    import com.liferay.portal.kernel.util.ParamUtil;
036    import com.liferay.portal.kernel.util.UnicodeProperties;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.liveusers.LiveUsers;
039    import com.liferay.portal.model.Group;
040    import com.liferay.portal.model.GroupConstants;
041    import com.liferay.portal.model.Layout;
042    import com.liferay.portal.model.LayoutConstants;
043    import com.liferay.portal.model.LayoutSet;
044    import com.liferay.portal.model.MembershipRequest;
045    import com.liferay.portal.model.MembershipRequestConstants;
046    import com.liferay.portal.security.auth.PrincipalException;
047    import com.liferay.portal.service.GroupLocalServiceUtil;
048    import com.liferay.portal.service.GroupServiceUtil;
049    import com.liferay.portal.service.LayoutLocalServiceUtil;
050    import com.liferay.portal.service.LayoutSetServiceUtil;
051    import com.liferay.portal.service.MembershipRequestLocalServiceUtil;
052    import com.liferay.portal.service.MembershipRequestServiceUtil;
053    import com.liferay.portal.service.ServiceContext;
054    import com.liferay.portal.service.ServiceContextFactory;
055    import com.liferay.portal.struts.PortletAction;
056    import com.liferay.portal.theme.ThemeDisplay;
057    import com.liferay.portal.util.PortalUtil;
058    import com.liferay.portal.util.WebKeys;
059    import com.liferay.portlet.asset.AssetCategoryException;
060    import com.liferay.portlet.asset.AssetTagException;
061    import com.liferay.portlet.sites.util.SitesUtil;
062    
063    import java.util.List;
064    
065    import javax.portlet.ActionRequest;
066    import javax.portlet.ActionResponse;
067    import javax.portlet.PortletConfig;
068    import javax.portlet.RenderRequest;
069    import javax.portlet.RenderResponse;
070    
071    import org.apache.struts.action.ActionForm;
072    import org.apache.struts.action.ActionForward;
073    import org.apache.struts.action.ActionMapping;
074    
075    /**
076     * @author Brian Wing Shun Chan
077     * @author Zsolt Berentey
078     */
079    public class EditGroupAction extends PortletAction {
080    
081            @Override
082            public void processAction(
083                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
084                            ActionRequest actionRequest, ActionResponse actionResponse)
085                    throws Exception {
086    
087                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
088                            WebKeys.THEME_DISPLAY);
089    
090                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
091    
092                    String redirect = ParamUtil.getString(actionRequest, "redirect");
093    
094                    try {
095                            String closeRedirect = ParamUtil.getString(
096                                    actionRequest, "closeRedirect");
097    
098                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
099                                    Object[] returnValue = updateGroup(actionRequest);
100    
101                                    Group group = (Group)returnValue[0];
102                                    String oldFriendlyURL = (String)returnValue[1];
103                                    String oldStagingFriendlyURL = (String)returnValue[2];
104                                    long newRefererPlid = (Long)returnValue[3];
105    
106                                    redirect = HttpUtil.setParameter(
107                                            redirect, "doAsGroupId", group.getGroupId());
108                                    redirect = HttpUtil.setParameter(
109                                            redirect, "refererPlid", newRefererPlid);
110    
111                                    closeRedirect = updateCloseRedirect(
112                                            closeRedirect, group, themeDisplay, oldFriendlyURL,
113                                            oldStagingFriendlyURL);
114                            }
115                            else if (cmd.equals(Constants.DEACTIVATE) ||
116                                             cmd.equals(Constants.RESTORE)) {
117    
118                                    updateActive(actionRequest, cmd);
119                            }
120                            else if (cmd.equals(Constants.DELETE)) {
121                                    deleteGroup(actionRequest);
122                            }
123    
124                            if (Validator.isNotNull(closeRedirect)) {
125                                    SessionMessages.add(
126                                            actionRequest,
127                                            portletConfig.getPortletName() +
128                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
129                                            closeRedirect);
130                            }
131    
132                            sendRedirect(actionRequest, actionResponse, redirect);
133                    }
134                    catch (Exception e) {
135                            if (e instanceof NoSuchGroupException ||
136                                    e instanceof PrincipalException) {
137    
138                                    SessionErrors.add(actionRequest, e.getClass().getName());
139    
140                                    setForward(actionRequest, "portlet.sites_admin.error");
141                            }
142                            else if (e instanceof AssetCategoryException ||
143                                             e instanceof AssetTagException ||
144                                             e instanceof DuplicateGroupException ||
145                                             e instanceof GroupFriendlyURLException ||
146                                             e instanceof GroupNameException ||
147                                             e instanceof LayoutSetVirtualHostException ||
148                                             e instanceof RemoteExportException ||
149                                             e instanceof RemoteOptionsException ||
150                                             e instanceof RequiredGroupException ||
151                                             e instanceof SystemException) {
152    
153                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
154    
155                                    if (cmd.equals(Constants.DEACTIVATE) ||
156                                            cmd.equals(Constants.DELETE) ||
157                                            cmd.equals(Constants.RESTORE)) {
158    
159                                            if (Validator.isNotNull(redirect)) {
160                                                    actionResponse.sendRedirect(redirect);
161                                            }
162                                    }
163                            }
164                            else {
165                                    throw e;
166                            }
167                    }
168            }
169    
170            @Override
171            public ActionForward render(
172                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
173                            RenderRequest renderRequest, RenderResponse renderResponse)
174                    throws Exception {
175    
176                    try {
177                            ActionUtil.getGroup(renderRequest);
178                    }
179                    catch (Exception e) {
180                            if (e instanceof NoSuchGroupException ||
181                                    e instanceof PrincipalException) {
182    
183                                    SessionErrors.add(renderRequest, e.getClass().getName());
184    
185                                    return mapping.findForward("portlet.sites_admin.error");
186                            }
187                            else {
188                                    throw e;
189                            }
190                    }
191    
192                    return mapping.findForward(
193                            getForward(renderRequest, "portlet.sites_admin.edit_site"));
194            }
195    
196            protected void deleteGroup(ActionRequest actionRequest) throws Exception {
197                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
198                            WebKeys.THEME_DISPLAY);
199    
200                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
201    
202                    GroupServiceUtil.deleteGroup(groupId);
203    
204                    LiveUsers.deleteGroup(themeDisplay.getCompanyId(), groupId);
205            }
206    
207            protected long getRefererGroupId(ThemeDisplay themeDisplay)
208                    throws Exception {
209    
210                    long refererGroupId = 0;
211    
212                    try {
213                            Layout refererLayout = LayoutLocalServiceUtil.getLayout(
214                                    themeDisplay.getRefererPlid());
215    
216                            refererGroupId = refererLayout.getGroupId();
217                    }
218                    catch (NoSuchLayoutException nsle) {
219                    }
220    
221                    return refererGroupId;
222            }
223    
224            protected void updateActive(ActionRequest actionRequest, String cmd)
225                    throws Exception {
226    
227                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
228                            WebKeys.THEME_DISPLAY);
229    
230                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
231    
232                    if ((groupId == themeDisplay.getDoAsGroupId()) ||
233                            (groupId == themeDisplay.getScopeGroupId()) ||
234                            (groupId == getRefererGroupId(themeDisplay))) {
235    
236                            throw new RequiredGroupException(String.valueOf(groupId));
237                    }
238    
239                    Group group = GroupServiceUtil.getGroup(groupId);
240    
241                    boolean active = false;
242    
243                    if (cmd.equals(Constants.RESTORE)) {
244                            active = true;
245                    }
246    
247                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
248                            Group.class.getName(), actionRequest);
249    
250                    GroupServiceUtil.updateGroup(
251                            groupId, group.getName(), group.getDescription(), group.getType(),
252                            group.getFriendlyURL(), active, serviceContext);
253            }
254    
255            protected String updateCloseRedirect(
256                            String closeRedirect, Group group, ThemeDisplay themeDisplay,
257                            String oldFriendlyURL, String oldStagingFriendlyURL)
258                    throws SystemException, PortalException {
259    
260                    if (Validator.isNull(closeRedirect) || (group == null)) {
261                            return closeRedirect;
262                    }
263    
264                    String oldPath = null;
265                    String newPath = null;
266    
267                    if (Validator.isNotNull(oldFriendlyURL)) {
268                            oldPath = oldFriendlyURL;
269                            newPath = group.getFriendlyURL();
270    
271                            if (closeRedirect.indexOf(oldPath) != -1) {
272                                    closeRedirect = PortalUtil.updateRedirect(
273                                            closeRedirect, oldPath, newPath);
274                            }
275                            else {
276                                    closeRedirect = PortalUtil.getGroupFriendlyURL(
277                                            group, false, themeDisplay);
278                            }
279                    }
280    
281                    if (Validator.isNotNull(oldStagingFriendlyURL)) {
282                            Group stagingGroup = group.getStagingGroup();
283    
284                            if (GroupLocalServiceUtil.fetchGroup(
285                                            stagingGroup.getGroupId()) == null) {
286    
287                                    oldPath = oldStagingFriendlyURL;
288                                    newPath = group.getFriendlyURL();
289                            }
290                            else {
291                                    oldPath = oldStagingFriendlyURL;
292                                    newPath = stagingGroup.getFriendlyURL();
293                            }
294    
295                            if (closeRedirect.contains(oldPath)) {
296                                    closeRedirect = PortalUtil.updateRedirect(
297                                            closeRedirect, oldPath, newPath);
298                            }
299                            else {
300                                    closeRedirect = PortalUtil.getGroupFriendlyURL(
301                                            group, false, themeDisplay);
302                            }
303                    }
304    
305                    return closeRedirect;
306            }
307    
308            protected Object[] updateGroup(ActionRequest actionRequest)
309                    throws Exception {
310    
311                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
312                            WebKeys.THEME_DISPLAY);
313    
314                    long userId = PortalUtil.getUserId(actionRequest);
315    
316                    long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
317    
318                    String name = ParamUtil.getString(actionRequest, "name");
319                    String description = ParamUtil.getString(actionRequest, "description");
320                    int type = ParamUtil.getInteger(actionRequest, "type");
321                    String friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
322                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
323    
324                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
325                            Group.class.getName(), actionRequest);
326    
327                    Group liveGroup = null;
328                    String oldFriendlyURL = null;
329                    String oldStagingFriendlyURL = null;
330    
331                    if (liveGroupId <= 0) {
332    
333                            // Add group
334    
335                            liveGroup = GroupServiceUtil.addGroup(
336                                    name, description, type, friendlyURL, true, active,
337                                    serviceContext);
338    
339                            LiveUsers.joinGroup(
340                                    themeDisplay.getCompanyId(), liveGroup.getGroupId(), userId);
341                    }
342                    else {
343    
344                            // Update group
345    
346                            liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);
347    
348                            oldFriendlyURL = liveGroup.getFriendlyURL();
349    
350                            liveGroup = GroupServiceUtil.updateGroup(
351                                    liveGroupId, name, description, type, friendlyURL, active,
352                                    serviceContext);
353    
354                            if (type == GroupConstants.TYPE_SITE_OPEN) {
355                                    List<MembershipRequest> membershipRequests =
356                                            MembershipRequestLocalServiceUtil.search(
357                                                    liveGroupId, MembershipRequestConstants.STATUS_PENDING,
358                                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS);
359    
360                                    for (MembershipRequest membershipRequest : membershipRequests) {
361                                            MembershipRequestServiceUtil.updateStatus(
362                                                    membershipRequest.getMembershipRequestId(),
363                                                    themeDisplay.translate(
364                                                            "your-membership-has-been-approved"),
365                                                    MembershipRequestConstants.STATUS_APPROVED,
366                                                    serviceContext);
367    
368                                            LiveUsers.joinGroup(
369                                                    themeDisplay.getCompanyId(),
370                                                    membershipRequest.getGroupId(),
371                                                    new long[] {membershipRequest.getUserId()});
372                                    }
373                            }
374                    }
375    
376                    // Settings
377    
378                    UnicodeProperties typeSettingsProperties =
379                            liveGroup.getTypeSettingsProperties();
380    
381                    String customJspServletContextName = ParamUtil.getString(
382                            actionRequest, "customJspServletContextName");
383    
384                    typeSettingsProperties.setProperty(
385                            "customJspServletContextName", customJspServletContextName);
386    
387                    String googleAnalyticsId = ParamUtil.getString(
388                            actionRequest, "googleAnalyticsId");
389    
390                    typeSettingsProperties.setProperty(
391                            "googleAnalyticsId", googleAnalyticsId);
392    
393                    String publicRobots = ParamUtil.getString(
394                            actionRequest, "publicRobots");
395                    String privateRobots = ParamUtil.getString(
396                            actionRequest, "privateRobots");
397    
398                    typeSettingsProperties.setProperty("false-robots.txt", publicRobots);
399                    typeSettingsProperties.setProperty("true-robots.txt", privateRobots);
400    
401                    String publicVirtualHost = ParamUtil.getString(
402                            actionRequest, "publicVirtualHost");
403                    String privateVirtualHost = ParamUtil.getString(
404                            actionRequest, "privateVirtualHost");
405    
406                    LayoutSetServiceUtil.updateVirtualHost(
407                            liveGroup.getGroupId(), false, publicVirtualHost);
408    
409                    LayoutSetServiceUtil.updateVirtualHost(
410                            liveGroup.getGroupId(), true, privateVirtualHost);
411    
412                    if (liveGroup.hasStagingGroup()) {
413                            Group stagingGroup = liveGroup.getStagingGroup();
414    
415                            oldStagingFriendlyURL = stagingGroup.getFriendlyURL();
416    
417                            publicVirtualHost = ParamUtil.getString(
418                                    actionRequest, "stagingPublicVirtualHost");
419                            privateVirtualHost = ParamUtil.getString(
420                                    actionRequest, "stagingPrivateVirtualHost");
421                            friendlyURL = ParamUtil.getString(
422                                    actionRequest, "stagingFriendlyURL");
423    
424                            LayoutSetServiceUtil.updateVirtualHost(
425                                    stagingGroup.getGroupId(), false, publicVirtualHost);
426    
427                            LayoutSetServiceUtil.updateVirtualHost(
428                                    stagingGroup.getGroupId(), true, privateVirtualHost);
429    
430                            GroupServiceUtil.updateFriendlyURL(
431                                    stagingGroup.getGroupId(), friendlyURL);
432                    }
433    
434                    liveGroup = GroupServiceUtil.updateGroup(
435                            liveGroup.getGroupId(), typeSettingsProperties.toString());
436    
437                    // Layout set prototypes
438    
439                    LayoutSet privateLayoutSet = liveGroup.getPrivateLayoutSet();
440                    LayoutSet publicLayoutSet = liveGroup.getPublicLayoutSet();
441    
442                    if (!liveGroup.isStaged()) {
443                            long privateLayoutSetPrototypeId = ParamUtil.getLong(
444                                    actionRequest, "privateLayoutSetPrototypeId");
445                            long publicLayoutSetPrototypeId = ParamUtil.getLong(
446                                    actionRequest, "publicLayoutSetPrototypeId");
447                            boolean privateLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
448                                    actionRequest, "privateLayoutSetPrototypeLinkEnabled");
449                            boolean publicLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
450                                    actionRequest, "publicLayoutSetPrototypeLinkEnabled");
451    
452                            if ((privateLayoutSetPrototypeId == 0) &&
453                                    (publicLayoutSetPrototypeId == 0) &&
454                                    !privateLayoutSetPrototypeLinkEnabled &&
455                                    !publicLayoutSetPrototypeLinkEnabled) {
456    
457                                    long layoutSetPrototypeId = ParamUtil.getLong(
458                                            actionRequest, "layoutSetPrototypeId");
459                                    int layoutSetVisibility = ParamUtil.getInteger(
460                                            actionRequest, "layoutSetVisibility");
461                                    boolean layoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
462                                            actionRequest, "layoutSetPrototypeLinkEnabled",
463                                            (layoutSetPrototypeId > 0));
464    
465                                    if (layoutSetVisibility == _LAYOUT_SET_VISIBILITY_PRIVATE) {
466                                            privateLayoutSetPrototypeId = layoutSetPrototypeId;
467    
468                                            privateLayoutSetPrototypeLinkEnabled =
469                                                    layoutSetPrototypeLinkEnabled;
470                                    }
471                                    else {
472                                            publicLayoutSetPrototypeId = layoutSetPrototypeId;
473    
474                                            publicLayoutSetPrototypeLinkEnabled =
475                                                    layoutSetPrototypeLinkEnabled;
476                                    }
477                            }
478    
479                            SitesUtil.updateLayoutSetPrototypesLinks(
480                                    liveGroup, publicLayoutSetPrototypeId,
481                                    privateLayoutSetPrototypeId,
482                                    publicLayoutSetPrototypeLinkEnabled,
483                                    privateLayoutSetPrototypeLinkEnabled);
484                    }
485    
486                    // Staging
487    
488                    String redirect = ParamUtil.getString(actionRequest, "redirect");
489    
490                    long refererPlid = GetterUtil.getLong(
491                            HttpUtil.getParameter(redirect, "refererPlid", false));
492    
493                    if (!privateLayoutSet.isLayoutSetPrototypeLinkActive() &&
494                            !publicLayoutSet.isLayoutSetPrototypeLinkActive()) {
495    
496                            if ((refererPlid > 0) && liveGroup.hasStagingGroup() &&
497                                    (themeDisplay.getScopeGroupId() != liveGroup.getGroupId())) {
498    
499                                    Layout firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
500                                            liveGroup.getGroupId(), false,
501                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
502    
503                                    if (firstLayout == null) {
504                                            firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
505                                                    liveGroup.getGroupId(), true,
506                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
507                                    }
508    
509                                    if (firstLayout != null) {
510                                            refererPlid = firstLayout.getPlid();
511                                    }
512                                    else {
513                                            refererPlid = 0;
514                                    }
515                            }
516    
517                            StagingUtil.updateStaging(actionRequest, liveGroup);
518                    }
519    
520                    return new Object[] {
521                            liveGroup, oldFriendlyURL, oldStagingFriendlyURL, refererPlid};
522            }
523    
524            private static final int _LAYOUT_SET_VISIBILITY_PRIVATE = 1;
525    
526    }