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.dialogs;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.lock.CmsLockActionRecord;
033import org.opencms.lock.CmsLockActionRecord.LockChange;
034import org.opencms.lock.CmsLockException;
035import org.opencms.lock.CmsLockUtil;
036import org.opencms.main.CmsException;
037import org.opencms.main.CmsLog;
038import org.opencms.main.OpenCms;
039import org.opencms.ui.A_CmsUI;
040import org.opencms.ui.CmsVaadinUtils;
041import org.opencms.ui.I_CmsDialogContext;
042import org.opencms.ui.components.CmsBasicDialog;
043import org.opencms.ui.components.CmsOkCancelActionHandler;
044import org.opencms.util.CmsUUID;
045
046import java.util.ArrayList;
047import java.util.List;
048
049import org.apache.commons.logging.Log;
050
051import com.vaadin.ui.Button;
052import com.vaadin.ui.Button.ClickEvent;
053import com.vaadin.ui.Button.ClickListener;
054
055/**
056 * Dialog used to change resource modification times.<p>
057 */
058public class CmsUndeleteDialog extends CmsBasicDialog {
059
060    /** Logger instance for this class. */
061    private static final Log LOG = CmsLog.getLog(CmsUndeleteDialog.class);
062
063    /** Serial version id. */
064    private static final long serialVersionUID = 1L;
065
066    /** The dialog context. */
067    protected I_CmsDialogContext m_context;
068
069    /** The Cancel button. */
070    private Button m_cancelButton;
071
072    /** The OK  button. */
073    private Button m_okButton;
074
075    /**
076     * Creates a new instance.<p>
077     *
078     * @param context the dialog context
079     */
080    public CmsUndeleteDialog(I_CmsDialogContext context) {
081
082        m_context = context;
083        CmsVaadinUtils.readAndLocalizeDesign(
084            this,
085            OpenCms.getWorkplaceManager().getMessages(A_CmsUI.get().getLocale()),
086            null);
087
088        m_cancelButton.addClickListener(new ClickListener() {
089
090            private static final long serialVersionUID = 1L;
091
092            public void buttonClick(ClickEvent event) {
093
094                cancel();
095            }
096
097        });
098
099        m_okButton.addClickListener(new ClickListener() {
100
101            private static final long serialVersionUID = 1L;
102
103            public void buttonClick(ClickEvent event) {
104
105                submit();
106            }
107        });
108        setActionHandler(new CmsOkCancelActionHandler() {
109
110            private static final long serialVersionUID = 1L;
111
112            @Override
113            protected void cancel() {
114
115                CmsUndeleteDialog.this.cancel();
116            }
117
118            @Override
119            protected void ok() {
120
121                submit();
122            }
123        });
124    }
125
126    /**
127     * Undeletes the selected files
128     *
129     * @return the ids of the modified resources
130     *
131     * @throws CmsException if something goes wrong
132     */
133    protected List<CmsUUID> undelete() throws CmsException {
134
135        List<CmsUUID> modifiedResources = new ArrayList<CmsUUID>();
136        CmsObject cms = m_context.getCms();
137        for (CmsResource resource : m_context.getResources()) {
138            CmsLockActionRecord actionRecord = null;
139            try {
140                actionRecord = CmsLockUtil.ensureLock(m_context.getCms(), resource);
141                cms.undeleteResource(cms.getSitePath(resource), true);
142                modifiedResources.add(resource.getStructureId());
143            } finally {
144                if ((actionRecord != null) && (actionRecord.getChange() == LockChange.locked)) {
145
146                    try {
147                        cms.unlockResource(resource);
148                    } catch (CmsLockException e) {
149                        LOG.warn(e.getLocalizedMessage(), e);
150                    }
151                }
152            }
153        }
154        return modifiedResources;
155    }
156
157    /**
158     * Cancels the dialog.<p>
159     */
160    void cancel() {
161
162        m_context.finish(new ArrayList<CmsUUID>());
163    }
164
165    /**
166     * Submits the dialog.<p>
167     */
168    void submit() {
169
170        try {
171            List<CmsUUID> modifiedResources = undelete();
172            m_context.finish(modifiedResources);
173        } catch (Exception e) {
174            m_context.error(e);
175        }
176    }
177
178}