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