001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.snapshot.bean;
020
021import java.io.Serializable;
022
023import org.jboss.seam.ScopeType;
024import org.jboss.seam.annotations.In;
025import org.jboss.seam.annotations.Name;
026import org.jboss.seam.annotations.Scope;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
031import org.nuxeo.ecm.webapp.helpers.EventManager;
032import org.nuxeo.ecm.webapp.versioning.VersionedActions;
033import org.nuxeo.snapshot.Snapshot;
034
035@Name("vFolderActions")
036@Scope(ScopeType.PAGE)
037public class VFolderActionBean implements Serializable {
038
039    private static final long serialVersionUID = 1L;
040
041    @In(create = true)
042    protected VersionedActions versionedActions;
043
044    @In(create = true)
045    protected transient NavigationContext navigationContext;
046
047    @In(create = true, required = false)
048    protected transient CoreSession documentManager;
049
050    public String restoreToVersion() {
051        String vuuid = versionedActions.getSelectedVersionId();
052        if (vuuid != null) {
053
054            DocumentModel vfolder = documentManager.getDocument(new IdRef(vuuid));
055            DocumentModel livefolder = documentManager.getDocument(new IdRef(vfolder.getVersionSeriesId()));
056
057            Snapshot snap = livefolder.getAdapter(Snapshot.class);
058            DocumentModel restoredFolder = snap.restore(vfolder.getVersionLabel());
059
060            documentManager.save();
061
062            EventManager.raiseEventsOnDocumentChange(restoredFolder);
063            return navigationContext.navigateToDocument(restoredFolder, "after-edit");
064        }
065        return null;
066    }
067
068}