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