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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsVfsResourceNotFoundException;
033import org.opencms.main.CmsException;
034import org.opencms.main.OpenCms;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.util.CmsUUID;
037
038import java.util.ArrayList;
039import java.util.Collection;
040import java.util.Collections;
041import java.util.List;
042import java.util.Locale;
043
044/**
045 * This is a utility class which provides convenience methods for finding detail page names for resources which include
046 * the URL names of the resources themselves.<p>
047 *
048 * @see I_CmsDetailPageHandler
049 *
050 * @since 8.0.0
051 */
052public final class CmsDetailPageUtil {
053
054    /**
055     * The hidden default constructor.<p>
056     */
057    private CmsDetailPageUtil() {
058
059        // do nothing
060    }
061
062    /**
063     * Gets a list of detail page URIs for the given resource, with its URL name appended.<p>
064     *
065     * @param cms the current CMS context
066     * @param res the resource for which the detail pages should be retrieved
067     *
068     * @return the list of detail page URIs
069     *
070     * @throws CmsException if something goes wrong
071     */
072    public static List<String> getAllDetailPagesWithUrlName(CmsObject cms, CmsResource res) throws CmsException {
073
074        List<String> result = new ArrayList<String>();
075        Collection<String> detailPages = OpenCms.getADEManager().getDetailPageHandler().getAllDetailPages(
076            cms,
077            res.getTypeId());
078        if (detailPages.isEmpty()) {
079            return Collections.<String> emptyList();
080        }
081        List<String> detailNames = cms.readUrlNamesForAllLocales(res.getStructureId());
082        for (String urlName : detailNames) {
083            for (String detailPage : detailPages) {
084                String rootPath = CmsStringUtil.joinPaths(detailPage, urlName, "/");
085                result.add(rootPath);
086            }
087        }
088        return result;
089    }
090
091    /**
092     * Returns either the newest URL name for a structure id, or  the structure id as a string if there is no URL name.<p>
093     *
094     * @param cms the current CMS context
095     * @param id the structure id of a resource
096     *
097     * @return the best URL name for the structure id
098     *
099     * @throws CmsException if something goes wrong
100     */
101    public static String getBestUrlName(CmsObject cms, CmsUUID id) throws CmsException {
102
103        // this is currently only used for static export
104        Locale locale = cms.getRequestContext().getLocale();
105        List<Locale> defaultLocales = OpenCms.getLocaleManager().getDefaultLocales();
106        String urlName = cms.readBestUrlName(id, locale, defaultLocales);
107        if (urlName != null) {
108            return urlName;
109        }
110        return id.toString();
111    }
112
113    /**
114     * Looks up a page by URI (which may be a detail page URI, or a normal VFS uri).<p>
115     *
116     * @param cms the current CMS context
117     * @param uri the detail page or VFS uri
118     *
119     * @return the resource with the given uri
120     *
121     * @throws CmsException if something goes wrong
122     */
123    public static CmsResource lookupPage(CmsObject cms, String uri) throws CmsException {
124
125        try {
126            CmsResource res = cms.readResource(uri);
127            return res;
128        } catch (CmsVfsResourceNotFoundException e) {
129            String detailName = CmsResource.getName(uri).replaceAll("/$", "");
130            CmsUUID detailId = cms.readIdForUrlName(detailName);
131            if (detailId != null) {
132                return cms.readResource(detailId);
133            }
134            throw new CmsVfsResourceNotFoundException(
135                org.opencms.db.generic.Messages.get().container(
136                    org.opencms.db.generic.Messages.ERR_READ_RESOURCE_1,
137                    uri));
138        }
139    }
140}