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.file.history.I_CmsHistoryResource;
033import org.opencms.file.types.CmsResourceTypeUnknownFile;
034import org.opencms.file.types.CmsResourceTypeUnknownFolder;
035import org.opencms.file.types.I_CmsResourceType;
036import org.opencms.gwt.CmsVfsService;
037import org.opencms.main.CmsException;
038import org.opencms.main.OpenCms;
039import org.opencms.ui.CmsVaadinUtils;
040import org.opencms.ui.I_CmsDialogContext;
041import org.opencms.ui.components.CmsBasicDialog;
042import org.opencms.ui.components.CmsOkCancelActionHandler;
043import org.opencms.ui.components.CmsResourceInfo;
044import org.opencms.util.CmsUUID;
045import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
046import org.opencms.workplace.explorer.CmsResourceUtil;
047
048import java.util.ArrayList;
049import java.util.Collections;
050import java.util.Comparator;
051import java.util.List;
052
053import com.google.common.collect.Lists;
054import com.vaadin.v7.data.Property.ValueChangeEvent;
055import com.vaadin.v7.data.Property.ValueChangeListener;
056import com.vaadin.v7.data.util.IndexedContainer;
057import com.vaadin.ui.AbstractOrderedLayout;
058import com.vaadin.ui.Alignment;
059import com.vaadin.ui.Button;
060import com.vaadin.ui.Button.ClickEvent;
061import com.vaadin.ui.Button.ClickListener;
062import com.vaadin.v7.ui.CheckBox;
063import com.vaadin.v7.ui.HorizontalLayout;
064import com.vaadin.v7.ui.Label;
065
066/**
067 * Dialog for restoring deleted resources in a folder.<p>
068 */
069public class CmsRestoreDeletedDialog extends CmsBasicDialog {
070
071    /** Property for storing selection status. */
072    private static final String PROP_SELECTED = "selected";
073
074    /** Serial version id. */
075    private static final long serialVersionUID = 1L;
076
077    /** The cancel button. */
078    private Button m_cancelButton;
079
080    /** The box containing the widgets representing the deleted resources. */
081    private AbstractOrderedLayout m_deletedResourceContainer;
082
083    /** The dialog context. */
084    private I_CmsDialogContext m_dialogContext;
085
086    /** Checkbox for including subfolders. */
087    private CheckBox m_includeSubfoldersField;
088
089    /** The OK button. */
090    private Button m_okButton;
091
092    /** The resource. */
093    private CmsResource m_resource;
094
095    /** Check box to select all resources. */
096    private CheckBox m_selectAllField;
097
098    /** Data model for check boxes / selection. */
099    private IndexedContainer m_selectionContainer;
100
101    /**
102     * Creates a new instance.<p>
103     *
104     * @param context the dialog context
105     * @throws CmsException if something goes wrong
106     */
107    public CmsRestoreDeletedDialog(I_CmsDialogContext context)
108    throws CmsException {
109        m_dialogContext = context;
110        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
111        m_resource = context.getResources().get(0);
112        CmsObject cms = context.getCms();
113        List<I_CmsHistoryResource> deletedResources = cms.readDeletedResources(
114            cms.getSitePath(m_resource),
115            m_includeSubfoldersField.getValue().booleanValue());
116        initDeletedResources(cms, deletedResources);
117        m_cancelButton.addClickListener(new ClickListener() {
118
119            private static final long serialVersionUID = 1L;
120
121            public void buttonClick(ClickEvent event) {
122
123                cancel();
124            }
125        });
126
127        m_includeSubfoldersField.addValueChangeListener(new ValueChangeListener() {
128
129            private static final long serialVersionUID = 1L;
130
131            public void valueChange(ValueChangeEvent event) {
132
133                onSubFolderChange((Boolean)event.getProperty().getValue());
134            }
135        });
136
137        m_selectAllField.addValueChangeListener(new ValueChangeListener() {
138
139            private static final long serialVersionUID = 1L;
140
141            public void valueChange(ValueChangeEvent event) {
142
143                onSelectAllChange((Boolean)(event.getProperty().getValue()));
144            }
145        });
146        m_okButton.addClickListener(new ClickListener() {
147
148            private static final long serialVersionUID = 1L;
149
150            public void buttonClick(ClickEvent event) {
151
152                submit();
153            }
154        });
155
156        setActionHandler(new CmsOkCancelActionHandler() {
157
158            private static final long serialVersionUID = 1L;
159
160            @Override
161            protected void cancel() {
162
163                CmsRestoreDeletedDialog.this.cancel();
164            }
165
166            @Override
167            protected void ok() {
168
169                submit();
170            }
171        });
172    }
173
174    /**
175     * Gets the ids of the selected resources.<p>
176     *
177     * @return the ids of the selected resources
178     */
179    public List<CmsUUID> getSelectedIds() {
180
181        List<?> itemIds = m_selectionContainer.getItemIds();
182        List<CmsUUID> result = Lists.newArrayList();
183        for (Object itemId : itemIds) {
184            CmsUUID structureId = (CmsUUID)itemId;
185            Boolean value = (Boolean)(m_selectionContainer.getItem(itemId).getItemProperty(PROP_SELECTED).getValue());
186            if (value.booleanValue()) {
187                result.add(structureId);
188            }
189        }
190        return result;
191    }
192
193    /**
194     * Cancels the dialog.<p>
195     */
196    void cancel() {
197
198        m_dialogContext.finish(new ArrayList<CmsUUID>());
199    }
200
201    /**
202     * Called on select all change.<p>
203     *
204     * @param value the new value
205     */
206    void onSelectAllChange(Boolean value) {
207
208        for (Object id : m_selectionContainer.getItemIds()) {
209            m_selectionContainer.getItem(id).getItemProperty(PROP_SELECTED).setValue(value);
210        }
211    }
212
213    /**
214     * Called on include sub folders change.<p>
215     *
216     * @param value the new value
217     */
218    void onSubFolderChange(Boolean value) {
219
220        List<I_CmsHistoryResource> historyResources;
221        try {
222            CmsObject cms = m_dialogContext.getCms();
223            historyResources = cms.readDeletedResources(cms.getSitePath(m_resource), value.booleanValue());
224            initDeletedResources(cms, historyResources);
225        } catch (CmsException e) {
226            m_dialogContext.error(e);
227        }
228    }
229
230    /**
231     * Submits the dialog.<p>
232     */
233    void submit() {
234
235        List<CmsUUID> selectedIds = getSelectedIds();
236        List<CmsUUID> updated = Lists.newArrayList();
237        CmsObject cms = m_dialogContext.getCms();
238        try {
239            for (CmsUUID selectedId : selectedIds) {
240                cms.restoreDeletedResource(selectedId);
241                updated.add(selectedId);
242            }
243            m_dialogContext.finish(updated);
244        } catch (CmsException e) {
245            m_dialogContext.error(e);
246        }
247    }
248
249    /**
250     * Fills the list of resources to select from.<p>
251     *
252     * @param cms the cms context
253     * @param deletedResources the deleted resources
254     *
255     * @throws CmsException if something goes wrong
256     */
257    private void initDeletedResources(CmsObject cms, List<I_CmsHistoryResource> deletedResources) throws CmsException {
258
259        Collections.sort(deletedResources, new Comparator<I_CmsHistoryResource>() {
260
261            public int compare(I_CmsHistoryResource first, I_CmsHistoryResource second) {
262
263                return first.getRootPath().compareTo(second.getRootPath());
264            }
265        });
266        m_deletedResourceContainer.removeAllComponents();
267        m_selectionContainer = new IndexedContainer();
268        m_selectionContainer.addContainerProperty(PROP_SELECTED, Boolean.class, Boolean.FALSE);
269        m_okButton.setEnabled(!deletedResources.isEmpty());
270        if (deletedResources.isEmpty()) {
271            m_deletedResourceContainer.addComponent(
272                new Label(CmsVaadinUtils.getMessageText(org.opencms.workplace.list.Messages.GUI_LIST_EMPTY_0)));
273
274        }
275        for (I_CmsHistoryResource deleted : deletedResources) {
276            I_CmsResourceType resType = OpenCms.getResourceManager().getResourceType(deleted.getTypeId());
277            String typeName = resType.getTypeName();
278            CmsExplorerTypeSettings explorerType = OpenCms.getWorkplaceManager().getExplorerTypeSetting(typeName);
279            String title = cms.getRequestContext().removeSiteRoot(deleted.getRootPath());
280
281            String subtitle = CmsVaadinUtils.getMessageText(
282                org.opencms.ui.Messages.GUI_RESTOREDELETED_DATE_VERSION_2,
283                CmsVfsService.formatDateTime(cms, deleted.getDateLastModified()),
284                "" + deleted.getVersion());
285            if (explorerType == null) {
286                explorerType = OpenCms.getWorkplaceManager().getExplorerTypeSetting(
287                    deleted.isFile()
288                    ? CmsResourceTypeUnknownFile.RESOURCE_TYPE_NAME
289                    : CmsResourceTypeUnknownFolder.RESOURCE_TYPE_NAME);
290            }
291            CmsResourceInfo info = new CmsResourceInfo(
292                title,
293                subtitle,
294                CmsResourceUtil.getBigIconResource(explorerType, deleted.getName()));
295            info.setWidth("100%");
296            HorizontalLayout hl = new HorizontalLayout();
297            hl.setWidth("100%");
298            CheckBox checkbox = new CheckBox();
299            hl.addComponent(checkbox);
300            hl.addComponent(info);
301            hl.setExpandRatio(info, 1);
302            hl.setComponentAlignment(checkbox, Alignment.MIDDLE_LEFT);
303            m_selectionContainer.addItem(deleted.getStructureId());
304            checkbox.setPropertyDataSource(
305                m_selectionContainer.getItem(deleted.getStructureId()).getItemProperty(PROP_SELECTED));
306            m_deletedResourceContainer.addComponent(hl);
307        }
308
309    }
310
311}