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.portal.events;
016    
017    import com.liferay.portal.LayoutPermissionException;
018    import com.liferay.portal.NoSuchGroupException;
019    import com.liferay.portal.NoSuchLayoutException;
020    import com.liferay.portal.NoSuchUserException;
021    import com.liferay.portal.kernel.dao.orm.QueryUtil;
022    import com.liferay.portal.kernel.events.Action;
023    import com.liferay.portal.kernel.events.ActionException;
024    import com.liferay.portal.kernel.exception.PortalException;
025    import com.liferay.portal.kernel.exception.SystemException;
026    import com.liferay.portal.kernel.language.LanguageUtil;
027    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.portlet.LiferayPortletURL;
031    import com.liferay.portal.kernel.portlet.LiferayWindowState;
032    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
033    import com.liferay.portal.kernel.servlet.HttpHeaders;
034    import com.liferay.portal.kernel.servlet.SessionErrors;
035    import com.liferay.portal.kernel.util.FriendlyURLNormalizerUtil;
036    import com.liferay.portal.kernel.util.GetterUtil;
037    import com.liferay.portal.kernel.util.HttpUtil;
038    import com.liferay.portal.kernel.util.LocaleUtil;
039    import com.liferay.portal.kernel.util.ParamUtil;
040    import com.liferay.portal.kernel.util.PropsKeys;
041    import com.liferay.portal.kernel.util.SessionParamUtil;
042    import com.liferay.portal.kernel.util.StringBundler;
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.Validator;
047    import com.liferay.portal.model.ColorScheme;
048    import com.liferay.portal.model.Company;
049    import com.liferay.portal.model.Group;
050    import com.liferay.portal.model.GroupConstants;
051    import com.liferay.portal.model.Image;
052    import com.liferay.portal.model.Layout;
053    import com.liferay.portal.model.LayoutConstants;
054    import com.liferay.portal.model.LayoutSet;
055    import com.liferay.portal.model.LayoutTypePortlet;
056    import com.liferay.portal.model.LayoutTypePortletConstants;
057    import com.liferay.portal.model.Portlet;
058    import com.liferay.portal.model.RoleConstants;
059    import com.liferay.portal.model.Theme;
060    import com.liferay.portal.model.User;
061    import com.liferay.portal.model.impl.ColorSchemeImpl;
062    import com.liferay.portal.model.impl.VirtualLayout;
063    import com.liferay.portal.security.auth.PrincipalException;
064    import com.liferay.portal.security.permission.ActionKeys;
065    import com.liferay.portal.security.permission.PermissionChecker;
066    import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
067    import com.liferay.portal.security.permission.PermissionThreadLocal;
068    import com.liferay.portal.service.GroupLocalServiceUtil;
069    import com.liferay.portal.service.ImageLocalServiceUtil;
070    import com.liferay.portal.service.LayoutLocalServiceUtil;
071    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
072    import com.liferay.portal.service.PortletLocalServiceUtil;
073    import com.liferay.portal.service.RoleLocalServiceUtil;
074    import com.liferay.portal.service.ServiceContext;
075    import com.liferay.portal.service.ServiceContextFactory;
076    import com.liferay.portal.service.ServiceContextThreadLocal;
077    import com.liferay.portal.service.ThemeLocalServiceUtil;
078    import com.liferay.portal.service.UserLocalServiceUtil;
079    import com.liferay.portal.service.permission.GroupPermissionUtil;
080    import com.liferay.portal.service.permission.LayoutPermissionUtil;
081    import com.liferay.portal.service.permission.PortletPermissionUtil;
082    import com.liferay.portal.theme.ThemeDisplay;
083    import com.liferay.portal.theme.ThemeDisplayFactory;
084    import com.liferay.portal.util.CookieKeys;
085    import com.liferay.portal.util.LayoutClone;
086    import com.liferay.portal.util.LayoutCloneFactory;
087    import com.liferay.portal.util.PortalUtil;
088    import com.liferay.portal.util.PortletCategoryKeys;
089    import com.liferay.portal.util.PortletKeys;
090    import com.liferay.portal.util.PrefsPropsUtil;
091    import com.liferay.portal.util.PropsUtil;
092    import com.liferay.portal.util.PropsValues;
093    import com.liferay.portal.util.WebKeys;
094    import com.liferay.portal.webserver.WebServerServletTokenUtil;
095    import com.liferay.portlet.PortalPreferences;
096    import com.liferay.portlet.PortletPreferencesFactoryUtil;
097    import com.liferay.portlet.PortletURLImpl;
098    import com.liferay.portlet.asset.model.AssetEntry;
099    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
100    import com.liferay.portlet.journal.NoSuchArticleException;
101    import com.liferay.portlet.journal.model.JournalArticle;
102    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
103    import com.liferay.portlet.sites.util.SitesUtil;
104    
105    import java.io.File;
106    
107    import java.util.ArrayList;
108    import java.util.HashMap;
109    import java.util.LinkedHashMap;
110    import java.util.List;
111    import java.util.Locale;
112    import java.util.Map;
113    import java.util.TimeZone;
114    
115    import javax.portlet.PortletMode;
116    import javax.portlet.PortletRequest;
117    import javax.portlet.PortletURL;
118    import javax.portlet.WindowState;
119    
120    import javax.servlet.http.HttpServletRequest;
121    import javax.servlet.http.HttpServletResponse;
122    import javax.servlet.http.HttpSession;
123    
124    import org.apache.commons.lang.time.StopWatch;
125    import org.apache.struts.Globals;
126    
127    /**
128     * @author Brian Wing Shun Chan
129     * @author Felix Ventero
130     * @author Jorge Ferrer
131     */
132    public class ServicePreAction extends Action {
133    
134            public ServicePreAction() {
135                    initImportLARFiles();
136            }
137    
138            public ThemeDisplay initThemeDisplay(
139                            HttpServletRequest request, HttpServletResponse response)
140                    throws Exception {
141    
142                    HttpSession session = request.getSession();
143    
144                    // Company
145    
146                    Company company = PortalUtil.getCompany(request);
147    
148                    long companyId = company.getCompanyId();
149    
150                    // CDN host
151    
152                    String cdnHost = PortalUtil.getCDNHost(request);
153    
154                    String dynamicResourcesCDNHost = StringPool.BLANK;
155    
156                    boolean cdnDynamicResourceEnabled =
157                            PortalUtil.isCDNDynamicResourcesEnabled(request);
158    
159                    if (cdnDynamicResourceEnabled) {
160                            dynamicResourcesCDNHost = cdnHost;
161                    }
162    
163                    // Portal URL
164    
165                    String portalURL = PortalUtil.getPortalURL(request);
166    
167                    // Paths
168    
169                    String contextPath = PortalUtil.getPathContext();
170                    String friendlyURLPrivateGroupPath =
171                            PortalUtil.getPathFriendlyURLPrivateGroup();
172                    String friendlyURLPrivateUserPath =
173                            PortalUtil.getPathFriendlyURLPrivateUser();
174                    String friendlyURLPublicPath = PortalUtil.getPathFriendlyURLPublic();
175                    String imagePath = dynamicResourcesCDNHost.concat(
176                            PortalUtil.getPathImage());
177                    String mainPath = PortalUtil.getPathMain();
178    
179                    String i18nPath = (String)request.getAttribute(WebKeys.I18N_PATH);
180    
181                    if (Validator.isNotNull(i18nPath)) {
182                            if (Validator.isNotNull(contextPath)) {
183                                    String i18nContextPath = contextPath.concat(i18nPath);
184    
185                                    friendlyURLPrivateGroupPath = StringUtil.replaceFirst(
186                                            friendlyURLPrivateGroupPath, contextPath, i18nContextPath);
187                                    friendlyURLPrivateUserPath = StringUtil.replaceFirst(
188                                            friendlyURLPrivateUserPath, contextPath, i18nContextPath);
189                                    friendlyURLPublicPath = StringUtil.replaceFirst(
190                                            friendlyURLPublicPath, contextPath, i18nContextPath);
191                                    mainPath = StringUtil.replaceFirst(
192                                            mainPath, contextPath, i18nContextPath);
193                            }
194                            else {
195                                    friendlyURLPrivateGroupPath = i18nPath.concat(
196                                            friendlyURLPrivateGroupPath);
197                                    friendlyURLPrivateUserPath = i18nPath.concat(
198                                            friendlyURLPrivateUserPath);
199                                    friendlyURLPublicPath = i18nPath.concat(friendlyURLPublicPath);
200                                    mainPath = i18nPath.concat(mainPath);
201                            }
202                    }
203    
204                    // Company logo
205    
206                    StringBundler sb = new StringBundler(5);
207    
208                    sb.append(imagePath);
209                    sb.append("/company_logo?img_id=");
210                    sb.append(company.getLogoId());
211                    sb.append("&t=");
212                    sb.append(WebServerServletTokenUtil.getToken(company.getLogoId()));
213    
214                    String companyLogo = sb.toString();
215    
216                    int companyLogoHeight = 0;
217                    int companyLogoWidth = 0;
218    
219                    Image companyLogoImage = ImageLocalServiceUtil.getCompanyLogo(
220                            company.getLogoId());
221    
222                    if (companyLogoImage != null) {
223                            companyLogoHeight = companyLogoImage.getHeight();
224                            companyLogoWidth = companyLogoImage.getWidth();
225                    }
226    
227                    String realCompanyLogo = companyLogo;
228                    int realCompanyLogoHeight = companyLogoHeight;
229                    int realCompanyLogoWidth = companyLogoWidth;
230    
231                    // User
232    
233                    User user = null;
234    
235                    try {
236                            user = PortalUtil.getUser(request);
237                    }
238                    catch (NoSuchUserException nsue) {
239                            if (_log.isWarnEnabled()) {
240                                    _log.warn(nsue.getMessage());
241                            }
242    
243                            long userId = PortalUtil.getUserId(request);
244    
245                            if (userId > 0) {
246                                    session.invalidate();
247                            }
248    
249                            return null;
250                    }
251    
252                    boolean signedIn = false;
253    
254                    if (user == null) {
255                            user = company.getDefaultUser();
256                    }
257                    else if (!user.isDefaultUser()) {
258                            signedIn = true;
259                    }
260    
261                    if (PropsValues.BROWSER_CACHE_DISABLED ||
262                            (PropsValues.BROWSER_CACHE_SIGNED_IN_DISABLED && signedIn)) {
263    
264                            response.setDateHeader(HttpHeaders.EXPIRES, 0);
265                            response.setHeader(
266                                    HttpHeaders.CACHE_CONTROL,
267                                    HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE);
268                            response.setHeader(
269                                    HttpHeaders.PRAGMA, HttpHeaders.PRAGMA_NO_CACHE_VALUE);
270                    }
271    
272                    User realUser = user;
273    
274                    Long realUserId = (Long)session.getAttribute(WebKeys.USER_ID);
275    
276                    if (realUserId != null) {
277                            if (user.getUserId() != realUserId.longValue()) {
278                                    realUser = UserLocalServiceUtil.getUserById(
279                                            realUserId.longValue());
280                            }
281                    }
282    
283                    String doAsUserId = ParamUtil.getString(request, "doAsUserId");
284                    String doAsUserLanguageId = ParamUtil.getString(
285                            request, "doAsUserLanguageId");
286                    long doAsGroupId = ParamUtil.getLong(request, "doAsGroupId");
287    
288                    long refererPlid = ParamUtil.getLong(request, "refererPlid");
289    
290                    if (LayoutLocalServiceUtil.fetchLayout(refererPlid) == null) {
291                            refererPlid = 0;
292                    }
293    
294                    String controlPanelCategory = ParamUtil.getString(
295                            request, "controlPanelCategory");
296    
297                    // Permission checker
298    
299                    PermissionChecker permissionChecker =
300                            PermissionCheckerFactoryUtil.create(user);
301    
302                    PermissionThreadLocal.setPermissionChecker(permissionChecker);
303    
304                    // Locale
305    
306                    Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
307    
308                    if (Validator.isNotNull(doAsUserLanguageId)) {
309                            locale = LocaleUtil.fromLanguageId(doAsUserLanguageId);
310                    }
311    
312                    String i18nLanguageId = (String)request.getAttribute(
313                            WebKeys.I18N_LANGUAGE_ID);
314    
315                    if (Validator.isNotNull(i18nLanguageId)) {
316                            locale = LocaleUtil.fromLanguageId(i18nLanguageId);
317                    }
318                    else if (locale == null) {
319                            if (signedIn) {
320                                    locale = user.getLocale();
321                            }
322                            else {
323    
324                                    // User previously set their preferred language
325    
326                                    String languageId = CookieKeys.getCookie(
327                                            request, CookieKeys.GUEST_LANGUAGE_ID, false);
328    
329                                    if (Validator.isNotNull(languageId)) {
330                                            locale = LocaleUtil.fromLanguageId(languageId);
331                                    }
332    
333                                    // Get locale from the request
334    
335                                    if ((locale == null) && PropsValues.LOCALE_DEFAULT_REQUEST) {
336                                            locale = request.getLocale();
337                                    }
338    
339                                    // Get locale from the default user
340    
341                                    if (locale == null) {
342                                            locale = user.getLocale();
343                                    }
344    
345                                    if (Validator.isNull(locale.getCountry())) {
346    
347                                            // Locales must contain a country code
348    
349                                            locale = LanguageUtil.getLocale(locale.getLanguage());
350                                    }
351    
352                                    if (!LanguageUtil.isAvailableLocale(locale)) {
353                                            locale = user.getLocale();
354                                    }
355                            }
356    
357                            session.setAttribute(Globals.LOCALE_KEY, locale);
358    
359                            LanguageUtil.updateCookie(request, response, locale);
360                    }
361    
362                    // Cookie support
363    
364                    try {
365    
366                            // LEP-4069
367    
368                            CookieKeys.validateSupportCookie(request);
369                    }
370                    catch (Exception e) {
371                            CookieKeys.addSupportCookie(request, response);
372                    }
373    
374                    // Time zone
375    
376                    TimeZone timeZone = user.getTimeZone();
377    
378                    if (timeZone == null) {
379                            timeZone = company.getTimeZone();
380                    }
381    
382                    // Layouts
383    
384                    if (signedIn) {
385                            updateUserLayouts(user);
386                    }
387    
388                    Layout layout = null;
389                    List<Layout> layouts = null;
390    
391                    long plid = ParamUtil.getLong(request, "p_l_id");
392    
393                    if (plid > 0) {
394                            layout = LayoutLocalServiceUtil.getLayout(plid);
395    
396                            long sourceGroupId = ParamUtil.getLong(request, "p_v_l_s_g_id");
397    
398                            if ((sourceGroupId > 0) && (sourceGroupId != layout.getGroupId())) {
399                                    Group sourceGroup = GroupLocalServiceUtil.getGroup(
400                                            sourceGroupId);
401    
402                                    if (SitesUtil.isUserGroupLayoutSetViewable(
403                                                    permissionChecker, layout.getGroup())) {
404    
405                                            layout = new VirtualLayout(layout, sourceGroup);
406                                    }
407                                    else {
408                                            layout = null;
409                                    }
410                            }
411                    }
412                    else {
413                            long groupId = ParamUtil.getLong(request, "groupId");
414                            boolean privateLayout = ParamUtil.getBoolean(
415                                    request, "privateLayout");
416                            long layoutId = ParamUtil.getLong(request, "layoutId");
417    
418                            if ((groupId > 0) && (layoutId > 0)) {
419                                    layout = LayoutLocalServiceUtil.getLayout(
420                                            groupId, privateLayout, layoutId);
421                            }
422                    }
423    
424                    Boolean redirectToDefaultLayout = (Boolean)request.getAttribute(
425                            WebKeys.REDIRECT_TO_DEFAULT_LAYOUT);
426    
427                    if (redirectToDefaultLayout == null) {
428                            redirectToDefaultLayout = Boolean.FALSE;
429                    }
430    
431                    if (layout != null) {
432                            Group group = layout.getGroup();
433    
434                            if (!signedIn && PropsValues.AUTH_FORWARD_BY_REDIRECT) {
435                                    request.setAttribute(WebKeys.REQUESTED_LAYOUT, layout);
436                            }
437    
438                            boolean isViewableGroup = LayoutPermissionUtil.contains(
439                                    permissionChecker, layout, controlPanelCategory, true,
440                                    ActionKeys.VIEW);
441                            boolean isViewableStaging = GroupPermissionUtil.contains(
442                                    permissionChecker, group.getGroupId(), ActionKeys.VIEW_STAGING);
443    
444                            if (isViewableStaging) {
445                                    layouts = LayoutLocalServiceUtil.getLayouts(
446                                            layout.getGroupId(), layout.isPrivateLayout(),
447                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
448                            }
449                            else if (!isViewableGroup && group.isStagingGroup()) {
450                                    layout = null;
451                            }
452                            else if (!isLoginRequest(request) &&
453                                             (!isViewableGroup ||
454                                              (!redirectToDefaultLayout &&
455                                               !LayoutPermissionUtil.contains(
456                                                       permissionChecker, layout, false,
457                                                       ActionKeys.VIEW)))) {
458    
459                                    if (user.isDefaultUser() &&
460                                            PropsValues.AUTH_LOGIN_PROMPT_ENABLED) {
461    
462                                            throw new PrincipalException("User is not authenticated");
463                                    }
464    
465                                    sb = new StringBundler(6);
466    
467                                    sb.append("User ");
468                                    sb.append(user.getUserId());
469                                    sb.append(" is not allowed to access the ");
470                                    sb.append(layout.isPrivateLayout() ? "private": "public");
471                                    sb.append(" pages of group ");
472                                    sb.append(layout.getGroupId());
473    
474                                    if (_log.isWarnEnabled()) {
475                                            _log.warn(sb.toString());
476                                    }
477    
478                                    throw new NoSuchLayoutException(sb.toString());
479                            }
480                            else if (isLoginRequest(request) && !isViewableGroup) {
481                                    layout = null;
482                            }
483                            else if (group.isLayoutPrototype()) {
484                                    layouts = new ArrayList<Layout>();
485                            }
486                            else {
487                                    layouts = LayoutLocalServiceUtil.getLayouts(
488                                            layout.getGroupId(), layout.isPrivateLayout(),
489                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
490    
491                                    if (!group.isControlPanel()) {
492                                            doAsGroupId = 0;
493                                    }
494                            }
495                    }
496    
497                    List<Layout> unfilteredLayouts = layouts;
498    
499                    if (layout == null) {
500                            Object[] defaultLayout = getDefaultLayout(request, user, signedIn);
501    
502                            layout = (Layout)defaultLayout[0];
503                            layouts = (List<Layout>)defaultLayout[1];
504    
505                            request.setAttribute(WebKeys.LAYOUT_DEFAULT, Boolean.TRUE);
506                    }
507    
508                    Object[] viewableLayouts = getViewableLayouts(
509                            request, user, permissionChecker, layout, layouts);
510    
511                    String layoutSetLogo = null;
512    
513                    layout = (Layout)viewableLayouts[0];
514                    layouts = (List<Layout>)viewableLayouts[1];
515    
516                    Group group = null;
517    
518                    if (layout != null) {
519                            group = layout.getGroup();
520    
521                            if (!group.isControlPanel()) {
522                                    rememberVisitedGroupIds(request, group.getGroupId());
523                            }
524                    }
525    
526                    LayoutTypePortlet layoutTypePortlet = null;
527    
528                    layouts = mergeAdditionalLayouts(
529                            request, user, permissionChecker, layout, layouts);
530    
531                    LayoutSet layoutSet = null;
532    
533                    boolean hasCustomizeLayoutPermission = false;
534                    boolean hasUpdateLayoutPermission = false;
535    
536                    boolean customizedView = SessionParamUtil.getBoolean(
537                            request, "customized_view", true);
538    
539                    if (layout != null) {
540                            hasCustomizeLayoutPermission = LayoutPermissionUtil.contains(
541                                    permissionChecker, layout, ActionKeys.CUSTOMIZE);
542                            hasUpdateLayoutPermission = LayoutPermissionUtil.contains(
543                                    permissionChecker, layout, ActionKeys.UPDATE);
544    
545                            layoutSet = layout.getLayoutSet();
546    
547                            if (company.isSiteLogo()) {
548                                    long logoId = 0;
549    
550                                    if (layoutSet.isLogo()) {
551                                            logoId = layoutSet.getLogoId();
552    
553                                            if (logoId == 0) {
554                                                    logoId = layoutSet.getLiveLogoId();
555                                            }
556                                    }
557                                    else {
558                                            LayoutSet siblingLayoutSet =
559                                                    LayoutSetLocalServiceUtil.getLayoutSet(
560                                                            layout.getGroupId(), !layout.isPrivateLayout());
561    
562                                            if (siblingLayoutSet.isLogo()) {
563                                                    logoId = siblingLayoutSet.getLogoId();
564                                            }
565                                    }
566    
567                                    if (logoId > 0) {
568                                            sb = new StringBundler(5);
569    
570                                            sb.append(imagePath);
571                                            sb.append("/layout_set_logo?img_id=");
572                                            sb.append(logoId);
573                                            sb.append("&t=");
574                                            sb.append(WebServerServletTokenUtil.getToken(logoId));
575    
576                                            layoutSetLogo = sb.toString();
577    
578                                            Image layoutSetLogoImage =
579                                                    ImageLocalServiceUtil.getCompanyLogo(logoId);
580    
581                                            companyLogo = layoutSetLogo;
582                                            companyLogoHeight = layoutSetLogoImage.getHeight();
583                                            companyLogoWidth = layoutSetLogoImage.getWidth();
584                                    }
585                            }
586    
587                            plid = layout.getPlid();
588    
589                            // Updates to shared layouts are not reflected until the next time
590                            // the user logs in because group layouts are cached in the session
591    
592                            layout = (Layout)layout.clone();
593    
594                            layoutTypePortlet = (LayoutTypePortlet)layout.getLayoutType();
595    
596                            boolean customizable = layoutTypePortlet.isCustomizable();
597    
598                            if (!customizable ||
599                                    (group.isLayoutPrototype() || group.isLayoutSetPrototype())) {
600    
601                                    customizedView = false;
602                            }
603    
604                            layoutTypePortlet.setCustomizedView(customizedView);
605                            layoutTypePortlet.setUpdatePermission(hasUpdateLayoutPermission);
606    
607                            if (signedIn && customizable && customizedView &&
608                                    hasCustomizeLayoutPermission) {
609    
610                                    PortalPreferences portalPreferences =
611                                            PortletPreferencesFactoryUtil.getPortalPreferences(
612                                                    companyId, user.getUserId(), true);
613    
614                                    layoutTypePortlet.setPortalPreferences(portalPreferences);
615                            }
616    
617                            LayoutClone layoutClone = LayoutCloneFactory.getInstance();
618    
619                            if (layoutClone != null) {
620                                    String typeSettings = layoutClone.get(request, plid);
621    
622                                    if (typeSettings != null) {
623                                            UnicodeProperties typeSettingsProperties =
624                                                    new UnicodeProperties(true);
625    
626                                            typeSettingsProperties.load(typeSettings);
627    
628                                            String stateMax = typeSettingsProperties.getProperty(
629                                                    LayoutTypePortletConstants.STATE_MAX);
630                                            String stateMin = typeSettingsProperties.getProperty(
631                                                    LayoutTypePortletConstants.STATE_MIN);
632                                            String modeAbout = typeSettingsProperties.getProperty(
633                                                    LayoutTypePortletConstants.MODE_ABOUT);
634                                            String modeConfig = typeSettingsProperties.getProperty(
635                                                    LayoutTypePortletConstants.MODE_CONFIG);
636                                            String modeEdit = typeSettingsProperties.getProperty(
637                                                    LayoutTypePortletConstants.MODE_EDIT);
638                                            String modeEditDefaults =
639                                                    typeSettingsProperties.getProperty(
640                                                            LayoutTypePortletConstants.MODE_EDIT_DEFAULTS);
641                                            String modeEditGuest = typeSettingsProperties.getProperty(
642                                                    LayoutTypePortletConstants.MODE_EDIT_GUEST);
643                                            String modeHelp = typeSettingsProperties.getProperty(
644                                                    LayoutTypePortletConstants.MODE_HELP);
645                                            String modePreview = typeSettingsProperties.getProperty(
646                                                    LayoutTypePortletConstants.MODE_PREVIEW);
647                                            String modePrint = typeSettingsProperties.getProperty(
648                                                    LayoutTypePortletConstants.MODE_PRINT);
649    
650                                            layoutTypePortlet.setStateMax(stateMax);
651                                            layoutTypePortlet.setStateMin(stateMin);
652                                            layoutTypePortlet.setModeAbout(modeAbout);
653                                            layoutTypePortlet.setModeConfig(modeConfig);
654                                            layoutTypePortlet.setModeEdit(modeEdit);
655                                            layoutTypePortlet.setModeEditDefaults(modeEditDefaults);
656                                            layoutTypePortlet.setModeEditGuest(modeEditGuest);
657                                            layoutTypePortlet.setModeHelp(modeHelp);
658                                            layoutTypePortlet.setModePreview(modePreview);
659                                            layoutTypePortlet.setModePrint(modePrint);
660                                    }
661                            }
662    
663                            request.setAttribute(WebKeys.LAYOUT, layout);
664                            request.setAttribute(WebKeys.LAYOUTS, layouts);
665                    }
666    
667                    // Scope
668    
669                    long scopeGroupId = PortalUtil.getScopeGroupId(request);
670                    long parentGroupId = PortalUtil.getParentGroupId(scopeGroupId);
671    
672                    // Theme and color scheme
673    
674                    Theme theme = null;
675                    ColorScheme colorScheme = null;
676    
677                    boolean wapTheme = BrowserSnifferUtil.isWap(request);
678    
679                    if ((layout != null) && group.isControlPanel()) {
680                            String themeId = PrefsPropsUtil.getString(
681                                    companyId, PropsKeys.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID);
682                            String colorSchemeId =
683                                    ColorSchemeImpl.getDefaultRegularColorSchemeId();
684    
685                            theme = ThemeLocalServiceUtil.getTheme(
686                                    companyId, themeId, wapTheme);
687                            colorScheme = ThemeLocalServiceUtil.getColorScheme(
688                                    companyId, theme.getThemeId(), colorSchemeId, wapTheme);
689    
690                            if (!wapTheme && theme.isWapTheme()) {
691                                    theme = ThemeLocalServiceUtil.getTheme(
692                                            companyId,
693                                            PropsValues.CONTROL_PANEL_LAYOUT_REGULAR_THEME_ID, false);
694                                    colorScheme = ThemeLocalServiceUtil.getColorScheme(
695                                            companyId, theme.getThemeId(), colorSchemeId, false);
696                            }
697    
698                            request.setAttribute(WebKeys.THEME, theme);
699                            request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
700                    }
701    
702                    boolean themeCssFastLoad = SessionParamUtil.getBoolean(
703                            request, "css_fast_load", PropsValues.THEME_CSS_FAST_LOAD);
704                    boolean themeImagesFastLoad = SessionParamUtil.getBoolean(
705                            request, "images_fast_load", PropsValues.THEME_IMAGES_FAST_LOAD);
706    
707                    boolean themeJsBarebone = PropsValues.JAVASCRIPT_BAREBONE_ENABLED;
708    
709                    if (themeJsBarebone) {
710                            if (signedIn) {
711                                    themeJsBarebone = false;
712                            }
713                    }
714    
715                    boolean themeJsFastLoad = SessionParamUtil.getBoolean(
716                            request, "js_fast_load", PropsValues.JAVASCRIPT_FAST_LOAD);
717    
718                    String lifecycle = ParamUtil.getString(request, "p_p_lifecycle", "0");
719    
720                    lifecycle = ParamUtil.getString(request, "p_t_lifecycle", lifecycle);
721    
722                    boolean isolated = ParamUtil.getBoolean(request, "p_p_isolated");
723    
724                    String facebookCanvasPageURL = (String)request.getAttribute(
725                            WebKeys.FACEBOOK_CANVAS_PAGE_URL);
726    
727                    boolean widget = false;
728    
729                    Boolean widgetObj = (Boolean)request.getAttribute(WebKeys.WIDGET);
730    
731                    if (widgetObj != null) {
732                            widget = widgetObj.booleanValue();
733                    }
734    
735                    // Theme display
736    
737                    ThemeDisplay themeDisplay = ThemeDisplayFactory.create();
738    
739                    // Set the CDN host, portal URL, and Facebook application ID first
740                    // because other methods (setLookAndFeel) depend on them being set
741    
742                    themeDisplay.setCDNHost(cdnHost);
743                    themeDisplay.setCDNDynamicResourcesHost(dynamicResourcesCDNHost);
744                    themeDisplay.setPortalURL(portalURL);
745                    themeDisplay.setFacebookCanvasPageURL(facebookCanvasPageURL);
746                    themeDisplay.setWidget(widget);
747    
748                    themeDisplay.setCompany(company);
749                    themeDisplay.setCompanyLogo(companyLogo);
750                    themeDisplay.setCompanyLogoHeight(companyLogoHeight);
751                    themeDisplay.setCompanyLogoWidth(companyLogoWidth);
752                    themeDisplay.setRealCompanyLogo(realCompanyLogo);
753                    themeDisplay.setRealCompanyLogoHeight(realCompanyLogoHeight);
754                    themeDisplay.setRealCompanyLogoWidth(realCompanyLogoWidth);
755                    themeDisplay.setUser(user);
756                    themeDisplay.setRealUser(realUser);
757                    themeDisplay.setDoAsUserId(doAsUserId);
758                    themeDisplay.setDoAsUserLanguageId(doAsUserLanguageId);
759                    themeDisplay.setDoAsGroupId(doAsGroupId);
760                    themeDisplay.setRefererPlid(refererPlid);
761                    themeDisplay.setControlPanelCategory(controlPanelCategory);
762                    themeDisplay.setLayoutSet(layoutSet);
763                    themeDisplay.setLayoutSetLogo(layoutSetLogo);
764                    themeDisplay.setLayout(layout);
765                    themeDisplay.setLayouts(layouts);
766                    themeDisplay.setUnfilteredLayouts(unfilteredLayouts);
767                    themeDisplay.setPlid(plid);
768                    themeDisplay.setLayoutTypePortlet(layoutTypePortlet);
769                    themeDisplay.setScopeGroupId(scopeGroupId);
770                    themeDisplay.setParentGroupId(parentGroupId);
771                    themeDisplay.setSignedIn(signedIn);
772                    themeDisplay.setPermissionChecker(permissionChecker);
773                    themeDisplay.setLocale(locale);
774                    themeDisplay.setLanguageId(LocaleUtil.toLanguageId(locale));
775                    themeDisplay.setI18nLanguageId(i18nLanguageId);
776                    themeDisplay.setI18nPath(i18nPath);
777                    themeDisplay.setTimeZone(timeZone);
778                    themeDisplay.setLookAndFeel(theme, colorScheme);
779                    themeDisplay.setThemeCssFastLoad(themeCssFastLoad);
780                    themeDisplay.setThemeImagesFastLoad(themeImagesFastLoad);
781                    themeDisplay.setThemeJsBarebone(themeJsBarebone);
782                    themeDisplay.setThemeJsFastLoad(themeJsFastLoad);
783                    themeDisplay.setServerName(request.getServerName());
784                    themeDisplay.setServerPort(request.getServerPort());
785                    themeDisplay.setSecure(PortalUtil.isSecure(request));
786                    themeDisplay.setLifecycle(lifecycle);
787                    themeDisplay.setLifecycleAction(lifecycle.equals("1"));
788                    themeDisplay.setLifecycleRender(lifecycle.equals("0"));
789                    themeDisplay.setLifecycleResource(lifecycle.equals("2"));
790                    themeDisplay.setStateExclusive(LiferayWindowState.isExclusive(request));
791                    themeDisplay.setStateMaximized(LiferayWindowState.isMaximized(request));
792                    themeDisplay.setStatePopUp(LiferayWindowState.isPopUp(request));
793                    themeDisplay.setIsolated(isolated);
794                    themeDisplay.setPathApplet(contextPath.concat("/applets"));
795                    themeDisplay.setPathCms(contextPath.concat("/cms"));
796                    themeDisplay.setPathContext(contextPath);
797                    themeDisplay.setPathFlash(contextPath.concat("/flash"));
798                    themeDisplay.setPathFriendlyURLPrivateGroup(
799                            friendlyURLPrivateGroupPath);
800                    themeDisplay.setPathFriendlyURLPrivateUser(friendlyURLPrivateUserPath);
801                    themeDisplay.setPathFriendlyURLPublic(friendlyURLPublicPath);
802                    themeDisplay.setPathImage(imagePath);
803                    themeDisplay.setPathJavaScript(contextPath.concat("/html/js"));
804                    themeDisplay.setPathMain(mainPath);
805                    themeDisplay.setPathSound(contextPath.concat("/html/sound"));
806    
807                    // Icons
808    
809                    themeDisplay.setShowAddContentIcon(false);
810                    themeDisplay.setShowControlPanelIcon(signedIn);
811                    themeDisplay.setShowHomeIcon(true);
812                    themeDisplay.setShowMyAccountIcon(signedIn);
813                    themeDisplay.setShowPageSettingsIcon(false);
814                    themeDisplay.setShowPortalIcon(true);
815                    themeDisplay.setShowSignInIcon(!signedIn);
816                    themeDisplay.setShowSignOutIcon(signedIn);
817    
818                    boolean showSiteContentIcon = false;
819    
820                    long controlPanelPlid = 0;
821    
822                    if (signedIn) {
823                            Group controlPanelGroup = GroupLocalServiceUtil.getGroup(
824                                    companyId, GroupConstants.CONTROL_PANEL);
825    
826                            controlPanelPlid = LayoutLocalServiceUtil.getDefaultPlid(
827                                    controlPanelGroup.getGroupId(), true);
828    
829                            List<Portlet> siteContentPortlets =
830                                    PortalUtil.getControlPanelPortlets(
831                                            PortletCategoryKeys.CONTENT, themeDisplay);
832    
833                            Portlet groupPagesPortlet = PortletLocalServiceUtil.getPortletById(
834                                    PortletKeys.GROUP_PAGES);
835    
836                            siteContentPortlets.remove(groupPagesPortlet);
837    
838                            Portlet siteMembershipsAdminPortlet =
839                                    PortletLocalServiceUtil.getPortletById(
840                                            PortletKeys.SITE_MEMBERSHIPS_ADMIN);
841    
842                            siteContentPortlets.remove(siteMembershipsAdminPortlet);
843    
844                            Portlet siteSettingsPortlet =
845                                    PortletLocalServiceUtil.getPortletById(
846                                            PortletKeys.SITE_SETTINGS);
847    
848                            siteContentPortlets.remove(siteSettingsPortlet);
849    
850                            showSiteContentIcon = PortletPermissionUtil.contains(
851                                    permissionChecker, scopeGroupId, controlPanelPlid,
852                                    siteContentPortlets, ActionKeys.VIEW);
853                    }
854    
855                    themeDisplay.setShowSiteContentIcon(showSiteContentIcon);
856    
857                    themeDisplay.setShowStagingIcon(false);
858    
859                    // Session
860    
861                    if (PropsValues.SESSION_ENABLE_URL_WITH_SESSION_ID &&
862                            !CookieKeys.hasSessionId(request)) {
863    
864                            themeDisplay.setAddSessionIdToURL(true);
865                            themeDisplay.setSessionId(session.getId());
866                    }
867    
868                    // URLs
869    
870                    String urlControlPanel = friendlyURLPrivateGroupPath.concat(
871                            GroupConstants.CONTROL_PANEL_FRIENDLY_URL);
872    
873                    if (Validator.isNotNull(doAsUserId)) {
874                            urlControlPanel = HttpUtil.addParameter(
875                                    urlControlPanel, "doAsUserId", doAsUserId);
876                    }
877    
878                    if (scopeGroupId > 0) {
879                            urlControlPanel = HttpUtil.addParameter(
880                                    urlControlPanel, "doAsGroupId", scopeGroupId);
881                    }
882    
883                    if (refererPlid > 0) {
884                            urlControlPanel = HttpUtil.addParameter(
885                                    urlControlPanel, "refererPlid", refererPlid);
886                    }
887                    else if (plid > 0) {
888                            urlControlPanel = HttpUtil.addParameter(
889                                    urlControlPanel, "refererPlid", plid);
890                    }
891    
892                    if (themeDisplay.isAddSessionIdToURL()) {
893                            urlControlPanel = PortalUtil.getURLWithSessionId(
894                                    urlControlPanel, session.getId());
895                    }
896    
897                    themeDisplay.setURLControlPanel(urlControlPanel);
898    
899                    String siteContentURL = urlControlPanel;
900    
901                    siteContentURL = HttpUtil.addParameter(
902                            siteContentURL, "controlPanelCategory",
903                            PortletCategoryKeys.CONTENT);
904    
905                    themeDisplay.setURLSiteContent(siteContentURL);
906    
907                    String currentURL = PortalUtil.getCurrentURL(request);
908    
909                    themeDisplay.setURLCurrent(currentURL);
910    
911                    String urlHome = PortalUtil.getHomeURL(request);
912    
913                    themeDisplay.setURLHome(urlHome);
914    
915                    if (layout != null) {
916                            if (layout.isTypePortlet()) {
917                                    boolean freeformLayout =
918                                            layoutTypePortlet.getLayoutTemplateId().equals("freeform");
919    
920                                    themeDisplay.setFreeformLayout(freeformLayout);
921    
922                                    if (hasUpdateLayoutPermission) {
923                                            themeDisplay.setShowAddContentIconPermission(true);
924    
925                                            if (!LiferayWindowState.isMaximized(request)) {
926                                                    themeDisplay.setShowAddContentIcon(true);
927                                            }
928    
929                                            themeDisplay.setShowLayoutTemplatesIcon(true);
930    
931                                            if (!group.isUser()) {
932                                                    themeDisplay.setShowPageCustomizationIcon(true);
933                                            }
934    
935                                            themeDisplay.setURLAddContent(
936                                                    "Liferay.LayoutConfiguration.toggle('".concat(
937                                                            PortletKeys.LAYOUT_CONFIGURATION).concat("');"));
938    
939                                            themeDisplay.setURLLayoutTemplates(
940                                                    "Liferay.LayoutConfiguration.showTemplates();");
941                                    }
942    
943                                    if (hasCustomizeLayoutPermission && customizedView) {
944                                            themeDisplay.setShowAddContentIconPermission(true);
945    
946                                            if (!LiferayWindowState.isMaximized(request)) {
947                                                    themeDisplay.setShowAddContentIcon(true);
948                                            }
949    
950                                            themeDisplay.setURLAddContent(
951                                                    "Liferay.LayoutConfiguration.toggle('".concat(
952                                                            PortletKeys.LAYOUT_CONFIGURATION).concat("');"));
953                                    }
954                            }
955    
956                            if (hasUpdateLayoutPermission) {
957                                    themeDisplay.setShowPageSettingsIcon(true);
958    
959                                    LiferayPortletURL pageSettingsURL = new PortletURLImpl(
960                                            request, PortletKeys.LAYOUTS_ADMIN, controlPanelPlid,
961                                            PortletRequest.RENDER_PHASE);
962    
963                                    pageSettingsURL.setControlPanelCategory(
964                                            _CONTROL_PANEL_CATEGORY_PORTLET_PREFIX +
965                                                    PortletKeys.LAYOUTS_ADMIN);
966                                    pageSettingsURL.setDoAsGroupId(scopeGroupId);
967                                    pageSettingsURL.setParameter(
968                                            "struts_action", "/layouts_admin/edit_layouts");
969    
970                                    if (layout.isPrivateLayout()) {
971                                            pageSettingsURL.setParameter("tabs1", "private-pages");
972                                    }
973                                    else {
974                                            pageSettingsURL.setParameter("tabs1", "public-pages");
975                                    }
976    
977                                    pageSettingsURL.setParameter("closeRedirect", currentURL);
978                                    pageSettingsURL.setParameter(
979                                            "groupId", String.valueOf(scopeGroupId));
980                                    pageSettingsURL.setParameter("selPlid", String.valueOf(plid));
981                                    pageSettingsURL.setPortletMode(PortletMode.VIEW);
982                                    pageSettingsURL.setWindowState(LiferayWindowState.POP_UP);
983    
984                                    themeDisplay.setURLPageSettings(pageSettingsURL);
985    
986                                    boolean site = group.isSite();
987    
988                                    if (!site && group.isStagingGroup()) {
989                                            Group liveGroup = group.getLiveGroup();
990    
991                                            site = liveGroup.isSite();
992                                    }
993    
994                                    if (site &&
995                                            GroupPermissionUtil.contains(
996                                                    permissionChecker, scopeGroupId,
997                                                    ActionKeys.ASSIGN_MEMBERS)) {
998    
999                                            themeDisplay.setShowManageSiteMembershipsIcon(true);
1000    
1001                                            LiferayPortletURL manageSiteMembershipsURL =
1002                                                    new PortletURLImpl(
1003                                                            request, PortletKeys.SITE_MEMBERSHIPS_ADMIN,
1004                                                            controlPanelPlid, PortletRequest.RENDER_PHASE);
1005    
1006                                            manageSiteMembershipsURL.setControlPanelCategory(
1007                                                    _CONTROL_PANEL_CATEGORY_PORTLET_PREFIX +
1008                                                            PortletKeys.SITE_MEMBERSHIPS_ADMIN);
1009                                            manageSiteMembershipsURL.setDoAsGroupId(scopeGroupId);
1010                                            manageSiteMembershipsURL.setParameter(
1011                                                    "struts_action", "/sites_admin/edit_site_assignments");
1012                                            manageSiteMembershipsURL.setParameter(
1013                                                    "groupId", String.valueOf(scopeGroupId));
1014                                            manageSiteMembershipsURL.setParameter(
1015                                                    "selPlid", String.valueOf(plid));
1016                                            manageSiteMembershipsURL.setPortletMode(PortletMode.VIEW);
1017                                            manageSiteMembershipsURL.setWindowState(
1018                                                    LiferayWindowState.POP_UP);
1019    
1020                                            themeDisplay.setURLManageSiteMemberships(
1021                                                    manageSiteMembershipsURL);
1022                                    }
1023                                    else {
1024                                            themeDisplay.setShowManageSiteMembershipsIcon(false);
1025                                    }
1026                            }
1027    
1028                            boolean hasAddLayoutGroupPermission = GroupPermissionUtil.contains(
1029                                    permissionChecker, scopeGroupId, ActionKeys.ADD_LAYOUT);
1030                            boolean hasAddLayoutLayoutPermission =
1031                                    LayoutPermissionUtil.contains(
1032                                            permissionChecker, layout, ActionKeys.ADD_LAYOUT);
1033                            boolean hasManageLayoutsGroupPermission =
1034                                    GroupPermissionUtil.contains(
1035                                            permissionChecker, scopeGroupId, ActionKeys.MANAGE_LAYOUTS);
1036                            boolean hasManageStagingPermission = GroupPermissionUtil.contains(
1037                                    permissionChecker, scopeGroupId, ActionKeys.MANAGE_STAGING);
1038                            boolean hasPublishStagingPermission = GroupPermissionUtil.contains(
1039                                    permissionChecker, scopeGroupId, ActionKeys.PUBLISH_STAGING);
1040                            boolean hasUpdateGroupPermission = GroupPermissionUtil.contains(
1041                                    permissionChecker, scopeGroupId, ActionKeys.UPDATE);
1042                            boolean hasViewStagingPermission = GroupPermissionUtil.contains(
1043                                    permissionChecker, scopeGroupId, ActionKeys.VIEW_STAGING);
1044    
1045                            if (!group.isControlPanel() && !group.isUser() &&
1046                                    !group.isUserGroup() && hasUpdateGroupPermission) {
1047    
1048                                    themeDisplay.setShowSiteSettingsIcon(true);
1049    
1050                                    LiferayPortletURL siteSettingsURL = new PortletURLImpl(
1051                                            request, PortletKeys.SITE_SETTINGS, controlPanelPlid,
1052                                            PortletRequest.RENDER_PHASE);
1053    
1054                                    siteSettingsURL.setControlPanelCategory(
1055                                            _CONTROL_PANEL_CATEGORY_PORTLET_PREFIX +
1056                                                    PortletKeys.SITE_SETTINGS);
1057                                    siteSettingsURL.setDoAsGroupId(scopeGroupId);
1058                                    siteSettingsURL.setParameter(
1059                                            "struts_action", "/sites_admin/edit_site");
1060                                    siteSettingsURL.setParameter("closeRedirect", currentURL);
1061                                    siteSettingsURL.setParameter(
1062                                            "groupId", String.valueOf(scopeGroupId));
1063                                    siteSettingsURL.setPortletMode(PortletMode.VIEW);
1064                                    siteSettingsURL.setWindowState(LiferayWindowState.POP_UP);
1065    
1066                                    themeDisplay.setURLSiteSettings(siteSettingsURL);
1067                            }
1068    
1069                            if (!group.isLayoutPrototype() &&
1070                                    (hasAddLayoutGroupPermission || hasAddLayoutLayoutPermission ||
1071                                     hasManageLayoutsGroupPermission || hasUpdateGroupPermission)) {
1072    
1073                                    themeDisplay.setShowSiteMapSettingsIcon(true);
1074    
1075                                    LiferayPortletURL siteMapSettingsURL = new PortletURLImpl(
1076                                            request, PortletKeys.LAYOUTS_ADMIN, controlPanelPlid,
1077                                            PortletRequest.RENDER_PHASE);
1078    
1079                                    siteMapSettingsURL.setControlPanelCategory(
1080                                            _CONTROL_PANEL_CATEGORY_PORTLET_PREFIX +
1081                                                    PortletKeys.LAYOUTS_ADMIN);
1082                                    siteMapSettingsURL.setDoAsGroupId(scopeGroupId);
1083                                    siteMapSettingsURL.setParameter(
1084                                            "struts_action", "/layouts_admin/edit_layouts");
1085    
1086                                    if (layout.isPrivateLayout()) {
1087                                            siteMapSettingsURL.setParameter("tabs1", "private-pages");
1088                                    }
1089                                    else {
1090                                            siteMapSettingsURL.setParameter("tabs1", "public-pages");
1091                                    }
1092    
1093                                    siteMapSettingsURL.setParameter("closeRedirect", currentURL);
1094                                    siteMapSettingsURL.setParameter(
1095                                            "groupId", String.valueOf(scopeGroupId));
1096                                    siteMapSettingsURL.setPortletMode(PortletMode.VIEW);
1097                                    siteMapSettingsURL.setWindowState(LiferayWindowState.POP_UP);
1098    
1099                                    themeDisplay.setURLSiteMapSettings(siteMapSettingsURL);
1100                            }
1101    
1102                            if (group.hasStagingGroup() && !group.isStagingGroup()) {
1103                                    themeDisplay.setShowAddContentIcon(false);
1104                                    themeDisplay.setShowLayoutTemplatesIcon(false);
1105                                    themeDisplay.setShowPageSettingsIcon(false);
1106                                    themeDisplay.setURLPublishToLive(null);
1107                            }
1108    
1109                            if (group.isControlPanel()) {
1110                                    themeDisplay.setShowPageSettingsIcon(false);
1111                                    themeDisplay.setURLPublishToLive(null);
1112                            }
1113    
1114                            // LEP-4987
1115    
1116                            if (group.isStaged() || group.isStagingGroup()) {
1117                                    if (hasManageStagingPermission || hasPublishStagingPermission ||
1118                                            hasUpdateLayoutPermission || hasViewStagingPermission) {
1119    
1120                                            themeDisplay.setShowStagingIcon(true);
1121                                    }
1122    
1123                                    if (hasPublishStagingPermission) {
1124                                            PortletURL publishToLiveURL = new PortletURLImpl(
1125                                                    request, PortletKeys.LAYOUTS_ADMIN, plid,
1126                                                    PortletRequest.RENDER_PHASE);
1127    
1128                                            publishToLiveURL.setParameter(
1129                                                    "struts_action", "/layouts_admin/publish_layouts");
1130    
1131                                            if (layout.isPrivateLayout()) {
1132                                                    publishToLiveURL.setParameter("tabs1", "private-pages");
1133                                            }
1134                                            else {
1135                                                    publishToLiveURL.setParameter("tabs1", "public-pages");
1136                                            }
1137    
1138                                            publishToLiveURL.setParameter("pagesRedirect", currentURL);
1139                                            publishToLiveURL.setParameter(
1140                                                    "groupId", String.valueOf(scopeGroupId));
1141                                            publishToLiveURL.setParameter(
1142                                                    "selPlid", String.valueOf(plid));
1143                                            publishToLiveURL.setPortletMode(PortletMode.VIEW);
1144                                            publishToLiveURL.setWindowState(
1145                                                    LiferayWindowState.EXCLUSIVE);
1146    
1147                                            themeDisplay.setURLPublishToLive(publishToLiveURL);
1148                                    }
1149                            }
1150    
1151                            PortletURLImpl myAccountURL = new PortletURLImpl(
1152                                    request, PortletKeys.MY_ACCOUNT, controlPanelPlid,
1153                                    PortletRequest.RENDER_PHASE);
1154    
1155                            if (scopeGroupId > 0) {
1156                                    myAccountURL.setDoAsGroupId(scopeGroupId);
1157                            }
1158    
1159                            myAccountURL.setParameter("struts_action", "/my_account/edit_user");
1160                            myAccountURL.setPortletMode(PortletMode.VIEW);
1161    
1162                            if (refererPlid > 0) {
1163                                    myAccountURL.setRefererPlid(refererPlid);
1164                            }
1165                            else {
1166                                    myAccountURL.setRefererPlid(plid);
1167                            }
1168    
1169                            myAccountURL.setWindowState(WindowState.MAXIMIZED);
1170    
1171                            themeDisplay.setURLMyAccount(myAccountURL);
1172                    }
1173    
1174                    if ((!user.isActive()) ||
1175                            (PrefsPropsUtil.getBoolean(
1176                                    companyId, PropsKeys.TERMS_OF_USE_REQUIRED) &&
1177                             !user.isAgreedToTermsOfUse())) {
1178    
1179                            themeDisplay.setShowAddContentIcon(false);
1180                            themeDisplay.setShowMyAccountIcon(false);
1181                            themeDisplay.setShowPageSettingsIcon(false);
1182                    }
1183    
1184                    if (layout.isLayoutPrototypeLinkActive()) {
1185                            themeDisplay.setShowPageCustomizationIcon(false);
1186                    }
1187    
1188                    if (group.isLayoutPrototype()) {
1189                            themeDisplay.setShowControlPanelIcon(false);
1190                            themeDisplay.setShowHomeIcon(false);
1191                            themeDisplay.setShowManageSiteMembershipsIcon(false);
1192                            themeDisplay.setShowMyAccountIcon(false);
1193                            themeDisplay.setShowPageCustomizationIcon(false);
1194                            themeDisplay.setShowPageSettingsIcon(true);
1195                            themeDisplay.setShowPortalIcon(false);
1196                            themeDisplay.setShowSignInIcon(false);
1197                            themeDisplay.setShowSignOutIcon(false);
1198                            themeDisplay.setShowSiteContentIcon(false);
1199                            themeDisplay.setShowSiteSettingsIcon(false);
1200                            themeDisplay.setShowStagingIcon(false);
1201                    }
1202    
1203                    if (group.isLayoutSetPrototype()) {
1204                            themeDisplay.setShowPageCustomizationIcon(false);
1205                            themeDisplay.setShowSiteSettingsIcon(false);
1206                    }
1207    
1208                    if (group.hasStagingGroup() && !group.isStagingGroup()) {
1209                            themeDisplay.setShowLayoutTemplatesIcon(false);
1210                            themeDisplay.setShowPageCustomizationIcon(false);
1211                            themeDisplay.setShowPageSettingsIcon(false);
1212                            themeDisplay.setShowSiteContentIcon(false);
1213                            themeDisplay.setShowSiteMapSettingsIcon(false);
1214                            themeDisplay.setShowSiteSettingsIcon(false);
1215                    }
1216    
1217                    themeDisplay.setURLPortal(portalURL.concat(contextPath));
1218    
1219                    String urlSignIn = mainPath.concat("/portal/login");
1220    
1221                    if (layout != null) {
1222                            urlSignIn = HttpUtil.addParameter(
1223                                    urlSignIn, "p_l_id", layout.getPlid());
1224                    }
1225    
1226                    themeDisplay.setURLSignIn(urlSignIn);
1227    
1228                    themeDisplay.setURLSignOut(mainPath.concat("/portal/logout"));
1229    
1230                    PortletURL updateManagerURL = new PortletURLImpl(
1231                            request, PortletKeys.UPDATE_MANAGER, plid,
1232                            PortletRequest.RENDER_PHASE);
1233    
1234                    updateManagerURL.setParameter("struts_action", "/update_manager/view");
1235                    updateManagerURL.setPortletMode(PortletMode.VIEW);
1236                    updateManagerURL.setWindowState(WindowState.MAXIMIZED);
1237    
1238                    themeDisplay.setURLUpdateManager(updateManagerURL);
1239    
1240                    return themeDisplay;
1241            }
1242    
1243            @Override
1244            public void run(HttpServletRequest request, HttpServletResponse response)
1245                    throws ActionException {
1246    
1247                    StopWatch stopWatch = null;
1248    
1249                    if (_log.isDebugEnabled()) {
1250                            stopWatch = new StopWatch();
1251    
1252                            stopWatch.start();
1253                    }
1254    
1255                    try {
1256                            servicePre(request, response);
1257                    }
1258                    catch (Exception e) {
1259                            throw new ActionException(e);
1260                    }
1261    
1262                    if (_log.isDebugEnabled()) {
1263                            _log.debug("Running takes " + stopWatch.getTime() + " ms");
1264                    }
1265            }
1266    
1267            protected void addDefaultLayoutsByLAR(
1268                            long userId, long groupId, boolean privateLayout, File larFile)
1269                    throws PortalException, SystemException {
1270    
1271                    Map<String, String[]> parameterMap = new HashMap<String, String[]>();
1272    
1273                    parameterMap.put(
1274                            PortletDataHandlerKeys.PERMISSIONS,
1275                            new String[] {Boolean.TRUE.toString()});
1276                    parameterMap.put(
1277                            PortletDataHandlerKeys.PORTLET_DATA,
1278                            new String[] {Boolean.TRUE.toString()});
1279                    parameterMap.put(
1280                            PortletDataHandlerKeys.PORTLET_DATA_CONTROL_DEFAULT,
1281                            new String[] {Boolean.TRUE.toString()});
1282                    parameterMap.put(
1283                            PortletDataHandlerKeys.PORTLET_SETUP,
1284                            new String[] {Boolean.TRUE.toString()});
1285                    parameterMap.put(
1286                            PortletDataHandlerKeys.USER_PERMISSIONS,
1287                            new String[] {Boolean.FALSE.toString()});
1288    
1289                    LayoutLocalServiceUtil.importLayouts(
1290                            userId, groupId, privateLayout, parameterMap, larFile);
1291            }
1292    
1293            protected void addDefaultUserPrivateLayoutByProperties(
1294                            long userId, long groupId)
1295                    throws PortalException, SystemException {
1296    
1297                    String friendlyURL = getFriendlyURL(
1298                            PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_FRIENDLY_URL);
1299    
1300                    ServiceContext serviceContext = new ServiceContext();
1301    
1302                    Layout layout = LayoutLocalServiceUtil.addLayout(
1303                            userId, groupId, true, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
1304                            PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_NAME, StringPool.BLANK,
1305                            StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
1306                            serviceContext);
1307    
1308                    LayoutTypePortlet layoutTypePortlet =
1309                            (LayoutTypePortlet)layout.getLayoutType();
1310    
1311                    layoutTypePortlet.setLayoutTemplateId(
1312                            0, PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_TEMPLATE_ID, false);
1313    
1314                    for (int i = 0; i < 10; i++) {
1315                            String columnId = "column-" + i;
1316                            String portletIds = PropsUtil.get(
1317                                    PropsKeys.DEFAULT_USER_PRIVATE_LAYOUT_COLUMN + i);
1318    
1319                            String[] portletIdsArray = StringUtil.split(portletIds);
1320    
1321                            layoutTypePortlet.addPortletIds(
1322                                    0, portletIdsArray, columnId, false);
1323                    }
1324    
1325                    LayoutLocalServiceUtil.updateLayout(
1326                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
1327                            layout.getTypeSettings());
1328    
1329                    boolean updateLayoutSet = false;
1330    
1331                    LayoutSet layoutSet = layout.getLayoutSet();
1332    
1333                    if (Validator.isNotNull(
1334                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_THEME_ID)) {
1335    
1336                            layoutSet.setThemeId(
1337                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_THEME_ID);
1338    
1339                            updateLayoutSet = true;
1340                    }
1341    
1342                    if (Validator.isNotNull(
1343                                    PropsValues.
1344                                            DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_COLOR_SCHEME_ID)) {
1345    
1346                            layoutSet.setColorSchemeId(
1347                                    PropsValues.
1348                                            DEFAULT_USER_PRIVATE_LAYOUT_REGULAR_COLOR_SCHEME_ID);
1349    
1350                            updateLayoutSet = true;
1351                    }
1352    
1353                    if (Validator.isNotNull(
1354                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_THEME_ID)) {
1355    
1356                            layoutSet.setWapThemeId(
1357                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_THEME_ID);
1358    
1359                            updateLayoutSet = true;
1360                    }
1361    
1362                    if (Validator.isNotNull(
1363                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_COLOR_SCHEME_ID)) {
1364    
1365                            layoutSet.setWapColorSchemeId(
1366                                    PropsValues.DEFAULT_USER_PRIVATE_LAYOUT_WAP_COLOR_SCHEME_ID);
1367    
1368                            updateLayoutSet = true;
1369                    }
1370    
1371                    if (updateLayoutSet) {
1372                            LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet);
1373                    }
1374            }
1375    
1376            protected void addDefaultUserPrivateLayouts(User user)
1377                    throws PortalException, SystemException {
1378    
1379                    Group userGroup = user.getGroup();
1380    
1381                    if (privateLARFile != null) {
1382                            addDefaultLayoutsByLAR(
1383                                    user.getUserId(), userGroup.getGroupId(), true, privateLARFile);
1384                    }
1385                    else {
1386                            addDefaultUserPrivateLayoutByProperties(
1387                                    user.getUserId(), userGroup.getGroupId());
1388                    }
1389            }
1390    
1391            protected void addDefaultUserPublicLayoutByProperties(
1392                            long userId, long groupId)
1393                    throws PortalException, SystemException {
1394    
1395                    String friendlyURL = getFriendlyURL(
1396                            PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_FRIENDLY_URL);
1397    
1398                    ServiceContext serviceContext = new ServiceContext();
1399    
1400                    Layout layout = LayoutLocalServiceUtil.addLayout(
1401                            userId, groupId, false, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID,
1402                            PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_NAME, StringPool.BLANK,
1403                            StringPool.BLANK, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
1404                            serviceContext);
1405    
1406                    LayoutTypePortlet layoutTypePortlet =
1407                            (LayoutTypePortlet)layout.getLayoutType();
1408    
1409                    layoutTypePortlet.setLayoutTemplateId(
1410                            0, PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_TEMPLATE_ID, false);
1411    
1412                    for (int i = 0; i < 10; i++) {
1413                            String columnId = "column-" + i;
1414                            String portletIds = PropsUtil.get(
1415                                    PropsKeys.DEFAULT_USER_PUBLIC_LAYOUT_COLUMN + i);
1416    
1417                            String[] portletIdsArray = StringUtil.split(portletIds);
1418    
1419                            layoutTypePortlet.addPortletIds(
1420                                    0, portletIdsArray, columnId, false);
1421                    }
1422    
1423                    LayoutLocalServiceUtil.updateLayout(
1424                            layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
1425                            layout.getTypeSettings());
1426    
1427                    boolean updateLayoutSet = false;
1428    
1429                    LayoutSet layoutSet = layout.getLayoutSet();
1430    
1431                    if (Validator.isNotNull(
1432                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_REGULAR_THEME_ID)) {
1433    
1434                            layoutSet.setThemeId(
1435                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_REGULAR_THEME_ID);
1436    
1437                            updateLayoutSet = true;
1438                    }
1439    
1440                    if (Validator.isNotNull(
1441                                    PropsValues.
1442                                            DEFAULT_USER_PUBLIC_LAYOUT_REGULAR_COLOR_SCHEME_ID)) {
1443    
1444                            layoutSet.setColorSchemeId(
1445                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_REGULAR_COLOR_SCHEME_ID);
1446    
1447                            updateLayoutSet = true;
1448                    }
1449    
1450                    if (Validator.isNotNull(
1451                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_WAP_THEME_ID)) {
1452    
1453                            layoutSet.setWapThemeId(
1454                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_WAP_THEME_ID);
1455    
1456                            updateLayoutSet = true;
1457                    }
1458    
1459                    if (Validator.isNotNull(
1460                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_WAP_COLOR_SCHEME_ID)) {
1461    
1462                            layoutSet.setWapColorSchemeId(
1463                                    PropsValues.DEFAULT_USER_PUBLIC_LAYOUT_WAP_COLOR_SCHEME_ID);
1464    
1465                            updateLayoutSet = true;
1466                    }
1467    
1468                    if (updateLayoutSet) {
1469                            LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet);
1470                    }
1471            }
1472    
1473            protected void addDefaultUserPublicLayouts(User user)
1474                    throws PortalException, SystemException {
1475    
1476                    Group userGroup = user.getGroup();
1477    
1478                    if (publicLARFile != null) {
1479                            addDefaultLayoutsByLAR(
1480                                    user.getUserId(), userGroup.getGroupId(), false, publicLARFile);
1481                    }
1482                    else {
1483                            addDefaultUserPublicLayoutByProperties(
1484                                    user.getUserId(), userGroup.getGroupId());
1485                    }
1486            }
1487    
1488            protected void deleteDefaultUserPrivateLayouts(User user)
1489                    throws PortalException, SystemException {
1490    
1491                    Group userGroup = user.getGroup();
1492    
1493                    ServiceContext serviceContext = new ServiceContext();
1494    
1495                    LayoutLocalServiceUtil.deleteLayouts(
1496                            userGroup.getGroupId(), true, serviceContext);
1497            }
1498    
1499            protected void deleteDefaultUserPublicLayouts(User user)
1500                    throws PortalException, SystemException {
1501    
1502                    Group userGroup = user.getGroup();
1503    
1504                    ServiceContext serviceContext = new ServiceContext();
1505    
1506                    LayoutLocalServiceUtil.deleteLayouts(
1507                            userGroup.getGroupId(), false, serviceContext);
1508            }
1509    
1510            protected Object[] getDefaultLayout(
1511                            HttpServletRequest request, User user, boolean signedIn)
1512                    throws PortalException, SystemException {
1513    
1514                    // Check the virtual host
1515    
1516                    LayoutSet layoutSet = (LayoutSet)request.getAttribute(
1517                            WebKeys.VIRTUAL_HOST_LAYOUT_SET);
1518    
1519                    if (layoutSet != null) {
1520                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
1521                                    layoutSet.getGroupId(), layoutSet.isPrivateLayout(),
1522                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1523    
1524                            if (layouts.size() > 0) {
1525                                    Layout layout = layouts.get(0);
1526    
1527                                    return new Object[] {layout, layouts};
1528                            }
1529                    }
1530    
1531                    Layout layout = null;
1532                    List<Layout> layouts = null;
1533    
1534                    if (signedIn) {
1535    
1536                            // Check the user's personal layouts
1537    
1538                            Group userGroup = user.getGroup();
1539    
1540                            layouts = LayoutLocalServiceUtil.getLayouts(
1541                                    userGroup.getGroupId(), true,
1542                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1543    
1544                            if (layouts.size() == 0) {
1545                                    layouts = LayoutLocalServiceUtil.getLayouts(
1546                                            userGroup.getGroupId(), false,
1547                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1548                            }
1549    
1550                            if (layouts.size() > 0) {
1551                                    layout = layouts.get(0);
1552                            }
1553    
1554                            // Check the user's sites
1555    
1556                            if (layout == null) {
1557                                    LinkedHashMap<String, Object> groupParams =
1558                                            new LinkedHashMap<String, Object>();
1559    
1560                                    groupParams.put("usersGroups", new Long(user.getUserId()));
1561    
1562                                    List<Group> groups = GroupLocalServiceUtil.search(
1563                                            user.getCompanyId(), null, null, groupParams,
1564                                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1565    
1566                                    for (Group group : groups) {
1567                                            layouts = LayoutLocalServiceUtil.getLayouts(
1568                                                    group.getGroupId(), true,
1569                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1570    
1571                                            if (layouts.size() == 0) {
1572                                                    layouts = LayoutLocalServiceUtil.getLayouts(
1573                                                            group.getGroupId(), false,
1574                                                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1575                                            }
1576    
1577                                            if (layouts.size() > 0) {
1578                                                    layout = layouts.get(0);
1579    
1580                                                    break;
1581                                            }
1582                                    }
1583                            }
1584                    }
1585    
1586                    if (layout == null) {
1587    
1588                            // Check the Guest site
1589    
1590                            Group guestGroup = GroupLocalServiceUtil.getGroup(
1591                                    user.getCompanyId(), GroupConstants.GUEST);
1592    
1593                            layouts = LayoutLocalServiceUtil.getLayouts(
1594                                    guestGroup.getGroupId(), false,
1595                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1596    
1597                            if (layouts.size() > 0) {
1598                                    layout = layouts.get(0);
1599                            }
1600                    }
1601    
1602                    return new Object[] {layout, layouts};
1603            }
1604    
1605            protected String getFriendlyURL(String friendlyURL) {
1606                    friendlyURL = GetterUtil.getString(friendlyURL);
1607    
1608                    return FriendlyURLNormalizerUtil.normalize(friendlyURL);
1609            }
1610    
1611            protected Object[] getViewableLayouts(
1612                            HttpServletRequest request, User user,
1613                            PermissionChecker permissionChecker, Layout layout,
1614                            List<Layout> layouts)
1615                    throws PortalException, SystemException {
1616    
1617                    if ((layouts == null) || layouts.isEmpty()) {
1618                            return new Object[] {layout, layouts};
1619                    }
1620    
1621                    Group group = layout.getGroup();
1622    
1623                    boolean hasViewLayoutPermission = false;
1624                    boolean hasViewStagingPermission =
1625                            (group.isStagingGroup() || group.isStagedRemotely()) &&
1626                             GroupPermissionUtil.contains(
1627                                     permissionChecker, group.getGroupId(),
1628                                     ActionKeys.VIEW_STAGING);
1629    
1630                    if (LayoutPermissionUtil.contains(
1631                                    permissionChecker, layout, false, ActionKeys.VIEW) ||
1632                            hasViewStagingPermission) {
1633    
1634                            hasViewLayoutPermission = true;
1635                    }
1636    
1637                    List<Layout> accessibleLayouts = new ArrayList<Layout>();
1638    
1639                    for (int i = 0; i < layouts.size(); i++) {
1640                            Layout curLayout = layouts.get(i);
1641    
1642                            if (!curLayout.isHidden() &&
1643                                    (LayoutPermissionUtil.contains(
1644                                            permissionChecker, curLayout, false, ActionKeys.VIEW) ||
1645                                     hasViewStagingPermission)) {
1646    
1647                                    if (accessibleLayouts.isEmpty() && !hasViewLayoutPermission) {
1648                                            layout = curLayout;
1649                                    }
1650    
1651                                    accessibleLayouts.add(curLayout);
1652                            }
1653                    }
1654    
1655                    if (accessibleLayouts.isEmpty()) {
1656                            layouts = null;
1657    
1658                            if (!hasViewLayoutPermission) {
1659                                    SessionErrors.add(
1660                                            request, LayoutPermissionException.class.getName());
1661                            }
1662                    }
1663                    else {
1664                            layouts = accessibleLayouts;
1665                    }
1666    
1667                    return new Object[] {layout, layouts};
1668            }
1669    
1670            protected Boolean hasPowerUserRole(User user) throws Exception {
1671                    return RoleLocalServiceUtil.hasUserRole(
1672                            user.getUserId(), user.getCompanyId(), RoleConstants.POWER_USER,
1673                            true);
1674            }
1675    
1676            protected void initImportLARFiles() {
1677                    String privateLARFileName =
1678                            PropsValues.DEFAULT_USER_PRIVATE_LAYOUTS_LAR;
1679    
1680                    if (_log.isDebugEnabled()) {
1681                            _log.debug("Reading private LAR file " + privateLARFileName);
1682                    }
1683    
1684                    if (Validator.isNotNull(privateLARFileName)) {
1685                            privateLARFile = new File(privateLARFileName);
1686    
1687                            if (!privateLARFile.exists()) {
1688                                    _log.error(
1689                                            "Private LAR file " + privateLARFile + " does not exist");
1690    
1691                                    privateLARFile = null;
1692                            }
1693                            else {
1694                                    if (_log.isDebugEnabled()) {
1695                                            _log.debug("Using private LAR file " + privateLARFileName);
1696                                    }
1697                            }
1698                    }
1699    
1700                    String publicLARFileName = PropsValues.DEFAULT_USER_PUBLIC_LAYOUTS_LAR;
1701    
1702                    if (_log.isDebugEnabled()) {
1703                            _log.debug("Reading public LAR file " + publicLARFileName);
1704                    }
1705    
1706                    if (Validator.isNotNull(publicLARFileName)) {
1707                            publicLARFile = new File(publicLARFileName);
1708    
1709                            if (!publicLARFile.exists()) {
1710                                    _log.error(
1711                                            "Public LAR file " + publicLARFile + " does not exist");
1712    
1713                                    publicLARFile = null;
1714                            }
1715                            else {
1716                                    if (_log.isDebugEnabled()) {
1717                                            _log.debug("Using public LAR file " + publicLARFileName);
1718                                    }
1719                            }
1720                    }
1721            }
1722    
1723            protected boolean isLoginRequest(HttpServletRequest request) {
1724                    String requestURI = request.getRequestURI();
1725    
1726                    String mainPath = PortalUtil.getPathMain();
1727    
1728                    if (requestURI.startsWith(mainPath.concat("/portal/login"))) {
1729                            return true;
1730                    }
1731                    else {
1732                            return false;
1733                    }
1734            }
1735    
1736            /**
1737             * @deprecated
1738             */
1739            protected boolean isViewableCommunity(
1740                            User user, long groupId, boolean privateLayout,
1741                            PermissionChecker permissionChecker)
1742                    throws PortalException, SystemException {
1743    
1744                    return LayoutPermissionUtil.contains(
1745                            permissionChecker, groupId, privateLayout, 0, ActionKeys.VIEW);
1746            }
1747    
1748            /**
1749             * @deprecated
1750             */
1751            protected boolean isViewableGroup(
1752                            User user, long groupId, boolean privateLayout, long layoutId,
1753                            String controlPanelCategory, PermissionChecker permissionChecker)
1754                    throws PortalException, SystemException {
1755    
1756                    return LayoutPermissionUtil.contains(
1757                            permissionChecker, groupId, privateLayout, layoutId,
1758                            controlPanelCategory, ActionKeys.VIEW);
1759            }
1760    
1761            protected List<Layout> mergeAdditionalLayouts(
1762                            HttpServletRequest request, User user,
1763                            PermissionChecker permissionChecker, Layout layout,
1764                            List<Layout> layouts)
1765                    throws PortalException, SystemException {
1766    
1767                    if ((layout == null) || layout.isPrivateLayout()) {
1768                            return layouts;
1769                    }
1770    
1771                    long layoutGroupId = layout.getGroupId();
1772    
1773                    Group guestGroup = GroupLocalServiceUtil.getGroup(
1774                            user.getCompanyId(), GroupConstants.GUEST);
1775    
1776                    if (layoutGroupId != guestGroup.getGroupId()) {
1777                            Group layoutGroup = GroupLocalServiceUtil.getGroup(layoutGroupId);
1778    
1779                            UnicodeProperties typeSettingsProperties =
1780                                    layoutGroup.getTypeSettingsProperties();
1781    
1782                            boolean mergeGuestPublicPages = GetterUtil.getBoolean(
1783                                    typeSettingsProperties.getProperty("mergeGuestPublicPages"));
1784    
1785                            if (!mergeGuestPublicPages) {
1786                                    return layouts;
1787                            }
1788    
1789                            List<Layout> guestLayouts = LayoutLocalServiceUtil.getLayouts(
1790                                    guestGroup.getGroupId(), false,
1791                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1792    
1793                            Object[] viewableLayouts = getViewableLayouts(
1794                                    request, user, permissionChecker, layout, guestLayouts);
1795    
1796                            guestLayouts = (List<Layout>)viewableLayouts[1];
1797    
1798                            if (layouts == null) {
1799                                    return guestLayouts;
1800                            }
1801    
1802                            layouts.addAll(0, guestLayouts);
1803                    }
1804                    else {
1805                            HttpSession session = request.getSession();
1806    
1807                            Long previousGroupId = (Long)session.getAttribute(
1808                                    WebKeys.VISITED_GROUP_ID_PREVIOUS);
1809    
1810                            if ((previousGroupId != null) &&
1811                                    (previousGroupId.longValue() != layoutGroupId)) {
1812    
1813                                    Group previousGroup = null;
1814    
1815                                    try {
1816                                            previousGroup = GroupLocalServiceUtil.getGroup(
1817                                                    previousGroupId.longValue());
1818                                    }
1819                                    catch (NoSuchGroupException nsge) {
1820                                            if (_log.isWarnEnabled()) {
1821                                                    _log.warn(nsge);
1822                                            }
1823    
1824                                            return layouts;
1825                                    }
1826    
1827                                    UnicodeProperties typeSettingsProperties =
1828                                            previousGroup.getTypeSettingsProperties();
1829    
1830                                    boolean mergeGuestPublicPages = GetterUtil.getBoolean(
1831                                            typeSettingsProperties.getProperty(
1832                                                    "mergeGuestPublicPages"));
1833    
1834                                    if (!mergeGuestPublicPages) {
1835                                            return layouts;
1836                                    }
1837    
1838                                    List<Layout> previousLayouts =
1839                                            LayoutLocalServiceUtil.getLayouts(
1840                                                    previousGroupId.longValue(), false,
1841                                                    LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
1842    
1843                                    Object[] viewableLayouts = getViewableLayouts(
1844                                            request, user, permissionChecker, layout, previousLayouts);
1845    
1846                                    previousLayouts = (List<Layout>)viewableLayouts[1];
1847    
1848                                    if (previousLayouts != null) {
1849                                            layouts.addAll(previousLayouts);
1850                                    }
1851                            }
1852                    }
1853    
1854                    return layouts;
1855            }
1856    
1857            protected void rememberVisitedGroupIds(
1858                    HttpServletRequest request, long currentGroupId) {
1859    
1860                    String requestURI = GetterUtil.getString(request.getRequestURI());
1861    
1862                    if (!requestURI.endsWith(_PATH_PORTAL_LAYOUT)) {
1863                            return;
1864                    }
1865    
1866                    HttpSession session = request.getSession();
1867    
1868                    Long recentGroupId = (Long)session.getAttribute(
1869                            WebKeys.VISITED_GROUP_ID_RECENT);
1870    
1871                    Long previousGroupId = (Long)session.getAttribute(
1872                            WebKeys.VISITED_GROUP_ID_PREVIOUS);
1873    
1874                    if (recentGroupId == null) {
1875                            recentGroupId = new Long(currentGroupId);
1876    
1877                            session.setAttribute(
1878                                    WebKeys.VISITED_GROUP_ID_RECENT, recentGroupId);
1879                    }
1880                    else if (recentGroupId.longValue() != currentGroupId) {
1881                            previousGroupId = new Long(recentGroupId.longValue());
1882    
1883                            recentGroupId = new Long(currentGroupId);
1884    
1885                            session.setAttribute(
1886                                    WebKeys.VISITED_GROUP_ID_RECENT, recentGroupId);
1887    
1888                            session.setAttribute(
1889                                    WebKeys.VISITED_GROUP_ID_PREVIOUS, previousGroupId);
1890                    }
1891    
1892                    if (_log.isDebugEnabled()) {
1893                            _log.debug("Current group id " + currentGroupId);
1894                            _log.debug("Recent group id " + recentGroupId);
1895                            _log.debug("Previous group id " + previousGroupId);
1896                    }
1897            }
1898    
1899            protected void servicePre(
1900                            HttpServletRequest request, HttpServletResponse response)
1901                    throws Exception {
1902    
1903                    ThemeDisplay themeDisplay = initThemeDisplay(request, response);
1904    
1905                    if (themeDisplay == null) {
1906                            return;
1907                    }
1908    
1909                    request.setAttribute(WebKeys.THEME_DISPLAY, themeDisplay);
1910    
1911                    // Service context
1912    
1913                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
1914                            request);
1915    
1916                    ServiceContextThreadLocal.pushServiceContext(serviceContext);
1917    
1918                    // Parallel render
1919    
1920                    boolean parallelRenderEnable = true;
1921    
1922                    Layout layout = themeDisplay.getLayout();
1923    
1924                    if (layout != null) {
1925                            LayoutTypePortlet layoutTypePortlet =
1926                                    themeDisplay.getLayoutTypePortlet();
1927    
1928                            List<String> portletIds = layoutTypePortlet.getPortletIds();
1929    
1930                            if (portletIds.size() == 1) {
1931                                    String portletId = portletIds.get(0);
1932    
1933                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
1934                                            portletId);
1935    
1936                                    if ((portlet != null) && !portlet.isAjaxable()) {
1937                                            parallelRenderEnable = false;
1938                                    }
1939                            }
1940                    }
1941    
1942                    Boolean parallelRenderEnableObj = Boolean.valueOf(ParamUtil.getBoolean(
1943                            request, "p_p_parallel", parallelRenderEnable));
1944    
1945                    request.setAttribute(
1946                            WebKeys.PORTLET_PARALLEL_RENDER, parallelRenderEnableObj);
1947    
1948                    // Main Journal article
1949    
1950                    long mainJournalArticleId = ParamUtil.getLong(request, "p_j_a_id");
1951    
1952                    if (mainJournalArticleId > 0) {
1953                            try{
1954                                    JournalArticle mainJournalArticle =
1955                                            JournalArticleServiceUtil.getArticle(mainJournalArticleId);
1956    
1957                                    AssetEntry layoutAssetEntry =
1958                                            AssetEntryLocalServiceUtil.getEntry(
1959                                                    JournalArticle.class.getName(),
1960                                                    mainJournalArticle.getResourcePrimKey());
1961    
1962                                    request.setAttribute(
1963                                            WebKeys.LAYOUT_ASSET_ENTRY, layoutAssetEntry);
1964                            }
1965                            catch (NoSuchArticleException nsae) {
1966                                    if (_log.isWarnEnabled()) {
1967                                            _log.warn(nsae.getMessage());
1968                                    }
1969                            }
1970                    }
1971            }
1972    
1973            protected void updateUserLayouts(User user) throws Exception {
1974                    Boolean hasPowerUserRole = null;
1975    
1976                    // Private layouts
1977    
1978                    boolean addDefaultUserPrivateLayouts = false;
1979    
1980                    if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED &&
1981                            PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_AUTO_CREATE) {
1982    
1983                            addDefaultUserPrivateLayouts = true;
1984    
1985                            if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED) {
1986                                    if (hasPowerUserRole == null) {
1987                                            hasPowerUserRole = hasPowerUserRole(user);
1988                                    }
1989    
1990                                    if (!hasPowerUserRole.booleanValue()) {
1991                                            addDefaultUserPrivateLayouts = false;
1992                                    }
1993                            }
1994                    }
1995    
1996                    Boolean hasPrivateLayouts = null;
1997    
1998                    if (addDefaultUserPrivateLayouts) {
1999                            hasPrivateLayouts = LayoutLocalServiceUtil.hasLayouts(user, true);
2000    
2001                            if (!hasPrivateLayouts) {
2002                                    addDefaultUserPrivateLayouts(user);
2003                            }
2004                    }
2005    
2006                    boolean deleteDefaultUserPrivateLayouts = false;
2007    
2008                    if (!PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_ENABLED) {
2009                            deleteDefaultUserPrivateLayouts = true;
2010                    }
2011                    else if (PropsValues.LAYOUT_USER_PRIVATE_LAYOUTS_POWER_USER_REQUIRED) {
2012                            if (hasPowerUserRole == null) {
2013                                    hasPowerUserRole = hasPowerUserRole(user);
2014                            }
2015    
2016                            if (!hasPowerUserRole.booleanValue()) {
2017                                    deleteDefaultUserPrivateLayouts = true;
2018                            }
2019                    }
2020    
2021                    if (deleteDefaultUserPrivateLayouts) {
2022                            if (hasPrivateLayouts == null) {
2023                                    hasPrivateLayouts = LayoutLocalServiceUtil.hasLayouts(
2024                                            user, true);
2025                            }
2026    
2027                            if (hasPrivateLayouts) {
2028                                    deleteDefaultUserPrivateLayouts(user);
2029                            }
2030                    }
2031    
2032                    // Public pages
2033    
2034                    boolean addDefaultUserPublicLayouts = false;
2035    
2036                    if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED &&
2037                            PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_AUTO_CREATE) {
2038    
2039                            addDefaultUserPublicLayouts = true;
2040    
2041                            if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED) {
2042                                    if (hasPowerUserRole == null) {
2043                                            hasPowerUserRole = hasPowerUserRole(user);
2044                                    }
2045    
2046                                    if (!hasPowerUserRole.booleanValue()) {
2047                                            addDefaultUserPublicLayouts = false;
2048                                    }
2049                            }
2050                    }
2051    
2052                    Boolean hasPublicLayouts = null;
2053    
2054                    if (addDefaultUserPublicLayouts) {
2055                            hasPublicLayouts = LayoutLocalServiceUtil.hasLayouts(user, false);
2056    
2057                            if (!hasPublicLayouts) {
2058                                    addDefaultUserPublicLayouts(user);
2059                            }
2060                    }
2061    
2062                    boolean deleteDefaultUserPublicLayouts = false;
2063    
2064                    if (!PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_ENABLED) {
2065                            deleteDefaultUserPublicLayouts = true;
2066                    }
2067                    else if (PropsValues.LAYOUT_USER_PUBLIC_LAYOUTS_POWER_USER_REQUIRED) {
2068                            if (hasPowerUserRole == null) {
2069                                    hasPowerUserRole = hasPowerUserRole(user);
2070                            }
2071    
2072                            if (!hasPowerUserRole.booleanValue()) {
2073                                    deleteDefaultUserPublicLayouts = true;
2074                            }
2075                    }
2076    
2077                    if (deleteDefaultUserPublicLayouts) {
2078                            if (hasPublicLayouts == null) {
2079                                    hasPublicLayouts = LayoutLocalServiceUtil.hasLayouts(
2080                                            user, false);
2081                            }
2082    
2083                            if (hasPublicLayouts) {
2084                                    deleteDefaultUserPublicLayouts(user);
2085                            }
2086                    }
2087            }
2088    
2089            protected File privateLARFile;
2090            protected File publicLARFile;
2091    
2092            private static final String _CONTROL_PANEL_CATEGORY_PORTLET_PREFIX =
2093                    "portlet_";
2094    
2095            private static final String _PATH_PORTAL_LAYOUT = "/portal/layout";
2096    
2097            private static Log _log = LogFactoryUtil.getLog(ServicePreAction.class);
2098    
2099    }