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.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        m_context = context;
082        CmsVaadinUtils.readAndLocalizeDesign(
083            this,
084            OpenCms.getWorkplaceManager().getMessages(A_CmsUI.get().getLocale()),
085            null);
086
087        m_cancelButton.addClickListener(new ClickListener() {
088
089            private static final long serialVersionUID = 1L;
090
091            public void buttonClick(ClickEvent event) {
092
093                cancel();
094            }
095
096        });
097
098        m_okButton.addClickListener(new ClickListener() {
099
100            private static final long serialVersionUID = 1L;
101
102            public void buttonClick(ClickEvent event) {
103
104                submit();
105            }
106        });
107        setActionHandler(new CmsOkCancelActionHandler() {
108
109            private static final long serialVersionUID = 1L;
110
111            @Override
112            protected void cancel() {
113
114                CmsUndeleteDialog.this.cancel();
115            }
116
117            @Override
118            protected void ok() {
119
120                submit();
121            }
122        });
123    }
124
125    /**
126     * Undeletes the selected files
127     *
128     * @return the ids of the modified resources
129     *
130     * @throws CmsException if something goes wrong
131     */
132    protected List<CmsUUID> undelete() throws CmsException {
133
134        List<CmsUUID> modifiedResources = new ArrayList<CmsUUID>();
135        CmsObject cms = m_context.getCms();
136        for (CmsResource resource : m_context.getResources()) {
137            CmsLockActionRecord actionRecord = null;
138            try {
139                actionRecord = CmsLockUtil.ensureLock(m_context.getCms(), resource);
140                cms.undeleteResource(cms.getSitePath(resource), true);
141                modifiedResources.add(resource.getStructureId());
142            } finally {
143                if ((actionRecord != null) && (actionRecord.getChange() == LockChange.locked)) {
144
145                    try {
146                        cms.unlockResource(resource);
147                    } catch (CmsLockException e) {
148                        LOG.warn(e.getLocalizedMessage(), e);
149                    }
150                }
151            }
152        }
153        return modifiedResources;
154    }
155
156    /**
157     * Cancels the dialog.<p>
158     */
159    void cancel() {
160
161        m_context.finish(new ArrayList<CmsUUID>());
162    }
163
164    /**
165     * Submits the dialog.<p>
166     */
167    void submit() {
168
169        try {
170            List<CmsUUID> modifiedResources = undelete();
171            m_context.finish(modifiedResources);
172        } catch (Exception e) {
173            m_context.error(e);
174        }
175    }
176
177}