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