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.LocaleException;
022    import com.liferay.portal.NoSuchGroupException;
023    import com.liferay.portal.NoSuchLayoutException;
024    import com.liferay.portal.RemoteExportException;
025    import com.liferay.portal.RemoteOptionsException;
026    import com.liferay.portal.RequiredGroupException;
027    import com.liferay.portal.kernel.dao.orm.QueryUtil;
028    import com.liferay.portal.kernel.exception.PortalException;
029    import com.liferay.portal.kernel.exception.SystemException;
030    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
031    import com.liferay.portal.kernel.servlet.SessionErrors;
032    import com.liferay.portal.kernel.servlet.SessionMessages;
033    import com.liferay.portal.kernel.staging.StagingUtil;
034    import com.liferay.portal.kernel.util.Constants;
035    import com.liferay.portal.kernel.util.GetterUtil;
036    import com.liferay.portal.kernel.util.HttpUtil;
037    import com.liferay.portal.kernel.util.ListUtil;
038    import com.liferay.portal.kernel.util.ParamUtil;
039    import com.liferay.portal.kernel.util.PrefsPropsUtil;
040    import com.liferay.portal.kernel.util.PropsKeys;
041    import com.liferay.portal.kernel.util.StringPool;
042    import com.liferay.portal.kernel.util.StringUtil;
043    import com.liferay.portal.kernel.util.UnicodeProperties;
044    import com.liferay.portal.kernel.util.UniqueList;
045    import com.liferay.portal.kernel.util.Validator;
046    import com.liferay.portal.liveusers.LiveUsers;
047    import com.liferay.portal.model.Group;
048    import com.liferay.portal.model.GroupConstants;
049    import com.liferay.portal.model.Layout;
050    import com.liferay.portal.model.LayoutConstants;
051    import com.liferay.portal.model.LayoutSet;
052    import com.liferay.portal.model.MembershipRequest;
053    import com.liferay.portal.model.MembershipRequestConstants;
054    import com.liferay.portal.model.Role;
055    import com.liferay.portal.model.Team;
056    import com.liferay.portal.security.auth.PrincipalException;
057    import com.liferay.portal.service.GroupLocalServiceUtil;
058    import com.liferay.portal.service.GroupServiceUtil;
059    import com.liferay.portal.service.LayoutLocalServiceUtil;
060    import com.liferay.portal.service.LayoutSetServiceUtil;
061    import com.liferay.portal.service.MembershipRequestLocalServiceUtil;
062    import com.liferay.portal.service.MembershipRequestServiceUtil;
063    import com.liferay.portal.service.RoleLocalServiceUtil;
064    import com.liferay.portal.service.ServiceContext;
065    import com.liferay.portal.service.ServiceContextFactory;
066    import com.liferay.portal.service.TeamLocalServiceUtil;
067    import com.liferay.portal.struts.PortletAction;
068    import com.liferay.portal.theme.ThemeDisplay;
069    import com.liferay.portal.util.PortalUtil;
070    import com.liferay.portal.util.WebKeys;
071    import com.liferay.portlet.asset.AssetCategoryException;
072    import com.liferay.portlet.asset.AssetTagException;
073    import com.liferay.portlet.sites.util.SitesUtil;
074    import com.liferay.portlet.trash.util.TrashUtil;
075    
076    import java.util.ArrayList;
077    import java.util.List;
078    
079    import javax.portlet.ActionRequest;
080    import javax.portlet.ActionResponse;
081    import javax.portlet.PortletConfig;
082    import javax.portlet.PortletRequest;
083    import javax.portlet.RenderRequest;
084    import javax.portlet.RenderResponse;
085    
086    import org.apache.struts.action.ActionForm;
087    import org.apache.struts.action.ActionForward;
088    import org.apache.struts.action.ActionMapping;
089    
090    /**
091     * @author Brian Wing Shun Chan
092     * @author Zsolt Berentey
093     */
094    public class EditGroupAction extends PortletAction {
095    
096            @Override
097            public void processAction(
098                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
099                            ActionRequest actionRequest, ActionResponse actionResponse)
100                    throws Exception {
101    
102                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
103                            WebKeys.THEME_DISPLAY);
104    
105                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
106    
107                    String redirect = ParamUtil.getString(actionRequest, "redirect");
108    
109                    try {
110                            String closeRedirect = ParamUtil.getString(
111                                    actionRequest, "closeRedirect");
112    
113                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
114                                    Object[] returnValue = updateGroup(actionRequest);
115    
116                                    Group group = (Group)returnValue[0];
117                                    String oldFriendlyURL = (String)returnValue[1];
118                                    String oldStagingFriendlyURL = (String)returnValue[2];
119                                    long newRefererPlid = (Long)returnValue[3];
120    
121                                    redirect = HttpUtil.setParameter(
122                                            redirect, "doAsGroupId", group.getGroupId());
123                                    redirect = HttpUtil.setParameter(
124                                            redirect, "refererPlid", newRefererPlid);
125    
126                                    closeRedirect = updateCloseRedirect(
127                                            closeRedirect, group, themeDisplay, oldFriendlyURL,
128                                            oldStagingFriendlyURL);
129                            }
130                            else if (cmd.equals(Constants.DEACTIVATE) ||
131                                             cmd.equals(Constants.RESTORE)) {
132    
133                                    updateActive(actionRequest, cmd);
134                            }
135                            else if (cmd.equals(Constants.DELETE)) {
136                                    deleteGroups(actionRequest);
137                            }
138    
139                            if (Validator.isNotNull(closeRedirect)) {
140                                    redirect = HttpUtil.setParameter(
141                                            redirect, "closeRedirect", closeRedirect);
142    
143                                    LiferayPortletConfig liferayPortletConfig =
144                                            (LiferayPortletConfig)portletConfig;
145    
146                                    SessionMessages.add(
147                                            actionRequest,
148                                            liferayPortletConfig.getPortletId() +
149                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
150                                            closeRedirect);
151                            }
152    
153                            sendRedirect(actionRequest, actionResponse, redirect);
154                    }
155                    catch (Exception e) {
156                            if (e instanceof NoSuchGroupException ||
157                                    e instanceof PrincipalException) {
158    
159                                    SessionErrors.add(actionRequest, e.getClass());
160    
161                                    setForward(actionRequest, "portlet.sites_admin.error");
162                            }
163                            else if (e instanceof AssetCategoryException ||
164                                             e instanceof AssetTagException ||
165                                             e instanceof DuplicateGroupException ||
166                                             e instanceof GroupFriendlyURLException ||
167                                             e instanceof GroupNameException ||
168                                             e instanceof LayoutSetVirtualHostException ||
169                                             e instanceof LocaleException ||
170                                             e instanceof RemoteExportException ||
171                                             e instanceof RemoteOptionsException ||
172                                             e instanceof RequiredGroupException ||
173                                             e instanceof SystemException) {
174    
175                                    SessionErrors.add(actionRequest, e.getClass(), e);
176    
177                                    if (cmd.equals(Constants.DEACTIVATE) ||
178                                            cmd.equals(Constants.DELETE) ||
179                                            cmd.equals(Constants.RESTORE)) {
180    
181                                            if (Validator.isNotNull(redirect)) {
182                                                    actionResponse.sendRedirect(redirect);
183                                            }
184                                    }
185                            }
186                            else {
187                                    throw e;
188                            }
189                    }
190            }
191    
192            @Override
193            public ActionForward render(
194                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
195                            RenderRequest renderRequest, RenderResponse renderResponse)
196                    throws Exception {
197    
198                    try {
199                            ActionUtil.getGroup(renderRequest);
200                    }
201                    catch (Exception e) {
202                            if (e instanceof NoSuchGroupException ||
203                                    e instanceof PrincipalException) {
204    
205                                    SessionErrors.add(renderRequest, e.getClass());
206    
207                                    return mapping.findForward("portlet.sites_admin.error");
208                            }
209                            else {
210                                    throw e;
211                            }
212                    }
213    
214                    return mapping.findForward(
215                            getForward(renderRequest, "portlet.sites_admin.edit_site"));
216            }
217    
218            protected void deleteGroups(ActionRequest actionRequest) throws Exception {
219                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
220                            WebKeys.THEME_DISPLAY);
221    
222                    long[] deleteGroupIds = null;
223    
224                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
225    
226                    if (groupId > 0) {
227                            deleteGroupIds = new long[] {groupId};
228                    }
229                    else {
230                            deleteGroupIds = StringUtil.split(
231                                    ParamUtil.getString(actionRequest, "deleteGroupIds"), 0L);
232                    }
233    
234                    for (long deleteGroupId : deleteGroupIds) {
235                            GroupServiceUtil.deleteGroup(deleteGroupId);
236    
237                            LiveUsers.deleteGroup(themeDisplay.getCompanyId(), deleteGroupId);
238                    }
239            }
240    
241            protected long getRefererGroupId(ThemeDisplay themeDisplay)
242                    throws Exception {
243    
244                    long refererGroupId = 0;
245    
246                    try {
247                            Layout refererLayout = LayoutLocalServiceUtil.getLayout(
248                                    themeDisplay.getRefererPlid());
249    
250                            refererGroupId = refererLayout.getGroupId();
251                    }
252                    catch (NoSuchLayoutException nsle) {
253                    }
254    
255                    return refererGroupId;
256            }
257    
258            protected List<Role> getRoles(PortletRequest portletRequest)
259                    throws Exception {
260    
261                    List<Role> roles = new ArrayList<Role>();
262    
263                    long[] siteRolesRoleIds = StringUtil.split(
264                            ParamUtil.getString(portletRequest, "siteRolesRoleIds"), 0L);
265    
266                    for (long siteRolesRoleId : siteRolesRoleIds) {
267                            if (siteRolesRoleId == 0) {
268                                    continue;
269                            }
270    
271                            Role role = RoleLocalServiceUtil.getRole(siteRolesRoleId);
272    
273                            roles.add(role);
274                    }
275    
276                    return roles;
277            }
278    
279            protected List<Team> getTeams(PortletRequest portletRequest)
280                    throws Exception {
281    
282                    List<Team> teams = new UniqueList<Team>();
283    
284                    long[] teamsTeamIds= StringUtil.split(
285                            ParamUtil.getString(portletRequest, "teamsTeamIds"), 0L);
286    
287                    for (long teamsTeamId : teamsTeamIds) {
288                            if (teamsTeamId == 0) {
289                                    continue;
290                            }
291    
292                            Team team = TeamLocalServiceUtil.getTeam(teamsTeamId);
293    
294                            teams.add(team);
295                    }
296    
297                    return teams;
298            }
299    
300            protected void updateActive(ActionRequest actionRequest, String cmd)
301                    throws Exception {
302    
303                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
304                            WebKeys.THEME_DISPLAY);
305    
306                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
307    
308                    if ((groupId == themeDisplay.getDoAsGroupId()) ||
309                            (groupId == themeDisplay.getScopeGroupId()) ||
310                            (groupId == getRefererGroupId(themeDisplay))) {
311    
312                            throw new RequiredGroupException(
313                                    String.valueOf(groupId), RequiredGroupException.CURRENT_GROUP);
314                    }
315    
316                    Group group = GroupServiceUtil.getGroup(groupId);
317    
318                    boolean active = false;
319    
320                    if (cmd.equals(Constants.RESTORE)) {
321                            active = true;
322                    }
323    
324                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
325                            Group.class.getName(), actionRequest);
326    
327                    GroupServiceUtil.updateGroup(
328                            groupId, group.getParentGroupId(), group.getName(),
329                            group.getDescription(), group.getType(), group.getFriendlyURL(),
330                            active, serviceContext);
331            }
332    
333            protected String updateCloseRedirect(
334                            String closeRedirect, Group group, ThemeDisplay themeDisplay,
335                            String oldFriendlyURL, String oldStagingFriendlyURL)
336                    throws PortalException, SystemException {
337    
338                    if (Validator.isNull(closeRedirect) || (group == null)) {
339                            return closeRedirect;
340                    }
341    
342                    String oldPath = null;
343                    String newPath = null;
344    
345                    if (Validator.isNotNull(oldFriendlyURL)) {
346                            oldPath = oldFriendlyURL;
347                            newPath = group.getFriendlyURL();
348    
349                            if (closeRedirect.contains(oldPath)) {
350                                    closeRedirect = PortalUtil.updateRedirect(
351                                            closeRedirect, oldPath, newPath);
352                            }
353                            else {
354                                    closeRedirect = PortalUtil.getGroupFriendlyURL(
355                                            group, false, themeDisplay);
356                            }
357                    }
358    
359                    if (Validator.isNotNull(oldStagingFriendlyURL)) {
360                            Group stagingGroup = group.getStagingGroup();
361    
362                            if (GroupLocalServiceUtil.fetchGroup(
363                                            stagingGroup.getGroupId()) == null) {
364    
365                                    oldPath = oldStagingFriendlyURL;
366                                    newPath = group.getFriendlyURL();
367                            }
368                            else {
369                                    oldPath = oldStagingFriendlyURL;
370                                    newPath = stagingGroup.getFriendlyURL();
371                            }
372    
373                            if (closeRedirect.contains(oldPath)) {
374                                    closeRedirect = PortalUtil.updateRedirect(
375                                            closeRedirect, oldPath, newPath);
376                            }
377                            else {
378                                    closeRedirect = PortalUtil.getGroupFriendlyURL(
379                                            group, false, themeDisplay);
380                            }
381                    }
382    
383                    return closeRedirect;
384            }
385    
386            protected Object[] updateGroup(ActionRequest actionRequest)
387                    throws Exception {
388    
389                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
390                            WebKeys.THEME_DISPLAY);
391    
392                    long userId = PortalUtil.getUserId(actionRequest);
393    
394                    long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
395    
396                    long parentGroupId = ParamUtil.getLong(
397                            actionRequest, "parentGroupSearchContainerPrimaryKeys",
398                            GroupConstants.DEFAULT_PARENT_GROUP_ID);
399                    String name = null;
400                    String description = null;
401                    int type = 0;
402                    String friendlyURL = null;
403                    boolean active = false;
404    
405                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
406                            Group.class.getName(), actionRequest);
407    
408                    Group liveGroup = null;
409                    String oldFriendlyURL = null;
410                    String oldStagingFriendlyURL = null;
411    
412                    if (liveGroupId <= 0) {
413    
414                            // Add group
415    
416                            name = ParamUtil.getString(actionRequest, "name");
417                            description = ParamUtil.getString(actionRequest, "description");
418                            type = ParamUtil.getInteger(actionRequest, "type");
419                            friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
420                            active = ParamUtil.getBoolean(actionRequest, "active");
421    
422                            liveGroup = GroupServiceUtil.addGroup(
423                                    parentGroupId, GroupConstants.DEFAULT_LIVE_GROUP_ID, name,
424                                    description, type, friendlyURL, true, active, serviceContext);
425    
426                            LiveUsers.joinGroup(
427                                    themeDisplay.getCompanyId(), liveGroup.getGroupId(), userId);
428                    }
429                    else {
430    
431                            // Update group
432    
433                            liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);
434    
435                            oldFriendlyURL = liveGroup.getFriendlyURL();
436    
437                            name = ParamUtil.getString(
438                                    actionRequest, "name", liveGroup.getName());
439                            description = ParamUtil.getString(
440                                    actionRequest, "description", liveGroup.getDescription());
441                            type = ParamUtil.getInteger(
442                                    actionRequest, "type", liveGroup.getType());
443                            friendlyURL = ParamUtil.getString(
444                                    actionRequest, "friendlyURL", liveGroup.getFriendlyURL());
445                            active = ParamUtil.getBoolean(
446                                    actionRequest, "active", liveGroup.getActive());
447    
448                            liveGroup = GroupServiceUtil.updateGroup(
449                                    liveGroupId, parentGroupId, name, description, type,
450                                    friendlyURL, active, serviceContext);
451    
452                            if (type == GroupConstants.TYPE_SITE_OPEN) {
453                                    List<MembershipRequest> membershipRequests =
454                                            MembershipRequestLocalServiceUtil.search(
455                                                    liveGroupId, MembershipRequestConstants.STATUS_PENDING,
456                                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS);
457    
458                                    for (MembershipRequest membershipRequest : membershipRequests) {
459                                            MembershipRequestServiceUtil.updateStatus(
460                                                    membershipRequest.getMembershipRequestId(),
461                                                    themeDisplay.translate(
462                                                            "your-membership-has-been-approved"),
463                                                    MembershipRequestConstants.STATUS_APPROVED,
464                                                    serviceContext);
465    
466                                            LiveUsers.joinGroup(
467                                                    themeDisplay.getCompanyId(),
468                                                    membershipRequest.getGroupId(),
469                                                    new long[] {membershipRequest.getUserId()});
470                                    }
471                            }
472                    }
473    
474                    // Settings
475    
476                    UnicodeProperties typeSettingsProperties =
477                            liveGroup.getTypeSettingsProperties();
478    
479                    String customJspServletContextName = ParamUtil.getString(
480                            actionRequest, "customJspServletContextName",
481                            typeSettingsProperties.getProperty("customJspServletContextName"));
482    
483                    typeSettingsProperties.setProperty(
484                            "customJspServletContextName", customJspServletContextName);
485    
486                    typeSettingsProperties.setProperty(
487                            "defaultSiteRoleIds",
488                            ListUtil.toString(
489                                    getRoles(actionRequest), Role.ROLE_ID_ACCESSOR,
490                                    StringPool.COMMA));
491                    typeSettingsProperties.setProperty(
492                            "defaultTeamIds",
493                            ListUtil.toString(
494                                    getTeams(actionRequest), Team.TEAM_ID_ACCESSOR,
495                                    StringPool.COMMA));
496    
497                    String[] analyticsTypes = PrefsPropsUtil.getStringArray(
498                            themeDisplay.getCompanyId(), PropsKeys.ADMIN_ANALYTICS_TYPES,
499                            StringPool.NEW_LINE);
500    
501                    for (String analyticsType : analyticsTypes) {
502                            if (analyticsType.equalsIgnoreCase("google")) {
503                                    String googleAnalyticsId = ParamUtil.getString(
504                                            actionRequest, "googleAnalyticsId",
505                                            typeSettingsProperties.getProperty("googleAnalyticsId"));
506    
507                                    typeSettingsProperties.setProperty(
508                                            "googleAnalyticsId", googleAnalyticsId);
509                            }
510                            else {
511                                    String analyticsScript = ParamUtil.getString(
512                                            actionRequest, SitesUtil.ANALYTICS_PREFIX + analyticsType,
513                                            typeSettingsProperties.getProperty(analyticsType));
514    
515                                    typeSettingsProperties.setProperty(
516                                            SitesUtil.ANALYTICS_PREFIX + analyticsType,
517                                            analyticsScript);
518                            }
519                    }
520    
521                    String publicRobots = ParamUtil.getString(
522                            actionRequest, "publicRobots",
523                            liveGroup.getTypeSettingsProperty("false-robots.txt"));
524                    String privateRobots = ParamUtil.getString(
525                            actionRequest, "privateRobots",
526                            liveGroup.getTypeSettingsProperty("true-robots.txt"));
527    
528                    typeSettingsProperties.setProperty("false-robots.txt", publicRobots);
529                    typeSettingsProperties.setProperty("true-robots.txt", privateRobots);
530    
531                    int trashEnabled = ParamUtil.getInteger(
532                            actionRequest, "trashEnabled",
533                            GetterUtil.getInteger(
534                                    typeSettingsProperties.getProperty("trashEnabled"),
535                                    TrashUtil.TRASH_DEFAULT_VALUE));
536    
537                    typeSettingsProperties.setProperty(
538                            "trashEnabled", String.valueOf(trashEnabled));
539    
540                    int trashEntriesMaxAgeCompany = PrefsPropsUtil.getInteger(
541                            themeDisplay.getCompanyId(), PropsKeys.TRASH_ENTRIES_MAX_AGE);
542    
543                    int defaultTrashEntriesMaxAgeGroup = GetterUtil.getInteger(
544                            typeSettingsProperties.getProperty("trashEntriesMaxAge"),
545                            trashEntriesMaxAgeCompany);
546    
547                    int trashEntriesMaxAgeGroup = ParamUtil.getInteger(
548                            actionRequest, "trashEntriesMaxAge",
549                            defaultTrashEntriesMaxAgeGroup);
550    
551                    if (trashEntriesMaxAgeGroup != trashEntriesMaxAgeCompany) {
552                            typeSettingsProperties.setProperty(
553                                    "trashEntriesMaxAge", String.valueOf(trashEntriesMaxAgeGroup));
554                    }
555                    else {
556                            typeSettingsProperties.remove("trashEntriesMaxAge");
557                    }
558    
559                    // Virtual hosts
560    
561                    LayoutSet publicLayoutSet = liveGroup.getPublicLayoutSet();
562    
563                    String publicVirtualHost = ParamUtil.getString(
564                            actionRequest, "publicVirtualHost",
565                            publicLayoutSet.getVirtualHostname());
566    
567                    LayoutSetServiceUtil.updateVirtualHost(
568                            liveGroup.getGroupId(), false, publicVirtualHost);
569    
570                    LayoutSet privateLayoutSet = liveGroup.getPrivateLayoutSet();
571    
572                    String privateVirtualHost = ParamUtil.getString(
573                            actionRequest, "privateVirtualHost",
574                            privateLayoutSet.getVirtualHostname());
575    
576                    LayoutSetServiceUtil.updateVirtualHost(
577                            liveGroup.getGroupId(), true, privateVirtualHost);
578    
579                    // Staging
580    
581                    if (liveGroup.hasStagingGroup()) {
582                            Group stagingGroup = liveGroup.getStagingGroup();
583    
584                            oldStagingFriendlyURL = stagingGroup.getFriendlyURL();
585    
586                            friendlyURL = ParamUtil.getString(
587                                    actionRequest, "stagingFriendlyURL",
588                                    stagingGroup.getFriendlyURL());
589    
590                            GroupServiceUtil.updateFriendlyURL(
591                                    stagingGroup.getGroupId(), friendlyURL);
592    
593                            LayoutSet stagingPublicLayoutSet =
594                                    stagingGroup.getPublicLayoutSet();
595    
596                            publicVirtualHost = ParamUtil.getString(
597                                    actionRequest, "stagingPublicVirtualHost",
598                                    stagingPublicLayoutSet.getVirtualHostname());
599    
600                            LayoutSetServiceUtil.updateVirtualHost(
601                                    stagingGroup.getGroupId(), false, publicVirtualHost);
602    
603                            LayoutSet stagingPrivateLayoutSet =
604                                    stagingGroup.getPrivateLayoutSet();
605    
606                            privateVirtualHost = ParamUtil.getString(
607                                    actionRequest, "stagingPrivateVirtualHost",
608                                    stagingPrivateLayoutSet.getVirtualHostname());
609    
610                            LayoutSetServiceUtil.updateVirtualHost(
611                                    stagingGroup.getGroupId(), true, privateVirtualHost);
612                    }
613    
614                    liveGroup = GroupServiceUtil.updateGroup(
615                            liveGroup.getGroupId(), typeSettingsProperties.toString());
616    
617                    // Layout set prototypes
618    
619                    if (!liveGroup.isStaged()) {
620                            long privateLayoutSetPrototypeId = ParamUtil.getLong(
621                                    actionRequest, "privateLayoutSetPrototypeId");
622                            long publicLayoutSetPrototypeId = ParamUtil.getLong(
623                                    actionRequest, "publicLayoutSetPrototypeId");
624    
625                            boolean privateLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
626                                    actionRequest, "privateLayoutSetPrototypeLinkEnabled",
627                                    privateLayoutSet.isLayoutSetPrototypeLinkEnabled());
628                            boolean publicLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
629                                    actionRequest, "publicLayoutSetPrototypeLinkEnabled",
630                                    publicLayoutSet.isLayoutSetPrototypeLinkEnabled());
631    
632                            if ((privateLayoutSetPrototypeId == 0) &&
633                                    (publicLayoutSetPrototypeId == 0) &&
634                                    !privateLayoutSetPrototypeLinkEnabled &&
635                                    !publicLayoutSetPrototypeLinkEnabled) {
636    
637                                    long layoutSetPrototypeId = ParamUtil.getLong(
638                                            actionRequest, "layoutSetPrototypeId");
639                                    int layoutSetVisibility = ParamUtil.getInteger(
640                                            actionRequest, "layoutSetVisibility");
641                                    boolean layoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
642                                            actionRequest, "layoutSetPrototypeLinkEnabled",
643                                            (layoutSetPrototypeId > 0));
644    
645                                    if (layoutSetVisibility == _LAYOUT_SET_VISIBILITY_PRIVATE) {
646                                            privateLayoutSetPrototypeId = layoutSetPrototypeId;
647    
648                                            privateLayoutSetPrototypeLinkEnabled =
649                                                    layoutSetPrototypeLinkEnabled;
650                                    }
651                                    else {
652                                            publicLayoutSetPrototypeId = layoutSetPrototypeId;
653    
654                                            publicLayoutSetPrototypeLinkEnabled =
655                                                    layoutSetPrototypeLinkEnabled;
656                                    }
657                            }
658    
659                            SitesUtil.updateLayoutSetPrototypesLinks(
660                                    liveGroup, publicLayoutSetPrototypeId,
661                                    privateLayoutSetPrototypeId,
662                                    publicLayoutSetPrototypeLinkEnabled,
663                                    privateLayoutSetPrototypeLinkEnabled);
664                    }
665    
666                    // Staging
667    
668                    String redirect = ParamUtil.getString(actionRequest, "redirect");
669    
670                    long refererPlid = GetterUtil.getLong(
671                            HttpUtil.getParameter(redirect, "refererPlid", false));
672    
673                    if (!privateLayoutSet.isLayoutSetPrototypeLinkActive() &&
674                            !publicLayoutSet.isLayoutSetPrototypeLinkActive()) {
675    
676                            if ((refererPlid > 0) && liveGroup.hasStagingGroup() &&
677                                    (themeDisplay.getScopeGroupId() != liveGroup.getGroupId())) {
678    
679                                    Layout firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
680                                            liveGroup.getGroupId(), false,
681                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
682    
683                                    if (firstLayout == null) {
684                                            firstLayout = LayoutLocalServiceUtil.fetchFirstLayout(
685                                                    liveGroup.getGroupId(), true,
686                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
687                                    }
688    
689                                    if (firstLayout != null) {
690                                            refererPlid = firstLayout.getPlid();
691                                    }
692                                    else {
693                                            refererPlid = 0;
694                                    }
695                            }
696    
697                            StagingUtil.updateStaging(actionRequest, liveGroup);
698                    }
699    
700                    return new Object[] {
701                            liveGroup, oldFriendlyURL, oldStagingFriendlyURL, refererPlid};
702            }
703    
704            private static final int _LAYOUT_SET_VISIBILITY_PRIVATE = 1;
705    
706    }