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.workplace.threads;
029
030import org.opencms.db.CmsPublishList;
031import org.opencms.file.CmsObject;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.report.A_CmsReportThread;
036import org.opencms.workplace.CmsWorkplaceSettings;
037
038import org.apache.commons.logging.Log;
039
040/**
041 * A report thread for the relations validator.<p>
042 *
043 * @since 6.0.0
044 */
045public class CmsRelationsValidatorThread extends A_CmsReportThread {
046
047    /** The log object for this class. */
048    private static final Log LOG = CmsLog.getLog(CmsRelationsValidatorThread.class);
049
050    /** A list of cms resources to be published directly.<p> */
051    private CmsPublishList m_publishList;
052
053    /** Flag that indicates whether the publish list should be svaed in the workplace settings. */
054    private boolean m_savePublishList;
055
056    /** The current user's workplace settings.<p> */
057    private CmsWorkplaceSettings m_settings;
058
059    /**
060     * Creates a thread that validates the relations for all files of the current project.<p>
061     *
062     * @param cms the current OpenCms context object
063     */
064    public CmsRelationsValidatorThread(CmsObject cms) {
065
066        super(
067            cms,
068            Messages.get().getBundle().key(
069                Messages.GUI_HTML_LINK_VALIDATOR_THREAD_NAME_1,
070                new Object[] {cms.getRequestContext().getCurrentProject().getName()}));
071
072        m_publishList = null;
073        m_savePublishList = false;
074        m_settings = null;
075
076        initHtmlReport(cms.getRequestContext().getLocale());
077    }
078
079    /**
080     * Creates a thread that validates the relations in the list of unpublished file(s) of the
081     * current (offline) project.<p>
082     *
083     * The publish list *IS* saved in the current user's workplace settings for
084     * further processing by other threads. The last thread processing this publish list *MUST*
085     * ensure that the publish list gets removed from the current user's workplace settings!<p>
086     *
087     * @param cms the current OpenCms context object
088     * @param publishList the list of resources which will be directly published
089     * @param settings the current user's workplace settings
090     */
091    public CmsRelationsValidatorThread(CmsObject cms, CmsPublishList publishList, CmsWorkplaceSettings settings) {
092
093        super(
094            cms,
095            Messages.get().getBundle().key(
096                Messages.GUI_HTML_LINK_VALIDATOR_THREAD_NAME_1,
097                new Object[] {cms.getRequestContext().getCurrentProject().getName()}));
098
099        m_publishList = publishList;
100        if (m_publishList == null) {
101            try {
102                m_publishList = OpenCms.getPublishManager().getPublishList(cms);
103            } catch (CmsException e) {
104                // ignore
105            }
106        }
107        m_savePublishList = true;
108        m_settings = settings;
109
110        initHtmlReport(cms.getRequestContext().getLocale());
111    }
112
113    /**
114     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
115     */
116    @Override
117    public String getReportUpdate() {
118
119        return getReport().getReportUpdate();
120    }
121
122    /**
123     * @see java.lang.Runnable#run()
124     */
125    @Override
126    public void run() {
127
128        try {
129            // validate the HTML links in the resources that actually get published
130            OpenCms.getPublishManager().validateRelations(getCms(), m_publishList, getReport());
131
132            if (m_savePublishList && (m_settings != null)) {
133                // save the publish list optionally to be processed by further workplace threads
134                m_settings.setPublishList(m_publishList);
135            }
136        } catch (Throwable e) {
137            if (m_savePublishList && (m_settings != null)) {
138                // overwrite the publish list in any case with null
139                m_settings.setPublishList(null);
140            }
141            getReport().println(e);
142            LOG.error(Messages.get().getBundle().key(Messages.ERR_LINK_VALIDATION_0), e);
143        }
144    }
145}