001/*
002 * (C) Copyright 2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 * Contributors:
014 * Nuxeo - initial API and implementation
015 */
016package org.nuxeo.ecm.platform.rendition.publisher;
017
018import static org.nuxeo.ecm.platform.rendition.Constants.RENDITION_SOURCE_VERSIONABLE_ID_PROPERTY;
019
020import org.nuxeo.ecm.core.api.CoreSession;
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.api.IdRef;
023import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
024
025/**
026 * Fetched the live doc for a given proxy on a Rendition
027 * 
028 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
029 */
030public class RenditionLiveDocFetcher extends UnrestrictedSessionRunner {
031
032    protected final DocumentModel proxy;
033
034    protected final String sid;
035
036    protected DocumentModel liveDocument;
037
038    protected RenditionLiveDocFetcher(CoreSession session, DocumentModel source) {
039        super(session);
040        this.proxy = source;
041        this.sid = session.getSessionId();
042    }
043
044    @Override
045    public void run() {
046
047        String targetUUID = (String) proxy.getPropertyValue(RENDITION_SOURCE_VERSIONABLE_ID_PROPERTY);
048        liveDocument = session.getDocument(new IdRef(targetUUID));
049        liveDocument.detach(true);
050        liveDocument.attach(sid);
051    }
052
053    public DocumentModel getLiveDocument() {
054        return liveDocument;
055    }
056
057}