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.portlet.assetpublisher.util;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
018    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.PortletURLUtil;
025    import com.liferay.portlet.asset.model.AssetEntry;
026    import com.liferay.portlet.asset.model.AssetRenderer;
027    import com.liferay.portlet.asset.model.AssetRendererFactory;
028    import com.liferay.portlet.asset.util.AssetUtil;
029    
030    import javax.portlet.PortletURL;
031    
032    /**
033     * @author Juan Fern??ndez
034     */
035    public class AssetPublisherHelperImpl implements AssetPublisherHelper {
036    
037            @Override
038            public String getAssetViewURL(
039                    LiferayPortletRequest liferayPortletRequest,
040                    LiferayPortletResponse liferayPortletResponse, AssetEntry assetEntry) {
041    
042                    return getAssetViewURL(
043                            liferayPortletRequest, liferayPortletResponse, assetEntry, false);
044            }
045    
046            @Override
047            public String getAssetViewURL(
048                    LiferayPortletRequest liferayPortletRequest,
049                    LiferayPortletResponse liferayPortletResponse, AssetEntry assetEntry,
050                    boolean viewInContext) {
051    
052                    PortletURL viewFullContentURL =
053                            liferayPortletResponse.createRenderURL();
054    
055                    viewFullContentURL.setParameter(
056                            "struts_action", "/asset_publisher/view_content");
057    
058                    viewFullContentURL.setParameter(
059                            "assetEntryId", String.valueOf(assetEntry.getEntryId()));
060    
061                    AssetRendererFactory assetRendererFactory =
062                            assetEntry.getAssetRendererFactory();
063    
064                    AssetRenderer assetRenderer = assetEntry.getAssetRenderer();
065    
066                    viewFullContentURL.setParameter("type", assetRendererFactory.getType());
067    
068                    ThemeDisplay themeDisplay =
069                            (ThemeDisplay)liferayPortletRequest.getAttribute(
070                                    WebKeys.THEME_DISPLAY);
071    
072                    if (Validator.isNotNull(assetRenderer.getUrlTitle())) {
073                            if (assetRenderer.getGroupId() != themeDisplay.getScopeGroupId()) {
074                                    viewFullContentURL.setParameter(
075                                            "groupId", String.valueOf(assetRenderer.getGroupId()));
076                            }
077    
078                            viewFullContentURL.setParameter(
079                                    "urlTitle", assetRenderer.getUrlTitle());
080                    }
081    
082                    String viewURL = null;
083    
084                    String currentURL = null;
085    
086                    if (viewInContext) {
087                            currentURL = PortalUtil.getCurrentURL(liferayPortletRequest);
088    
089                            String viewFullContentURLString = viewFullContentURL.toString();
090    
091                            viewFullContentURLString = HttpUtil.setParameter(
092                                    viewFullContentURLString, "redirect", currentURL);
093    
094                            try {
095                                    viewURL = assetRenderer.getURLViewInContext(
096                                            liferayPortletRequest, liferayPortletResponse,
097                                            viewFullContentURLString);
098                            }
099                            catch (Exception e) {
100                            }
101                    }
102                    else {
103                            PortletURL currentURLObj = PortletURLUtil.getCurrent(
104                                    liferayPortletRequest, liferayPortletResponse);
105    
106                            currentURL = currentURLObj.toString();
107                    }
108    
109                    if (Validator.isNull(viewURL)) {
110                            viewURL = viewFullContentURL.toString();
111                    }
112    
113                    viewURL = AssetUtil.checkViewURL(
114                            assetEntry, viewInContext, viewURL, currentURL, themeDisplay);
115    
116                    return viewURL;
117            }
118    
119    }