Creating your own Version Number Adapter

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

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

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 in the package "com.openkm.plugin.vernum".
  • The new Form Validator class must be annotated with "@PluginImplementation".
  • The new Form Validator class must extend "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 the application plugin system will try to load it from there.

Do not omit 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 set the value to com.openkm.vernum.OwnNumerationAdapter.

For this change to take effect, restart the application.

Methods description

MethodTypeDescription

getInitialVersionNumber()

String

Returns the first version of a new document.

The first version is usually "1.0".

getVersionNumberIncrement()

int

Returns a number that indicates the available increase options.

Available values:

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

INCREASE_NONE: Does not show any option; increases by default.

INCREASE_MINOR: Only shows the option to increase the minor version; if not selected, it increases by default.

INCREASE_MAJOR: Only shows the option to increase the major version; if not selected, it increases by default.

INCREASE_MAJOR_MINOR: Shows the options to increase major and minor versions; if not selected, it increases by default.

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

String

Calculates 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);
    }
}