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.gwt.client.ui;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.Messages;
032import org.opencms.gwt.client.rpc.CmsRpcAction;
033import org.opencms.gwt.shared.CmsDeleteResourceBean;
034import org.opencms.util.CmsUUID;
035
036import com.google.gwt.user.client.Command;
037
038/**
039 * A dialog which informs the user that deleting a resource will break links
040 * from other resources.<p>
041 *
042 * @since 8.0.0
043 */
044public class CmsDeleteWarningDialog extends CmsConfirmDialog {
045
046    /** Text metrics key. */
047    private static final String TM_DIALOG_LIST = "dialogList";
048
049    /** The callback command. */
050    protected Command m_cmd;
051
052    /** The content of the dialog. */
053    protected CmsLinkWarningPanel m_content = new CmsLinkWarningPanel();
054
055    /** The site path of the resource to delete. */
056    private String m_sitePath;
057
058    /** The structure id of the resource to delete. */
059    private CmsUUID m_structureId;
060
061    /**
062     * Constructor.<p>
063     *
064     * @param structureId the structure id of the resource going to be deleted
065     */
066    public CmsDeleteWarningDialog(CmsUUID structureId) {
067
068        this();
069        m_structureId = structureId;
070    }
071
072    /**
073     * Constructor.<p>
074     *
075     * @param sitePath the site-path of the resource going to be deleted
076     */
077    public CmsDeleteWarningDialog(String sitePath) {
078
079        this();
080        m_sitePath = sitePath;
081    }
082
083    /**
084     * Constructor.<p>
085     */
086    private CmsDeleteWarningDialog() {
087
088        super(Messages.get().key(Messages.GUI_DIALOG_DELETE_TITLE_0));
089        setWarningMessage(Messages.get().key(Messages.GUI_DIALOG_DELETE_TEXT_0));
090        setOkText(Messages.get().key(Messages.GUI_DELETE_0));
091        setCloseText(Messages.get().key(Messages.GUI_CANCEL_0));
092        setHandler(new I_CmsConfirmDialogHandler() {
093
094            /**
095             * @see org.opencms.gwt.client.ui.I_CmsCloseDialogHandler#onClose()
096             */
097            public void onClose() {
098
099                // do nothing
100            }
101
102            /**
103             * @see org.opencms.gwt.client.ui.I_CmsConfirmDialogHandler#onOk()
104             */
105            public void onOk() {
106
107                deleteResource();
108
109                // execute the callback if present
110                if (m_cmd != null) {
111                    m_cmd.execute();
112                }
113            }
114        });
115    }
116
117    /**
118     * Loads and shows the delete dialog.<p>
119     *
120     * @param callback the callback that is executed when the resource was deleted (can be <code>null</code>)
121     */
122    public void loadAndShow(Command callback) {
123
124        m_cmd = callback;
125        checkBrokenLinks();
126    }
127
128    /**
129     * Deletes a resource from the vfs.<p>
130     */
131    protected void deleteResource() {
132
133        if (m_sitePath != null) {
134            deleteResource(m_sitePath);
135        } else if (m_structureId != null) {
136            deleteResource(m_structureId);
137        }
138    }
139
140    /**
141     * Displays the broken links information.<p>
142     *
143     * @param brokenLinks the broken links information
144     */
145    protected void displayBrokenLinks(CmsDeleteResourceBean brokenLinks) {
146
147        CmsListItemWidget widget = new CmsListItemWidget(brokenLinks.getPageInfo());
148        widget.truncate(TM_DIALOG_LIST, 370);
149        addTopWidget(widget);
150        if (brokenLinks.getBrokenLinks().size() > 0) {
151            m_content.fill(brokenLinks.getBrokenLinks());
152            addBottomWidget(m_content);
153        }
154        center();
155    }
156
157    /**
158     * Method which should be called after the deletion has been performed.<p>
159     */
160    protected void onAfterDeletion() {
161
162        // do nothing
163
164    }
165
166    /**
167     * Checks for broken links, ask for confirmation and finally deletes the given resource.<p>
168     */
169    private void checkBrokenLinks() {
170
171        if (m_sitePath != null) {
172            final String sitePath = m_sitePath;
173
174            CmsRpcAction<CmsDeleteResourceBean> action = new CmsRpcAction<CmsDeleteResourceBean>() {
175
176                /**
177                 * @see org.opencms.gwt.client.rpc.CmsRpcAction#execute()
178                 */
179                @Override
180                public void execute() {
181
182                    start(0, true);
183
184                    CmsCoreProvider.getVfsService().getBrokenLinks(sitePath, this);
185                }
186
187                /**
188                 * @see org.opencms.gwt.client.rpc.CmsRpcAction#onResponse(java.lang.Object)
189                 */
190                @Override
191                protected void onResponse(CmsDeleteResourceBean result) {
192
193                    stop(false);
194                    displayBrokenLinks(result);
195                }
196            };
197
198            action.execute();
199        } else if (m_structureId != null) {
200            final CmsUUID structureId = m_structureId;
201
202            CmsRpcAction<CmsDeleteResourceBean> action = new CmsRpcAction<CmsDeleteResourceBean>() {
203
204                /**
205                 * @see org.opencms.gwt.client.rpc.CmsRpcAction#execute()
206                 */
207                @Override
208                public void execute() {
209
210                    start(0, true);
211
212                    CmsCoreProvider.getVfsService().getBrokenLinks(structureId, this);
213                }
214
215                /**
216                 * @see org.opencms.gwt.client.rpc.CmsRpcAction#onResponse(java.lang.Object)
217                 */
218                @Override
219                protected void onResponse(CmsDeleteResourceBean result) {
220
221                    stop(false);
222                    displayBrokenLinks(result);
223                }
224            };
225
226            action.execute();
227        }
228    }
229
230    /**
231     * Deletes a resource from the vfs.<p>
232     *
233     * @param structureId the resource structure id
234     */
235    private void deleteResource(final CmsUUID structureId) {
236
237        CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
238
239            /**
240             * @see org.opencms.gwt.client.rpc.CmsRpcAction#execute()
241             */
242            @Override
243            public void execute() {
244
245                CmsCoreProvider.getVfsService().deleteResource(structureId, this);
246
247            }
248
249            /**
250             * @see org.opencms.gwt.client.rpc.CmsRpcAction#onResponse(java.lang.Object)
251             */
252            @Override
253            protected void onResponse(Void result) {
254
255                onAfterDeletion();
256            }
257        };
258        action.execute();
259    }
260
261    /**
262     * Deletes a resource from the vfs.<p>
263     *
264     * @param sitePath the resource site path
265     */
266    private void deleteResource(final String sitePath) {
267
268        CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
269
270            /**
271             * @see org.opencms.gwt.client.rpc.CmsRpcAction#execute()
272             */
273            @Override
274            public void execute() {
275
276                CmsCoreProvider.getVfsService().deleteResource(sitePath, this);
277
278            }
279
280            /**
281             * @see org.opencms.gwt.client.rpc.CmsRpcAction#onResponse(java.lang.Object)
282             */
283            @Override
284            protected void onResponse(Void result) {
285
286                onAfterDeletion();
287            }
288        };
289        action.execute();
290    }
291
292}