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