001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsCssIcon;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.FontOpenCms;
039import org.opencms.ui.components.CmsBasicDialog;
040import org.opencms.ui.components.CmsResourceInfo;
041import org.opencms.ui.components.OpenCmsTheme;
042import org.opencms.ui.report.CmsReportWidget;
043import org.opencms.workplace.Messages;
044
045import java.util.Collections;
046import java.util.List;
047
048import org.apache.commons.logging.Log;
049
050import com.vaadin.ui.Button;
051import com.vaadin.ui.Button.ClickEvent;
052import com.vaadin.ui.Button.ClickListener;
053import com.vaadin.ui.FormLayout;
054import com.vaadin.ui.Panel;
055import com.vaadin.ui.Window;
056import com.vaadin.v7.data.Property.ValueChangeEvent;
057import com.vaadin.v7.data.Property.ValueChangeListener;
058import com.vaadin.v7.shared.ui.label.ContentMode;
059import com.vaadin.v7.ui.CheckBox;
060import com.vaadin.v7.ui.Label;
061import com.vaadin.v7.ui.VerticalLayout;
062
063/**
064 * Dialog to delete property definitions.<p>
065 */
066public class CmsPropertyDeleteDialog extends CmsBasicDialog {
067
068    /**vaadin serial id.*/
069    private static final long serialVersionUID = -3397853426667893254L;
070
071    /** The logger for this class. */
072    static Log LOG = CmsLog.getLog(CmsPropertyDeleteDialog.class.getName());
073
074    /**List of resources having the property to be deleted.*/
075    protected List<CmsResource> m_resources;
076
077    /**Vaadin component.*/
078    private VerticalLayout m_forceDeleteLayout;
079
080    /**Vaadin component.*/
081    protected VerticalLayout m_start;
082
083    /**Vaadin component.*/
084    private Label m_icon;
085
086    /**Vaadin component.*/
087    protected Button m_cancelButton;
088
089    /**Vaadin component.*/
090    protected Button m_okButton;
091
092    /**Vaadin component.*/
093    protected CheckBox m_forceDelete;
094
095    /**Vaadin component.*/
096    protected FormLayout m_threadReport;
097
098    /**Vaadin component.*/
099    protected Panel m_report;
100
101    /**CmsObject.*/
102    private CmsObject m_cms;
103
104    /**
105     * public constructor.<p>
106     *
107     * @param propName name of property
108     * @param window to be closed
109     * @param runnable to update table
110     */
111    public CmsPropertyDeleteDialog(final String propName, final Window window, final Runnable runnable) {
112
113        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
114
115        displayResourceInfoDirectly(
116            Collections.singletonList(new CmsResourceInfo(propName, "", new CmsCssIcon(OpenCmsTheme.ICON_DATABASE))));
117
118        //Setup icon
119        m_icon.setContentMode(ContentMode.HTML);
120        m_icon.setValue(FontOpenCms.WARNING.getHtml());
121
122        m_report.setVisible(false);
123
124        try {
125            m_resources = getCms().readResourcesWithProperty(propName);
126            m_forceDeleteLayout.setVisible(!m_resources.isEmpty());
127            m_okButton.setEnabled(m_resources.isEmpty());
128        } catch (CmsException e) {
129            //
130        }
131        m_cancelButton.addClickListener(new ClickListener() {
132
133            private static final long serialVersionUID = 4788359189538313935L;
134
135            public void buttonClick(ClickEvent event) {
136
137                window.close();
138            }
139        });
140
141        m_forceDelete.addValueChangeListener(new ValueChangeListener() {
142
143            private static final long serialVersionUID = 7482690600008082762L;
144
145            public void valueChange(ValueChangeEvent event) {
146
147                m_okButton.setEnabled(m_forceDelete.getValue().booleanValue());
148
149            }
150
151        });
152
153        m_okButton.addClickListener(new ClickListener() {
154
155            private static final long serialVersionUID = -7861406021237202016L;
156
157            public void buttonClick(ClickEvent event) {
158
159                if (!m_resources.isEmpty()) {
160                    m_start.setVisible(false);
161                    m_report.setVisible(true);
162                    m_okButton.setEnabled(false);
163
164                    CmsRemovePropertyFromResourcesThread thread = new CmsRemovePropertyFromResourcesThread(
165                        getCms(),
166                        propName);
167                    m_threadReport.addComponent(new CmsReportWidget(thread));
168
169                    thread.start();
170                    m_cancelButton.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_DIALOG_BUTTON_CLOSE_0));
171                    m_cancelButton.addClickListener(new ClickListener() {
172
173                        public void buttonClick(ClickEvent event) {
174
175                            runnable.run();
176
177                        }
178
179                    });
180                    return;
181                }
182                try {
183                    getCms().deletePropertyDefinition(propName);
184                    runnable.run();
185                } catch (CmsException e) {
186                    LOG.error("Unable to delete property definition", e);
187                }
188                window.close();
189            }
190
191        });
192    }
193
194    /**
195     * Gets a copy of the cms object set to root site.<p>
196     *
197     * @return CmsObject
198     */
199    protected CmsObject getCms() {
200
201        if (m_cms == null) {
202            try {
203                m_cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
204                m_cms.getRequestContext().setSiteRoot("");
205            } catch (CmsException e) {
206                return null;
207            }
208        }
209        return m_cms;
210    }
211}