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 GmbH & Co. KG, 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.relations;
029
030import org.opencms.db.CmsPublishList;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036
037import java.util.Collection;
038import java.util.Collections;
039import java.util.List;
040import java.util.Map;
041import java.util.Set;
042
043import org.apache.commons.logging.Log;
044
045/**
046 * Util class to find broken links in a bundle of resources to be published.<p>
047 *
048 * @since 6.5.5
049 */
050public class CmsRelationPublishValidator {
051
052    /** The log object for this class. */
053    private static final Log LOG = CmsLog.getLog(CmsRelationPublishValidator.class);
054
055    /** The internal computed broken relations map. */
056    protected Map<String, List<CmsRelation>> m_brokenRelations;
057
058    /** the cms context object. */
059    private CmsObject m_cms;
060
061    /**
062     * Creates a new helper object.<p>
063     *
064     * @param cms the cms object
065     * @param publishList a publish list to validate
066     */
067    public CmsRelationPublishValidator(CmsObject cms, CmsPublishList publishList) {
068
069        try {
070            m_cms = OpenCms.initCmsObject(cms);
071        } catch (CmsException e) {
072            // should never happen
073            LOG.error(e.getLocalizedMessage(), e);
074            m_cms = cms;
075        }
076        try {
077            m_brokenRelations = OpenCms.getPublishManager().validateRelations(m_cms, publishList, null);
078        } catch (Exception e) {
079            // should never happen
080            LOG.error(e.getLocalizedMessage(), e);
081        }
082    }
083
084    /**
085     * Returns the information bean for the given entry.<p>
086     *
087     * @param resourceName the entry name
088     *
089     * @return the information bean for the given entry
090     */
091    public CmsRelationValidatorInfoEntry getInfoEntry(String resourceName) {
092
093        String resName = resourceName;
094        String siteRoot = m_cms.getRequestContext().getSiteRoot();
095        String siteName = null;
096        if (resName.startsWith(m_cms.getRequestContext().getSiteRoot())) {
097            resName = m_cms.getRequestContext().removeSiteRoot(resName);
098        } else {
099            siteRoot = OpenCms.getSiteManager().getSiteRoot(resName);
100            siteName = siteRoot;
101            if (siteRoot != null) {
102                String oldSite = m_cms.getRequestContext().getSiteRoot();
103                try {
104                    m_cms.getRequestContext().setSiteRoot("/");
105                    siteName = m_cms.readPropertyObject(siteRoot, CmsPropertyDefinition.PROPERTY_TITLE, false).getValue(
106                        siteRoot);
107                } catch (CmsException e) {
108                    siteName = siteRoot;
109                } finally {
110                    m_cms.getRequestContext().setSiteRoot(oldSite);
111                }
112                resName = resName.substring(siteRoot.length());
113            } else {
114                siteName = "/";
115            }
116        }
117        return new CmsRelationValidatorInfoEntry(
118            resourceName,
119            resName,
120            siteName,
121            siteRoot,
122            Collections.unmodifiableList(m_brokenRelations.get(resourceName)));
123    }
124
125    /**
126     * If no relation would be broken deleting the given resources.<p>
127     *
128     * @return <code>true</code> if no relation would be broken deleting the given resources
129     */
130    public boolean isEmpty() {
131
132        return m_brokenRelations.isEmpty();
133    }
134
135    /**
136     * @see java.util.Map#keySet()
137     *
138     * @return the broken relations key set
139     */
140    public Set<String> keySet() {
141
142        return m_brokenRelations.keySet();
143    }
144
145    /**
146     * @see java.util.Map#values()
147     *
148     * @return the broken relations value set
149     */
150    public Collection<List<CmsRelation>> values() {
151
152        return m_brokenRelations.values();
153    }
154}