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.pub;
020
021import java.util.Map;
022
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
026import org.nuxeo.ecm.core.api.VersioningOption;
027import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
028import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
029import org.nuxeo.ecm.platform.publisher.impl.core.SimpleCorePublishedDocument;
030import org.nuxeo.ecm.platform.publisher.task.CoreProxyWithWorkflowFactory;
031import org.nuxeo.snapshot.Snapshot;
032import org.nuxeo.snapshot.Snapshotable;
033
034public class FolderishProxyFactory extends CoreProxyWithWorkflowFactory {
035
036    protected DocumentModel subPublish(CoreSession session, DocumentModel parentProxy, Snapshot tree, boolean skipParent)
037            {
038
039        DocumentModel newFolderishProxy = null;
040        if (skipParent) {
041            newFolderishProxy = parentProxy;
042        } else {
043            DocumentModel version = tree.getDocument();
044            newFolderishProxy = session.createProxy(version.getRef(), parentProxy.getRef());
045        }
046
047        for (Snapshot snap : tree.getChildrenSnapshots()) {
048            subPublish(session, newFolderishProxy, snap, false);
049        }
050
051        return newFolderishProxy;
052    }
053
054    @Override
055    public PublishedDocument publishDocument(DocumentModel doc, PublicationNode targetNode, Map<String, String> params)
056            {
057
058        Snapshot snapshot = null;
059
060        if (doc.isFolder()) {
061            Snapshotable snapshotable = doc.getAdapter(Snapshotable.class);
062            if (snapshotable != null) {
063                snapshot = snapshotable.createSnapshot(VersioningOption.MINOR);
064            }
065        }
066
067        PublishedDocument result = super.publishDocument(doc, targetNode, params);
068
069        if (snapshot != null) {
070
071            final Snapshot tree = snapshot;
072            final DocumentModel parent = ((SimpleCorePublishedDocument) result).getProxy();
073
074            UnrestrictedSessionRunner runner = new UnrestrictedSessionRunner(doc.getCoreSession()) {
075                @Override
076                public void run() {
077                    // force cleanup of the tree !!!
078                    session.removeChildren(parent.getRef());
079                    subPublish(session, parent, tree, true);
080                }
081            };
082            runner.runUnrestricted();
083        }
084
085        return result;
086
087    }
088}