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.sitemanager;
029
030import org.opencms.file.CmsResource;
031import org.opencms.i18n.CmsEncoder;
032import org.opencms.lock.CmsLockException;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.site.CmsSite;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.apps.Messages;
039import org.opencms.ui.components.CmsBasicDialog;
040import org.opencms.ui.components.CmsResourceInfo;
041
042import java.util.ArrayList;
043import java.util.List;
044import java.util.Set;
045
046import org.apache.commons.logging.Log;
047
048import com.vaadin.ui.Button;
049import com.vaadin.ui.Button.ClickEvent;
050import com.vaadin.ui.Button.ClickListener;
051import com.vaadin.v7.shared.ui.label.ContentMode;
052import com.vaadin.v7.ui.CheckBox;
053import com.vaadin.v7.ui.Label;
054import com.vaadin.v7.ui.VerticalLayout;
055
056/**
057 * Dialog for deleting Sites.<p>
058 */
059public class CmsDeleteSiteDialog extends CmsBasicDialog {
060
061    /** The logger for this class. */
062    static Log LOG = CmsLog.getLog(CmsDeleteSiteDialog.class.getName());
063
064    /**vaadin serial id.*/
065    private static final long serialVersionUID = 4861877088383896218L;
066
067    /** The site manager instance.*/
068    protected CmsSiteManager m_manager;
069
070    /**cancel button.*/
071    private Button m_cancelButton;
072
073    /**check box: should resources be deleted?*/
074    private CheckBox m_deleteResources;
075
076    /**ok button.*/
077    private Button m_okButton;
078
079    /**sites to delete.*/
080    protected final List<CmsSite> m_sitesToDelete = new ArrayList<CmsSite>();
081
082    /**
083     * Public constructor.<p>
084     *
085     * @param manager the site manager instance
086     * @param data with values for siteroots to delete.
087     */
088    public CmsDeleteSiteDialog(CmsSiteManager manager, Set<String> data) {
089
090        m_manager = manager;
091
092        for (String site : data) {
093            m_sitesToDelete.add(manager.getElement(site));
094        }
095
096        displayResourceInfoDirectly(getResourceInfos());
097
098        setContent(getContent());
099        m_okButton = new Button(CmsVaadinUtils.getMessageText(org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_OK_0));
100        m_cancelButton = new Button(
101            CmsVaadinUtils.getMessageText(org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_CANCEL_0));
102        addButton(m_okButton);
103        addButton(m_cancelButton);
104        //Set Clicklistener
105        m_cancelButton.addClickListener(new ClickListener() {
106
107            private static final long serialVersionUID = -5769891739879269176L;
108
109            public void buttonClick(ClickEvent event) {
110
111                m_manager.closeDialogWindow(false);
112            }
113        });
114        m_okButton.addClickListener(new ClickListener() {
115
116            private static final long serialVersionUID = 6932464669055039855L;
117
118            public void buttonClick(ClickEvent event) {
119
120                submit();
121                m_manager.closeDialogWindow(true);
122            }
123        });
124    }
125
126    /**
127     * Creates content of dialog containing CheckBox if resources should be deleted and a messages.<p>
128     *
129     * @return vertical layout component.
130     */
131    protected VerticalLayout getContent() {
132
133        String message;
134
135        if (m_sitesToDelete.size() == 1) {
136            message = CmsVaadinUtils.getMessageText(
137                Messages.GUI_SITE_CONFIRM_DELETE_SITE_1,
138                CmsEncoder.escapeXml(m_sitesToDelete.get(0).getTitle()));
139        } else {
140            message = "";
141            for (CmsSite site : m_sitesToDelete) {
142                if (message.length() > 0) {
143                    message += ", ";
144                }
145                message += CmsEncoder.escapeXml(site.getTitle());
146            }
147            message = CmsVaadinUtils.getMessageText(Messages.GUI_SITE_CONFIRM_DELETE_SITES_1, message);
148        }
149
150        VerticalLayout layout = new VerticalLayout();
151
152        m_deleteResources = new CheckBox();
153        m_deleteResources.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_SITE_DELETE_RESOURCES_0));
154        m_deleteResources.setDescription(CmsVaadinUtils.getMessageText(Messages.GUI_SITE_DELETE_RESOURCES_HELP_0));
155
156        layout.addComponent(m_deleteResources);
157
158        Label label = new Label();
159        label.setContentMode(ContentMode.HTML);
160        label.setValue(message);
161
162        layout.addComponent(label);
163        return layout;
164    }
165
166    /**
167     * delete sites.<p>
168     */
169    protected void submit() {
170
171        List<String> siteRootsToDelete = new ArrayList<String>();
172        for (CmsSite site : m_sitesToDelete) {
173
174            String currentSite = A_CmsUI.getCmsObject().getRequestContext().getSiteRoot();
175            if (currentSite.equals(site.getSiteRoot())) {
176                A_CmsUI.getCmsObject().getRequestContext().setSiteRoot("");
177            }
178            siteRootsToDelete.add(site.getSiteRoot());
179        }
180        m_manager.deleteElements(siteRootsToDelete);
181        if (m_deleteResources.getValue().booleanValue()) {
182            for (CmsSite site : m_sitesToDelete) {
183                try {
184                    m_manager.getRootCmsObject().lockResource(site.getSiteRoot());
185                } catch (CmsException e) {
186                    LOG.error("unable to lock resource");
187                }
188                try {
189                    m_manager.getRootCmsObject().deleteResource(
190                        site.getSiteRoot(),
191                        CmsResource.DELETE_PRESERVE_SIBLINGS);
192                    try {
193                        m_manager.getRootCmsObject().unlockResource(site.getSiteRoot());
194                    } catch (CmsLockException e) {
195                        LOG.info("Unlock failed.", e);
196                    }
197                } catch (CmsException e) {
198                    //ok, resource was not published and can not be unlocked anymore..
199                }
200            }
201        }
202    }
203
204    /**
205     * Returns a list of CmsResourceInfo objects.<p>
206     *
207     * @return list of cmsresourceinfo.
208     */
209    private List<CmsResourceInfo> getResourceInfos() {
210
211        List<CmsResourceInfo> infos = new ArrayList<CmsResourceInfo>();
212        for (CmsSite site : m_sitesToDelete) {
213            infos.add(
214                new CmsResourceInfo(site.getTitle(), site.getSiteRoot(), m_manager.getFavIcon(site.getSiteRoot())));
215        }
216        return infos;
217    }
218}