001/*
002 * Copyright (c) 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 */
013package org.nuxeo.ecm.core.io.impl.extensions;
014
015import static org.nuxeo.ecm.core.api.CoreSession.IMPORT_VERSION_CREATED;
016import static org.nuxeo.ecm.core.api.CoreSession.IMPORT_VERSION_DESCRIPTION;
017import static org.nuxeo.ecm.core.api.CoreSession.IMPORT_VERSION_LABEL;
018import static org.nuxeo.ecm.core.api.CoreSession.IMPORT_VERSION_VERSIONABLE_ID;
019
020import java.util.List;
021
022import org.dom4j.Element;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.VersionModel;
025import org.nuxeo.ecm.core.io.ExportExtension;
026import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl;
027import org.nuxeo.ecm.core.schema.types.primitives.DateType;
028
029/**
030 * Exports version information for a given {@link DocumentModel}
031 *
032 * @since 7.4
033 */
034public class VersionInfoExportExtension implements ExportExtension {
035
036    @Override
037    public void updateExport(DocumentModel docModel, ExportedDocumentImpl result) throws Exception {
038
039        Element versionElement = result.getDocument().getRootElement().addElement("version");
040
041        if (docModel.isVersion()) {
042            // IMPORT_VERSION_LABEL
043            versionElement.addElement("isVersion").setText("true");
044            ;
045            versionElement.addElement(IMPORT_VERSION_LABEL.substring(4)).setText(docModel.getVersionLabel());
046
047            // IMPORT_VERSION_VERSIONABLE_ID
048            String sourceId = docModel.getSourceId();
049            versionElement.addElement(IMPORT_VERSION_VERSIONABLE_ID.substring(4)).setText(sourceId);
050            DocumentModel liveDocument = docModel.getCoreSession().getSourceDocument(docModel.getRef());
051
052            List<VersionModel> versions = docModel.getCoreSession().getVersionsForDocument(liveDocument.getRef());
053            for (VersionModel version : versions) {
054                if (!docModel.getVersionLabel().equals(version.getLabel())) {
055                    continue;
056                }
057                // IMPORT_VERSION_DESCRIPTION
058                String description = version.getDescription();
059                if (description != null) {
060                    versionElement.addElement(IMPORT_VERSION_DESCRIPTION.substring(4)).setText(description);
061                }
062
063                // IMPORT_VERSION_CREATED
064                if (version.getCreated() != null) {
065                    String created = new DateType().encode(version.getCreated());
066                    versionElement.addElement(IMPORT_VERSION_CREATED.substring(4)).setText(created);
067                }
068                break;
069            }
070        }
071    }
072}