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 versionElement.addElement(IMPORT_VERSION_LABEL.substring(4)).setText(docModel.getVersionLabel()); 045 046 // IMPORT_VERSION_VERSIONABLE_ID 047 String sourceId = docModel.getSourceId(); 048 versionElement.addElement(IMPORT_VERSION_VERSIONABLE_ID.substring(4)).setText(sourceId); 049 DocumentModel liveDocument = docModel.getCoreSession().getSourceDocument(docModel.getRef()); 050 051 List<VersionModel> versions = docModel.getCoreSession().getVersionsForDocument(liveDocument.getRef()); 052 for (VersionModel version : versions) { 053 if (!docModel.getVersionLabel().equals(version.getLabel())) { 054 continue; 055 } 056 // IMPORT_VERSION_DESCRIPTION 057 String description = version.getDescription(); 058 if (description != null) { 059 versionElement.addElement(IMPORT_VERSION_DESCRIPTION.substring(4)).setText(description); 060 } 061 062 // IMPORT_VERSION_CREATED 063 if (version.getCreated() != null) { 064 String created = new DateType().encode(version.getCreated()); 065 versionElement.addElement(IMPORT_VERSION_CREATED.substring(4)).setText(created); 066 } 067 break; 068 } 069 } 070 } 071}