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