001    /**
002     * Copyright (c) 2000-present 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.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.model.Layout;
019    import com.liferay.portal.kernel.model.LayoutConstants;
020    import com.liferay.portal.kernel.service.ServiceContext;
021    import com.liferay.portal.kernel.service.persistence.LayoutUtil;
022    import com.liferay.portal.kernel.theme.ThemeDisplay;
023    import com.liferay.portal.kernel.util.PortalUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    
026    import javax.servlet.http.HttpServletRequest;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class LayoutURLUtil {
032    
033            public static String getLayoutURL(
034                    Layout layout, ServiceContext serviceContext) {
035    
036                    HttpServletRequest request = serviceContext.getRequest();
037    
038                    if (request == null) {
039                            return StringPool.BLANK;
040                    }
041    
042                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
043                            com.liferay.portal.kernel.util.WebKeys.THEME_DISPLAY);
044    
045                    try {
046                            return PortalUtil.getLayoutURL(layout, themeDisplay);
047                    }
048                    catch (Exception e) {
049                            return StringPool.BLANK;
050                    }
051            }
052    
053            public static String getLayoutURL(
054                            long groupId, String portletId, ServiceContext serviceContext)
055                    throws PortalException {
056    
057                    long plid = serviceContext.getPlid();
058    
059                    long controlPanelPlid = PortalUtil.getControlPanelPlid(
060                            serviceContext.getCompanyId());
061    
062                    if (plid == controlPanelPlid) {
063                            plid = PortalUtil.getPlidFromPortletId(groupId, portletId);
064                    }
065    
066                    String layoutURL = StringPool.BLANK;
067    
068                    if (plid != LayoutConstants.DEFAULT_PLID) {
069                            Layout layout = LayoutUtil.findByPrimaryKey(plid);
070    
071                            layoutURL = getLayoutURL(layout, serviceContext);
072                    }
073    
074                    return layoutURL;
075            }
076    
077    }