Today we have a question from maxr who asks how to import a document and put it in Nuxeo with version 12 as the initial version.

There are different options for this. The easiest one is to setup a custom versioning rule during the import.

Every document created at that time will be in version 12.0. Just remove the import-versioning-contrib.xml once the import is done. This will only work if you do a one-shot import and nobody creates new documents during the import.

All you need to do is deploy an import-versioning-contrib.xml file containing for instance the following code:

<component name="org.nuxeo.ecm.platform.versionoverride" version="1.0"><extension target="org.nuxeo.ecm.core.versioning.VersioningService" point="versioningRules"><defaultversioningrule><initialstate major="12" minor="0"></initialstate></defaultversioningrule></extension></component>

Now let’s say my import is done continuously. Every five minutes, the importer looks into a specific folder on a server and if it finds a file, it imports it. Our previous solution won’t work. We have to override the VersioningService. Don’t worry, it doesn’t hurt. I did a small project to showcase this.

Starting with a simple unit test to make sure we’re on the right track:

` public class TestImportDocCreation extends SQLRepositoryTestCase {

@Override public void setUp() throws Exception { super.setUp(); deployBundle(“custom-versioningservice”); openSession(); }

public void testDocumentCreation() throws Exception { DocumentModel doc = session.createDocumentModel(“/“, “test”, “File”); doc.putContextData(“comesFromImport”, true); doc = session.createDocument(doc); assertEquals(“12.0+”, doc.getVersionLabel()); doc = session.createDocumentModel(“/“, “test2”, “File”); doc = session.createDocument(doc); assertEquals(“0.0”, doc.getVersionLabel()); }

@Override public void tearDown() throws Exception { super.tearDown(); closeSession(); } } `

As you can see we have to put the comesFromImport boolean in the contextMap. This means we’ll have to do this in the code of your importer.

The first thing to do as usual is to run the Nuxeo Plugin project wizard in Nuxeo IDE. We need to contribute a new VersioningService so we need a new class. Here’s the code:

` package org.nuxeo.sample.versioning;

import java.io.Serializable; import java.util.Map;

import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuxeo.ecm.core.api.DocumentException; import org.nuxeo.ecm.core.model.Document; import org.nuxeo.ecm.core.versioning.StandardVersioningService;

public class CustomVersioningService extends StandardVersioningService {

private static final Log log = LogFactory.getLog(CustomVersioningService.class);

@Override public void doPostCreate(Document doc, Map<String, Serializable> options) { if (doc.isVersion() || doc.isProxy()) { return; } try { Boolean comesFromImport = (Boolean) options.get(“comesFromImport”); if (comesFromImport != null && comesFromImport) { setVersion(doc, 12, 0); } else { setInitialVersion(doc); } } catch (DocumentException e) { // ignore, should not happen log.warn(“Issue while setting the initial document version”, e); } } } `

It simply extends the default VersioningService and overrides the doPostCreate method. This method has access to the document context map. So we look for a Boolean comeFromImport in this map. If it’s true, we know we come from the importer and we can set the version to 12.0. If not we just keep with the usual implementation.

Now we need the contribution to the right extension point so that Nuxeo knows the VersioningService to use is CustomVersioningService.

<component name="org.nuxeo.sample.versioning.contrib">;</component>

The source code of this project is on my GitHub. That’s it for today, see ya’ next Monday :-)