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.
To create your own Version number adapter you must create a new class that implements VersionNumerationAdapter interface:
package com.openkm.vernum;
import net.xeoh.plugins.base.Plugin;
import org.hibernate.Session;
import com.openkm.dao.bean.NodeDocument;
import com.openkm.dao.bean.NodeDocumentVersion;
/**
*
*/
public interface VersionNumerationAdapter extends Plugin {
public final static int INCREASE_DEFAULT = 0;
public final static int INCREASE_MINOR = 1;
public final static int INCREASE_MAJOR = 2;
public final static int INCREASE_MAJOR_MINOR = 3;
final String qs = "from NodeDocumentVersion ndv where ndv.parent=:parent and ndv.name=:name";
public String getInitialVersionNumber();
public int getVersionNumberIncrement();
public String getNextVersionNumber(Session session, NodeDocument nDoc, NodeDocumentVersion nDocVer, int increment);
}
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.
The new class must be loaded into the package com.openkm.vernum because application plugin system will try to load from there. See the sample below:
Do not miss the tag @PluginImplementation otherwise the application plugin system will not be able to retrieve the new class.
Methods description
Method | Type | Description |
---|---|---|
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:
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.vernum;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.hibernate.Query;
import org.hibernate.Session;
import com.openkm.core.Config;
import com.openkm.dao.bean.NodeDocument;
import com.openkm.dao.bean.NodeDocumentVersion;
/**
*/
@PluginImplementation
public class PlainVersionNumerationAdapter implements VersionNumerationAdapter {
@Override
public String getInitialVersionNumber() {
return String.format(Config.VERSION_NUMERATION_FORMAT, 1);
}
@Override
public int getVersionNumberIncrement() {
return INCREASE_DEFAULT;
}
@Override
public String getNextVersionNumber(Session session, NodeDocument nDoc, NodeDocumentVersion nDocVer, int increment) {
String versionNumber = nDocVer.getName();
int nextVerNumber = Integer.parseInt(versionNumber);
Query q = session.createQuery(qs);
NodeDocumentVersion ndv = null;
do {
nextVerNumber++;
q.setString("parent", nDoc.getUuid());
q.setString("name", String.valueOf(nextVerNumber));
ndv = (NodeDocumentVersion) q.setMaxResults(1).uniqueResult();
} while (ndv != null);
return String.format(Config.VERSION_NUMERATION_FORMAT, nextVerNumber);
}
}