001/*
002 * (C) Copyright 2015 Nuxeo SA (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-2.1.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 *
014 * Contributors:
015 *     Nuxeo
016 */
017
018package org.nuxeo.ecm.showcase.content;
019
020import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.CTX_FORCE_VIEWS_GENERATION;
021import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_FACET;
022import static org.nuxeo.ecm.platform.video.VideoConstants.CTX_FORCE_INFORMATIONS_GENERATION;
023import static org.nuxeo.ecm.platform.video.VideoConstants.VIDEO_FACET;
024
025import java.util.Collections;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.dom4j.Element;
030import org.nuxeo.common.collections.ScopeType;
031import org.nuxeo.common.utils.Path;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.PathRef;
035import org.nuxeo.ecm.core.api.impl.DocumentModelImpl;
036import org.nuxeo.ecm.core.io.ExportConstants;
037import org.nuxeo.ecm.core.io.ExportedDocument;
038import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter;
039import org.nuxeo.ecm.core.versioning.VersioningService;
040
041/**
042 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
043 * @since 7.10
044 */
045public class ShowcaseWriter extends DocumentModelWriter {
046
047    private static final Log log = LogFactory.getLog(ShowcaseWriter.class);
048
049    public ShowcaseWriter(CoreSession session, String parentPath, int saveInterval) {
050        super(session, parentPath, saveInterval);
051    }
052
053    /**
054     * Import a new document given its path keeping his id
055     * <p>
056     * The parent of this document is assumed to exist.
057     *
058     * @param xdoc the document containing
059     * @param toPath the path of the doc to create
060     */
061    protected DocumentModel createDocument(ExportedDocument xdoc, Path toPath) {
062        Path parentPath = toPath.removeLastSegments(1);
063        String name = toPath.lastSegment();
064
065        DocumentModel doc = new DocumentModelImpl(null, xdoc.getType(), xdoc.getId(), toPath, null, null, new PathRef(
066                parentPath.toString()), null, null, null, null);
067
068        // set lifecycle state at creation
069        Element system = xdoc.getDocument().getRootElement().element(ExportConstants.SYSTEM_TAG);
070        String lifeCycleState = system.element(ExportConstants.LIFECYCLE_STATE_TAG).getText();
071        doc.putContextData(CoreSession.IMPORT_LIFECYCLE_STATE, lifeCycleState);
072        String lifeCyclePolicy = system.element(ExportConstants.LIFECYCLE_POLICY_TAG).getText();
073        doc.putContextData(CoreSession.IMPORT_LIFECYCLE_POLICY, lifeCyclePolicy);
074
075        // loadFacets before schemas so that additional schemas are not skipped
076        loadFacetsInfo(doc, xdoc.getDocument());
077
078        // then load schemas data
079        loadSchemas(xdoc, doc, xdoc.getDocument());
080
081        if (doc.hasSchema("uid")) {
082            doc.putContextData(ScopeType.REQUEST, VersioningService.SKIP_VERSIONING, true);
083        }
084
085        // XXX Not used, as we override the listener; but it is the right way to force video informations generation.
086        if (doc.hasFacet(VIDEO_FACET)) {
087            doc.putContextData(CTX_FORCE_INFORMATIONS_GENERATION, true);
088        }
089
090        if (doc.hasFacet(PICTURE_FACET)) {
091            doc.putContextData(CTX_FORCE_VIEWS_GENERATION, true);
092        }
093
094        session.importDocuments(Collections.singletonList(doc));
095
096        // load into the document the system properties, document needs to exist
097        loadSystemInfo(doc, xdoc.getDocument());
098
099        unsavedDocuments += 1;
100        saveIfNeeded();
101
102        return doc;
103    }
104}