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