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:

  <?xml version="1.0"?>
   <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" />
        </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(&quot;custom-versioningservice&quot;);
        openSession();
    }

    public void testDocumentCreation() throws Exception {
        DocumentModel doc = session.createDocumentModel(&quot;/&quot;, &quot;test&quot;, &quot;File&quot;);
        doc.putContextData(&quot;comesFromImport&quot;, true);
        doc = session.createDocument(doc);
        assertEquals(&quot;12.0+&quot;, doc.getVersionLabel());
        doc = session.createDocumentModel(&quot;/&quot;, &quot;test2&quot;, &quot;File&quot;);
        doc = session.createDocument(doc);
        assertEquals(&quot;0.0&quot;, 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&lt;String, Serializable&gt; options) {
        if (doc.isVersion() || doc.isProxy()) {
            return;
        }
        try {
            Boolean comesFromImport = (Boolean) options.get(&quot;comesFromImport&quot;);
            if (comesFromImport != null &amp;&amp; comesFromImport) {
                setVersion(doc, 12, 0);
            } else {
                setInitialVersion(doc);
            }
        } catch (DocumentException e) {
            // ignore, should not happen
            log.warn(&quot;Issue while setting the initial document version&quot;, 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">;
<extension target="org.nuxeo.ecm.core.versioning.VersioningService" point="versioningService">
    <service class="org.nuxeo.sample.versioning.CustomVersioningService" />
</extension>
</component>

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