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