001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade.detailpage;
029
030import org.opencms.ade.configuration.CmsADEConfigData;
031import org.opencms.ade.configuration.CmsADEManager;
032import org.opencms.configuration.CmsParameterConfiguration;
033import org.opencms.file.CmsObject;
034import org.opencms.file.CmsResource;
035import org.opencms.main.CmsException;
036import org.opencms.main.OpenCms;
037import org.opencms.site.CmsSite;
038import org.opencms.workplace.CmsWorkplace;
039
040import java.util.ArrayList;
041import java.util.Arrays;
042import java.util.Collection;
043import java.util.List;
044
045/**
046 * Original detail page handler implementing the detail page logic from OpenCms versions up to 11.0.
047 *
048 * <p>Disregards the 'exclude external detail contents' option, allows all combinations of detail page / detail content on the same site.
049 */
050public class CmsPermissiveDetailPageHandler implements I_CmsDetailPageHandler {
051
052    /** The configuration. */
053    private CmsParameterConfiguration m_config = new CmsParameterConfiguration();
054
055    /**
056     * Gets the detail page for a content element.<p>
057     *
058     * @param manager the ADE manager instance.
059     * @param cms the CMS context
060     * @param pageRootPath the element's root path
061     * @param originPath the path in which the the detail page is being requested
062     * @param targetDetailPage the target detail page to use
063     *
064     * @return the detail page for the content element
065     */
066    public static String getDetailPage(
067        CmsADEManager manager,
068        CmsObject cms,
069        String pageRootPath,
070        String originPath,
071        String targetDetailPage) {
072
073        boolean online = cms.getRequestContext().getCurrentProject().isOnlineProject();
074        String resType = manager.getParentFolderType(online, pageRootPath);
075        if (resType == null) {
076            return null;
077        }
078        if ((targetDetailPage != null) && manager.getDetailPages(cms, resType).contains(targetDetailPage)) {
079            return targetDetailPage;
080        }
081
082        String originRootPath = cms.getRequestContext().addSiteRoot(originPath);
083        CmsADEConfigData configData = manager.lookupConfiguration(cms, originRootPath);
084        CmsADEConfigData targetConfigData = manager.lookupConfiguration(cms, pageRootPath);
085        boolean targetFirst = targetConfigData.isPreferDetailPagesForLocalContents();
086        List<CmsADEConfigData> configs = targetFirst
087        ? Arrays.asList(targetConfigData, configData)
088        : Arrays.asList(configData, targetConfigData);
089        for (CmsADEConfigData config : configs) {
090            List<CmsDetailPageInfo> pageInfo = config.getDetailPagesForType(resType);
091            if ((pageInfo != null) && !pageInfo.isEmpty()) {
092                return pageInfo.get(0).getUri();
093            }
094        }
095        return null;
096
097    }
098
099    /**
100     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
101     */
102    public void addConfigurationParameter(String paramName, String paramValue) {
103
104        m_config.add(paramName, paramValue);
105
106    }
107
108    /**
109     * @see org.opencms.ade.detailpage.I_CmsDetailPageHandler#getAllDetailPages(org.opencms.file.CmsObject, int)
110     */
111    public Collection<String> getAllDetailPages(CmsObject cms, int resType) throws CmsException {
112
113        if (!OpenCms.getADEManager().isInitialized()) {
114            return new ArrayList<String>();
115        }
116        String typeName = OpenCms.getResourceManager().getResourceType(resType).getTypeName();
117        return OpenCms.getADEManager().getDetailPages(cms, typeName);
118    }
119
120    /**
121     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
122     */
123    public CmsParameterConfiguration getConfiguration() {
124
125        return m_config;
126
127    }
128
129    /**
130     * @see org.opencms.ade.detailpage.I_CmsDetailPageHandler#getDetailPage(org.opencms.file.CmsObject, java.lang.String, java.lang.String, java.lang.String)
131     */
132    public String getDetailPage(CmsObject cms, String rootPath, String linkSource, String targetDetailPage) {
133
134        CmsADEManager manager = OpenCms.getADEManager();
135        if (!manager.isInitialized()) {
136            return null;
137        }
138
139        if (rootPath.endsWith(".jsp") || rootPath.startsWith(CmsWorkplace.VFS_PATH_WORKPLACE)) {
140            // exclude these for performance reasons
141            return null;
142        }
143        String result = getDetailPage(manager, cms, rootPath, linkSource, targetDetailPage);
144        if (result == null) {
145            return null;
146        }
147        if (!CmsResource.isFolder(result)) {
148            result = CmsResource.getFolderPath(result);
149        }
150        return result;
151    }
152
153    /**
154     * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
155     */
156    public void initConfiguration() {
157
158        m_config = CmsParameterConfiguration.unmodifiableVersion(m_config);
159
160    }
161
162    /**
163     * @see org.opencms.ade.detailpage.I_CmsDetailPageHandler#initialize(org.opencms.file.CmsObject, org.opencms.file.CmsObject)
164     */
165    public void initialize(CmsObject offlineCms, CmsObject onlineCms) {
166
167        // do nothing
168    }
169
170    /**
171     * Checks whether the given detail page is valid for the given resource.<p>
172     *
173     * @param cms the CMS context
174     * @param page the detail page
175     * @param detailRes the detail resource
176     *
177     * @return true if the given detail page is valid
178     */
179    public boolean isValidDetailPage(CmsObject cms, CmsResource page, CmsResource detailRes) {
180
181        if (OpenCms.getSystemInfo().isRestrictDetailContents()) {
182            // in 'restrict detail contents mode', do not allow detail contents from a real site on a detail page of a different real site
183            CmsSite pageSite = OpenCms.getSiteManager().getSiteForRootPath(page.getRootPath());
184            CmsSite detailSite = OpenCms.getSiteManager().getSiteForRootPath(detailRes.getRootPath());
185            if ((pageSite != null)
186                && (detailSite != null)
187                && !pageSite.getSiteRoot().equals(detailSite.getSiteRoot())) {
188                return false;
189            }
190        }
191        return OpenCms.getADEManager().isDetailPage(cms, page);
192    }
193
194}