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