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