001    /**
002     * Copyright (c) 2000-2013 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            public static String getAssetViewURL(
047                    LiferayPortletRequest liferayPortletRequest,
048                    LiferayPortletResponse liferayPortletResponse, AssetEntry assetEntry,
049                    boolean viewInContext) {
050    
051                    PortletURL viewFullContentURL =
052                            liferayPortletResponse.createRenderURL();
053    
054                    viewFullContentURL.setParameter(
055                            "struts_action", "/asset_publisher/view_content");
056    
057                    viewFullContentURL.setParameter(
058                            "assetEntryId", String.valueOf(assetEntry.getEntryId()));
059    
060                    AssetRendererFactory assetRendererFactory =
061                            assetEntry.getAssetRendererFactory();
062    
063                    AssetRenderer assetRenderer = assetEntry.getAssetRenderer();
064    
065                    viewFullContentURL.setParameter("type", assetRendererFactory.getType());
066    
067                    ThemeDisplay themeDisplay =
068                            (ThemeDisplay)liferayPortletRequest.getAttribute(
069                                    WebKeys.THEME_DISPLAY);
070    
071                    if (Validator.isNotNull(assetRenderer.getUrlTitle())) {
072                            if (assetRenderer.getGroupId() != themeDisplay.getScopeGroupId()) {
073                                    viewFullContentURL.setParameter(
074                                            "groupId", String.valueOf(assetRenderer.getGroupId()));
075                            }
076    
077                            viewFullContentURL.setParameter(
078                                    "urlTitle", assetRenderer.getUrlTitle());
079                    }
080    
081                    String viewURL = null;
082    
083                    String currentURL = null;
084    
085                    if (viewInContext) {
086                            currentURL = PortalUtil.getCurrentURL(liferayPortletRequest);
087    
088                            String viewFullContentURLString = viewFullContentURL.toString();
089    
090                            viewFullContentURLString = HttpUtil.setParameter(
091                                    viewFullContentURLString, "redirect", currentURL);
092    
093                            try {
094                                    viewURL = assetRenderer.getURLViewInContext(
095                                            liferayPortletRequest, liferayPortletResponse,
096                                            viewFullContentURLString);
097                            }
098                            catch (Exception e) {
099                            }
100                    }
101                    else {
102                            PortletURL currentURLObj = PortletURLUtil.getCurrent(
103                                    liferayPortletRequest, liferayPortletResponse);
104    
105                            currentURL = currentURLObj.toString();
106                    }
107    
108                    if (Validator.isNull(viewURL)) {
109                            viewURL = viewFullContentURL.toString();
110                    }
111    
112                    viewURL = AssetUtil.checkViewURL(
113                            assetEntry, viewInContext, viewURL, currentURL, themeDisplay, true);
114    
115                    return viewURL;
116            }
117    
118    }