Creating your own Version Number Adapter

The version number adapter is used by the application to set the logic that takes control of document version number each time the document is updated.

Keep in mind changing version number adapter when application already has document version it means sometimes you must migrate older version numbers to the new format or a new implementation must take in consideration older ones.

You can create your own Version Number Adapter

Conditions:

  • The new Form Validator class must implement the "VersionNumerationAdapter" interface.
  • The new Form Validator class must be declared under the package "com.openkm.plugin.vernum".
  • The new Form Validator class must be annotated with "@PluginImplementation".
  • The new Form Validator class must extend of "BasePlugin".

Version Number Adapter interface:

package com.openkm.plugin.vernum;

import com.openkm.db.bean.NodeVersion;
import net.xeoh.plugins.base.Plugin;

public interface VersionNumerationAdapter extends Plugin {

    int INCREASE_DEFAULT = 0;
    int INCREASE_MINOR = 1;
    int INCREASE_MAJOR = 2;
    int INCREASE_MAJOR_MINOR = 3;

    String qs = "from NodeVersion nv where nv.parent=:parent and nv.name=:name";

    String getInitialVersionNumber();

    int getVersionNumberIncrement();

    String getNextVersionNumber(NodeVersion nVer, int increment);
}

The new class must be loaded into the package com.openkm.plugin.vernum because application plugin system will try to load from there.

Do not miss the tag @PluginImplementation otherwise the application plugin system will not be able to retrieve the new class.

More information at Register a new plugin.

To enable the new Version number adapter go to Administration > Configuration parameters > find the parameter named version.numeration.adapter and modify the value with com.openkm.vernum.OwnNumerationAdapter

To take effect this change need to restating application.

Methods description

MethodTypeDescription

getInitialVersionNumber()

String

Return the first version of a new document.

First version usually will be "1.0".

getVersionNumberIncrement()

int

Return a number that indicate the available increase options.

Available values:

  • VersionNumerationAdapter.INCREASE_DEFAULT
  • VersionNumerationAdapter.INCREASE_MINOR
  • VersionNumerationAdapter.INCREASE_MAJOR
  • VersionNumerationAdapter.INCREASE_MAJOR_MINOR

INCREASE_NONE: Not showing any option, increase by default.

INCREASE_MINOR: Only shows the option of increase minor version, if not selected, increase by default.

INCREASE_MAYOR: Only shows the option of increase mayor version, if not selected, increase by default.

INCREASE_MAJOR_MINOR: Shows the options of increase major and minor version, if not selected, increase by default.

String getNextVersionNumber(Session session, NodeDocument nDoc, NodeDocumentVersion nDocVer, int increment)()

String

Calculate the next version number from a given one

Example of the Version number adapter implementation

package com.openkm.plugin.vernum;

import com.openkm.core.Config;
import com.openkm.db.bean.NodeVersion;
import com.openkm.db.repository.NodeVersionRepo;
import com.openkm.plugin.BasePlugin;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.springframework.beans.factory.annotation.Autowired;

@PluginImplementation
public class PlainVersionNumerationAdapter extends BasePlugin implements VersionNumerationAdapter {

    @Autowired
    private NodeVersionRepo nodeVersionRepo;

    @Override
    public String getInitialVersionNumber() {
        return String.format(Config.VERSION_NUMERATION_FORMAT, 1);
    }

    @Override
    public int getVersionNumberIncrement() {
        return INCREASE_DEFAULT;
    }

    @Override
    public String getNextVersionNumber(NodeVersion nVer, int increment) {
        String versionNumber = nVer.getName();
        int nextVerNumber = Integer.parseInt(versionNumber);
        NodeVersion nv;

        do {
            nextVerNumber++;
            String name = String.valueOf(nextVerNumber);
            nv = nodeVersionRepo.getNodeVersionByNodeAndName(nVer.getParent(), name);
        } while (nv != null);

        return String.format(Config.VERSION_NUMERATION_FORMAT, nextVerNumber);
    }
}