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.ui.apps.dbmanager;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProperty;
032import org.opencms.file.CmsResource;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.report.A_CmsReportThread;
036import org.opencms.report.I_CmsReport;
037import org.opencms.ui.apps.Messages;
038import org.opencms.ui.apps.sitemanager.CmsSitesWebserverThread;
039
040import org.apache.commons.logging.Log;
041
042/**
043 * Thread which removes property from ressources and deletes property definition.<p>
044 */
045public class CmsRemovePropertyFromResourcesThread extends A_CmsReportThread {
046
047    /** The logger for this class. */
048    static Log LOG = CmsLog.getLog(CmsSitesWebserverThread.class.getName());
049
050    /** The file path. */
051    private String m_propName;
052
053    /**
054     * Public constructor.<p>
055     *
056     * @param cms CmsObejct
057     * @param propName propertydefinition name
058     */
059    public CmsRemovePropertyFromResourcesThread(CmsObject cms, String propName) {
060
061        super(cms, "write-to-webserver");
062        m_propName = propName;
063
064        initHtmlReport(cms.getRequestContext().getLocale());
065    }
066
067    /**
068     * @see org.opencms.report.A_CmsReportThread#getReportUpdate()
069     */
070    @Override
071    public String getReportUpdate() {
072
073        return getReport().getReportUpdate();
074    }
075
076    /**
077     * @see java.lang.Thread#run()
078     */
079    @Override
080    public void run() {
081
082        try {
083            getReport().println(
084                Messages.get().container(Messages.RPT_DATABASEAPP_DEL_PROPERTY_START_0),
085                I_CmsReport.FORMAT_DEFAULT);
086            getReport().println();
087            getReport().println(
088                Messages.get().container(Messages.RPT_DATABASEAPP_DEL_PROPERTY_REMOVE_FROM_RESOURCE_START_0),
089                I_CmsReport.FORMAT_DEFAULT);
090            for (CmsResource res : getCms().readResourcesWithProperty(m_propName)) {
091                getReport().print(
092                    Messages.get().container(
093                        Messages.RPT_DATABASEAPP_DEL_PROPERTY_REMOVE_FROM_RESOURCE_1,
094                        res.getRootPath()),
095                    I_CmsReport.FORMAT_DEFAULT);
096                CmsProperty prop = new CmsProperty(m_propName, "", "");
097                try {
098                    getCms().lockResource(res);
099                    getCms().writePropertyObject(res.getRootPath(), prop);
100                    getCms().unlockResource(res);
101                    getReport().println(Messages.get().container(Messages.RPT_DATABASEAPP_OK_0), I_CmsReport.FORMAT_OK);
102                } catch (CmsException e) {
103                    LOG.error("unable to remove property from resource", e);
104                    getReport().println(
105                        Messages.get().container(Messages.RPT_DATABASEAPP_FAILED_0),
106                        I_CmsReport.FORMAT_ERROR);
107                }
108            }
109            try {
110                getCms().deletePropertyDefinition(m_propName);
111                getReport().println();
112                getReport().println(
113                    Messages.get().container(Messages.RPT_DATABASEAPP_DEL_PROPERTY_END_OK_0),
114                    I_CmsReport.FORMAT_OK);
115            } catch (CmsException e) {
116                LOG.error("Unable to delete property definition", e);
117                getReport().println(
118                    Messages.get().container(Messages.RPT_DATABASEAPP_DEL_PROPERTY_END_FAILED_0),
119                    I_CmsReport.FORMAT_ERROR);
120            }
121
122        } catch (Exception e) {
123            LOG.error("Unable to read resources for property", e);
124            getReport().println(e);
125        }
126    }
127}